sysstr.inc 30 KB

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