sysstr.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. { NewStr creates a new PString and assigns S to it
  20. if length(s) = 0 NewStr returns Nil }
  21. function NewStr(const S: string): PString;
  22. begin
  23. result := Nil;
  24. {
  25. if Length(S) <> 0 then begin
  26. result := New(PString);
  27. result^ := S;
  28. end ;
  29. }
  30. end ;
  31. { DisposeStr frees the memory occupied by S }
  32. procedure DisposeStr(S: PString);
  33. begin
  34. {
  35. if S <> Nil then begin
  36. Dispose(S);
  37. S := Nil;
  38. end ;
  39. }
  40. end ;
  41. { AssignStr assigns S to P^ }
  42. procedure AssignStr(var P: PString; const S: string);
  43. begin
  44. P^ := s;
  45. end ;
  46. { AppendStr appends S to Dest }
  47. procedure AppendStr(var Dest: PString; const S: string);
  48. begin
  49. Dest^ := Dest^ + S;
  50. end ;
  51. { UpperCase returns a copy of S where all lowercase characters ( from a to z )
  52. have been converted to uppercase }
  53. function UpperCase(const S: string): string;
  54. var i: integer;
  55. begin
  56. result := S;
  57. i := Length(S);
  58. while i <> 0 do begin
  59. if (result[i] in ['a'..'z']) then result[i] := char(byte(result[i]) - 32);
  60. Dec(i);
  61. end;
  62. end;
  63. { LowerCase returns a copy of S where all uppercase characters ( from A to Z )
  64. have been converted to lowercase }
  65. function LowerCase(const S: string): string;
  66. var i: integer;
  67. begin
  68. result := S;
  69. i := Length(result);
  70. while i <> 0 do begin
  71. if (result[i] in ['A'..'Z']) then result[i] := char(byte(result[i]) + 32);
  72. dec(i);
  73. end;
  74. end;
  75. { CompareStr compares S1 and S2, the result is the based on
  76. substraction of the ascii values of the characters in S1 and S2
  77. case result
  78. S1 < S2 < 0
  79. S1 > S2 > 0
  80. S1 = S2 = 0 }
  81. function CompareStr(const S1, S2: string): Integer;
  82. var i, count, count1, count2: integer;
  83. begin
  84. result := 0;
  85. Count1 := Length(S1);
  86. Count2 := Length(S2);
  87. if Count1 > Count2 then Count := Count2
  88. else Count := Count1;
  89. result := CompareMem(@S1[1], @S2[1], Count);
  90. if (result = 0) and (Count1 <> Count2) then begin
  91. if Count1 > Count2 then result := ord(s1[Count1 + 1])
  92. else result := -ord(s2[Count2 + 1]);
  93. end ;
  94. end ;
  95. { CompareMem returns the result of comparison of Length bytes at P1 and P2
  96. case result
  97. P1 < P2 < 0
  98. P1 > P2 > 0
  99. P1 = P2 = 0 }
  100. function CompareMem(P1, P2: Pointer; Length: cardinal): integer;
  101. var i: integer;
  102. begin
  103. i := 0;
  104. result := 0;
  105. while (result = 0) and (i < length) do begin
  106. result := byte(P1^) - byte(P2^);
  107. P1 := P1 + 1;
  108. P2 := P2 + 1;
  109. i := i + 1;
  110. end ;
  111. end ;
  112. { CompareText compares S1 and S2, the result is the based on
  113. substraction of the ascii values of characters in S1 and S2
  114. comparison is case-insensitive
  115. case result
  116. S1 < S2 < 0
  117. S1 > S2 > 0
  118. S1 = S2 = 0 }
  119. function CompareText(const S1, S2: string): integer;
  120. var i, count, count1, count2: integer; Chr1, Chr2: byte;
  121. begin
  122. result := 0;
  123. Count1 := Length(S1);
  124. Count2 := Length(S2);
  125. if Count1 > Count2 then Count := Count2
  126. else Count := Count1;
  127. i := 0;
  128. while (result = 0) and (i < count) do begin
  129. i := i + 1;
  130. Chr1 := byte(s1[i]);
  131. Chr2 := byte(s2[i]);
  132. if Chr1 in [97..122] then Chr1 := Chr1 - 32;
  133. if Chr2 in [97..122] then Chr2 := Chr2 - 32;
  134. result := Chr1 - Chr2;
  135. end ;
  136. if (result = 0) and (Count1 <> Count2) then begin
  137. if Count1 > Count2 then result := byte(UpCase(s1[Count1 + 1]))
  138. else result := -byte(UpCase(s2[Count2 + 1]));
  139. end ;
  140. end ;
  141. {==============================================================================}
  142. { Ansi string functions }
  143. { these functions rely on the character set loaded by the OS }
  144. {==============================================================================}
  145. type
  146. TCaseTranslationTable = array[0..255] of char;
  147. var
  148. UpperCaseTable: TCaseTranslationTable;
  149. LowerCaseTable: TCaseTranslationTable;
  150. function AnsiUpperCase(const s: string): string;
  151. begin
  152. end ;
  153. function AnsiLowerCase(const s: string): string;
  154. begin
  155. end ;
  156. function AnsiCompareStr(const S1, S2: string): integer;
  157. begin
  158. end ;
  159. function AnsiCompareText(const S1, S2: string): integer;
  160. begin
  161. end ;
  162. function AnsiStrComp(S1, S2: PChar): integer;
  163. begin
  164. end ;
  165. function AnsiStrIComp(S1, S2: PChar): integer;
  166. begin
  167. end ;
  168. function AnsiStrLComp(S1, S2: PChar; MaxLen: cardinal): integer;
  169. begin
  170. end ;
  171. function AnsiStrLIComp(S1, S2: PChar; MaxLen: cardinal): integer;
  172. begin
  173. end ;
  174. function AnsiStrLower(Str: PChar): PChar;
  175. begin
  176. end ;
  177. function AnsiStrUpper(Str: PChar): PChar;
  178. begin
  179. end ;
  180. function AnsiLastChar(const S: string): PChar;
  181. begin
  182. end ;
  183. function AnsiStrLastChar(Str: PChar): PChar;
  184. begin
  185. end ;
  186. {==============================================================================}
  187. { End of Ansi functions }
  188. {==============================================================================}
  189. { Trim returns a copy of S with blanks characters on the left and right stripped off }
  190. function Trim(const S: string): string;
  191. var Ofs, Len: integer;
  192. begin
  193. len := Length(S);
  194. while (S[Len] = ' ') and (Len > 0) do
  195. dec(Len);
  196. Ofs := 1;
  197. while (S[Ofs] = ' ') and (Ofs <= Len) do
  198. Inc(Ofs);
  199. result := Copy(S, Ofs, 1 + Len - Ofs);
  200. end ;
  201. { TrimLeft returns a copy of S with all blank characters on the left stripped off }
  202. function TrimLeft(const S: string): string;
  203. var i,l:integer;
  204. begin
  205. l := length(s);
  206. i := 1;
  207. while (s[i] = ' ') and (i <= l) do inc(i);
  208. Result := copy(s, i, l);
  209. end ;
  210. { TrimRight returns a copy of S with all blank characters on the right stripped off }
  211. function TrimRight(const S: string): string;
  212. var l:integer;
  213. begin
  214. l := length(s);
  215. while (s[l] = ' ') and (l > 0) do dec(l);
  216. result := copy(s,1,l);
  217. end ;
  218. { QuotedStr returns S quoted left and right and every single quote in S
  219. replaced by two quotes }
  220. function QuotedStr(const S: string): string;
  221. begin
  222. result := AnsiQuotedStr(s, '''');
  223. end ;
  224. { AnsiQuotedStr returns S quoted left and right by Quote,
  225. and every single occurance of Quote replaced by two }
  226. function AnsiQuotedStr(const S: string; Quote: char): string;
  227. var i, j, count: integer;
  228. begin
  229. result := '' + Quote;
  230. count := length(s);
  231. i := 0;
  232. j := 0;
  233. while i < count do begin
  234. i := i + 1;
  235. if S[i] = Quote then begin
  236. result := result + copy(S, 1 + j, i - j) + Quote;
  237. j := i;
  238. end ;
  239. end ;
  240. if i <> j then
  241. result := result + copy(S, 1 + j, i - j);
  242. result := result + Quote;
  243. end ;
  244. { AnsiExtractQuotedStr returns a copy of Src with quote characters
  245. deleted to the left and right and double occurances
  246. of Quote replaced by a single Quote }
  247. function AnsiExtractQuotedStr(var Src: PChar; Quote: Char): string;
  248. var i: integer; P, Q: PChar;
  249. begin
  250. P := Src;
  251. if Src^ = Quote then P := P + 1;
  252. Q := StrEnd(P);
  253. if PChar(Q - 1)^ = Quote then Q := Q - 1;
  254. SetLength(result, Q - P);
  255. i := 0;
  256. while P <> Q do begin
  257. i := i + 1;
  258. result[i] := P^;
  259. if (P^ = Quote) and (PChar(P + 1)^ = Quote) then
  260. P := P + 1;
  261. P := P + 1;
  262. end ;
  263. SetLength(result, i);
  264. end ;
  265. { AdjustLineBreaks returns S with all CR characters not followed by LF
  266. replaced with CR/LF }
  267. // under Linux all CR characters or CR/LF combinations should be replaced with LF
  268. function AdjustLineBreaks(const S: string): string;
  269. var i, j, count: integer;
  270. begin
  271. result := '';
  272. i := 0;
  273. j := 0;
  274. count := Length(S);
  275. while i < count do begin
  276. i := i + 1;
  277. if (S[i] = #13) and ((i = count) or (S[i + 1] <> #10)) then begin
  278. result := result + Copy(S, 1 + j, i - j) + #10;
  279. j := i;
  280. end ;
  281. end ;
  282. if j <> i then
  283. result := result + copy(S, 1 + j, i - j);
  284. end ;
  285. { IsValidIdent returns true if the first character of Ident is in:
  286. 'A' to 'Z', 'a' to 'z' or '_' and the following characters are
  287. on of: 'A' to 'Z', 'a' to 'z', '0'..'9' or '_' }
  288. function IsValidIdent(const Ident: string): boolean;
  289. var i, len: integer;
  290. begin
  291. result := false;
  292. len := length(Ident);
  293. if len <> 0 then begin
  294. result := Ident[1] in ['A'..'Z', 'a'..'z', '_'];
  295. i := 1;
  296. while (result) and (i < len) do begin
  297. i := i + 1;
  298. result := result and (Ident[i] in ['A'..'Z', 'a'..'z', '0'..'9', '_']);
  299. end ;
  300. end ;
  301. end ;
  302. { IntToStr returns a string representing the value of Value }
  303. function IntToStr(Value: integer): string;
  304. begin
  305. System.Str(Value, result);
  306. end ;
  307. { IntToHex returns a string representing the hexadecimal value of Value }
  308. const
  309. HexDigits: array[0..15] of char = '0123456789ABCDEF';
  310. function IntToHex(Value: integer; Digits: integer): string;
  311. var i: integer;
  312. begin
  313. SetLength(result, digits);
  314. for i := 0 to digits - 1 do begin
  315. result[digits - i] := HexDigits[value and 15];
  316. value := value shr 4;
  317. end ;
  318. end ;
  319. { StrToInt converts the string S to an integer value,
  320. if S does not represent a valid integer value EConvertError is raised }
  321. function StrToInt(const S: string): integer;
  322. var Error: word;
  323. begin
  324. Val(S, result, Error);
  325. // if Error <> 0 then raise EConvertError.create(s + ' is not a valid integer');
  326. end ;
  327. { StrToIntDef converts the string S to an integer value,
  328. Default is returned in case S does not represent a valid integer value }
  329. function StrToIntDef(const S: string; Default: integer): integer;
  330. var Error: word;
  331. begin
  332. Val(S, result, Error);
  333. if Error <> 0 then result := Default;
  334. end ;
  335. { LoadStr returns the string resource Ident. }
  336. function LoadStr(Ident: integer): string;
  337. begin
  338. end ;
  339. { FmtLoadStr returns the string resource Ident and formats it accordingly }
  340. {
  341. function FmtLoadStr(Ident: integer; const Args: array of const): string;
  342. begin
  343. end ;
  344. }
  345. {==============================================================================}
  346. { extra functions }
  347. {==============================================================================}
  348. { SetLength sets the length of S to NewLength }
  349. // SetLength should be in the system unit
  350. // which lacks the ShortString version of SetLength
  351. function SetLength(var S: string; NewLength: integer): integer;
  352. begin
  353. if (NewLength > 255) then
  354. NewLength := 255;
  355. S[0] := char(NewLength);
  356. Result := Ord(S[0]);
  357. end ;
  358. { LeftStr returns Count left-most characters from S }
  359. function LeftStr(const S: string; Count: integer): string;
  360. begin
  361. result := Copy(S, 1, Count);
  362. end ;
  363. { RightStr returns Count right-most characters from S }
  364. function RightStr(const S: string; Count: integer): string;
  365. begin
  366. result := Copy(S, 1 + Length(S) - Count, Count);
  367. end ;
  368. { BCDToInt converts the BCD value Value to an integer }
  369. function BCDToInt(Value: integer): integer;
  370. var i, j: integer;
  371. begin
  372. result := 0;
  373. j := 1;
  374. for i := 0 to SizeOf(Value) shr 1 - 1 do begin
  375. result := result + j * (Value and 15);
  376. j := j * 10;
  377. Value := Value shr 4;
  378. end ;
  379. end ;
  380. {$IFDEF GO32V2}
  381. { Codepage constants }
  382. const
  383. CP_US = 437;
  384. CP_MultiLingual = 850;
  385. CP_SlavicLatin2 = 852;
  386. CP_Turkish = 857;
  387. CP_Portugal = 860;
  388. CP_IceLand = 861;
  389. CP_Canada = 863;
  390. CP_NorwayDenmark = 865;
  391. { CountryInfo }
  392. {$PACKRECORDS 1}
  393. type
  394. TCountryInfo = record
  395. InfoId: byte;
  396. case integer of
  397. 1: ( Size: word;
  398. CountryId: word;
  399. CodePage: word;
  400. CountryInfo: array[0..33] of byte );
  401. 2: ( UpperCaseTable: longint );
  402. 4: ( FilenameUpperCaseTable: longint );
  403. 5: ( FilecharacterTable: longint );
  404. 6: ( CollatingTable: longint );
  405. 7: ( DBCSLeadByteTable: longint );
  406. end ;
  407. {$PACKRECORDS NORMAL}
  408. procedure GetExtendedCountryInfo(InfoId: integer; CodePage, CountryId: word; var CountryInfo: TCountryInfo);
  409. var Regs: Registers;
  410. begin
  411. Regs.AH := $65;
  412. Regs.AL := InfoId;
  413. Regs.BX := CodePage;
  414. Regs.DX := CountryId;
  415. Regs.ES := transfer_buffer shr 16;
  416. Regs.DI := transfer_buffer and 65535;
  417. Regs.CX := SizeOf(TCountryInfo);
  418. RealIntr($21, Regs);
  419. DosMemGet(transfer_buffer shr 16, transfer_buffer and 65535, CountryInfo, Regs.CX );
  420. end ;
  421. procedure InitAnsi;
  422. var CountryInfo: TCountryInfo;
  423. begin
  424. GetExtendedCountryInfo(2, $FFFF, $FFFF, CountryInfo);
  425. DosMemGet(CountryInfo.UpperCaseTable shr 16, 2 + CountryInfo.UpperCaseTable and 65535, UpperCaseTable[128], 128);
  426. end ;
  427. {$ENDIF}
  428. {
  429. $Log$
  430. Revision 1.2 1998-09-16 08:28:42 michael
  431. Update from gertjan Schouten, plus small fix for linux
  432. Revision 1.1 1998/04/10 15:17:46 michael
  433. + Initial implementation; Donated by Gertjan Schouten
  434. His file was split into several files, to keep it a little bit structured.
  435. 27 April 1998:
  436. Function: BCDToInt added
  437. }