sysstr.inc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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: 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. function CompareMem(P1, P2: Pointer; Length: cardinal): Boolean;
  113. var
  114. i: Integer;
  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 : longint;
  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 : longint;
  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 linux}
  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. { IntToHex returns a string representing the hexadecimal value of Value }
  457. const
  458. HexDigits: array[0..15] of char = '0123456789ABCDEF';
  459. function IntToHex(Value: integer; Digits: integer): string;
  460. var i: integer;
  461. begin
  462. SetLength(result, digits);
  463. for i := 0 to digits - 1 do begin
  464. result[digits - i] := HexDigits[value and 15];
  465. value := value shr 4;
  466. end ;
  467. end ;
  468. { StrToInt converts the string S to an integer value,
  469. if S does not represent a valid integer value EConvertError is raised }
  470. function StrToInt(const S: string): integer;
  471. var Error: word;
  472. begin
  473. Val(S, result, Error);
  474. if Error <> 0 then raise EConvertError.createfmt(SInValidInteger,[S]);
  475. end ;
  476. { StrToIntDef converts the string S to an integer value,
  477. Default is returned in case S does not represent a valid integer value }
  478. function StrToIntDef(const S: string; Default: integer): integer;
  479. var Error: word;
  480. begin
  481. Val(S, result, Error);
  482. if Error <> 0 then result := Default;
  483. end ;
  484. { LoadStr returns the string resource Ident. }
  485. function LoadStr(Ident: integer): string;
  486. begin
  487. result:='';
  488. end ;
  489. { FmtLoadStr returns the string resource Ident and formats it accordingly }
  490. function FmtLoadStr(Ident: integer; const Args: array of const): string;
  491. begin
  492. result:='';
  493. end;
  494. Const
  495. feInvalidFormat = 1;
  496. feMissingArgument = 2;
  497. feInvalidArgIndex = 3;
  498. {$ifdef fmtdebug}
  499. Procedure Log (Const S: String);
  500. begin
  501. Writeln (S);
  502. end;
  503. {$endif}
  504. Procedure DoFormatError (ErrCode : Longint);
  505. Var
  506. S : String;
  507. begin
  508. //!! must be changed to contain format string...
  509. S:='';
  510. Case ErrCode of
  511. feInvalidFormat : raise EConvertError.Createfmt(SInvalidFormat,[s]);
  512. feMissingArgument : raise EConvertError.Createfmt(SArgumentMissing,[s]);
  513. feInvalidArgIndex : raise EConvertError.Createfmt(SInvalidArgIndex,[s]);
  514. end;
  515. end;
  516. Function Format (Const Fmt : String; const Args : Array of const) : String;
  517. Var ChPos,OldPos,ArgPos,DoArg,Len : Longint;
  518. Hs,ToAdd : String;
  519. Index,Width,Prec : Longint;
  520. Left : Boolean;
  521. Fchar : char;
  522. {
  523. ReadFormat reads the format string. It returns the type character in
  524. uppercase, and sets index, Width, Prec to their correct values,
  525. or -1 if not set. It sets Left to true if left alignment was requested.
  526. In case of an error, DoFormatError is called.
  527. }
  528. Function ReadFormat : Char;
  529. Var Value : longint;
  530. Procedure ReadInteger;
  531. Var Code : Word;
  532. begin
  533. If Value<>-1 then exit; // Was already read.
  534. OldPos:=chPos;
  535. While (Chpos<=Len) and
  536. (Pos(Fmt[chpos],'1234567890')<>0) do inc(chpos);
  537. If Chpos>len then
  538. DoFormatError(feInvalidFormat);
  539. If Fmt[Chpos]='*' then
  540. begin
  541. If (Chpos>OldPos) or (ArgPos>High(Args))
  542. or (Args[ArgPos].Vtype<>vtInteger) then
  543. DoFormatError(feInvalidFormat);
  544. Value:=Args[ArgPos].VInteger;
  545. Inc(ArgPos);
  546. Inc(chPos);
  547. end
  548. else
  549. begin
  550. If (OldPos<chPos) Then
  551. begin
  552. Val (Copy(Fmt,OldPos,ChPos-OldPos),value,code);
  553. // This should never happen !!
  554. If Code>0 then DoFormatError (feInvalidFormat);
  555. end
  556. else
  557. Value:=-1;
  558. end;
  559. end;
  560. Procedure ReadIndex;
  561. begin
  562. ReadInteger;
  563. If Fmt[ChPos]=':' then
  564. begin
  565. If Value=-1 then DoFormatError(feMissingArgument);
  566. Index:=Value;
  567. Value:=-1;
  568. Inc(Chpos);
  569. end;
  570. {$ifdef fmtdebug}
  571. Log ('Read index');
  572. {$endif}
  573. end;
  574. Procedure ReadLeft;
  575. begin
  576. If Fmt[chpos]='-' then
  577. begin
  578. left:=True;
  579. Inc(chpos);
  580. end
  581. else
  582. Left:=False;
  583. {$ifdef fmtdebug}
  584. Log ('Read Left');
  585. {$endif}
  586. end;
  587. Procedure ReadWidth;
  588. begin
  589. ReadInteger;
  590. If Value<>-1 then
  591. begin
  592. Width:=Value;
  593. Value:=-1;
  594. end;
  595. {$ifdef fmtdebug}
  596. Log ('Read width');
  597. {$endif}
  598. end;
  599. Procedure ReadPrec;
  600. begin
  601. If Fmt[chpos]='.' then
  602. begin
  603. inc(chpos);
  604. ReadInteger;
  605. If Value=-1 then DoFormaterror(feMissingArgument);
  606. prec:=Value;
  607. end;
  608. {$ifdef fmtdebug}
  609. Log ('Read precision');
  610. {$endif}
  611. end;
  612. begin
  613. {$ifdef fmtdebug}
  614. Log ('Start format');
  615. {$endif}
  616. Index:=-1;
  617. Width:=-1;
  618. Prec:=-1;
  619. Value:=-1;
  620. inc(chpos);
  621. If Fmt[Chpos]='%' then exit('%');
  622. ReadIndex;
  623. ReadLeft;
  624. ReadWidth;
  625. ReadPrec;
  626. ReadFormat:=Upcase(Fmt[ChPos]);
  627. {$ifdef fmtdebug}
  628. Log ('End format');
  629. {$endif}
  630. end;
  631. {$ifdef fmtdebug}
  632. Procedure DumpFormat (C : char);
  633. begin
  634. Write ('Fmt : ',fmt:10);
  635. Write (' Index : ',Index:3);
  636. Write (' Left : ',left:5);
  637. Write (' Width : ',Width:3);
  638. Write (' Prec : ',prec:3);
  639. Writeln (' Type : ',C);
  640. end;
  641. {$endif}
  642. function Checkarg (AT : Longint;err:boolean):boolean;
  643. {
  644. Check if argument INDEX is of correct type (AT)
  645. If Index=-1, ArgPos is used, and argpos is augmented with 1
  646. DoArg is set to the argument that must be used.
  647. }
  648. begin
  649. result:=false;
  650. if Index=-1 then
  651. begin
  652. DoArg:=Argpos;
  653. inc(ArgPos);
  654. end
  655. else
  656. DoArg:=Index;
  657. If (Doarg>High(Args)) or (Args[Doarg].Vtype<>AT) then
  658. begin
  659. if err then
  660. DoFormatError(feInvalidArgindex);
  661. dec(ArgPos);
  662. exit;
  663. end;
  664. result:=true;
  665. end;
  666. Const Zero = '000000000000000000000000000000000000000000000000000000000000000';
  667. begin
  668. Result:='';
  669. Len:=Length(Fmt);
  670. Chpos:=1;
  671. OldPos:=1;
  672. ArgPos:=0;
  673. While chpos<=len do
  674. begin
  675. While (ChPos<=Len) and (Fmt[chpos]<>'%') do
  676. inc(chpos);
  677. If ChPos>OldPos Then
  678. Result:=Result+Copy(Fmt,OldPos,Chpos-Oldpos);
  679. If ChPos<Len then
  680. begin
  681. FChar:=ReadFormat;
  682. {$ifdef fmtdebug}
  683. DumpFormat(FCHar);
  684. {$endif}
  685. Case FChar of
  686. 'D' : begin
  687. Checkarg(vtinteger,true);
  688. Width:=Abs(width);
  689. Str(Args[Doarg].VInteger,ToAdd);
  690. While Length(ToAdd)<Prec do
  691. begin
  692. Index:=Prec-Length(ToAdd);
  693. If Index>64 then Index:=64;
  694. ToAdd:=Copy(Zero,1,Index)+ToAdd;
  695. end;
  696. end;
  697. 'E' : begin
  698. CheckArg(vtExtended,true);
  699. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffexponent,Prec,3);
  700. end;
  701. 'F' : begin
  702. CheckArg(vtExtended,true);
  703. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffFixed,9999,Prec);
  704. end;
  705. 'G' : begin
  706. CheckArg(vtExtended,true);
  707. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffGeneral,Prec,3);
  708. end;
  709. 'N' : begin
  710. CheckArg(vtExtended,true);
  711. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffNumber,9999,Prec);
  712. end;
  713. 'M' : begin
  714. CheckArg(vtExtended,true);
  715. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffCurrency,9999,Prec);
  716. end;
  717. 'S' : begin
  718. if CheckArg(vtString,false) then
  719. hs:=Args[doarg].VString^
  720. else
  721. if CheckArg(vtChar,false) then
  722. hs:=Args[doarg].VChar
  723. else
  724. if CheckArg(vtPChar,false) then
  725. hs:=Args[doarg].VPChar
  726. else
  727. if CheckArg(vtAnsiString,true) then
  728. hs:=ansistring(Args[doarg].VAnsiString);
  729. Index:=Length(hs);
  730. If (Prec<>-1) and (Index>Prec) then
  731. Index:=Prec;
  732. ToAdd:=Copy(hs,1,Index);
  733. end;
  734. 'P' : Begin
  735. CheckArg(vtpointer,true);
  736. ToAdd:=HexStr(Longint(Args[DoArg].VPointer),8);
  737. // Insert ':'. Is this needed in 32 bit ? No it isn't.
  738. // Insert(':',ToAdd,5);
  739. end;
  740. 'X' : begin
  741. Checkarg(vtinteger,true);
  742. If Prec>15 then
  743. ToAdd:=HexStr(Args[Doarg].VInteger,15)
  744. else
  745. begin
  746. // determine minimum needed number of hex digits.
  747. Index:=1;
  748. While (DWord(1 shl (Index*4))<DWord(Args[DoArg].VInteger)) and (index<8) do
  749. inc(Index);
  750. If Index>Prec then
  751. Prec:=Index;
  752. ToAdd:=HexStr(Args[DoArg].VInteger,Prec);
  753. end;
  754. end;
  755. '%': ToAdd:='%';
  756. end;
  757. If Width<>-1 then
  758. If Length(ToAdd)<Width then
  759. If not Left then
  760. ToAdd:=Space(Width-Length(ToAdd))+ToAdd
  761. else
  762. ToAdd:=ToAdd+space(Width-Length(ToAdd));
  763. Result:=Result+ToAdd;
  764. end;
  765. inc(chpos);
  766. Oldpos:=chpos;
  767. end;
  768. end;
  769. Function FormatBuf (Var Buffer; BufLen : Cardinal;
  770. Const Fmt; fmtLen : Cardinal;
  771. Const Args : Array of const) : Cardinal;
  772. Var S,F : String;
  773. begin
  774. Setlength(F,fmtlen);
  775. Move(fmt,F[1],fmtlen);
  776. S:=Format (F,Args);
  777. If Length(S)>Buflen then
  778. Result:=Length(S)
  779. else
  780. Result:=Buflen;
  781. Move(S[1],Buffer,Result);
  782. end;
  783. Procedure FmtStr(Var Res: String; Const Fmt : String; Const args: Array of const);
  784. begin
  785. Res:=Format(fmt,Args);
  786. end;
  787. Function StrFmt(Buffer,Fmt : PChar; Const args: Array of const) : Pchar;
  788. begin
  789. Buffer[FormatBuf(Buffer^,Maxint,Fmt^,strlen(fmt),args)]:=#0;
  790. Result:=Buffer;
  791. end;
  792. Function StrLFmt(Buffer : PCHar; Maxlen : Cardinal;Fmt : PChar; Const args: Array of const) : Pchar;
  793. begin
  794. Buffer[FormatBuf(Buffer^,MaxLen,Fmt^,strlen(fmt),args)]:=#0;
  795. Result:=Buffer;
  796. end;
  797. function StrToFloat(Value: string): Extended;
  798. var Error: word;
  799. begin
  800. Val(Value, result, Error);
  801. if Error <> 0 then raise
  802. EConvertError.createfmt(SInValidFLoat,[Value]);
  803. end ;
  804. Function FloatToStr(Value: Extended): String;
  805. Begin
  806. Result := FloatToStrF(Value, ffGeneral, 15, 0);
  807. End;
  808. Function FloatToText(Buffer: PChar; Value: Extended; format: TFloatFormat; Precision, Digits: Integer): Longint;
  809. Var
  810. Tmp: String[40];
  811. Begin
  812. Tmp := FloatToStrF(Value, format, Precision, Digits);
  813. Result := Length(Tmp);
  814. Move(Tmp[1], Buffer[0], Result);
  815. End;
  816. Function FloatToStrF(Value: Extended; format: TFloatFormat; Precision, Digits: Integer): String;
  817. Var
  818. P: Integer;
  819. Negative, TooSmall, TooLarge: Boolean;
  820. Begin
  821. Case format Of
  822. ffGeneral:
  823. Begin
  824. If (Precision = -1) Or (Precision > 15) Then Precision := 15;
  825. TooSmall := (Abs(Value) < 0.00001) and (Value>0.0);
  826. If Not TooSmall Then
  827. Begin
  828. Str(Value:0:999, Result);
  829. P := Pos('.', Result);
  830. Result[P] := DecimalSeparator;
  831. TooLarge := P > Precision + 1;
  832. End;
  833. If TooSmall Or TooLarge Then
  834. begin
  835. Result := FloatToStrF(Value, ffExponent, Precision, Digits);
  836. // Strip unneeded zeroes.
  837. P:=Pos('E',result)-1;
  838. If P<>-1 then
  839. While (P>1) and (Result[P]='0') do
  840. begin
  841. system.Delete(Result,P,1);
  842. Dec(P);
  843. end;
  844. end
  845. else
  846. begin
  847. P := Length(Result);
  848. While Result[P] = '0' Do Dec(P);
  849. If Result[P] = DecimalSeparator Then Dec(P);
  850. SetLength(Result, P);
  851. end;
  852. End;
  853. ffExponent:
  854. Begin
  855. If (Precision = -1) Or (Precision > 15) Then Precision := 15;
  856. Str(Value:Precision + 8, Result);
  857. Result[3] := DecimalSeparator;
  858. P:=4;
  859. While (P>0) and (Digits < P) And (Result[Precision + 5] = '0') do
  860. Begin
  861. If P<>1 then
  862. system.Delete(Result, Precision + 5, 1)
  863. else
  864. system.Delete(Result, Precision + 3, 3);
  865. Dec(P);
  866. end;
  867. If Result[1] = ' ' Then
  868. System.Delete(Result, 1, 1);
  869. End;
  870. ffFixed:
  871. Begin
  872. If Digits = -1 Then Digits := 2
  873. Else If Digits > 15 Then Digits := 15;
  874. Str(Value:0:Digits, Result);
  875. If Result[1] = ' ' Then
  876. System.Delete(Result, 1, 1);
  877. P := Pos('.', Result);
  878. If P <> 0 Then Result[P] := DecimalSeparator;
  879. End;
  880. ffNumber:
  881. Begin
  882. If Digits = -1 Then Digits := 2
  883. Else If Digits > 15 Then Digits := 15;
  884. Str(Value:0:Digits, Result);
  885. If Result[1] = ' ' Then System.Delete(Result, 1, 1);
  886. P := Pos('.', Result);
  887. If P <> 0 Then Result[P] := DecimalSeparator;
  888. Dec(P, 3);
  889. While (P > 1) Do
  890. Begin
  891. If Result[P - 1] <> '-' Then Insert(ThousandSeparator, Result, P);
  892. Dec(P, 3);
  893. End;
  894. End;
  895. ffCurrency:
  896. Begin
  897. If Value < 0 Then
  898. Begin
  899. Negative := True;
  900. Value := -Value;
  901. End
  902. Else Negative := False;
  903. If Digits = -1 Then Digits := CurrencyDecimals
  904. Else If Digits > 18 Then Digits := 18;
  905. Str(Value:0:Digits, Result);
  906. If Result[1] = ' ' Then System.Delete(Result, 1, 1);
  907. P := Pos('.', Result);
  908. If P <> 0 Then Result[P] := DecimalSeparator;
  909. Dec(P, 3);
  910. While (P > 1) Do
  911. Begin
  912. Insert(ThousandSeparator, Result, P);
  913. Dec(P, 3);
  914. End;
  915. If Not Negative Then
  916. Begin
  917. Case CurrencyFormat Of
  918. 0: Result := CurrencyString + Result;
  919. 1: Result := Result + CurrencyString;
  920. 2: Result := CurrencyString + ' ' + Result;
  921. 3: Result := Result + ' ' + CurrencyString;
  922. End
  923. End
  924. Else
  925. Begin
  926. Case NegCurrFormat Of
  927. 0: Result := '(' + CurrencyString + Result + ')';
  928. 1: Result := '-' + CurrencyString + Result;
  929. 2: Result := CurrencyString + '-' + Result;
  930. 3: Result := CurrencyString + Result + '-';
  931. 4: Result := '(' + Result + CurrencyString + ')';
  932. 5: Result := '-' + Result + CurrencyString;
  933. 6: Result := Result + '-' + CurrencyString;
  934. 7: Result := Result + CurrencyString + '-';
  935. 8: Result := '-' + Result + ' ' + CurrencyString;
  936. 9: Result := '-' + CurrencyString + ' ' + Result;
  937. 10: Result := CurrencyString + ' ' + Result + '-';
  938. End;
  939. End;
  940. End;
  941. End;
  942. End;
  943. {==============================================================================}
  944. { extra functions }
  945. {==============================================================================}
  946. { LeftStr returns Count left-most characters from S }
  947. function LeftStr(const S: string; Count: integer): string;
  948. begin
  949. result := Copy(S, 1, Count);
  950. end ;
  951. { RightStr returns Count right-most characters from S }
  952. function RightStr(const S: string; Count: integer): string;
  953. begin
  954. If Count>Length(S) then
  955. Count:=Length(S);
  956. result := Copy(S, 1 + Length(S) - Count, Count);
  957. end;
  958. { BCDToInt converts the BCD value Value to an integer }
  959. function BCDToInt(Value: integer): integer;
  960. var i, j: integer;
  961. begin
  962. result := 0;
  963. j := 1;
  964. for i := 0 to SizeOf(Value) shr 1 - 1 do begin
  965. result := result + j * (Value and 15);
  966. j := j * 10;
  967. Value := Value shr 4;
  968. end ;
  969. end ;
  970. {
  971. Case Translation Tables
  972. Can be used in internationalization support.
  973. Although these tables can be obtained through system calls
  974. it is better to not use those, since most implementation are not 100%
  975. WARNING:
  976. before modifying a translation table make sure that the current codepage
  977. of the OS corresponds to the one you make changes to
  978. }
  979. const
  980. { upper case translation table for character set 850 }
  981. CP850UCT: array[128..255] of char =
  982. ('€', 'š', '�', '¶', 'Ž', '¶', '�', '€', 'Ò', 'Ó', 'Ô', 'Ø', '×', 'Þ', 'Ž', '�',
  983. '�', '’', '’', 'â', '™', 'ã', 'ê', 'ë', 'Y', '™', 'š', '�', 'œ', '�', 'ž', 'Ÿ',
  984. 'µ', 'Ö', 'à', 'é', '¥', '¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯',
  985. '°', '±', '²', '³', '´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿',
  986. 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Ç', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï',
  987. 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', '×', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß',
  988. 'à', 'á', 'â', 'ã', 'å', 'å', 'æ', 'í', 'è', 'é', 'ê', 'ë', 'í', 'í', 'î', 'ï',
  989. 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ');
  990. { lower case translation table for character set 850 }
  991. CP850LCT: array[128..255] of char =
  992. ('‡', '�', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '�', '„', '†',
  993. '‚', '‘', '‘', '“', '”', '•', '–', '—', '˜', '”', '�', '›', 'œ', '›', 'ž', 'Ÿ',
  994. ' ', '¡', '¢', '£', '¤', '¤', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯',
  995. '°', '±', '²', '³', '´', ' ', 'ƒ', '…', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿',
  996. 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Æ', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï',
  997. 'Ð', 'Ñ', 'ˆ', '‰', 'Š', 'Õ', '¡', 'Œ', '‹', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', '�', 'ß',
  998. '¢', 'á', '“', '•', 'ä', 'ä', 'æ', 'í', 'è', '£', '–', '—', 'ì', 'ì', 'î', 'ï',
  999. 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ');
  1000. { upper case translation table for character set ISO 8859/1 Latin 1 }
  1001. CPISO88591UCT: array[192..255] of char =
  1002. ( #192, #193, #194, #195, #196, #197, #198, #199,
  1003. #200, #201, #202, #203, #204, #205, #206, #207,
  1004. #208, #209, #210, #211, #212, #213, #214, #215,
  1005. #216, #217, #218, #219, #220, #221, #222, #223,
  1006. #192, #193, #194, #195, #196, #197, #198, #199,
  1007. #200, #201, #202, #203, #204, #205, #206, #207,
  1008. #208, #209, #210, #211, #212, #213, #214, #247,
  1009. #216, #217, #218, #219, #220, #221, #222, #89 );
  1010. { lower case translation table for character set ISO 8859/1 Latin 1 }
  1011. CPISO88591LCT: array[192..255] of char =
  1012. ( #224, #225, #226, #227, #228, #229, #230, #231,
  1013. #232, #233, #234, #235, #236, #237, #238, #239,
  1014. #240, #241, #242, #243, #244, #245, #246, #215,
  1015. #248, #249, #250, #251, #252, #253, #254, #223,
  1016. #224, #225, #226, #227, #228, #229, #230, #231,
  1017. #232, #233, #234, #235, #236, #237, #238, #239,
  1018. #240, #241, #242, #243, #244, #245, #246, #247,
  1019. #248, #249, #250, #251, #252, #253, #254, #255 );
  1020. {
  1021. $Log$
  1022. Revision 1.34 2000-05-08 17:03:02 sg
  1023. * Changed CompareMem to CompareMemRange and added new (Delphi compatible)
  1024. CompareMem. (CompareMem needs a Boolean as result type, not Integer)
  1025. Revision 1.33 2000/05/08 13:26:42 peter
  1026. * vtchar support for %s
  1027. * define debug -> define fmtdebug
  1028. Revision 1.32 2000/04/03 06:40:37 michael
  1029. * TRim(right|Left) more Delphi compatible
  1030. Revision 1.31 2000/02/09 16:59:33 peter
  1031. * truncated log
  1032. Revision 1.30 2000/02/01 12:53:23 peter
  1033. * fixed rangecheck error in format()
  1034. Revision 1.29 1999/11/06 14:41:31 peter
  1035. * truncated log
  1036. Revision 1.28 1999/10/12 19:16:27 florian
  1037. * bug 645 fixed: format('%x',...) should writes unsigned hexadecimals, also
  1038. prec fixed: max. value in delphi is 15 (and not 32)
  1039. Revision 1.27 1999/10/03 19:42:40 peter
  1040. * fixed comparetext
  1041. Revision 1.26 1999/09/04 20:48:34 florian
  1042. * format('%g',[0.0]) returned long format string, fixed
  1043. Revision 1.25 1999/08/25 13:13:58 michael
  1044. fixed Formaterror, added missing raise
  1045. Revision 1.24 1999/08/16 22:38:53 peter
  1046. * fixed newstr/disposestr
  1047. }