wutils.pas 11 KB

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