sysstr.inc 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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. if (S='') then
  24. Result:=nil
  25. else
  26. begin
  27. getmem(Result,length(s)+1);
  28. if (Result<>nil) then
  29. Result^:=s;
  30. end;
  31. end;
  32. { DisposeStr frees the memory occupied by S }
  33. procedure DisposeStr(S: PString);
  34. begin
  35. if S <> Nil then
  36. begin
  37. Freemem(S,Length(S^)+1);
  38. S:=nil;
  39. end;
  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: String; 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 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 := CompareMemRange(Pointer(S1),Pointer(S2), 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. { CompareMemRange 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 CompareMemRange(P1, P2: Pointer; Length: cardinal): integer;
  101. var i: cardinal;
  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. function CompareMem(P1, P2: Pointer; Length: cardinal): Boolean;
  113. var
  114. i: cardinal;
  115. begin
  116. for i := 0 to Length - 1 do
  117. begin
  118. if Byte(P1^) <> Byte(P2^) then
  119. begin
  120. Result := False;
  121. exit;
  122. end;
  123. Inc(P1);
  124. Inc(P2);
  125. end;
  126. Result := True;
  127. end;
  128. { CompareText compares S1 and S2, the result is the based on
  129. substraction of the ascii values of characters in S1 and S2
  130. comparison is case-insensitive
  131. case result
  132. S1 < S2 < 0
  133. S1 > S2 > 0
  134. S1 = S2 = 0 }
  135. function CompareText(const S1, S2: string): integer;
  136. var i, count, count1, count2: integer; Chr1, Chr2: byte;
  137. begin
  138. result := 0;
  139. Count1 := Length(S1);
  140. Count2 := Length(S2);
  141. if Count1 > Count2 then Count := Count2
  142. else Count := Count1;
  143. i := 0;
  144. while (result = 0) and (i < count) do begin
  145. inc (i);
  146. Chr1 := byte(s1[i]);
  147. Chr2 := byte(s2[i]);
  148. if Chr1 in [97..122] then dec(Chr1,32);
  149. if Chr2 in [97..122] then dec(Chr2,32);
  150. result := Chr1 - Chr2;
  151. end ;
  152. if (result = 0) then
  153. result:=(count1-count2);
  154. end ;
  155. {==============================================================================}
  156. { Ansi string functions }
  157. { these functions rely on the character set loaded by the OS }
  158. {==============================================================================}
  159. function AnsiUpperCase(const s: string): string;
  160. var len, i: integer;
  161. begin
  162. len := length(s);
  163. SetLength(result, len);
  164. for i := 1 to len do
  165. result[i] := UpperCaseTable[ord(s[i])];
  166. end ;
  167. function AnsiLowerCase(const s: string): string;
  168. var len, i: integer;
  169. begin
  170. len := length(s);
  171. SetLength(result, len);
  172. for i := 1 to len do
  173. result[i] := LowerCaseTable[ord(s[i])];
  174. end ;
  175. function AnsiCompareStr(const S1, S2: string): integer;
  176. Var I,L1,L2 : Longint;
  177. begin
  178. Result:=0;
  179. L1:=Length(S1);
  180. L2:=Length(S2);
  181. I:=1;
  182. While (Result=0) and ((I<=L1) and (I<=L2)) do
  183. begin
  184. Result:=Ord(S1[I])-Ord(S2[I]); //!! Must be replaced by ansi characters !!
  185. Inc(I);
  186. end;
  187. If Result=0 Then
  188. Result:=L1-L2;
  189. end;
  190. function AnsiCompareText(const S1, S2: string): integer;
  191. Var I,L1,L2 : Longint;
  192. begin
  193. Result:=0;
  194. L1:=Length(S1);
  195. L2:=Length(S2);
  196. I:=1;
  197. While (Result=0) and ((I<=L1) and (I<=L2)) do
  198. begin
  199. Result:=Ord(LowerCaseTable[Ord(S1[I])])-Ord(LowerCaseTable[Ord(S2[I])]); //!! Must be replaced by ansi characters !!
  200. Inc(I);
  201. end;
  202. If Result=0 Then
  203. Result:=L1-L2;
  204. end;
  205. function AnsiStrComp(S1, S2: PChar): integer;
  206. begin
  207. Result:=0;
  208. If S1=Nil then
  209. begin
  210. If S2=Nil Then Exit;
  211. result:=-1;
  212. end;
  213. If S2=Nil then
  214. begin
  215. Result:=1;
  216. exit;
  217. end;
  218. Repeat
  219. Result:=Ord(S1[0])-Ord(S2[0]); //!! Must be replaced by ansi characters !!
  220. Inc(S1);
  221. Inc(S2);
  222. Until (Result<>0) or ((S1[0]=#0) or (S2[0]=#0))
  223. end;
  224. function AnsiStrIComp(S1, S2: PChar): integer;
  225. begin
  226. Result:=0;
  227. If S1=Nil then
  228. begin
  229. If S2=Nil Then Exit;
  230. result:=-1;
  231. end;
  232. If S2=Nil then
  233. begin
  234. Result:=1;
  235. exit;
  236. end;
  237. Repeat
  238. Result:=Ord(LowerCaseTable[Ord(S1[0])])-Ord(LowerCaseTable[Ord(S2[0])]); //!! Must be replaced by ansi characters !!
  239. Inc(S1);
  240. Inc(S2);
  241. Until (Result<>0) or ((S1[0]=#0) or (S2[0]=#0))
  242. end;
  243. function AnsiStrLComp(S1, S2: PChar; MaxLen: cardinal): integer;
  244. Var I : cardinal;
  245. begin
  246. Result:=0;
  247. If MaxLen=0 then exit;
  248. If S1=Nil then
  249. begin
  250. If S2=Nil Then Exit;
  251. result:=-1;
  252. end;
  253. If S2=Nil then
  254. begin
  255. Result:=1;
  256. exit;
  257. end;
  258. I:=0;
  259. Repeat
  260. Result:=Ord(S1[0])-Ord(S2[0]); //!! Must be replaced by ansi characters !!
  261. Inc(S1);
  262. Inc(S2);
  263. Inc(I);
  264. Until (Result<>0) or ((S1[0]=#0) or (S2[0]=#0)) or (I=MaxLen)
  265. end ;
  266. function AnsiStrLIComp(S1, S2: PChar; MaxLen: cardinal): integer;
  267. Var I : cardinal;
  268. begin
  269. Result:=0;
  270. If MaxLen=0 then exit;
  271. If S1=Nil then
  272. begin
  273. If S2=Nil Then Exit;
  274. result:=-1;
  275. end;
  276. If S2=Nil then
  277. begin
  278. Result:=1;
  279. exit;
  280. end;
  281. I:=0;
  282. Repeat
  283. Result:=Ord(LowerCaseTable[Ord(S1[0])])-Ord(LowerCaseTable[Ord(S2[0])]); //!! Must be replaced by ansi characters !!
  284. Inc(S1);
  285. Inc(S2);
  286. Inc(I);
  287. Until (Result<>0) or ((S1[0]=#0) or (S2[0]=#0)) or (I=MaxLen)
  288. end ;
  289. function AnsiStrLower(Str: PChar): PChar;
  290. begin
  291. result := Str;
  292. if Str <> Nil then begin
  293. while Str^ <> #0 do begin
  294. Str^ := LowerCaseTable[byte(Str^)];
  295. Str := Str + 1;
  296. end ;
  297. end ;
  298. end ;
  299. function AnsiStrUpper(Str: PChar): PChar;
  300. begin
  301. result := Str;
  302. if Str <> Nil then begin
  303. while Str^ <> #0 do begin
  304. Str^ := UpperCaseTable[byte(Str^)];
  305. Str := Str + 1;
  306. end ;
  307. end ;
  308. end ;
  309. function AnsiLastChar(const S: string): PChar;
  310. begin
  311. //!! No multibyte yet, so we return the last one.
  312. result:=StrEnd(Pchar(S));
  313. Dec(Result);
  314. end ;
  315. function AnsiStrLastChar(Str: PChar): PChar;
  316. begin
  317. //!! No multibyte yet, so we return the last one.
  318. result:=StrEnd(Str);
  319. Dec(Result);
  320. end ;
  321. {==============================================================================}
  322. { End of Ansi functions }
  323. {==============================================================================}
  324. { Trim returns a copy of S with blanks characters on the left and right stripped off }
  325. Const WhiteSpace = [' ',#10,#13,#9];
  326. function Trim(const S: string): string;
  327. var Ofs, Len: integer;
  328. begin
  329. len := Length(S);
  330. while (Len>0) and (S[Len] in WhiteSpace) do
  331. dec(Len);
  332. Ofs := 1;
  333. while (Ofs<=Len) and (S[Ofs] in WhiteSpace) do
  334. Inc(Ofs);
  335. result := Copy(S, Ofs, 1 + Len - Ofs);
  336. end ;
  337. { TrimLeft returns a copy of S with all blank characters on the left stripped off }
  338. function TrimLeft(const S: string): string;
  339. var i,l:integer;
  340. begin
  341. l := length(s);
  342. i := 1;
  343. while (i<=l) and (s[i] in whitespace) do
  344. inc(i);
  345. Result := copy(s, i, l);
  346. end ;
  347. { TrimRight returns a copy of S with all blank characters on the right stripped off }
  348. function TrimRight(const S: string): string;
  349. var l:integer;
  350. begin
  351. l := length(s);
  352. while (l>0) and (s[l] in whitespace) do
  353. dec(l);
  354. result := copy(s,1,l);
  355. end ;
  356. { QuotedStr returns S quoted left and right and every single quote in S
  357. replaced by two quotes }
  358. function QuotedStr(const S: string): string;
  359. begin
  360. result := AnsiQuotedStr(s, '''');
  361. end ;
  362. { AnsiQuotedStr returns S quoted left and right by Quote,
  363. and every single occurance of Quote replaced by two }
  364. function AnsiQuotedStr(const S: string; Quote: char): string;
  365. var i, j, count: integer;
  366. begin
  367. result := '' + Quote;
  368. count := length(s);
  369. i := 0;
  370. j := 0;
  371. while i < count do begin
  372. i := i + 1;
  373. if S[i] = Quote then begin
  374. result := result + copy(S, 1 + j, i - j) + Quote;
  375. j := i;
  376. end ;
  377. end ;
  378. if i <> j then
  379. result := result + copy(S, 1 + j, i - j);
  380. result := result + Quote;
  381. end ;
  382. { AnsiExtractQuotedStr returns a copy of Src with quote characters
  383. deleted to the left and right and double occurances
  384. of Quote replaced by a single Quote }
  385. function AnsiExtractQuotedStr(Const Src: PChar; Quote: Char): string;
  386. var i: integer; P, Q: PChar;
  387. begin
  388. P := Src;
  389. if Src^ = Quote then P := P + 1;
  390. Q := StrEnd(P);
  391. if PChar(Q - 1)^ = Quote then Q := Q - 1;
  392. SetLength(result, Q - P);
  393. i := 0;
  394. while P <> Q do begin
  395. i := i + 1;
  396. result[i] := P^;
  397. if (P^ = Quote) and (PChar(P + 1)^ = Quote) then
  398. P := P + 1;
  399. P := P + 1;
  400. end ;
  401. SetLength(result, i);
  402. end ;
  403. { AdjustLineBreaks returns S with all CR characters not followed by LF
  404. replaced with CR/LF }
  405. // under Linux all CR characters or CR/LF combinations should be replaced with LF
  406. function AdjustLineBreaks(const S: string): string;
  407. var i, j, count: integer;
  408. begin
  409. result := '';
  410. i := 0;
  411. j := 0;
  412. count := Length(S);
  413. while i < count do begin
  414. i := i + 1;
  415. {$ifndef Unix}
  416. if (S[i] = #13) and ((i = count) or (S[i + 1] <> #10)) then
  417. begin
  418. result := result + Copy(S, 1 + j, i - j) + #10;
  419. j := i;
  420. end;
  421. {$else}
  422. If S[i]=#13 then
  423. begin
  424. Result:= Result+Copy(S,J+1,i-j-1)+#10;
  425. If I<>Count Then
  426. If S[I+1]=#10 then inc(i);
  427. J :=I;
  428. end;
  429. {$endif}
  430. end ;
  431. if j <> i then
  432. result := result + copy(S, 1 + j, i - j);
  433. end ;
  434. { IsValidIdent returns true if the first character of Ident is in:
  435. 'A' to 'Z', 'a' to 'z' or '_' and the following characters are
  436. on of: 'A' to 'Z', 'a' to 'z', '0'..'9' or '_' }
  437. function IsValidIdent(const Ident: string): boolean;
  438. var i, len: integer;
  439. begin
  440. result := false;
  441. len := length(Ident);
  442. if len <> 0 then begin
  443. result := Ident[1] in ['A'..'Z', 'a'..'z', '_'];
  444. i := 1;
  445. while (result) and (i < len) do begin
  446. i := i + 1;
  447. result := result and (Ident[i] in ['A'..'Z', 'a'..'z', '0'..'9', '_']);
  448. end ;
  449. end ;
  450. end ;
  451. { IntToStr returns a string representing the value of Value }
  452. function IntToStr(Value: integer): string;
  453. begin
  454. System.Str(Value, result);
  455. end ;
  456. function IntToStr(Value: int64): string;
  457. begin
  458. System.Str(Value, result);
  459. end ;
  460. { IntToHex returns a string representing the hexadecimal value of Value }
  461. const
  462. HexDigits: array[0..15] of char = '0123456789ABCDEF';
  463. function IntToHex(Value: integer; Digits: integer): string;
  464. var i: integer;
  465. begin
  466. SetLength(result, digits);
  467. for i := 0 to digits - 1 do
  468. begin
  469. result[digits - i] := HexDigits[value and 15];
  470. value := value shr 4;
  471. end ;
  472. end ;
  473. function IntToHex(Value: int64; Digits: integer): string;
  474. var i: integer;
  475. begin
  476. SetLength(result, digits);
  477. for i := 0 to digits - 1 do
  478. begin
  479. result[digits - i] := HexDigits[value and 15];
  480. value := value shr 4;
  481. end ;
  482. end ;
  483. { StrToInt converts the string S to an integer value,
  484. if S does not represent a valid integer value EConvertError is raised }
  485. function StrToInt(const S: string): integer;
  486. var Error: word;
  487. begin
  488. Val(S, result, Error);
  489. if Error <> 0 then raise EConvertError.createfmt(SInValidInteger,[S]);
  490. end ;
  491. function StrToInt64(const S: string): int64;
  492. var Error: word;
  493. begin
  494. Val(S, result, Error);
  495. if Error <> 0 then raise EConvertError.createfmt(SInValidInteger,[S]);
  496. end ;
  497. { StrToIntDef converts the string S to an integer value,
  498. Default is returned in case S does not represent a valid integer value }
  499. function StrToIntDef(const S: string; Default: integer): integer;
  500. var Error: word;
  501. begin
  502. Val(S, result, Error);
  503. if Error <> 0 then result := Default;
  504. end ;
  505. { StrToIntDef converts the string S to an integer value,
  506. Default is returned in case S does not represent a valid integer value }
  507. function StrToInt64Def(const S: string; Default: int64): int64;
  508. var Error: word;
  509. begin
  510. Val(S, result, Error);
  511. if Error <> 0 then result := Default;
  512. end ;
  513. { LoadStr returns the string resource Ident. }
  514. function LoadStr(Ident: integer): string;
  515. begin
  516. result:='';
  517. end ;
  518. { FmtLoadStr returns the string resource Ident and formats it accordingly }
  519. function FmtLoadStr(Ident: integer; const Args: array of const): string;
  520. begin
  521. result:='';
  522. end;
  523. Const
  524. feInvalidFormat = 1;
  525. feMissingArgument = 2;
  526. feInvalidArgIndex = 3;
  527. {$ifdef fmtdebug}
  528. Procedure Log (Const S: String);
  529. begin
  530. Writeln (S);
  531. end;
  532. {$endif}
  533. Procedure DoFormatError (ErrCode : Longint);
  534. Var
  535. S : String;
  536. begin
  537. //!! must be changed to contain format string...
  538. S:='';
  539. Case ErrCode of
  540. feInvalidFormat : raise EConvertError.Createfmt(SInvalidFormat,[s]);
  541. feMissingArgument : raise EConvertError.Createfmt(SArgumentMissing,[s]);
  542. feInvalidArgIndex : raise EConvertError.Createfmt(SInvalidArgIndex,[s]);
  543. end;
  544. end;
  545. Function Format (Const Fmt : String; const Args : Array of const) : String;
  546. Var ChPos,OldPos,ArgPos,DoArg,Len : Longint;
  547. Hs,ToAdd : String;
  548. Index,Width,Prec : Longint;
  549. Left : Boolean;
  550. Fchar : char;
  551. {
  552. ReadFormat reads the format string. It returns the type character in
  553. uppercase, and sets index, Width, Prec to their correct values,
  554. or -1 if not set. It sets Left to true if left alignment was requested.
  555. In case of an error, DoFormatError is called.
  556. }
  557. Function ReadFormat : Char;
  558. Var Value : longint;
  559. Procedure ReadInteger;
  560. Var Code : Word;
  561. begin
  562. If Value<>-1 then exit; // Was already read.
  563. OldPos:=chPos;
  564. While (Chpos<=Len) and
  565. (Pos(Fmt[chpos],'1234567890')<>0) do inc(chpos);
  566. If Chpos>len then
  567. DoFormatError(feInvalidFormat);
  568. If Fmt[Chpos]='*' then
  569. begin
  570. If (Chpos>OldPos) or (ArgPos>High(Args))
  571. or (Args[ArgPos].Vtype<>vtInteger) then
  572. DoFormatError(feInvalidFormat);
  573. Value:=Args[ArgPos].VInteger;
  574. Inc(ArgPos);
  575. Inc(chPos);
  576. end
  577. else
  578. begin
  579. If (OldPos<chPos) Then
  580. begin
  581. Val (Copy(Fmt,OldPos,ChPos-OldPos),value,code);
  582. // This should never happen !!
  583. If Code>0 then DoFormatError (feInvalidFormat);
  584. end
  585. else
  586. Value:=-1;
  587. end;
  588. end;
  589. Procedure ReadIndex;
  590. begin
  591. ReadInteger;
  592. If Fmt[ChPos]=':' then
  593. begin
  594. If Value=-1 then DoFormatError(feMissingArgument);
  595. Index:=Value;
  596. Value:=-1;
  597. Inc(Chpos);
  598. end;
  599. {$ifdef fmtdebug}
  600. Log ('Read index');
  601. {$endif}
  602. end;
  603. Procedure ReadLeft;
  604. begin
  605. If Fmt[chpos]='-' then
  606. begin
  607. left:=True;
  608. Inc(chpos);
  609. end
  610. else
  611. Left:=False;
  612. {$ifdef fmtdebug}
  613. Log ('Read Left');
  614. {$endif}
  615. end;
  616. Procedure ReadWidth;
  617. begin
  618. ReadInteger;
  619. If Value<>-1 then
  620. begin
  621. Width:=Value;
  622. Value:=-1;
  623. end;
  624. {$ifdef fmtdebug}
  625. Log ('Read width');
  626. {$endif}
  627. end;
  628. Procedure ReadPrec;
  629. begin
  630. If Fmt[chpos]='.' then
  631. begin
  632. inc(chpos);
  633. ReadInteger;
  634. If Value=-1 then DoFormaterror(feMissingArgument);
  635. prec:=Value;
  636. end;
  637. {$ifdef fmtdebug}
  638. Log ('Read precision');
  639. {$endif}
  640. end;
  641. begin
  642. {$ifdef fmtdebug}
  643. Log ('Start format');
  644. {$endif}
  645. Index:=-1;
  646. Width:=-1;
  647. Prec:=-1;
  648. Value:=-1;
  649. inc(chpos);
  650. If Fmt[Chpos]='%' then exit('%');
  651. ReadIndex;
  652. ReadLeft;
  653. ReadWidth;
  654. ReadPrec;
  655. ReadFormat:=Upcase(Fmt[ChPos]);
  656. {$ifdef fmtdebug}
  657. Log ('End format');
  658. {$endif}
  659. end;
  660. {$ifdef fmtdebug}
  661. Procedure DumpFormat (C : char);
  662. begin
  663. Write ('Fmt : ',fmt:10);
  664. Write (' Index : ',Index:3);
  665. Write (' Left : ',left:5);
  666. Write (' Width : ',Width:3);
  667. Write (' Prec : ',prec:3);
  668. Writeln (' Type : ',C);
  669. end;
  670. {$endif}
  671. function Checkarg (AT : Longint;err:boolean):boolean;
  672. {
  673. Check if argument INDEX is of correct type (AT)
  674. If Index=-1, ArgPos is used, and argpos is augmented with 1
  675. DoArg is set to the argument that must be used.
  676. }
  677. begin
  678. result:=false;
  679. if Index=-1 then
  680. begin
  681. DoArg:=Argpos;
  682. inc(ArgPos);
  683. end
  684. else
  685. DoArg:=Index;
  686. If (Doarg>High(Args)) or (Args[Doarg].Vtype<>AT) then
  687. begin
  688. if err then
  689. DoFormatError(feInvalidArgindex);
  690. dec(ArgPos);
  691. exit;
  692. end;
  693. result:=true;
  694. end;
  695. Const Zero = '000000000000000000000000000000000000000000000000000000000000000';
  696. begin
  697. Result:='';
  698. Len:=Length(Fmt);
  699. Chpos:=1;
  700. OldPos:=1;
  701. ArgPos:=0;
  702. While chpos<=len do
  703. begin
  704. While (ChPos<=Len) and (Fmt[chpos]<>'%') do
  705. inc(chpos);
  706. If ChPos>OldPos Then
  707. Result:=Result+Copy(Fmt,OldPos,Chpos-Oldpos);
  708. If ChPos<Len then
  709. begin
  710. FChar:=ReadFormat;
  711. {$ifdef fmtdebug}
  712. DumpFormat(FCHar);
  713. {$endif}
  714. Case FChar of
  715. 'D' : begin
  716. if Checkarg(vtinteger,false) then
  717. Str(Args[Doarg].VInteger,ToAdd)
  718. else if CheckArg(vtInt64,true) then
  719. Str(Args[DoArg].VInt64^,toadd);
  720. Width:=Abs(width);
  721. Index:=Prec-Length(ToAdd);
  722. If ToAdd[1]<>'-' then
  723. ToAdd:=StringOfChar('0',Index)+ToAdd
  724. else
  725. // + 1 to accomodate for - sign in length !!
  726. Insert(StringOfChar('0',Index+1),toadd,2);
  727. end;
  728. 'E' : begin
  729. CheckArg(vtExtended,true);
  730. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffexponent,Prec,3);
  731. end;
  732. 'F' : begin
  733. CheckArg(vtExtended,true);
  734. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffFixed,9999,Prec);
  735. end;
  736. 'G' : begin
  737. CheckArg(vtExtended,true);
  738. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffGeneral,Prec,3);
  739. end;
  740. 'N' : begin
  741. CheckArg(vtExtended,true);
  742. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffNumber,9999,Prec);
  743. end;
  744. 'M' : begin
  745. CheckArg(vtExtended,true);
  746. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffCurrency,9999,Prec);
  747. end;
  748. 'S' : begin
  749. if CheckArg(vtString,false) then
  750. hs:=Args[doarg].VString^
  751. else
  752. if CheckArg(vtChar,false) then
  753. hs:=Args[doarg].VChar
  754. else
  755. if CheckArg(vtPChar,false) then
  756. hs:=Args[doarg].VPChar
  757. else
  758. if CheckArg(vtAnsiString,true) then
  759. hs:=ansistring(Args[doarg].VAnsiString);
  760. Index:=Length(hs);
  761. If (Prec<>-1) and (Index>Prec) then
  762. Index:=Prec;
  763. ToAdd:=Copy(hs,1,Index);
  764. end;
  765. 'P' : Begin
  766. CheckArg(vtpointer,true);
  767. ToAdd:=HexStr(Longint(Args[DoArg].VPointer),8);
  768. // Insert ':'. Is this needed in 32 bit ? No it isn't.
  769. // Insert(':',ToAdd,5);
  770. end;
  771. 'X' : begin
  772. Checkarg(vtinteger,true);
  773. If Prec>15 then
  774. ToAdd:=HexStr(Args[Doarg].VInteger,15)
  775. else
  776. begin
  777. // determine minimum needed number of hex digits.
  778. Index:=1;
  779. While (DWord(1 shl (Index*4))<=DWord(Args[DoArg].VInteger)) and (index<8) do
  780. inc(Index);
  781. If Index>Prec then
  782. Prec:=Index;
  783. ToAdd:=HexStr(Args[DoArg].VInteger,Prec);
  784. end;
  785. end;
  786. '%': ToAdd:='%';
  787. end;
  788. If Width<>-1 then
  789. If Length(ToAdd)<Width then
  790. If not Left then
  791. ToAdd:=Space(Width-Length(ToAdd))+ToAdd
  792. else
  793. ToAdd:=ToAdd+space(Width-Length(ToAdd));
  794. Result:=Result+ToAdd;
  795. end;
  796. inc(chpos);
  797. Oldpos:=chpos;
  798. end;
  799. end;
  800. Function FormatBuf (Var Buffer; BufLen : Cardinal;
  801. Const Fmt; fmtLen : Cardinal;
  802. Const Args : Array of const) : Cardinal;
  803. Var S,F : String;
  804. begin
  805. Setlength(F,fmtlen);
  806. if fmtlen > 0 then
  807. Move(fmt,F[1],fmtlen);
  808. S:=Format (F,Args);
  809. If Cardinal(Length(S))>Buflen then
  810. Result:=Length(S)
  811. else
  812. Result:=Buflen;
  813. if Result > 0 then
  814. Move(S[1],Buffer,Result);
  815. end;
  816. Procedure FmtStr(Var Res: String; Const Fmt : String; Const args: Array of const);
  817. begin
  818. Res:=Format(fmt,Args);
  819. end;
  820. Function StrFmt(Buffer,Fmt : PChar; Const args: Array of const) : Pchar;
  821. begin
  822. Buffer[FormatBuf(Buffer^,Maxint,Fmt^,strlen(fmt),args)]:=#0;
  823. Result:=Buffer;
  824. end;
  825. Function StrLFmt(Buffer : PCHar; Maxlen : Cardinal;Fmt : PChar; Const args: Array of const) : Pchar;
  826. begin
  827. Buffer[FormatBuf(Buffer^,MaxLen,Fmt^,strlen(fmt),args)]:=#0;
  828. Result:=Buffer;
  829. end;
  830. function StrToFloat(Value: string): Extended;
  831. var Error: word;
  832. begin
  833. Val(Value, result, Error);
  834. if Error <> 0 then raise
  835. EConvertError.createfmt(SInValidFLoat,[Value]);
  836. end ;
  837. Function FloatToStr(Value: Extended): String;
  838. Begin
  839. Result := FloatToStrF(Value, ffGeneral, 15, 0);
  840. End;
  841. Function FloatToText(Buffer: PChar; Value: Extended; format: TFloatFormat; Precision, Digits: Integer): Longint;
  842. Var
  843. Tmp: String[40];
  844. Begin
  845. Tmp := FloatToStrF(Value, format, Precision, Digits);
  846. Result := Length(Tmp);
  847. Move(Tmp[1], Buffer[0], Result);
  848. End;
  849. Function FloatToStrF(Value: Extended; format: TFloatFormat; Precision, Digits: Integer): String;
  850. Var
  851. P: Integer;
  852. Negative, TooSmall, TooLarge: Boolean;
  853. Begin
  854. Case format Of
  855. ffGeneral:
  856. Begin
  857. If (Precision = -1) Or (Precision > 15) Then Precision := 15;
  858. TooSmall := (Abs(Value) < 0.00001) and (Value>0.0);
  859. If Not TooSmall Then
  860. Begin
  861. Str(Value:0:999, Result);
  862. P := Pos('.', Result);
  863. Result[P] := DecimalSeparator;
  864. TooLarge := P > Precision + 1;
  865. End;
  866. If TooSmall Or TooLarge Then
  867. begin
  868. Result := FloatToStrF(Value, ffExponent, Precision, Digits);
  869. // Strip unneeded zeroes.
  870. P:=Pos('E',result)-1;
  871. If P<>-1 then
  872. While (P>1) and (Result[P]='0') do
  873. begin
  874. system.Delete(Result,P,1);
  875. Dec(P);
  876. end;
  877. end
  878. else
  879. begin
  880. P := Length(Result);
  881. While Result[P] = '0' Do Dec(P);
  882. If Result[P] = DecimalSeparator Then Dec(P);
  883. SetLength(Result, P);
  884. end;
  885. End;
  886. ffExponent:
  887. Begin
  888. If (Precision = -1) Or (Precision > 15) Then Precision := 15;
  889. Str(Value:Precision + 8, Result);
  890. Result[3] := DecimalSeparator;
  891. P:=4;
  892. While (P>0) and (Digits < P) And (Result[Precision + 5] = '0') do
  893. Begin
  894. If P<>1 then
  895. system.Delete(Result, Precision + 5, 1)
  896. else
  897. system.Delete(Result, Precision + 3, 3);
  898. Dec(P);
  899. end;
  900. If Result[1] = ' ' Then
  901. System.Delete(Result, 1, 1);
  902. End;
  903. ffFixed:
  904. Begin
  905. If Digits = -1 Then Digits := 2
  906. Else If Digits > 15 Then Digits := 15;
  907. Str(Value:0:Digits, Result);
  908. If Result[1] = ' ' Then
  909. System.Delete(Result, 1, 1);
  910. P := Pos('.', Result);
  911. If P <> 0 Then Result[P] := DecimalSeparator;
  912. End;
  913. ffNumber:
  914. Begin
  915. If Digits = -1 Then Digits := 2
  916. Else If Digits > 15 Then Digits := 15;
  917. Str(Value:0:Digits, Result);
  918. If Result[1] = ' ' Then System.Delete(Result, 1, 1);
  919. P := Pos('.', Result);
  920. If P <> 0 Then Result[P] := DecimalSeparator;
  921. Dec(P, 3);
  922. While (P > 1) Do
  923. Begin
  924. If Result[P - 1] <> '-' Then Insert(ThousandSeparator, Result, P);
  925. Dec(P, 3);
  926. End;
  927. End;
  928. ffCurrency:
  929. Begin
  930. If Value < 0 Then
  931. Begin
  932. Negative := True;
  933. Value := -Value;
  934. End
  935. Else Negative := False;
  936. If Digits = -1 Then Digits := CurrencyDecimals
  937. Else If Digits > 18 Then Digits := 18;
  938. Str(Value:0:Digits, Result);
  939. If Result[1] = ' ' Then System.Delete(Result, 1, 1);
  940. P := Pos('.', Result);
  941. If P <> 0 Then Result[P] := DecimalSeparator;
  942. Dec(P, 3);
  943. While (P > 1) Do
  944. Begin
  945. Insert(ThousandSeparator, Result, P);
  946. Dec(P, 3);
  947. End;
  948. If Not Negative Then
  949. Begin
  950. Case CurrencyFormat Of
  951. 0: Result := CurrencyString + Result;
  952. 1: Result := Result + CurrencyString;
  953. 2: Result := CurrencyString + ' ' + Result;
  954. 3: Result := Result + ' ' + CurrencyString;
  955. End
  956. End
  957. Else
  958. Begin
  959. Case NegCurrFormat Of
  960. 0: Result := '(' + CurrencyString + Result + ')';
  961. 1: Result := '-' + CurrencyString + Result;
  962. 2: Result := CurrencyString + '-' + Result;
  963. 3: Result := CurrencyString + Result + '-';
  964. 4: Result := '(' + Result + CurrencyString + ')';
  965. 5: Result := '-' + Result + CurrencyString;
  966. 6: Result := Result + '-' + CurrencyString;
  967. 7: Result := Result + CurrencyString + '-';
  968. 8: Result := '-' + Result + ' ' + CurrencyString;
  969. 9: Result := '-' + CurrencyString + ' ' + Result;
  970. 10: Result := CurrencyString + ' ' + Result + '-';
  971. End;
  972. End;
  973. End;
  974. End;
  975. End;
  976. Function FloatToDateTime (Const Value : Extended) : TDateTime;
  977. begin
  978. If (Value<MinDateTime) or (Value>MaxDateTime) then
  979. Raise EConvertError.CreateFmt (SInvalidDateTime,[Value]);
  980. Result:=Value;
  981. end;
  982. Function FloatToCurr (Const Value : Extended) : Currency;
  983. begin
  984. end;
  985. Function CurrToStr(Value: Currency): string;
  986. begin
  987. end;
  988. function StrToCurr(const S: string): Currency;
  989. begin
  990. end;
  991. function StrToBool(const S: string): Boolean;
  992. Var
  993. Temp : String;
  994. D : Double;
  995. Code : word;
  996. begin
  997. Temp:=upcase(S);
  998. Val(temp,D,code);
  999. If Code=0 then
  1000. Result:=(D<>0.0)
  1001. else If Temp='TRUE' then
  1002. result:=true
  1003. else if Temp='FALSE' then
  1004. result:=false
  1005. else
  1006. Raise EConvertError.CreateFmt(SInvalidBoolean,[S]);
  1007. end;
  1008. function BoolToStr(B: Boolean): string;
  1009. begin
  1010. If B then
  1011. Result:='TRUE'
  1012. else
  1013. Result:='FALSE';
  1014. end;
  1015. {==============================================================================}
  1016. { extra functions }
  1017. {==============================================================================}
  1018. { LeftStr returns Count left-most characters from S }
  1019. function LeftStr(const S: string; Count: integer): string;
  1020. begin
  1021. result := Copy(S, 1, Count);
  1022. end ;
  1023. { RightStr returns Count right-most characters from S }
  1024. function RightStr(const S: string; Count: integer): string;
  1025. begin
  1026. If Count>Length(S) then
  1027. Count:=Length(S);
  1028. result := Copy(S, 1 + Length(S) - Count, Count);
  1029. end;
  1030. { BCDToInt converts the BCD value Value to an integer }
  1031. function BCDToInt(Value: integer): integer;
  1032. var i, j: integer;
  1033. begin
  1034. result := 0;
  1035. j := 1;
  1036. for i := 0 to SizeOf(Value) shr 1 - 1 do begin
  1037. result := result + j * (Value and 15);
  1038. j := j * 10;
  1039. Value := Value shr 4;
  1040. end ;
  1041. end ;
  1042. Function LastDelimiter(const Delimiters, S: string): Integer;
  1043. begin
  1044. Result:=Length(S);
  1045. While (Result>0) and (Pos(S[Result],Delimiters)=0) do
  1046. Dec(Result);
  1047. end;
  1048. {
  1049. Case Translation Tables
  1050. Can be used in internationalization support.
  1051. Although these tables can be obtained through system calls
  1052. it is better to not use those, since most implementation are not 100%
  1053. WARNING:
  1054. before modifying a translation table make sure that the current codepage
  1055. of the OS corresponds to the one you make changes to
  1056. }
  1057. const
  1058. { upper case translation table for character set 850 }
  1059. CP850UCT: array[128..255] of char =
  1060. ('€', 'š', '�', '¶', 'Ž', '¶', '�', '€', 'Ò', 'Ó', 'Ô', 'Ø', '×', 'Þ', 'Ž', '�',
  1061. '�', '’', '’', 'â', '™', 'ã', 'ê', 'ë', 'Y', '™', 'š', '�', 'œ', '�', 'ž', 'Ÿ',
  1062. 'µ', 'Ö', 'à', 'é', '¥', '¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯',
  1063. '°', '±', '²', '³', '´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿',
  1064. 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Ç', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï',
  1065. 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', '×', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß',
  1066. 'à', 'á', 'â', 'ã', 'å', 'å', 'æ', 'í', 'è', 'é', 'ê', 'ë', 'í', 'í', 'î', 'ï',
  1067. 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ');
  1068. { lower case translation table for character set 850 }
  1069. CP850LCT: array[128..255] of char =
  1070. ('‡', '�', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '�', '„', '†',
  1071. '‚', '‘', '‘', '“', '”', '•', '–', '—', '˜', '”', '�', '›', 'œ', '›', 'ž', 'Ÿ',
  1072. ' ', '¡', '¢', '£', '¤', '¤', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯',
  1073. '°', '±', '²', '³', '´', ' ', 'ƒ', '…', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿',
  1074. 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Æ', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï',
  1075. 'Ð', 'Ñ', 'ˆ', '‰', 'Š', 'Õ', '¡', 'Œ', '‹', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', '�', 'ß',
  1076. '¢', 'á', '“', '•', 'ä', 'ä', 'æ', 'í', 'è', '£', '–', '—', 'ì', 'ì', 'î', 'ï',
  1077. 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ');
  1078. { upper case translation table for character set ISO 8859/1 Latin 1 }
  1079. CPISO88591UCT: array[192..255] of char =
  1080. ( #192, #193, #194, #195, #196, #197, #198, #199,
  1081. #200, #201, #202, #203, #204, #205, #206, #207,
  1082. #208, #209, #210, #211, #212, #213, #214, #215,
  1083. #216, #217, #218, #219, #220, #221, #222, #223,
  1084. #192, #193, #194, #195, #196, #197, #198, #199,
  1085. #200, #201, #202, #203, #204, #205, #206, #207,
  1086. #208, #209, #210, #211, #212, #213, #214, #247,
  1087. #216, #217, #218, #219, #220, #221, #222, #89 );
  1088. { lower case translation table for character set ISO 8859/1 Latin 1 }
  1089. CPISO88591LCT: array[192..255] of char =
  1090. ( #224, #225, #226, #227, #228, #229, #230, #231,
  1091. #232, #233, #234, #235, #236, #237, #238, #239,
  1092. #240, #241, #242, #243, #244, #245, #246, #215,
  1093. #248, #249, #250, #251, #252, #253, #254, #223,
  1094. #224, #225, #226, #227, #228, #229, #230, #231,
  1095. #232, #233, #234, #235, #236, #237, #238, #239,
  1096. #240, #241, #242, #243, #244, #245, #246, #247,
  1097. #248, #249, #250, #251, #252, #253, #254, #255 );
  1098. {
  1099. $Log$
  1100. Revision 1.10 2000-12-16 15:58:18 jonas
  1101. * removed warnings about possible range check errors
  1102. Revision 1.9 2000/12/07 21:58:30 michael
  1103. + Merged lastdelimiter from fixbranch
  1104. Revision 1.8 2000/12/06 22:55:29 michael
  1105. + Merged format fix from fixbranch
  1106. Revision 1.1.2.4 2000/12/07 21:48:57 michael
  1107. + Added LastDelimiter function
  1108. }