sysstr.inc 31 KB

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