Recently, I took to wandering about in some of my decades-old projects. It is interesting to see the things I was passionate about then be considered so outmoded by myself today, and yet at the same time, still impressive given the limitations I was working in at the time. A more recent old project was a VB6 project that effectively creates a Geometry and drawing library and adds a Brushes and Pens to VB6, where normally you only have the ability to specify a solid colour as a brush and some limited pen styles. Another very old project is a “Hook” library designed to allow the implementation of Windows Hooks from within a Visual Basic Program. With some of the much older programs I consider myself lucky that I did not have regular, easy Internet Access at the time, as at the time I thought the programs I built were awesome when, today, they feel terrible. In particular some of my earliest QuickBASIC Programs have the standard air of teenage overconfidence one can find all over forums today that I am grateful is safely encapsulated within my own Disk Drive, rather than in the wild on a forum post that can be traced to me. And yet, ironically- I’ll toss one of these examples up in source code form, just so I can look at it in the setting of a blog post.
STRUCT
“STRUCT.BAS” was a program I wrote in QuickBASIC that has the purpose of taking a BASIC Source code file and formatting the code therein and writing that output to a new file. Effectively, it performed the same function as “Reformat Code” does in most modern IDE software today, on old QuickBASIC and Early VB Code.
Honestly I’m not sure if that is really a bad goal. At the time the IDE software didn’t really offer that feature and were basically just text editors, so it was left to me to format my source code properly and it was a simple enough premise that I could wrap my head around it and really dig into the problem. The program was written in QuickBASIC 7.1, and was designed to run on Visual Basic Source code files and QuickBASIC source code files, both saved in the text format.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
DIM tabtmp AS INTEGER DIM doneline AS LONG, allline AS LONG DIM inputs(q) AS STRING DIM outputs(q) AS STRING, workstr AS STRING DIM tabnum AS INTEGER, tabstr AS STRING DIM backupfile AS STRING DIM NL AS STRING DIM commtmp AS STRING DIM counter AS LONG DIM temps AS INTEGER CONST VER = 2.24@ DIM infile AS STRING, outfile AS STRING DIM Xplace AS INTEGER, Yplace AS INTEGER commtmp = COMMAND$ IF commtmp = "/H" OR commtmp = "-H" OR commtmp = "/?" OR commtmp = "-?" THEN 'user wants help. PRINT " ZSTRUCT.EXE" PRINT " BY:Michael Burgwin" PRINT " COPYRIGHT 2003-2004" PRINT "" PRINT " Are you lazy when it comes to Visual Basic or QuickBasic?" PRINT "Hate having to structure your code? well, run this little proggy" PRINT "Because your prayers have been answered!" PRINT "" PRINT PRINT "SYNTAX:" PRINT PRINT "ZSTRUCT <source /> <destination> [/?|/H|-H|-?]" PRINT "" PRINT "<source /> is destination file to structure. </destination><destination> is target file" PRINT "If </destination><destination> exists, you will be prompted to overwrite it.( you " PRINT "cannot overwrite the source file)" PRINT "structures all block statements, for example, IF..THEN..ELSE, SELECT CASE,etc.." PRINT "" PRINT "Have windows?... of course you do! get ZSTRUCT for windows!" PRINT PRINT "version "; VER END END IF RESET IF COMMAND$ = "" THEN PRINT "files must be specified on command line." END END IF NL = CHR$(13) + CHR$(10) 'enable error handler. ON ERROR GOTO ERRSW temps = INSTR(commtmp, " ") outfile = RIGHT$(commtmp, LEN(commtmp) - temps) infile = LEFT$(commtmp, temps) PRINT "files to structure:" PRINT "from "; infile PRINT "to "; outfile Xplace = currentX Yplace = currentY backupfile = "C:\" '48-90 'For I = 1 To 8 ' backupfile = backupfile + Chr$(Int(Rnd * 32) + 48) 'Next I ' backupfile = backupfile + "." 'For I = 1 To 3 ' backupfile = backupfile + Chr$(Int(Rnd * 32) + 48) 'Next I 'FIRSTLY, back up source file: 'FileCopy Infile, backupfile PRINT files$ OPEN infile FOR INPUT AS #1 DO UNTIL EOF(1) INPUT #1, null allline = allline + 1 LOOP RESET 'second, open both files. OPEN infile FOR INPUT AS #1 OPEN RTRIM$(LTRIM$(outfile)) FOR OUTPUT AS #2 PRINT "restructuring..." DO UNTIL EOF(1) REDIM PRESERVE inputs(counter) AS STRING LINE INPUT #1, workstr tabstr = SPACE$(5 * tabnum) workstr = LTRIM$(LCASE$(workstr)) 'convert to lowercase for the code. 'trim it for neatness. We don't want already structured code to be reindented. IF LEFT$(workstr, 4) = "sub " OR LEFT$(workstr, 10) = "static sub" OR LEFT$(workstr, 12) = "private sub " OR LEFT$(workstr, 11) = "public sub " THEN tabnum = tabnum + 1 END IF IF LEFT$(workstr, 8) = "function" OR LEFT$(workstr, 15) = "static function" OR LEFT$(workstr, 17) = "private function " OR LEFT$(workstr, 16) = "public function " THEN tabnum = tabnum + 1 END IF IF LEFT$(workstr, 3) = "if " AND RIGHT$(workstr, 4) = "then" THEN tabnum = tabnum + 1 END IF IF (LEFT$(workstr, 3) = "do ") OR workstr = "do" AND RIGHT$(workstr, 4) <> "loop" THEN tabnum = tabnum + 1 END IF IF LEFT$(workstr, 11) = "select case" AND RIGHT$(workstr, 10) <> "end select" THEN tabnum = tabnum + 2 END IF IF LEFT$(workstr, 5) = "while" AND RIGHT$(workstr, 4) <> "wend" THEN tabnum = tabnum + 1 END IF IF LEFT$(workstr, 5) = "type " THEN tabnum = tabnum + 1 END IF IF LEFT$(workstr, 4) = "for " AND INSTR(workstr, "next ") = 0 THEN tabnum = tabnum + 1 END IF IF LEFT$(workstr, 6) = "end if" THEN tabnum = tabnum - 1 tabstr = SPACE$(5 * tabnum) END IF IF LEFT$(workstr, 8) = "end type" THEN tabnum = tabnum - 1 tabstr = SPACE$(5 * tabnum) END IF IF LEFT$(workstr, 10) = "end select" THEN tabnum = tabnum - 2 tabstr = SPACE$(5 * tabnum) END IF IF LEFT$(workstr, 5) = "wend " OR workstr = "wend" THEN tabnum = tabnum - 1 tabstr = SPACE$(5 * tabnum) END IF IF LEFT$(workstr, 5) = "next " OR workstr = "next" THEN tabnum = tabnum - 1 tabstr = SPACE$(5 * tabnum) END IF IF LEFT$(workstr, 5) = "loop " OR workstr = "loop" THEN tabnum = tabnum - 1 tabstr = SPACE$(5 * tabnum) END IF IF LEFT$(workstr, 5) = "case " THEN IF NOT tabnum = 0 THEN tabnum = tabnum - 1 tabstr = SPACE$(5 * tabnum) tabnum = tabnum + 1 END IF IF LEFT$(workstr, 5) = "else " OR workstr = "else" THEN tabnum = tabnum - 1 tabstr = SPACE$(5 * tabnum) tabnum = tabnum + 1 END IF IF LEFT$(workstr, 7) = "end sub" THEN tabnum = 0 tabstr = SPACE$(5 * tabnum) END IF IF LEFT$(workstr, 12) = "end function" THEN tabnum = 0 tabstr = SPACE$(5 * tabnum) END IF IF RIGHT$(workstr, 1) = ":" THEN tabtemp = tabnum tabnum = 0 tabstr = SPACE$(5 * tabnum) tabnum = taptemp END IF workstr = tabstr + workstr counter = counter + 1 doneline = doneline + 1 PRINT #2, workstr IF tabnum < 0 THEN tabnum = 0 LOOP PRINT "RESTRUCTURE COMPLETED!" RESET END ERRSW: DIM errval AS INTEGER errval = ERR SELECT CASE errval CASE 53 'file not found PRINT "Error: file "; infile; " not found." END CASE 5 'illegal function call tabnum = 0 RESUME CASE 68, 71, 72 PRINT "Unable To perform requested operation... "; ERROR$ CASE ELSE END SELECT RESUME NEXT |
This is not a bad program. It works for it’s simple function. The main thing I notice is that the program has a lot of repeated code. What it boils down to is finding lines that start with certain text and using them as an indicator to either increment the number of tabs, decrement the number of tabs, or set the tab count to 0. The task isn’t super simplistic- as there are some considerations particularly for standard BASIC indenting that need to be considered. Though considering it, I imagine things like Regular Expressions could really be utilized for the tasks in question.
Have something to say about this post? Comment!