delp.pp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. {
  2. $Id$
  3. Copyright (c) 1999-2000 by Peter Vreman
  4. Deletes all files generated for Pascal (*.exe,units,objects,libs)
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************}
  17. program Delp;
  18. uses
  19. dos,getopts;
  20. const
  21. Version = 'Version 1.00';
  22. Title = 'DelPascal';
  23. Copyright = 'Copyright (c) 1999-2000 by the Free Pascal Development Team';
  24. {$ifdef linux}
  25. DirSep = '/';
  26. {$else}
  27. DirSep = '\';
  28. {$endif}
  29. function DStr(l:longint):string;
  30. var
  31. TmpStr : string[32];
  32. i : byte;
  33. begin
  34. Str(l,tmpstr);
  35. i:=Length(TmpStr);
  36. while (i>3) do
  37. begin
  38. i:=i-3;
  39. if TmpStr[i]<>'-' then
  40. Insert('.',TmpStr,i+1);
  41. end;
  42. DStr:=TmpStr;
  43. end;
  44. Procedure EraseFile(Const HStr:String);
  45. var
  46. f : file;
  47. begin
  48. Assign(f,Hstr);
  49. {$I-}
  50. Erase(f);
  51. {$I+}
  52. if ioresult<>0 then;
  53. end;
  54. function MatchesMask(What, Mask: string): boolean;
  55. function upper(const s : string) : string;
  56. var
  57. i : longint;
  58. begin
  59. for i:=1 to length(s) do
  60. if s[i] in ['a'..'z'] then
  61. upper[i]:=char(byte(s[i])-32)
  62. else
  63. upper[i]:=s[i];
  64. upper[0]:=s[0];
  65. end;
  66. Function CmpStr(const hstr1,hstr2:string):boolean;
  67. var
  68. found : boolean;
  69. i1,i2 : longint;
  70. begin
  71. i1:=0;
  72. i2:=0;
  73. found:=true;
  74. while found and (i1<length(hstr1)) and (i2<length(hstr2)) do
  75. begin
  76. inc(i2);
  77. inc(i1);
  78. case hstr1[i1] of
  79. '?' :
  80. found:=true;
  81. '*' :
  82. begin
  83. found:=true;
  84. if (i1=length(hstr1)) then
  85. i2:=length(hstr2)
  86. else
  87. if (i1<length(hstr1)) and (hstr1[i1+1]<>hstr2[i2]) then
  88. begin
  89. if i2<length(hstr2) then
  90. dec(i1)
  91. end
  92. else
  93. if i2>1 then
  94. dec(i2);
  95. end;
  96. else
  97. found:=(hstr1[i1]=hstr2[i2]) or (hstr2[i2]='?');
  98. end;
  99. end;
  100. if found then
  101. begin
  102. { allow 'p*' matching 'p' }
  103. if (i1<length(hstr1)) and (hstr1[i1+1]='*') then
  104. inc(i1);
  105. found:=(i1>=length(hstr1)) and (i2>=length(hstr2));
  106. end;
  107. CmpStr:=found;
  108. end;
  109. var
  110. D1,D2 : DirStr;
  111. N1,N2 : NameStr;
  112. E1,E2 : Extstr;
  113. begin
  114. {$ifdef linux}
  115. FSplit(What,D1,N1,E1);
  116. FSplit(Mask,D2,N2,E2);
  117. {$else}
  118. FSplit(Upper(What),D1,N1,E1);
  119. FSplit(Upper(Mask),D2,N2,E2);
  120. {$endif}
  121. MatchesMask:=CmpStr(N2,N1) and CmpStr(E2,E1);
  122. end;
  123. type
  124. PMaskItem=^TMaskItem;
  125. TMaskItem=record
  126. Mask : string[15];
  127. Files : longint;
  128. Size : longint;
  129. Next : PMaskItem;
  130. end;
  131. var
  132. masklist : pmaskitem;
  133. procedure AddMask(s:string);
  134. var
  135. maskitem : PMaskItem;
  136. i : longint;
  137. begin
  138. repeat
  139. i:=pos(' ',s);
  140. if i=0 then
  141. i:=length(s)+1;
  142. New(maskitem);
  143. fillchar(maskitem^,sizeof(tmaskitem),0);
  144. maskitem^.mask:=Copy(s,1,i-1);
  145. maskitem^.next:=masklist;
  146. masklist:=maskitem;
  147. Delete(s,1,i);
  148. until s='';
  149. end;
  150. Var quiet: boolean;
  151. procedure usage;
  152. begin
  153. Writeln('Delp [options] <directory>');
  154. Writeln('Where options is one of:');
  155. writeln(' -e Delete executables also (Not on linux)');
  156. writeln(' -h Display (this) help message.');
  157. writeln(' -q Quietly perfoms deleting.');
  158. Halt(1);
  159. end;
  160. procedure processoptions;
  161. Var c : char;
  162. begin
  163. quiet:=false;
  164. Repeat
  165. C:=Getopt('ehq');
  166. Case C of
  167. 'e' : AddMAsk('*.exe *.so *.dll');
  168. 'h' : Usage;
  169. 'q' : Quiet:=True;
  170. EndOfOptions : ;
  171. end;
  172. Until C=EndOfOptions;
  173. end;
  174. var
  175. Dir : Searchrec;
  176. Total : longint;
  177. hp : pmaskitem;
  178. found : boolean;
  179. basedir : string;
  180. begin
  181. ProcessOptions;
  182. if Optind<>ParamCount then
  183. Usage;
  184. BaseDir:=Paramstr(OptInd);
  185. If BaseDir[Length(BaseDir)]<>DirSep then
  186. BaseDir:=BaseDir+DirSep;
  187. AddMask('*.ppw *.ow *.aw *.sw');
  188. AddMask('ppas.bat ppas.sh link.res fpcmaked fpcmade fpcmade.*');
  189. AddMask('*.tpu *.tpp *.tpw *.tr');
  190. AddMask('*.log *.bak');
  191. AddMask('*.ppu *.o *.a *.s');
  192. AddMask('*.pp1 *.o1 *.a1 *.s1');
  193. AddMask('*.ppo *.oo *.ao *.so');
  194. AddMask('*.rst');
  195. if not quiet then
  196. begin
  197. writeln(Title+' '+Version);
  198. writeln(Copyright);
  199. Writeln;
  200. end;
  201. FindFirst(basedir+'*.*',anyfile,Dir);
  202. Total:=0;
  203. while (doserror=0) do
  204. begin
  205. hp:=masklist;
  206. while assigned(hp) do
  207. begin
  208. if MatchesMask(Dir.Name,hp^.mask) then
  209. begin
  210. EraseFile(Dir.Name);
  211. inc(hp^.Files);
  212. inc(hp^.Size,Dir.Size);
  213. break;
  214. end;
  215. hp:=hp^.next;
  216. end;
  217. FindNext(Dir);
  218. end;
  219. {Write Results}
  220. found:=false;
  221. hp:=masklist;
  222. while assigned(hp) do
  223. begin
  224. if hp^.Files>0 then
  225. begin
  226. if not quiet then
  227. WriteLn(' - Removed ',hp^.Files:2,' ',hp^.Mask,' (',DStr(hp^.Size)+' Bytes)');
  228. inc(Total,hp^.Size);
  229. found:=true;
  230. end;
  231. hp:=hp^.next;
  232. end;
  233. if not quiet then
  234. if not found then
  235. WriteLn(' - No Redundant Files Found!')
  236. else
  237. WriteLn(' - Total ',DStr(Total),' Bytes Freed');
  238. end.
  239. {
  240. $Log$
  241. Revision 1.2 2000-12-27 22:13:44 peter
  242. * .rst added
  243. Revision 1.1 2000/07/13 10:16:21 michael
  244. + Initial import
  245. Revision 1.11 2000/07/04 19:05:54 peter
  246. * be optimistic: version 1.00 for some utils
  247. Revision 1.10 2000/01/26 21:15:00 peter
  248. * Fixed dir separator for linux
  249. Revision 1.9 2000/01/24 16:31:12 michael
  250. + Adapted delp so it accepts a directory as an option
  251. Revision 1.8 2000/01/23 16:40:28 peter
  252. * *.dll, *.so are also executables and only turned on by -e
  253. * title+copyright like ppudump
  254. Revision 1.7 2000/01/23 14:23:48 michael
  255. + Typos fixed, version updated
  256. Revision 1.6 2000/01/23 14:20:44 michael
  257. + Added option to delete executables, plus help and quiet
  258. Revision 1.5 2000/01/12 10:40:59 peter
  259. * fixed bug which sometimes matched .ppw with .pp
  260. Revision 1.4 2000/01/07 16:46:02 daniel
  261. * copyright 2000
  262. Revision 1.3 1999/12/19 17:12:10 peter
  263. * added fpcmade
  264. Revision 1.2 1999/12/02 11:31:11 peter
  265. * removed temp comment
  266. Revision 1.1 1999/12/01 22:45:04 peter
  267. + delp tool which deletes all generated pascal files
  268. }