Advertisement
sglienke

Useless comments remover

Jan 29th, 2015
240
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. program UCR;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   Classes,
  7.   Dialogs,
  8.   IOUtils,
  9.   SysUtils;
  10.  
  11. function RemoveComments(const Path: string; const SearchRec: TSearchRec): Boolean;
  12. var
  13.   fileName: string;
  14.   s: TStrings;
  15.   i: Integer;
  16.   save: Boolean;
  17. begin
  18.   fileName := IncludeTrailingPathDelimiter(Path) + SearchRec.Name;
  19.   s := TStringList.Create;
  20.   try
  21.     s.LoadFromFile(fileName);
  22.     i := 0;
  23.     save := False;
  24.     while i < s.Count do
  25.     begin
  26.       if Trim(s[i]) = '{ Private declarations }' then
  27.       begin
  28.         s.Delete(i);
  29.         save := True;
  30.         if (Trim(s[i-1]) = 'private') and (Trim(s[i]) = 'public') then
  31.         begin
  32.           Dec(i);
  33.           s.Delete(i);
  34.         end;
  35.         Continue;
  36.       end;
  37.       if Trim(s[i]) = '{ Public declarations }' then
  38.       begin
  39.         s.Delete(i);
  40.         save := True;
  41.         if (Trim(s[i-1]) = 'public') and (Trim(s[i]) = 'end;') then
  42.         begin
  43.           Dec(i);
  44.           s.Delete(i);
  45.         end;
  46.         Continue;
  47.       end;
  48.       Inc(i);
  49.     end;
  50.     if save then
  51.     begin
  52.       Writeln('Removed comments from unit: ', fileName);
  53.       s.SaveToFile(fileName);
  54.     end;
  55.   finally
  56.     s.Free;
  57.   end;
  58. end;
  59.  
  60. procedure Main;
  61. var
  62.   dialog: TFileOpenDialog;
  63. begin
  64.   dialog := TFileOpenDialog.Create(nil);
  65.   dialog.Options := dialog.Options + [fdoPickFolders];
  66.   if dialog.Execute then
  67.   begin
  68.     TDirectory.GetFiles(dialog.FileName, '*.pas',
  69.       TSearchOption.soAllDirectories, RemoveComments);
  70.   end;
  71. end;
  72.  
  73. begin
  74.   Main;
  75.   Writeln('Press <Enter> to exit...');
  76.   Readln;
  77. end.
Advertisement
RAW Paste Data Copied
Advertisement