'=========================================================================== ' Subject: INDENT AND JUSTIFY SOURCE CODE Date: 01-28-99 (17:03) ' Author: Randall L. Glass Code: PB ' Origin: rlglass@yahoo.com Packet: PB.ABC '=========================================================================== ' Indent and Justified ' By Randall L Glass ' Copyright 1998 'You may do anything you want with this program as long as you give me 'credit in the aknowledgments section of the program 'This program is as is.No claims are made for it declare function extractword$(st?,string1$,matchstring$,wordend?) declare function findword&( wd$ ) DECLARE SUB justifed (thisline$,justline$,hold$,wd%) DECLARE FUNCTION FindLastChar%(wd%,thisline$,matchchars$) DECLARE FUNCTION Findrem%(thisline$) DECLARE FUNCTION QuoteLastChar%(start%,thisline$,matchchars$) Declare FUNCTION CountSpaces%(newline$,tabs%) DIM commands$(11),linein$(5000) ,lineout$(5000) Shared beginline$,startpos%,tabs$,spaces%,str%,linenumber%,lineout$() shared commands$(),index&,countout& on error resume next otherchar$=" "+chr$(9) spaces% = 0 tb% = 0 tbs$=chr$(9) d$=commands$(1) commandline$=rtrim$(ltrim$(command$,any otherchar$),any otherchar$) +" " st? = 1:filein$ = extractword$(st?,commandline$," ",wordend?) st? = wordend?:tabs% = val(extractword$(st?,commandline$," ",wordend?)) fileout$ = extract$(filein$,".") fileout$ = fileout$+".ind" if filein$ = "" then print "Format ";chr$(34);"Indent2.bas [tab size] default tab is 6"; end end if cls if tabs% = 0 then tabs% = 6 print "Tabs are 6 wide [Tab Default is on]" end if open filein$ for input as #1 linecount&=0 do incr linecount& line input #1,linein$(linecount&) loop until eof(1) close #1 countout&=0 for count& = 1 to linecount& locate 5,10:print "Lines = ";count&:print incr countout& printit? = 1 wordend? = 1 thisline$=linein$(count&) again4: thisline$ = rtrim$(ltrim$(thisline$,any otherchar$)) ln?=len(thisline$) if right$(thisline$,1) = "_" then thisline$ = left$(thisline$,ln?-1)+ltrim$(linein$(count&+1)) incr count&:goto again4 end if thisline$ = thisline$ + " " command1$ = extract$(thisline$,ANY " ,:") ln?=len(command1$) wordend?=ln? + 2 command1$ = ucase$(command1$) if command1$ = "IF" then thisline$ = rtrim$(thisline$) +" " thenpos% = instr(ucase$(thisline$)," THEN ") if thenpos% = 0 then thenpos% = instr(ucase$(thisline$),chr$(34)+"THEN ") ln? = len(thisline$) if ln? = thenpos% + 5 then lineout$(countout&)=string$(tb%,tbs$)+thisline$ incr tb% printit? = 0 incr if2& end if goto nearloop end if if command1$ ="END" then command2$= ucase$(extractword$(wordend?,thisline$,otherchar$,wordend?)) if command2$ = "IF" then tb% = tb%-1:incr endif2& if command2$ = "SELECT" then tb% = tb% -2 :incr endselect2& goto nearloop end if if command1$ = "LOOP" then decr tb% incr loop2& goto nearloop end if if command1$ = "WEND" then decr tb% incr wend2& goto nearloop end if if command1$ = "NEXT" then decr tb% incr next2& goto nearloop end if if command1$ = "SELECT" then printit?=0 lineout$(countout&) = string$(tb%,tbs$)+thisline$ incr tb%,2 incr select2& goto nearloop end if if command1$ = "DO" then printit?=0 lineout$(countout&) = string$(tb%,tbs$)+thisline$ incr tb% incr do2& goto nearloop end if if command1$ = "WHILE" then printit?=0 lineout$(countout&) = string$(tb%,tbs$)+thisline$ incr tb% incr while2& goto nearloop end if if command1$ = "FOR" then printit?=0 lineout$(countout&) = string$(tb%,tbs$)+thisline$ incr tb% incr for2& goto nearloop end if if command1$ = "ELSE" or command1$ = "ELSEIF" then lineout$(countout&) = string$(tb%-1,tbs$)+thisline$ printit?=0 incr else2& goto nearloop end if if command1$ = "CASE" then lineout$(countout&) = string$(tb%-1,tbs$)+thisline$ printit?=0 incr case2& end if nearloop: IF TB% < 0 THEN linecount& = count& lineout$(countout&)= "***********************************************" lineout$(countout&+1)= "PROGRAM ERROR" lineout$(countout&+2)= command1$ lineout$(countout&+3)= "lines = "+str$(count&) lineout$(countout&+4)= "tb%="+str$(tb%) gosub CommandError incr countout& lineout$(countout&) = "***********************************************" countout& = countout& + 6 tb% = 0 end if if printit?=1 then lineout$(countout&) = string$(tb%,tbs$)+thisline$ next count& print "Current Directory is ";curdir$ print "Output file is ";fileout$ print "Number of lines is ";count& open fileout$ for output as #2 linenumber% = 0 for linenumber% = 1 to countout& locate 5,10:print "Lines = ";linenumber%;" "; tabs$ = chr$(9) linewidth% = 77 beginline$ = "" spaces% = 0 thisline$ = rtrim$(lineout$(linenumber%)) newline$ = ltrim$(thisline$,Any " "+chr$(9)) startpos% = instr(1,thisline$,newline$) if startpos% > 0 then beginline$ = left$(thisline$,startpos% -1) spaces% = countspaces%(beginline$,tabs%) end if beglen% = len(beginline$) ln% = len(newline$) + beglen% + spaces% if ln% > linewidth% then uppercaseline$ = ucase$(newline$) if extract$(uppercaseline$," ") <> "DATA" and Findrem%(uppercaseline$)= 0 then hold$ = newline$ do Justifed hold$,justline$,OutHold$,linewidth% hold$ =OutHold$ if hold$ <> "" then print #2,justline$ + " _" else print #2,justline$ end if loop until hold$ = "" else print #2,thisline$ end if else print #2,thisline$ end if next linenumber% gosub LastErrors close #2 locate 11,1 end SUB Justifed(thisline$,justline$,hold$,wd%) Public newline$ = beginline$ + thisline$ ln% = len(newline$) + spaces% if ln% > wd% then lastpos% = FindLastChar%(wd%,newline$," :,;<>=+-*/\(" + chr$(34) ) if lastpos% >= wd%/2 then justline$ = left$(newline$,lastpos%-1) hold$ = right$(newline$,len(newline$)-lastpos% +1) else justline$ = newline$ hold$ = "" end if else justline$ = newline$ hold$ = "" end if END SUB FUNCTION FindLastChar%(wd%,thisline$,matchchars$) Public oldfound% = 0 found% = 0 do oldfound% = found% found% = instr(found%+1,thisline$,any matchchars$) letter$ = mid$(thisline$,found%,1) if letter$ = chr$(34) then found% = instr(found% + 1,thisline$,chr$(34)) if found% = 0 then print #2, "***** Quote Error *****" end if end if loop until found% + spaces% > wd% or found% = 0 FindLastChar% = oldfound% END FUNCTION FUNCTION Findrem%(thisline$) Public oldfound% = 0 found% = 0 ln% = len(thisline$) do oldfound% = found% quote% = instr(found%+1,thisline$,chr$(34)) remark% = instr(found%+1,thisline$,chr$(39)) if remark% = 0 then remark% = ln% +1 remarkword% = instr(found%+1,thisline$,"REM") if remarkword% = 0 then remarkword% = ln% +1 remark% = min%(remark%,remarkword%) if remark% < ln% + 1 and (remark% < quote% or quote% = 0) then findrem% =1 exit do end if found% = quote% if quote% > 0 then found% = instr(found% + 1,thisline$,chr$(34)) if found% = 0 then print #2, str$(linenumber%) print #2, "***** Quote Error *****" quote% = 0 end if end if loop until quote% = 0 or found% >= ln% END FUNCTION FUNCTION CountSpaces%(newline$,tabs%) sp% = 0 ln% = len(newline$) for i% =1 to ln% char$ = mid$(newline$,i%,1) if char$ = chr$(9) then incr sp%,tabs% - 1 next i% Countspaces% = sp% end function function extractword$(st?,string1$,matchstring$,wordend?) a?=st? ln?=len(string1$) if st? >= ln? then extractword$ = "":exit function again: wordend? = instr(a?,string1$,any matchstring$)+1 word$=mid$(string1$,a?,wordend?-a?-1) if word$ = "" and (wordend? < ln?) and (wordend? <> 0) then a?=wordend? goto again: end if extractword$ = word$ end function CommandError: 'if endif2& < if2& then ' incr countout& ' lineout$(countout&)="Need END IF statement's " 'end if if endif2& > if2& then incr countout& lineout$(countout&) = "Too many End IF's " end if 'if next2& < for2& then ' incr countout& ' lineout$(countout&) = "Need Next statement's" 'end if if next2& > for2& then incr countout& lineout$(countout&) = "Too many NEXT's statements" end if 'if loop2& < do2& then ' incr countout& ' lineout$(countout&) = "Need LOOP statement's" 'end if if loop2& > do2& then incr countout& lineout$(countout&) = "Too Many LOOP statement's" end if 'if wend2& < while2& then ' incr countout& ' lineout$(countout&) = "Need WEND statement's" 'end if if wend2& > while2& then incr countout& lineout$(countout&) = "Too Many WEND statement's" end if 'if endselect2& < select2& then ' incr countout& ' lineout$(countout&) = "Need END SELECT statemant's" 'end if if endselect2& > select2& then incr countout& lineout$(countout&) = "Too Many END SELECT statement's" end if return LastErrors: print #2,"'**************************************************" if endif2& < if2& then Print #2,"Need END IF statement's " end if if endif2& > if2& then print #2, "Too many End IF's " end if if next2& < for2& then print #2, "Need Next statement's" end if if next2& > for2& then print #2, "Too many NEXT's statements" end if if loop2& < do2& then print #2, "Need LOOP statement's" end if if loop2& > do2& then print #2, "Too Many LOOP statement's" end if if wend2& < while2& then print #2, "Need WEND statement's" end if if wend2& > while2& then print #2, "Too Many WEND statement's" end if if endselect2& < select2& then print #2, "Need END SELECT statemant's" end if if endselect2& > select2& then print #2, "Too Many END SELECT statement's" end if return