syspch.inc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. {
  2. *********************************************************************
  3. $Id$
  4. Copyright (C) 1997, 1998 Gertjan Schouten
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. *********************************************************************
  17. System Utilities For Free Pascal
  18. }
  19. { PChar functions }
  20. type
  21. pbyte = ^byte;
  22. CharArray = array[0..0] of char;
  23. { StrLen returns the length of Str ( terminator not included ) }
  24. function StrLen(Str: PChar): cardinal;
  25. begin
  26. result := 0;
  27. if Str <> nil then begin
  28. while CharArray(Str^)[result] <> #0 do
  29. result := result + 1;
  30. end ;
  31. end ;
  32. { StrEnd returns a pointer to the last character (terminator) of Str }
  33. function StrEnd(Str: PChar): PChar;
  34. begin
  35. result := Str;
  36. if Str <> nil then begin
  37. while result^ <> #0 do
  38. result := result + 1;
  39. end ;
  40. end ;
  41. { StrMove copies Count bytes from source to dest, source and dest may overlap. }
  42. function StrMove(Dest, Source: PChar; Count: cardinal): PChar;
  43. begin
  44. result := Dest;
  45. if (Dest <> nil) and (Source <> nil) and (Count > 0) then
  46. move(Source^, Dest^, Count);
  47. end ;
  48. { StrCopy copies StrLen(Source) characters from Source to Dest and returns Dest }
  49. function StrCopy(Dest, Source: PChar): PChar;
  50. begin
  51. result := StrMove(Dest, Source, 1 + StrLen(Source)); { copy nul character too ! }
  52. end ;
  53. { StrECopy copies StrLen(Source) characters from Source to Dest and returns StrEnd(Dest) }
  54. function StrECopy(Dest, Source: PChar): PChar;
  55. begin
  56. StrMove(Dest, Source, 1 + StrLen(Source));
  57. result := StrEnd(Dest);
  58. end ;
  59. { StrLCopy copies MaxLen or less characters from Source to Dest and returns Dest }
  60. function StrLCopy(Dest, Source: PChar; MaxLen: cardinal): PChar;
  61. var count: cardinal;
  62. begin
  63. result := Dest;
  64. if result <> Nil then begin
  65. count := StrLen(Source);
  66. if count > MaxLen then
  67. count := MaxLen;
  68. StrMove(Dest, Source, count);
  69. CharArray(result^)[Count] := #0; { terminate ! }
  70. end ;
  71. end ;
  72. { StrPCopy copies the pascal string Source to Dest and returns Dest }
  73. function StrPCopy(Dest: PChar; Source: string): PChar;
  74. begin
  75. result := StrMove(Dest, PChar(@Source[1]), length(Source));
  76. end ;
  77. { StrPLCopy copies MaxLen or less characters from the pascal string
  78. Source to Dest and returns Dest }
  79. function StrPLCopy(Dest: PChar; Source: string; MaxLen: cardinal): PChar;
  80. var Count: cardinal;
  81. begin
  82. result := Dest;
  83. if (Result <> Nil) and (MaxLen <> 0) then begin
  84. Count := Length(Source);
  85. if Count > MaxLen then
  86. Count := MaxLen;
  87. StrMove(Dest, PChar(@Source[1]), Count);
  88. CharArray(result^)[Count] := #0; { terminate ! }
  89. end ;
  90. end ;
  91. { StrCat concatenates Dest and Source and returns Dest }
  92. function StrCat(Dest, Source: PChar): PChar;
  93. begin
  94. result := Dest;
  95. StrMove(StrEnd(Dest), Source, 1 + StrLen(Source)); { include #0 }
  96. end ;
  97. { StrLCat concatenates Dest and MaxLen - StrLen(Dest) (or less) characters
  98. from Source, and returns Dest }
  99. function StrLCat(Dest, Source: PChar; MaxLen: cardinal): PChar;
  100. var Count: cardinal; P: PChar;
  101. begin
  102. result := Dest;
  103. if (Dest <> nil) and (MaxLen <> 0) then begin
  104. P := StrEnd(Dest);
  105. Count := StrLen(Source);
  106. if Count > MaxLen - (P - Dest) then
  107. Count := MaxLen - (P - Dest);
  108. if Count <> 0 then begin
  109. StrMove(P, Source, Count);
  110. CharArray(p^)[Count] := #0; { terminate Dest }
  111. end ;
  112. end ;
  113. end ;
  114. { StrComp returns 0 if Str1 and Str2 are equal,
  115. a value less than 0 in case Str1 < Str2
  116. and a value greater than 0 in case Str1 > Str2 }
  117. function StrComp(Str1, Str2: PChar): integer;
  118. begin
  119. result := 0;
  120. if (Str1 <> Nil) and (Str2 <> Nil) then begin
  121. while result = 0 do begin
  122. result := byte(Str1^) - byte(Str2^);
  123. if (Str1^ = #0) or (Str2^ = #0) then break;
  124. Str1 := Str1 + 1;
  125. Str2 := Str2 + 1;
  126. end ;
  127. end ;
  128. end ;
  129. { StrIComp returns 0 if Str1 and Str2 are equal,
  130. a value less than 0 in case Str1 < Str2
  131. and a value greater than 0 in case Str1 > Str2;
  132. comparison is case insensitive }
  133. function StrIComp(Str1, Str2: PChar): integer;
  134. var Chr1, Chr2: byte;
  135. begin
  136. result := 0;
  137. if (Str1 <> Nil) and (Str2 <> Nil) then begin
  138. while result = 0 do begin
  139. Chr1 := byte(Str1^);
  140. Chr2 := byte(Str2^);
  141. if Chr1 in [97..122] then Chr1 := Chr1 - 32;
  142. if Chr2 in [97..122] then Chr2 := Chr2 - 32;
  143. result := Chr1 - Chr2;
  144. if (Chr1 = 0) or (Chr2 = 0) then break;
  145. Str1 := Str1 + 1;
  146. Str2 := Str2 + 1;
  147. end ;
  148. end ;
  149. end ;
  150. { StrLComp returns 0 if Str1 and Str2 are equal,
  151. a value less than 0 in case Str1 < Str2
  152. and a value greater than 0 in case Str1 > Str2;
  153. MaxLen or less characters are compared }
  154. function StrLComp(Str1, Str2: PChar; MaxLen: cardinal): integer;
  155. var I: integer;
  156. begin
  157. result := 0;
  158. if (Str1 <> Nil) and (Str2 <> Nil) then begin
  159. I := 0;
  160. while (I < MaxLen) and (result = 0) do begin
  161. result := byte(Str1^) - byte(Str2^);
  162. if (Str1^ = #0) or (Str2^ = #0) then break;
  163. Str1 := Str1 + 1;
  164. Str2 := Str2 + 1;
  165. I := I + 1;
  166. end ;
  167. end ;
  168. end ;
  169. { StrLIComp returns 0 if Str1 and Str2 are equal,
  170. a value less than 0 in case Str1 < Str2
  171. and a value greater than 0 in case Str1 > Str2;
  172. comparison is case insensitive and MaxLen or less characters are compared }
  173. function StrLIComp(Str1, Str2: PChar; MaxLen: cardinal): integer;
  174. var Chr1, Chr2: byte; I: integer;
  175. begin
  176. result := 0;
  177. if (Str1 <> Nil) and (Str2 <> Nil) then begin
  178. I := 0;
  179. while (I < MaxLen) and (result = 0) do begin
  180. Chr1 := byte(Str1^);
  181. Chr2 := byte(Str2^);
  182. if Chr1 in [97..122] then Chr1 := Chr1 - 32;
  183. if Chr2 in [97..122] then Chr2 := Chr2 - 32;
  184. result := Chr1 - Chr2;
  185. if (Chr1 = 0) or (Chr2 = 0) then break;
  186. Str1 := Str1 + 1;
  187. Str2 := Str2 + 1;
  188. I := I + 1;
  189. end ;
  190. end ;
  191. end ;
  192. { StrScan returns a PChar to the first character Chr in Str }
  193. function StrScan(Str: PChar; Chr: char): PChar;
  194. var P: PChar;
  195. begin
  196. result := Nil;
  197. if Str <> Nil then begin
  198. P := Str;
  199. while (P^ <> #0) and (P^ <> Chr) do
  200. P := P + 1;
  201. if P^ = Chr then result := P;
  202. end ;
  203. end ;
  204. { StrRScan returns a PChar to the last character Chr in Str }
  205. function StrRScan(Str: PChar; Chr: char): PChar;
  206. var P: PChar;
  207. begin
  208. result := Nil;
  209. if Str <> Nil then begin
  210. P := StrEnd(Str);
  211. While (P^ <> Chr) and (P <> Str) do
  212. P := P - 1;
  213. If P^ = Chr then result := P;
  214. end ;
  215. end ;
  216. { StrPos returns a PChar to the first occurance of Str2 contained in Str1
  217. if no occurance can be found StrPos returns Nil }
  218. function StrPos(Str1, Str2: PChar): PChar;
  219. var E: PChar; Count1, Count2: Cardinal;
  220. begin
  221. Count1 := StrLen(Str1);
  222. Count2 := StrLen(Str2);
  223. if (Str1 <> Nil) and (Str2 <> Nil) and (Count1 > 0) and (Count1 >= Count2) then begin
  224. E := Str1 + 1 + Count1 - Count2;
  225. result := Str1;
  226. While result <> E do begin
  227. if StrLComp(result, Str2, Count2) = 0 then
  228. exit;
  229. result := result + 1;
  230. end ;
  231. end ;
  232. result := Nil;
  233. end ;
  234. { StrUpper converts all lowercase characters in Str to uppercase }
  235. function StrUpper(Str: PChar): PChar;
  236. begin
  237. Result := Str;
  238. if Str <> Nil then begin
  239. While Str^ <> #0 do begin
  240. if Str^ in ['a'..'z'] then
  241. dec(byte(Str^), 32);
  242. Str := Str + 1;
  243. end ;
  244. end ;
  245. end ;
  246. { StrLower converts all uppercase characters in Str to lowercase }
  247. function StrLower(Str: PChar): PChar;
  248. begin
  249. Result := Str;
  250. if Str <> Nil then begin
  251. While Str^ <> #0 do begin
  252. if Str^ in ['A'..'Z'] then
  253. inc(byte(Str^), 32);
  254. Str := Str + 1;
  255. end ;
  256. end ;
  257. end ;
  258. { StrPas converts a PChar to a pascal string }
  259. function StrPas(Str: PChar): string;
  260. begin
  261. SetLength(result, StrLen(Str));
  262. Move(Str^, result[1], Length(result));
  263. end ;
  264. { StrAlloc allocates a buffer of Size + 4
  265. the size of the allocated buffer is stored at result - 4
  266. StrDispose should be used to destroy the buffer }
  267. function StrAlloc(Size: cardinal): PChar;
  268. var Temp: pointer;
  269. begin
  270. GetMem(Temp, Size + SizeOf(cardinal));
  271. Move(Size, Temp^, SizeOf(cardinal));
  272. pbyte(Temp + SizeOf(cardinal))^ := 0;
  273. result := PChar(Temp + SizeOf(cardinal));
  274. end ;
  275. { StrBufSize returns the amount of memory allocated for pchar Str allocated with StrAlloc }
  276. function StrBufSize(var Str: PChar): cardinal;
  277. begin
  278. if Str <> Nil then
  279. result := Cardinal(pointer(Str - SizeOf(cardinal))^)
  280. else
  281. result := 0;
  282. end ;
  283. { StrNew creates an exact copy of Str }
  284. function StrNew(Str: PChar): PChar;
  285. begin
  286. if Str <> Nil then begin
  287. result := StrAlloc(1 + StrLen(Str));
  288. StrCopy(result, Str);
  289. end
  290. else result := Nil;
  291. end ;
  292. { StrDispose clears the memory allocated with StrAlloc }
  293. procedure StrDispose(var Str: PChar);
  294. var Size: cardinal;
  295. begin
  296. if (Str <> Nil) then begin
  297. Str := PChar(Str - SizeOf(cardinal));
  298. Move(Str^, Size, SizeOf(cardinal));
  299. FreeMem(Str, Size + SizeOf(cardinal));
  300. Str := Nil;
  301. end ;
  302. end ;
  303. {
  304. $Log$
  305. Revision 1.2 1998-09-16 08:28:40 michael
  306. Update from gertjan Schouten, plus small fix for linux
  307. 1998/08/26 Gertjan
  308. Most functions rewritten in pascal.
  309. Revision 1.1 1998/04/10 15:17:46 michael
  310. + Initial implementation; Donated by Gertjan Schouten
  311. His file was split into several files, to keep it a little bit structured.
  312. }