wutils.pas 11 KB

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