wutils.pas 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. PUnsortedStringCollection = ^TUnsortedStringCollection;
  22. TUnsortedStringCollection = object(TCollection)
  23. function At(Index: Integer): PString;
  24. procedure FreeItem(Item: Pointer); virtual;
  25. end;
  26. {$ifdef TPUNIXLF}
  27. procedure readln(var t:text;var s:string);
  28. {$endif}
  29. function Min(A,B: longint): longint;
  30. function Max(A,B: longint): longint;
  31. function CharStr(C: char; Count: byte): string;
  32. function UpcaseStr(const S: string): string;
  33. function RExpand(const S: string; MinLen: byte): string;
  34. function LTrim(const S: string): string;
  35. function RTrim(const S: string): string;
  36. function Trim(const S: string): string;
  37. function IntToStr(L: longint): string;
  38. function StrToInt(const S: string): longint;
  39. function GetStr(P: PString): string;
  40. function EatIO: integer;
  41. const LastStrToIntResult : integer = 0;
  42. implementation
  43. uses
  44. Dos;
  45. {$ifdef TPUNIXLF}
  46. procedure readln(var t:text;var s:string);
  47. var
  48. c : char;
  49. i : longint;
  50. begin
  51. if TextRec(t).UserData[1]=2 then
  52. system.readln(t,s)
  53. else
  54. begin
  55. c:=#0;
  56. i:=0;
  57. while (not eof(t)) and (c<>#10) do
  58. begin
  59. read(t,c);
  60. if c<>#10 then
  61. begin
  62. inc(i);
  63. s[i]:=c;
  64. end;
  65. end;
  66. if (i>0) and (s[i]=#13) then
  67. begin
  68. dec(i);
  69. TextRec(t).UserData[1]:=2;
  70. end;
  71. s[0]:=chr(i);
  72. end;
  73. end;
  74. {$endif}
  75. function Max(A,B: longint): longint;
  76. begin
  77. if A>B then Max:=A else Max:=B;
  78. end;
  79. function Min(A,B: longint): longint;
  80. begin
  81. if A<B then Min:=A else Min:=B;
  82. end;
  83. function CharStr(C: char; Count: byte): string;
  84. var S: string;
  85. begin
  86. S[0]:=chr(Count);
  87. FillChar(S[1],Count,C);
  88. CharStr:=S;
  89. end;
  90. function UpcaseStr(const S: string): string;
  91. var
  92. I: Longint;
  93. begin
  94. for I:=1 to length(S) do
  95. if S[I] in ['a'..'z'] then
  96. UpCaseStr[I]:=chr(ord(S[I])-32)
  97. else
  98. UpCaseStr[I]:=S[I];
  99. UpcaseStr[0]:=S[0];
  100. end;
  101. function LowerCaseStr(S: string): string;
  102. var
  103. I: Longint;
  104. begin
  105. for I:=1 to length(S) do
  106. if S[I] in ['A'..'Z'] then
  107. LowerCaseStr[I]:=chr(ord(S[I])+32)
  108. else
  109. LowerCaseStr[I]:=S[I];
  110. LowercaseStr[0]:=S[0];
  111. end;
  112. function RExpand(const S: string; MinLen: byte): string;
  113. begin
  114. if length(S)<MinLen then
  115. RExpand:=S+CharStr(' ',MinLen-length(S))
  116. else
  117. RExpand:=S;
  118. end;
  119. function LTrim(const S: string): string;
  120. var
  121. i : longint;
  122. begin
  123. i:=1;
  124. while (i<length(s)) and (s[i]=' ') do
  125. inc(i);
  126. LTrim:=Copy(s,i,255);
  127. end;
  128. function RTrim(const S: string): string;
  129. var
  130. i : longint;
  131. begin
  132. i:=length(s);
  133. while (i>0) and (s[i]=' ') do
  134. dec(i);
  135. RTrim:=Copy(s,1,i);
  136. end;
  137. function Trim(const S: string): string;
  138. begin
  139. Trim:=RTrim(LTrim(S));
  140. end;
  141. function IntToStr(L: longint): string;
  142. var S: string;
  143. begin
  144. Str(L,S);
  145. IntToStr:=S;
  146. end;
  147. function StrToInt(const S: string): longint;
  148. var L: longint;
  149. C: integer;
  150. begin
  151. Val(S,L,C); if C<>0 then L:=-1;
  152. LastStrToIntResult:=C;
  153. StrToInt:=L;
  154. end;
  155. function GetStr(P: PString): string;
  156. begin
  157. if P=nil then GetStr:='' else GetStr:=P^;
  158. end;
  159. function EatIO: integer;
  160. begin
  161. EatIO:=IOResult;
  162. end;
  163. function TUnsortedStringCollection.At(Index: Integer): PString;
  164. begin
  165. At:=inherited At(Index);
  166. end;
  167. procedure TUnsortedStringCollection.FreeItem(Item: Pointer);
  168. begin
  169. if Item<>nil then DisposeStr(Item);
  170. end;
  171. END.
  172. {
  173. $Log$
  174. Revision 1.2 1999-03-08 14:58:22 peter
  175. + prompt with dialogs for tools
  176. Revision 1.1 1999/03/01 15:51:43 peter
  177. + Log
  178. }