PROGRAM BGBAL; {$V-} { Counts the number of BEGIN/ENDs in a pascal program and makes sure that they balance. Optionally writes the lines of source code that contain the BEGIN/ENDs to a file. source file and output file may be specified in the CCP command line or after the program begins Usage BGBAL D:filename.ext D:filename.ext This is not a fancy program but I use it frequently (being a clumsy typist) and thought someone eles might find it useful Walter Henry 438 Hillcrest Ave. Livermore, CA 94550 xb.k98@forsythe.stanford.edu xb.k98@stanford Version: 1.00 Last Revision: 12/29/85 } TYPE filename_typ = STRING[14]; array_index_typ = (leftbracket,rightbracket, leftparen,rightparen, leftparenstar,rightparenstar); tally_typ = ARRAY[array_index_typ] OF INTEGER; BigString = STRING[255]; VAR garbage : STRING[50]; infile : TEXT; outfile : TEXT; fname,outname : filename_typ; tally : tally_typ; textline : BigString; inchar : CHAR; echo,hit : BOOLEAN; PROCEDURE Upper (VAR textline : BigString); VAR i : BYTE; BEGIN FOR i := 1 TO LENGTH(textline) DO textline[i] := UPCASE(textline[i]); END; {Of Proc Upper} FUNCTION CountWords (word : BigString; textline : BigString ) : INTEGER; {Returns the number of times that word appears in textline} BEGIN END; {Of Func CountWords} PROCEDURE ScanLine (textline :BigString; VAR hit : BOOLEAN ); VAR i : BYTE; BEGIN hit := FALSE; FOR i := 1 TO LENGTH(textline) DO BEGIN CASE textline[i] OF '(' : BEGIN hit := TRUE; tally[leftparen] := succ(tally[leftparen]); IF textline[i+1]='*' THEN tally[leftparenstar] :=succ(tally[leftparenstar]); END; {leftparen} ')' : BEGIN hit := TRUE; tally[rightparen] := succ(tally[rightparen]); IF textline[i-1]='*' THEN tally[rightparenstar] :=succ(tally[rightparenstar]); END; {rightparen} '{' : BEGIN hit := TRUE; tally[leftbracket] := succ(tally[leftbracket]); END; '}' : BEGIN hit := TRUE; tally[rightbracket] := succ(tally[rightbracket]); END; END; {case} END; {for} END; {Of Proc ScanLine} {main} BEGIN echo := FALSE; inchar := ' '; tally[leftbracket] := 0; tally[rightbracket] := 0; tally[leftparen] := 0; tally[rightparen] := 0; tally[leftparenstar] := 0; tally[rightparenstar] := 0; ClrScr; IF ParamCount < 1 THEN BEGIN WRITELN('Enter name of file to scan'); READLN(fname); Upper(fname); END {if then} ELSE BEGIN fname := ParamStr(1); END; {if else} ASSIGN(infile,fname); RESET(infile); IF ParamCount >= 2 THEN BEGIN echo := TRUE; outname := ParamStr(2); ASSIGN(outfile,outname); REWRITE(outfile); END {if then} ELSE BEGIN WRITELN('Echo lines to file? (Y/N)'); READLN(inchar); IF inchar IN ['Y','y'] THEN BEGIN echo := TRUE; WRITE('Drive:filename.ext: '); READLN(outname); Upper(outname); ASSIGN(outfile,outname); REWRITE(outfile); END; {if then inchar in Yy} END; {if else paramcount >=2} REPEAT READLN(infile,textline); ScanLine(textline,hit); IF echo AND hit THEN WRITELN(outfile,textline); UNTIL EOF(infile); WRITELN ( '{ ' , tally[leftbracket]) ; WRITELN ( '} ' , tally[rightbracket]) ; WRITELN ( '( ' , tally[leftparen]) ; WRITELN ( ') ' , tally[rightparen]) ; WRITELN ( '(* ' , tally[leftparenstar]) ; WRITELN ( '*) ' , tally[rightparenstar]) ; CLOSE(infile); IF echo THEN BEGIN WRITELN (outfile, 'Count for file ',outname); WRITELN (outfile, '{ ' , tally[leftbracket]) ; WRITELN (outfile, '} ' , tally[rightbracket]) ; WRITELN (outfile, '( ' , tally[leftparen]) ; WRITELN (outfile, ') ' , tally[rightparen]) ; WRITELN (outfile, '(* ' , tally[leftparenstar]) ; WRITELN (outfile, '*) ' , tally[rightparenstar]) ; CLOSE(outfile); END; {if then echo} END.