wutils.pas 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. PNulStream = ^TNulStream;
  31. TNulStream = object(TStream)
  32. constructor Init;
  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. end;
  39. PSubStream = ^TSubStream;
  40. TSubStream = object(TStream)
  41. constructor Init(AStream: PStream; AStartPos, ASize: longint);
  42. function GetPos: Longint; virtual;
  43. function GetSize: Longint; virtual;
  44. procedure Read(var Buf; Count: Word); virtual;
  45. procedure Seek(Pos: Longint); virtual;
  46. procedure Write(var Buf; Count: Word); virtual;
  47. private
  48. StartPos: longint;
  49. S : PStream;
  50. end;
  51. {$ifdef TPUNIXLF}
  52. procedure readln(var t:text;var s:string);
  53. {$endif}
  54. procedure readlnfromstream(Stream: PStream; var s:string);
  55. function eofstream(s: pstream): boolean;
  56. function Min(A,B: longint): longint;
  57. function Max(A,B: longint): longint;
  58. function CharStr(C: char; Count: byte): string;
  59. function UpcaseStr(const S: string): string;
  60. function LowCase(C: char): char;
  61. function LowcaseStr(S: string): string;
  62. function RExpand(const S: string; MinLen: byte): string;
  63. function LTrim(const S: string): string;
  64. function RTrim(const S: string): string;
  65. function Trim(const S: string): string;
  66. function IntToStr(L: longint): string;
  67. function StrToInt(const S: string): longint;
  68. function GetStr(P: PString): string;
  69. function DirOf(const S: string): string;
  70. function ExtOf(const S: string): string;
  71. function NameOf(const S: string): string;
  72. function NameAndExtOf(const S: string): string;
  73. function DirAndNameOf(const S: string): string;
  74. function EatIO: integer;
  75. const LastStrToIntResult : integer = 0;
  76. DirSep : char = {$ifdef Linux}'/'{$else}'\'{$endif};
  77. implementation
  78. uses
  79. Dos;
  80. {$ifdef TPUNIXLF}
  81. procedure readln(var t:text;var s:string);
  82. var
  83. c : char;
  84. i : longint;
  85. begin
  86. if TextRec(t).UserData[1]=2 then
  87. system.readln(t,s)
  88. else
  89. begin
  90. c:=#0;
  91. i:=0;
  92. while (not eof(t)) and (c<>#10) do
  93. begin
  94. read(t,c);
  95. if c<>#10 then
  96. begin
  97. inc(i);
  98. s[i]:=c;
  99. end;
  100. end;
  101. if (i>0) and (s[i]=#13) then
  102. begin
  103. dec(i);
  104. TextRec(t).UserData[1]:=2;
  105. end;
  106. s[0]:=chr(i);
  107. end;
  108. end;
  109. {$endif}
  110. function eofstream(s: pstream): boolean;
  111. begin
  112. eofstream:=(s^.getpos>=s^.getsize);
  113. end;
  114. procedure readlnfromstream(Stream: PStream; var S:string);
  115. var
  116. c : char;
  117. i : longint;
  118. begin
  119. c:=#0;
  120. i:=0;
  121. { this created problems for lines longer than 255 characters
  122. now those lines are cutted into pieces without warning PM }
  123. while (not eofstream(stream)) and (c<>#10) and (i<255) do
  124. begin
  125. stream^.read(c,sizeof(c));
  126. if c<>#10 then
  127. begin
  128. inc(i);
  129. s[i]:=c;
  130. end;
  131. end;
  132. if (i>0) and (s[i]=#13) then
  133. dec(i);
  134. s[0]:=chr(i);
  135. end;
  136. function Max(A,B: longint): longint;
  137. begin
  138. if A>B then Max:=A else Max:=B;
  139. end;
  140. function Min(A,B: longint): longint;
  141. begin
  142. if A<B then Min:=A else Min:=B;
  143. end;
  144. function CharStr(C: char; Count: byte): string;
  145. {$ifndef FPC}
  146. var S: string;
  147. {$endif}
  148. begin
  149. {$ifdef FPC}
  150. CharStr[0]:=chr(Count);
  151. FillChar(CharStr[1],Count,C);
  152. {$else}
  153. S[0]:=chr(Count);
  154. FillChar(S[1],Count,C);
  155. CharStr:=S;
  156. {$endif}
  157. end;
  158. function UpcaseStr(const S: string): string;
  159. var
  160. I: Longint;
  161. begin
  162. for I:=1 to length(S) do
  163. if S[I] in ['a'..'z'] then
  164. UpCaseStr[I]:=chr(ord(S[I])-32)
  165. else
  166. UpCaseStr[I]:=S[I];
  167. UpcaseStr[0]:=S[0];
  168. end;
  169. function LowerCaseStr(S: string): string;
  170. var
  171. I: Longint;
  172. begin
  173. for I:=1 to length(S) do
  174. if S[I] in ['A'..'Z'] then
  175. LowerCaseStr[I]:=chr(ord(S[I])+32)
  176. else
  177. LowerCaseStr[I]:=S[I];
  178. LowercaseStr[0]:=S[0];
  179. end;
  180. function RExpand(const S: string; MinLen: byte): string;
  181. begin
  182. if length(S)<MinLen then
  183. RExpand:=S+CharStr(' ',MinLen-length(S))
  184. else
  185. RExpand:=S;
  186. end;
  187. function LTrim(const S: string): string;
  188. var
  189. i : longint;
  190. begin
  191. i:=1;
  192. while (i<length(s)) and (s[i]=' ') do
  193. inc(i);
  194. LTrim:=Copy(s,i,255);
  195. end;
  196. function RTrim(const S: string): string;
  197. var
  198. i : longint;
  199. begin
  200. i:=length(s);
  201. while (i>0) and (s[i]=' ') do
  202. dec(i);
  203. RTrim:=Copy(s,1,i);
  204. end;
  205. function Trim(const S: string): string;
  206. begin
  207. Trim:=RTrim(LTrim(S));
  208. end;
  209. function IntToStr(L: longint): string;
  210. var S: string;
  211. begin
  212. Str(L,S);
  213. IntToStr:=S;
  214. end;
  215. function StrToInt(const S: string): longint;
  216. var L: longint;
  217. C: integer;
  218. begin
  219. Val(S,L,C); if C<>0 then L:=-1;
  220. LastStrToIntResult:=C;
  221. StrToInt:=L;
  222. end;
  223. function GetStr(P: PString): string;
  224. begin
  225. if P=nil then GetStr:='' else GetStr:=P^;
  226. end;
  227. function DirOf(const S: string): string;
  228. var D: DirStr; E: ExtStr; N: NameStr;
  229. begin
  230. FSplit(S,D,N,E);
  231. if (D<>'') and (D[Length(D)]<>DirSep) then
  232. DirOf:=D+DirSep
  233. else
  234. DirOf:=D;
  235. end;
  236. function ExtOf(const S: string): string;
  237. var D: DirStr; E: ExtStr; N: NameStr;
  238. begin
  239. FSplit(S,D,N,E);
  240. ExtOf:=E;
  241. end;
  242. function NameOf(const S: string): string;
  243. var D: DirStr; E: ExtStr; N: NameStr;
  244. begin
  245. FSplit(S,D,N,E);
  246. NameOf:=N;
  247. end;
  248. function NameAndExtOf(const S: string): string;
  249. var D: DirStr; E: ExtStr; N: NameStr;
  250. begin
  251. FSplit(S,D,N,E);
  252. NameAndExtOf:=N+E;
  253. end;
  254. function DirAndNameOf(const S: string): string;
  255. var D: DirStr; E: ExtStr; N: NameStr;
  256. begin
  257. FSplit(S,D,N,E);
  258. DirAndNameOf:=D+N;
  259. end;
  260. function EatIO: integer;
  261. begin
  262. EatIO:=IOResult;
  263. end;
  264. function LowCase(C: char): char;
  265. begin
  266. if ('A'<=C) and (C<='Z') then C:=chr(ord(C)+32);
  267. LowCase:=C;
  268. end;
  269. function LowcaseStr(S: string): string;
  270. var I: Longint;
  271. begin
  272. for I:=1 to length(S) do
  273. S[I]:=Lowcase(S[I]);
  274. LowcaseStr:=S;
  275. end;
  276. procedure TNoDisposeCollection.FreeItem(Item: Pointer);
  277. begin
  278. { don't do anything here }
  279. end;
  280. function TUnsortedStringCollection.At(Index: Integer): PString;
  281. begin
  282. At:=inherited At(Index);
  283. end;
  284. procedure TUnsortedStringCollection.FreeItem(Item: Pointer);
  285. begin
  286. if Item<>nil then DisposeStr(Item);
  287. end;
  288. constructor TNulStream.Init;
  289. begin
  290. inherited Init;
  291. Position:=0;
  292. end;
  293. function TNulStream.GetPos: Longint;
  294. begin
  295. GetPos:=Position;
  296. end;
  297. function TNulStream.GetSize: Longint;
  298. begin
  299. GetSize:=Position;
  300. end;
  301. procedure TNulStream.Read(var Buf; Count: Word);
  302. begin
  303. Error(stReadError,0);
  304. end;
  305. procedure TNulStream.Seek(Pos: Longint);
  306. begin
  307. if Pos<=Position then
  308. Position:=Pos;
  309. end;
  310. procedure TNulStream.Write(var Buf; Count: Word);
  311. begin
  312. Inc(Position,Count);
  313. end;
  314. constructor TSubStream.Init(AStream: PStream; AStartPos, ASize: longint);
  315. begin
  316. inherited Init;
  317. if Assigned(AStream)=false then Fail;
  318. S:=AStream; StartPos:=AStartPos; StreamSize:=ASize;
  319. Seek(0);
  320. end;
  321. function TSubStream.GetPos: Longint;
  322. var Pos: longint;
  323. begin
  324. Pos:=S^.GetPos; Dec(Pos,StartPos);
  325. GetPos:=Pos;
  326. end;
  327. function TSubStream.GetSize: Longint;
  328. begin
  329. GetSize:=StreamSize;
  330. end;
  331. procedure TSubStream.Read(var Buf; Count: Word);
  332. var Pos: longint;
  333. RCount: word;
  334. begin
  335. Pos:=GetPos;
  336. if Pos+Count>StreamSize then RCount:=StreamSize-Pos else RCount:=Count;
  337. S^.Read(Buf,RCount);
  338. if RCount<Count then
  339. Error(stReadError,0);
  340. end;
  341. procedure TSubStream.Seek(Pos: Longint);
  342. var RPos: longint;
  343. begin
  344. if (Pos<=StreamSize) then RPos:=Pos else RPos:=StreamSize;
  345. S^.Seek(StartPos+RPos);
  346. end;
  347. procedure TSubStream.Write(var Buf; Count: Word);
  348. begin
  349. S^.Write(Buf,Count);
  350. end;
  351. END.
  352. {
  353. $Log$
  354. Revision 1.6 1999-08-24 22:01:48 pierre
  355. * readlnfromstream length check added
  356. Revision 1.5 1999/08/03 20:22:45 peter
  357. + TTab acts now on Ctrl+Tab and Ctrl+Shift+Tab...
  358. + Desktop saving should work now
  359. - History saved
  360. - Clipboard content saved
  361. - Desktop saved
  362. - Symbol info saved
  363. * syntax-highlight bug fixed, which compared special keywords case sensitive
  364. (for ex. 'asm' caused asm-highlighting, while 'ASM' didn't)
  365. * with 'whole words only' set, the editor didn't found occourences of the
  366. searched text, if the text appeared previously in the same line, but didn't
  367. satisfied the 'whole-word' condition
  368. * ^QB jumped to (SelStart.X,SelEnd.X) instead of (SelStart.X,SelStart.Y)
  369. (ie. the beginning of the selection)
  370. * when started typing in a new line, but not at the start (X=0) of it,
  371. the editor inserted the text one character more to left as it should...
  372. * TCodeEditor.HideSelection (Ctrl-K+H) didn't update the screen
  373. * Shift shouldn't cause so much trouble in TCodeEditor now...
  374. * Syntax highlight had problems recognizing a special symbol if it was
  375. prefixed by another symbol character in the source text
  376. * Auto-save also occours at Dos shell, Tool execution, etc. now...
  377. Revision 1.4 1999/04/07 21:56:06 peter
  378. + object support for browser
  379. * html help fixes
  380. * more desktop saving things
  381. * NODEBUG directive to exclude debugger
  382. Revision 1.2 1999/03/08 14:58:22 peter
  383. + prompt with dialogs for tools
  384. Revision 1.1 1999/03/01 15:51:43 peter
  385. + Log
  386. }