wutils.pas 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. {
  2. $Id$
  3. This file is part of the Free Pascal Integrated Development Environment
  4. Copyright (c) 1998 by Berczi Gabor
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit WUtils;
  12. interface
  13. {$ifndef FPC}
  14. {$define TPUNIXLF}
  15. {$endif}
  16. uses
  17. Objects;
  18. type
  19. PByteArray = ^TByteArray;
  20. TByteArray = array[0..65520] of byte;
  21. PNoDisposeCollection = ^TNoDisposeCollection;
  22. TNoDisposeCollection = object(TCollection)
  23. procedure FreeItem(Item: Pointer); virtual;
  24. end;
  25. PUnsortedStringCollection = ^TUnsortedStringCollection;
  26. TUnsortedStringCollection = object(TCollection)
  27. function At(Index: Integer): PString;
  28. procedure FreeItem(Item: Pointer); virtual;
  29. end;
  30. PSubStream = ^TSubStream;
  31. TSubStream = object(TStream)
  32. constructor Init(AStream: PStream; AStartPos, ASize: longint);
  33. function GetPos: Longint; virtual;
  34. function GetSize: Longint; virtual;
  35. procedure Read(var Buf; Count: Word); virtual;
  36. procedure Seek(Pos: Longint); virtual;
  37. procedure Write(var Buf; Count: Word); virtual;
  38. private
  39. StartPos: longint;
  40. Size : longint;
  41. S : PStream;
  42. end;
  43. {$ifdef TPUNIXLF}
  44. procedure readln(var t:text;var s:string);
  45. {$endif}
  46. function Min(A,B: longint): longint;
  47. function Max(A,B: longint): longint;
  48. function CharStr(C: char; Count: byte): string;
  49. function UpcaseStr(const S: string): string;
  50. function LowCase(C: char): char;
  51. function LowcaseStr(S: string): string;
  52. function RExpand(const S: string; MinLen: byte): string;
  53. function LTrim(const S: string): string;
  54. function RTrim(const S: string): string;
  55. function Trim(const S: string): string;
  56. function IntToStr(L: longint): string;
  57. function StrToInt(const S: string): longint;
  58. function GetStr(P: PString): string;
  59. function DirOf(const S: string): string;
  60. function ExtOf(const S: string): string;
  61. function NameOf(const S: string): string;
  62. function NameAndExtOf(const S: string): string;
  63. function DirAndNameOf(const S: string): string;
  64. function EatIO: integer;
  65. const LastStrToIntResult : integer = 0;
  66. DirSep : char = {$ifdef Linux}'/'{$else}'\'{$endif};
  67. implementation
  68. uses
  69. Dos;
  70. {$ifdef TPUNIXLF}
  71. procedure readln(var t:text;var s:string);
  72. var
  73. c : char;
  74. i : longint;
  75. begin
  76. if TextRec(t).UserData[1]=2 then
  77. system.readln(t,s)
  78. else
  79. begin
  80. c:=#0;
  81. i:=0;
  82. while (not eof(t)) and (c<>#10) do
  83. begin
  84. read(t,c);
  85. if c<>#10 then
  86. begin
  87. inc(i);
  88. s[i]:=c;
  89. end;
  90. end;
  91. if (i>0) and (s[i]=#13) then
  92. begin
  93. dec(i);
  94. TextRec(t).UserData[1]:=2;
  95. end;
  96. s[0]:=chr(i);
  97. end;
  98. end;
  99. {$endif}
  100. function Max(A,B: longint): longint;
  101. begin
  102. if A>B then Max:=A else Max:=B;
  103. end;
  104. function Min(A,B: longint): longint;
  105. begin
  106. if A<B then Min:=A else Min:=B;
  107. end;
  108. function CharStr(C: char; Count: byte): string;
  109. {$ifndef FPC}
  110. var S: string;
  111. {$endif}
  112. begin
  113. {$ifdef FPC}
  114. CharStr[0]:=chr(Count);
  115. FillChar(CharStr[1],Count,C);
  116. {$else}
  117. S[0]:=chr(Count);
  118. FillChar(S[1],Count,C);
  119. CharStr:=S;
  120. {$endif}
  121. end;
  122. function UpcaseStr(const S: string): string;
  123. var
  124. I: Longint;
  125. begin
  126. for I:=1 to length(S) do
  127. if S[I] in ['a'..'z'] then
  128. UpCaseStr[I]:=chr(ord(S[I])-32)
  129. else
  130. UpCaseStr[I]:=S[I];
  131. UpcaseStr[0]:=S[0];
  132. end;
  133. function LowerCaseStr(S: string): string;
  134. var
  135. I: Longint;
  136. begin
  137. for I:=1 to length(S) do
  138. if S[I] in ['A'..'Z'] then
  139. LowerCaseStr[I]:=chr(ord(S[I])+32)
  140. else
  141. LowerCaseStr[I]:=S[I];
  142. LowercaseStr[0]:=S[0];
  143. end;
  144. function RExpand(const S: string; MinLen: byte): string;
  145. begin
  146. if length(S)<MinLen then
  147. RExpand:=S+CharStr(' ',MinLen-length(S))
  148. else
  149. RExpand:=S;
  150. end;
  151. function LTrim(const S: string): string;
  152. var
  153. i : longint;
  154. begin
  155. i:=1;
  156. while (i<length(s)) and (s[i]=' ') do
  157. inc(i);
  158. LTrim:=Copy(s,i,255);
  159. end;
  160. function RTrim(const S: string): string;
  161. var
  162. i : longint;
  163. begin
  164. i:=length(s);
  165. while (i>0) and (s[i]=' ') do
  166. dec(i);
  167. RTrim:=Copy(s,1,i);
  168. end;
  169. function Trim(const S: string): string;
  170. begin
  171. Trim:=RTrim(LTrim(S));
  172. end;
  173. function IntToStr(L: longint): string;
  174. var S: string;
  175. begin
  176. Str(L,S);
  177. IntToStr:=S;
  178. end;
  179. function StrToInt(const S: string): longint;
  180. var L: longint;
  181. C: integer;
  182. begin
  183. Val(S,L,C); if C<>0 then L:=-1;
  184. LastStrToIntResult:=C;
  185. StrToInt:=L;
  186. end;
  187. function GetStr(P: PString): string;
  188. begin
  189. if P=nil then GetStr:='' else GetStr:=P^;
  190. end;
  191. function DirOf(const S: string): string;
  192. var D: DirStr; E: ExtStr; N: NameStr;
  193. begin
  194. FSplit(S,D,N,E);
  195. if (D<>'') and (D[Length(D)]<>DirSep) then
  196. DirOf:=D+DirSep
  197. else
  198. DirOf:=D;
  199. end;
  200. function ExtOf(const S: string): string;
  201. var D: DirStr; E: ExtStr; N: NameStr;
  202. begin
  203. FSplit(S,D,N,E);
  204. ExtOf:=E;
  205. end;
  206. function NameOf(const S: string): string;
  207. var D: DirStr; E: ExtStr; N: NameStr;
  208. begin
  209. FSplit(S,D,N,E);
  210. NameOf:=N;
  211. end;
  212. function NameAndExtOf(const S: string): string;
  213. var D: DirStr; E: ExtStr; N: NameStr;
  214. begin
  215. FSplit(S,D,N,E);
  216. NameAndExtOf:=N+E;
  217. end;
  218. function DirAndNameOf(const S: string): string;
  219. var D: DirStr; E: ExtStr; N: NameStr;
  220. begin
  221. FSplit(S,D,N,E);
  222. DirAndNameOf:=D+N;
  223. end;
  224. function EatIO: integer;
  225. begin
  226. EatIO:=IOResult;
  227. end;
  228. function LowCase(C: char): char;
  229. begin
  230. if ('A'<=C) and (C<='Z') then C:=chr(ord(C)+32);
  231. LowCase:=C;
  232. end;
  233. function LowcaseStr(S: string): string;
  234. var I: Longint;
  235. begin
  236. for I:=1 to length(S) do
  237. S[I]:=Lowcase(S[I]);
  238. LowcaseStr:=S;
  239. end;
  240. procedure TNoDisposeCollection.FreeItem(Item: Pointer);
  241. begin
  242. { don't do anything here }
  243. end;
  244. function TUnsortedStringCollection.At(Index: Integer): PString;
  245. begin
  246. At:=inherited At(Index);
  247. end;
  248. procedure TUnsortedStringCollection.FreeItem(Item: Pointer);
  249. begin
  250. if Item<>nil then DisposeStr(Item);
  251. end;
  252. constructor TSubStream.Init(AStream: PStream; AStartPos, ASize: longint);
  253. begin
  254. inherited Init;
  255. S:=AStream; StartPos:=AStartPos; Size:=ASize;
  256. inherited Seek(StartPos);
  257. end;
  258. function TSubStream.GetPos: Longint;
  259. var Pos: longint;
  260. begin
  261. Pos:=inherited GetPos; Dec(Pos,StartPos);
  262. GetPos:=Pos;
  263. end;
  264. function TSubStream.GetSize: Longint;
  265. begin
  266. GetSize:=Size;
  267. end;
  268. procedure TSubStream.Read(var Buf; Count: Word);
  269. var Pos: longint;
  270. RCount: word;
  271. begin
  272. Pos:=GetPos;
  273. if Pos+Count>Size then RCount:=Size-Pos else RCount:=Count;
  274. inherited Read(Buf,RCount);
  275. if RCount<Count then
  276. Error(stReadError,0);
  277. end;
  278. procedure TSubStream.Seek(Pos: Longint);
  279. var RPos: longint;
  280. begin
  281. if (Pos<=Size) then RPos:=Pos else RPos:=Size;
  282. inherited Seek(StartPos+RPos);
  283. end;
  284. procedure TSubStream.Write(var Buf; Count: Word);
  285. begin
  286. inherited Write(Buf,Count);
  287. end;
  288. END.
  289. {
  290. $Log$
  291. Revision 1.4 1999-04-07 21:56:06 peter
  292. + object support for browser
  293. * html help fixes
  294. * more desktop saving things
  295. * NODEBUG directive to exclude debugger
  296. Revision 1.2 1999/03/08 14:58:22 peter
  297. + prompt with dialogs for tools
  298. Revision 1.1 1999/03/01 15:51:43 peter
  299. + Log
  300. }