Mega Code Archive

 
Categories / Delphi / Examples
 

Small application to count lines of a project

Title: Small application to count lines of a project Question: Do you need to count the total number of lines of your projects? Answer: Here it is the code for a small application (always ready for changes) that you can use to count the lines of your project files. == lc application == (* * 2001/08/24 - Code corrections * Original idea found on internet *) program lc; {$APPTYPE CONSOLE} uses SysUtils, Classes; var idx: Integer; FParams: string; StrList: TStringList; FileSpec: string; Attr: Integer; ShowFiles: Boolean; procedure PrintPublicDomain; begin Writeln('LineCount Utility v1.0'); Writeln('This is PUBLIC DOMAIN. Fernando J.A. Silva (magico@galaxycorp.com)'); end; procedure PrintHelp; begin Writeln('Usage: lc [options ...] '); Writeln(' -s Recurse subdirectories'); Writeln(' -f Show files while counting'); Writeln(' -? or -h Display this message'); Writeln(''); Writeln('Example: lc -s *.*'); end; procedure PrintCanClose; begin Writeln('Hit to exit'); Readln; end; function CountLines(AParentPath: string; AStrList: TStrings): Integer; var FSearchRec: TSearchRec; FDirsPath: string; FFilesPath: string; begin Result := 0; AParentPath := IncludeTrailingPathDelimiter(AParentPath); FDirsPath := AParentPath + '*.*'; FFilesPath := AParentPath + FileSpec; // Get directories if FindFirst(FDirsPath, Attr, FSearchRec) = 0 then begin if (FSearchRec.Name '..') and (FSearchRec.Name '.') and ((FSearchRec.Attr and faDirectory) 0) then Result := Result + CountLines(AParentPath + FSearchRec.Name, StrList); while FindNext(FSearchRec) = 0 do begin if (FSearchRec.Name '..') and (FSearchRec.Name '.') and ((FSearchRec.Attr and faDirectory) 0) then Result := Result + CountLines(AParentPath + FSearchRec.Name, StrList); end; end; FindClose(FSearchRec); // Get files if FindFirst(FFilesPath, Attr, FSearchRec) = 0 then begin if (FSearchRec.Name '..') and (FSearchRec.Name '.') then begin StrList.LoadFromFile(AParentPath + FSearchRec.Name); Result := Result + StrList.Count; if ShowFiles then Writeln(Format(FSearchRec.Name + ' (%d lines)', [StrList.Count])); end; while FindNext(FSearchRec) = 0 do begin if (FSearchRec.Name '..') and (FSearchRec.Name '.') then begin StrList.LoadFromFile(AParentPath + FSearchRec.Name); Result := Result + StrList.Count; if ShowFiles then Writeln(Format(FSearchRec.Name + ' (%d lines)', [StrList.Count])); end; end; end; FindClose(FSearchRec); end; begin FileSpec := ''; Attr := faAnyFile; FParams := ''; for idx := 1 to ParamCount do FParams := FParams + ' ' + UpperCase(ParamStr(idx)); // Print Public domain PrintPublicDomain; // Print help if (ParamCount = 0) or (Pos('-?', FParams) 0) or (Pos('-H', FParams) 0) then begin PrintHelp; PrintCanClose; Exit; end; // Map options to internal vars if (Pos('-S', FParams) = 0) then Attr := Attr - faDirectory; ShowFiles := (Pos('-F', FParams) 0); FileSpec := ExtractFileName(ParamStr(ParamCount)); // Count lines Writeln('Counting lines...'); // Initialize string list StrList := TStringList.Create; try Writeln(Format('Line Count: %d', [CountLines(ExtractFilePath(ParamStr(ParamCount)), StrList)])); finally StrList.Free; end; PrintCanClose; end.