sysstr.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. {
  2. *********************************************************************
  3. $Id$
  4. Copyright (C) 1997, 1998 Gertjan Schouten
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. *********************************************************************
  17. System Utilities For Free Pascal
  18. }
  19. { NewStr creates a new PString and assigns S to it
  20. if length(s) = 0 NewStr returns Nil }
  21. function NewStr(const S: string): PString;
  22. begin
  23. result := Nil;
  24. if Length(S) <> 0 then
  25. begin
  26. New(Result);
  27. result^ := S;
  28. end ;
  29. end ;
  30. { DisposeStr frees the memory occupied by S }
  31. procedure DisposeStr(S: PString);
  32. begin
  33. if S <> Nil then begin
  34. Dispose(S);
  35. S := Nil;
  36. end ;
  37. end ;
  38. { AssignStr assigns S to P^ }
  39. procedure AssignStr(var P: PString; const S: string);
  40. begin
  41. P^ := s;
  42. end ;
  43. { AppendStr appends S to Dest }
  44. procedure AppendStr(var Dest: PString; const S: string);
  45. begin
  46. Dest^ := Dest^ + S;
  47. end ;
  48. { UpperCase returns a copy of S where all lowercase characters ( from a to z )
  49. have been converted to uppercase }
  50. function UpperCase(const S: string): string;
  51. var i: integer;
  52. begin
  53. result := S;
  54. i := Length(S);
  55. while i <> 0 do begin
  56. if (result[i] in ['a'..'z']) then result[i] := char(byte(result[i]) - 32);
  57. Dec(i);
  58. end;
  59. end;
  60. { LowerCase returns a copy of S where all uppercase characters ( from A to Z )
  61. have been converted to lowercase }
  62. function LowerCase(const S: string): string;
  63. var i: integer;
  64. begin
  65. result := S;
  66. i := Length(result);
  67. while i <> 0 do begin
  68. if (result[i] in ['A'..'Z']) then result[i] := char(byte(result[i]) + 32);
  69. dec(i);
  70. end;
  71. end;
  72. { CompareStr compares S1 and S2, the result is the based on
  73. substraction of the ascii values of the characters in S1 and S2
  74. case result
  75. S1 < S2 < 0
  76. S1 > S2 > 0
  77. S1 = S2 = 0 }
  78. function CompareStr(const S1, S2: string): Integer;
  79. var i, count, count1, count2: integer;
  80. begin
  81. result := 0;
  82. Count1 := Length(S1);
  83. Count2 := Length(S2);
  84. if Count1 > Count2 then Count := Count2
  85. else Count := Count1;
  86. result := CompareMem(Pointer(S1),Pointer(S2), Count);
  87. if (result = 0) and (Count1 <> Count2) then begin
  88. if Count1 > Count2 then result := ord(s1[Count1 + 1])
  89. else result := -ord(s2[Count2 + 1]);
  90. end ;
  91. end ;
  92. { CompareMem returns the result of comparison of Length bytes at P1 and P2
  93. case result
  94. P1 < P2 < 0
  95. P1 > P2 > 0
  96. P1 = P2 = 0 }
  97. function CompareMem(P1, P2: Pointer; Length: cardinal): integer;
  98. var i: integer;
  99. begin
  100. i := 0;
  101. result := 0;
  102. while (result = 0) and (i < length) do begin
  103. result := byte(P1^) - byte(P2^);
  104. P1 := P1 + 1;
  105. P2 := P2 + 1;
  106. i := i + 1;
  107. end ;
  108. end ;
  109. { CompareText compares S1 and S2, the result is the based on
  110. substraction of the ascii values of characters in S1 and S2
  111. comparison is case-insensitive
  112. case result
  113. S1 < S2 < 0
  114. S1 > S2 > 0
  115. S1 = S2 = 0 }
  116. function CompareText(const S1, S2: string): integer;
  117. var i, count, count1, count2: integer; Chr1, Chr2: byte;
  118. begin
  119. result := 0;
  120. Count1 := Length(S1);
  121. Count2 := Length(S2);
  122. if Count1 > Count2 then Count := Count2
  123. else Count := Count1;
  124. i := 0;
  125. while (result = 0) and (i < count) do begin
  126. i := i + 1;
  127. Chr1 := byte(s1[i]);
  128. Chr2 := byte(s2[i]);
  129. if Chr1 in [97..122] then Chr1 := Chr1 - 32;
  130. if Chr2 in [97..122] then Chr2 := Chr2 - 32;
  131. result := Chr1 - Chr2;
  132. end ;
  133. if (result = 0) and (Count1 <> Count2) then begin
  134. if Count1 > Count2 then result := byte(UpCase(s1[Count1 + 1]))
  135. else result := -byte(UpCase(s2[Count2 + 1]));
  136. end ;
  137. end ;
  138. {==============================================================================}
  139. { Ansi string functions }
  140. { these functions rely on the character set loaded by the OS }
  141. {==============================================================================}
  142. function AnsiUpperCase(const s: string): string;
  143. var len, i: integer;
  144. begin
  145. len := length(s);
  146. SetLength(result, len);
  147. for i := 1 to len do
  148. result[i] := UpperCaseTable[ord(s[i])];
  149. end ;
  150. function AnsiLowerCase(const s: string): string;
  151. var len, i: integer;
  152. begin
  153. len := length(s);
  154. SetLength(result, len);
  155. for i := 1 to len do
  156. result[i] := LowerCaseTable[ord(s[i])];
  157. end ;
  158. function AnsiCompareStr(const S1, S2: string): integer;
  159. begin
  160. end ;
  161. function AnsiCompareText(const S1, S2: string): integer;
  162. begin
  163. end ;
  164. function AnsiStrComp(S1, S2: PChar): integer;
  165. begin
  166. end ;
  167. function AnsiStrIComp(S1, S2: PChar): integer;
  168. begin
  169. end ;
  170. function AnsiStrLComp(S1, S2: PChar; MaxLen: cardinal): integer;
  171. begin
  172. end ;
  173. function AnsiStrLIComp(S1, S2: PChar; MaxLen: cardinal): integer;
  174. begin
  175. end ;
  176. function AnsiStrLower(Str: PChar): PChar;
  177. begin
  178. if Str <> Nil then begin
  179. while Str^ <> #0 do begin
  180. Str^ := LowerCaseTable[byte(Str^)];
  181. Str := Str + 1;
  182. end ;
  183. end ;
  184. result := Str;
  185. end ;
  186. function AnsiStrUpper(Str: PChar): PChar;
  187. begin
  188. if Str <> Nil then begin
  189. while Str^ <> #0 do begin
  190. Str^ := UpperCaseTable[byte(Str^)];
  191. Str := Str + 1;
  192. end ;
  193. end ;
  194. result := Str;
  195. end ;
  196. function AnsiLastChar(const S: string): PChar;
  197. begin
  198. end ;
  199. function AnsiStrLastChar(Str: PChar): PChar;
  200. begin
  201. end ;
  202. {==============================================================================}
  203. { End of Ansi functions }
  204. {==============================================================================}
  205. { Trim returns a copy of S with blanks characters on the left and right stripped off }
  206. function Trim(const S: string): string;
  207. var Ofs, Len: integer;
  208. begin
  209. len := Length(S);
  210. while (Len>0) and (S[Len] = ' ') do
  211. dec(Len);
  212. Ofs := 1;
  213. while (Ofs<=Len) and (S[Ofs] = ' ') do
  214. Inc(Ofs);
  215. result := Copy(S, Ofs, 1 + Len - Ofs);
  216. end ;
  217. { TrimLeft returns a copy of S with all blank characters on the left stripped off }
  218. function TrimLeft(const S: string): string;
  219. var i,l:integer;
  220. begin
  221. l := length(s);
  222. i := 1;
  223. while (i<=l) and (s[i] = ' ') do
  224. inc(i);
  225. Result := copy(s, i, l);
  226. end ;
  227. { TrimRight returns a copy of S with all blank characters on the right stripped off }
  228. function TrimRight(const S: string): string;
  229. var l:integer;
  230. begin
  231. l := length(s);
  232. while (l>0) and (s[l] = ' ') do
  233. dec(l);
  234. result := copy(s,1,l);
  235. end ;
  236. { QuotedStr returns S quoted left and right and every single quote in S
  237. replaced by two quotes }
  238. function QuotedStr(const S: string): string;
  239. begin
  240. result := AnsiQuotedStr(s, '''');
  241. end ;
  242. { AnsiQuotedStr returns S quoted left and right by Quote,
  243. and every single occurance of Quote replaced by two }
  244. function AnsiQuotedStr(const S: string; Quote: char): string;
  245. var i, j, count: integer;
  246. begin
  247. result := '' + Quote;
  248. count := length(s);
  249. i := 0;
  250. j := 0;
  251. while i < count do begin
  252. i := i + 1;
  253. if S[i] = Quote then begin
  254. result := result + copy(S, 1 + j, i - j) + Quote;
  255. j := i;
  256. end ;
  257. end ;
  258. if i <> j then
  259. result := result + copy(S, 1 + j, i - j);
  260. result := result + Quote;
  261. end ;
  262. { AnsiExtractQuotedStr returns a copy of Src with quote characters
  263. deleted to the left and right and double occurances
  264. of Quote replaced by a single Quote }
  265. function AnsiExtractQuotedStr(var Src: PChar; Quote: Char): string;
  266. var i: integer; P, Q: PChar;
  267. begin
  268. P := Src;
  269. if Src^ = Quote then P := P + 1;
  270. Q := StrEnd(P);
  271. if PChar(Q - 1)^ = Quote then Q := Q - 1;
  272. SetLength(result, Q - P);
  273. i := 0;
  274. while P <> Q do begin
  275. i := i + 1;
  276. result[i] := P^;
  277. if (P^ = Quote) and (PChar(P + 1)^ = Quote) then
  278. P := P + 1;
  279. P := P + 1;
  280. end ;
  281. SetLength(result, i);
  282. end ;
  283. { AdjustLineBreaks returns S with all CR characters not followed by LF
  284. replaced with CR/LF }
  285. // under Linux all CR characters or CR/LF combinations should be replaced with LF
  286. function AdjustLineBreaks(const S: string): string;
  287. var i, j, count: integer;
  288. begin
  289. result := '';
  290. i := 0;
  291. j := 0;
  292. count := Length(S);
  293. while i < count do begin
  294. i := i + 1;
  295. if (S[i] = #13) and ((i = count) or (S[i + 1] <> #10)) then begin
  296. result := result + Copy(S, 1 + j, i - j) + #10;
  297. j := i;
  298. end ;
  299. end ;
  300. if j <> i then
  301. result := result + copy(S, 1 + j, i - j);
  302. end ;
  303. { IsValidIdent returns true if the first character of Ident is in:
  304. 'A' to 'Z', 'a' to 'z' or '_' and the following characters are
  305. on of: 'A' to 'Z', 'a' to 'z', '0'..'9' or '_' }
  306. function IsValidIdent(const Ident: string): boolean;
  307. var i, len: integer;
  308. begin
  309. result := false;
  310. len := length(Ident);
  311. if len <> 0 then begin
  312. result := Ident[1] in ['A'..'Z', 'a'..'z', '_'];
  313. i := 1;
  314. while (result) and (i < len) do begin
  315. i := i + 1;
  316. result := result and (Ident[i] in ['A'..'Z', 'a'..'z', '0'..'9', '_']);
  317. end ;
  318. end ;
  319. end ;
  320. { IntToStr returns a string representing the value of Value }
  321. function IntToStr(Value: integer): string;
  322. begin
  323. System.Str(Value, result);
  324. end ;
  325. { IntToHex returns a string representing the hexadecimal value of Value }
  326. const
  327. HexDigits: array[0..15] of char = '0123456789ABCDEF';
  328. function IntToHex(Value: integer; Digits: integer): string;
  329. var i: integer;
  330. begin
  331. SetLength(result, digits);
  332. for i := 0 to digits - 1 do begin
  333. result[digits - i] := HexDigits[value and 15];
  334. value := value shr 4;
  335. end ;
  336. end ;
  337. { StrToInt converts the string S to an integer value,
  338. if S does not represent a valid integer value EConvertError is raised }
  339. function StrToInt(const S: string): integer;
  340. var Error: word;
  341. begin
  342. Val(S, result, Error);
  343. if Error <> 0 then raise EConvertError.createfmt(SInValidInteger,[S]);
  344. end ;
  345. { StrToIntDef converts the string S to an integer value,
  346. Default is returned in case S does not represent a valid integer value }
  347. function StrToIntDef(const S: string; Default: integer): integer;
  348. var Error: word;
  349. begin
  350. Val(S, result, Error);
  351. if Error <> 0 then result := Default;
  352. end ;
  353. { LoadStr returns the string resource Ident. }
  354. function LoadStr(Ident: integer): string;
  355. begin
  356. end ;
  357. { FmtLoadStr returns the string resource Ident and formats it accordingly }
  358. function FmtLoadStr(Ident: integer; const Args: array of const): string;
  359. begin
  360. end;
  361. Const
  362. feInvalidFormat = 1;
  363. feMissingArgument = 2;
  364. feInvalidArgIndex = 3;
  365. Procedure Log (Const S: String);
  366. begin
  367. {$ifdef debug}
  368. Writeln (S);
  369. {$endif}
  370. end;
  371. Procedure DoFormatError (ErrCode : Longint);
  372. Var S : String;
  373. begin
  374. //!! must be changed to contain format string...
  375. S:='';
  376. Case ErrCode of
  377. feInvalidFormat : EConvertError.Createfmt(SInvalidFormat,[s]);
  378. feMissingArgument : EConvertError.Createfmt(SArgumentMissing,[s]);
  379. feInvalidArgIndex : EConvertError.Createfmt(SInvalidArgIndex,[s]);
  380. end;
  381. end;
  382. Function Format (Const Fmt : String; const Args : Array of const) : String;
  383. Var ChPos,OldPos,ArgPos,DoArg,Len : Longint;
  384. Hs,ToAdd : String;
  385. Index,Width,Prec : Longint;
  386. Left : Boolean;
  387. ExtVal: Extended;
  388. Fchar : char;
  389. {
  390. ReadFormat reads the format string. It returns the type character in
  391. uppercase, and sets index, Width, Prec to their correct values,
  392. or -1 if not set. It sets Left to true if left alignment was requested.
  393. In case of an error, DoFormatError is called.
  394. }
  395. Function ReadFormat : Char;
  396. Var Value : longint;
  397. Procedure ReadInteger;
  398. Var Code : Word;
  399. begin
  400. If Value<>-1 then exit; // Was already read.
  401. OldPos:=chPos;
  402. While (Chpos<Len) and
  403. (Pos(Fmt[chpos],'1234567890')<>0) do inc(chpos);
  404. If Chpos=len then DoFormatError(feInvalidFormat);
  405. If Fmt[Chpos]='*' then
  406. begin
  407. If (Chpos>OldPos) or (ArgPos>High(Args))
  408. or (Args[ArgPos].Vtype<>vtInteger) then
  409. DoFormatError(feInvalidFormat);
  410. Value:=Args[ArgPos].VInteger;
  411. Inc(ArgPos);
  412. Inc(chPos);
  413. end
  414. else
  415. begin
  416. If (OldPos<chPos) Then
  417. begin
  418. Val (Copy(Fmt,OldPos,ChPos-OldPos),value,code);
  419. // This should never happen !!
  420. If Code>0 then DoFormatError (feInvalidFormat);
  421. end
  422. else
  423. Value:=-1;
  424. end;
  425. end;
  426. Procedure ReadIndex;
  427. begin
  428. ReadInteger;
  429. If Fmt[ChPos]=':' then
  430. begin
  431. If Value=-1 then DoFormatError(feMissingArgument);
  432. Index:=Value;
  433. Value:=-1;
  434. Inc(Chpos);
  435. end;
  436. Log ('Read index');
  437. end;
  438. Procedure ReadLeft;
  439. begin
  440. If Fmt[chpos]='-' then
  441. begin
  442. left:=True;
  443. Inc(chpos);
  444. end
  445. else
  446. Left:=False;
  447. Log ('Read Left');
  448. end;
  449. Procedure ReadWidth;
  450. begin
  451. ReadInteger;
  452. If Value<>-1 then
  453. begin
  454. Width:=Value;
  455. Value:=-1;
  456. end;
  457. Log ('Read width');
  458. end;
  459. Procedure ReadPrec;
  460. begin
  461. If Fmt[chpos]='.' then
  462. begin
  463. inc(chpos);
  464. ReadInteger;
  465. If Value=-1 then DoFormaterror(feMissingArgument);
  466. prec:=Value;
  467. end;
  468. Log ('Read precision');
  469. end;
  470. begin
  471. Log ('Start format');
  472. Index:=-1;
  473. Width:=-1;
  474. Prec:=-1;
  475. Value:=-1;
  476. inc(chpos);
  477. If Fmt[Chpos]='%' then exit('%');
  478. ReadIndex;
  479. ReadLeft;
  480. ReadWidth;
  481. ReadPrec;
  482. ReadFormat:=Upcase(Fmt[ChPos]);
  483. Log ('End format');
  484. end;
  485. Procedure DumpFormat (C : char);
  486. begin
  487. Write ('Fmt : ',fmt:10);
  488. Write (' Index : ',Index:3);
  489. Write (' Left : ',left:5);
  490. Write (' Width : ',Width:3);
  491. Write (' Prec : ',prec:3);
  492. Writeln (' Type : ',C);
  493. end;
  494. function Checkarg (AT : Longint;err:boolean):boolean;
  495. {
  496. Check if argument INDEX is of correct type (AT)
  497. If Index=-1, ArgPos is used, and argpos is augmented with 1
  498. DoArg is set to the argument that must be used.
  499. }
  500. begin
  501. result:=false;
  502. if Index=-1 then
  503. begin
  504. DoArg:=Argpos;
  505. inc(ArgPos);
  506. end
  507. else
  508. DoArg:=Index;
  509. If (Doarg>High(Args)) or (Args[Doarg].Vtype<>AT) then
  510. begin
  511. if err then
  512. DoFormatError(feInvalidArgindex);
  513. exit;
  514. end;
  515. result:=true;
  516. end;
  517. Const Zero = '000000000000000000000000000000000000000000000000000000000000000';
  518. begin
  519. Result:='';
  520. Len:=Length(Fmt)+1;
  521. Chpos:=1;
  522. OldPos:=1;
  523. ArgPos:=0;
  524. While chpos<len do
  525. begin
  526. // uses shortcut evaluation !!
  527. While (ChPos<=Len) and (Fmt[chpos]<>'%') do inc(chpos);
  528. If ChPos>OldPos Then
  529. Result:=Result+Copy(Fmt,OldPos,Chpos-Oldpos);
  530. If ChPos<Len then
  531. begin
  532. FChar:=ReadFormat;
  533. {$ifdef debug}
  534. DumpFormat(FCHar);
  535. {$endif}
  536. Case FChar of
  537. 'D' : begin
  538. Checkarg(vtinteger,true);
  539. Width:=Abs(width);
  540. Str(Args[Doarg].VInteger,ToAdd);
  541. While Length(ToAdd)<Prec do
  542. begin
  543. Index:=Prec-Length(ToAdd);
  544. If Index>64 then Index:=64;
  545. ToAdd:=Copy(Zero,1,Index)+ToAdd;
  546. end;
  547. end;
  548. 'E' : begin
  549. CheckArg(vtExtended,true);
  550. If Prec=-1 then prec:=15;
  551. ExtVal:=Args[doarg].VExtended^;
  552. Prec:=Prec+5; // correct dot, eXXX
  553. If ExtVal<0 then Inc(Prec); // Corect for minus sign
  554. If Abs(Extval)<1 then Inc(Prec); // correct for - in E
  555. Str(Args[doarg].VExtended^:prec,ToAdd);
  556. end;
  557. 'F' : begin
  558. end;
  559. 'S' : begin
  560. if CheckArg(vtString,false) then
  561. hs:=Args[doarg].VString^
  562. else
  563. begin
  564. dec(argpos);
  565. if CheckArg(vtAnsiString,true) then
  566. hs:=ansistring(Args[doarg].VAnsiString);
  567. end;
  568. Index:=Length(hs);
  569. If (Prec<>-1) and (Index>Prec) then
  570. Index:=Prec;
  571. ToAdd:=Copy(hs,1,Index);
  572. end;
  573. 'P' : Begin
  574. CheckArg(vtpointer,true);
  575. ToAdd:=HexStr(Longint(Args[DoArg].VPointer),8);
  576. // Insert ':'. Is this needed in 32 bit ? No it isn't.
  577. // Insert(':',ToAdd,5);
  578. end;
  579. 'X' : begin
  580. Checkarg(vtinteger,true);
  581. If Prec>32 then
  582. ToAdd:=HexStr(Args[Doarg].VInteger,Prec)
  583. else
  584. begin
  585. // determine minimum needed number of hex digits.
  586. Index:=1;
  587. While (1 shl (Index*4))<Args[DoArg].VInteger do
  588. inc(Index);
  589. If Index>Prec then
  590. Prec:=Index;
  591. ToAdd:=HexStr(Args[DoArg].VInteger,Prec);
  592. end;
  593. end;
  594. '%': ToAdd:='%';
  595. end;
  596. If Width<>-1 then
  597. If Length(ToAdd)<Width then
  598. If not Left then
  599. ToAdd:=Space(Width-Length(ToAdd))+ToAdd
  600. else
  601. ToAdd:=ToAdd+space(Width-Length(ToAdd));
  602. Result:=Result+ToAdd;
  603. end;
  604. inc(chpos);
  605. Oldpos:=chpos;
  606. end;
  607. end;
  608. Function FormatBuf (Var Buffer; BufLen : Cardinal;
  609. Const Fmt; fmtLen : Cardinal;
  610. Const Args : Array of const) : Cardinal;
  611. Var S,F : String;
  612. begin
  613. Setlength(F,fmtlen);
  614. Move(fmt,F[1],fmtlen);
  615. S:=Format (F,Args);
  616. If Length(S)>Buflen then
  617. Result:=Length(S)
  618. else
  619. Result:=Buflen;
  620. Move(S[1],Buffer,Result);
  621. end;
  622. Procedure FmtStr(Var Res: String; Const Fmt : String; Const args: Array of const);
  623. begin
  624. Res:=Format(fmt,Args);
  625. end;
  626. Function StrFmt(Buffer,Fmt : PChar; Const args: Array of const) : Pchar;
  627. begin
  628. Buffer[FormatBuf(Buffer^,Maxint,Fmt^,strlen(fmt),args)]:=#0;
  629. Result:=Buffer;
  630. end;
  631. Function StrLFmt(Buffer : PCHar; Maxlen : Cardinal;Fmt : PChar; Const args: Array of const) : Pchar;
  632. begin
  633. Buffer[FormatBuf(Buffer^,MaxLen,Fmt^,strlen(fmt),args)]:=#0;
  634. Result:=Buffer;
  635. end;
  636. Function FloatToStr(Value: Extended): String;
  637. Begin
  638. Result := FloatToStrF(Value, ffGeneral, 15, 0);
  639. End;
  640. Function FloatToStrF(Value: Extended; format: TFloatFormat; Precision, Digits: Integer): String;
  641. Var
  642. P: Integer;
  643. Negative, TooSmall, TooLarge: Boolean;
  644. Begin
  645. Case format Of
  646. ffGeneral:
  647. Begin
  648. If (Precision = -1) Or (Precision > 15) Then Precision := 15;
  649. TooSmall := Abs(Value) < 0.00001;
  650. If Not TooSmall Then
  651. Begin
  652. Str(Value:0:999, Result);
  653. P := Pos('.', Result);
  654. Result[P] := DecimalSeparator;
  655. TooLarge := P > Precision + 1;
  656. End;
  657. If TooSmall Or TooLarge Then
  658. Result := FloatToStrF(Value, ffExponent, Precision, Digits);
  659. P := Length(Result);
  660. While Result[P] = '0' Do Dec(P);
  661. If Result[P] = DecimalSeparator Then Dec(P);
  662. SetLength(Result, P);
  663. End;
  664. ffExponent:
  665. Begin
  666. If (Precision = -1) Or (Precision > 15) Then Precision := 15;
  667. Str(Value:Precision + 8, Result);
  668. Result[3] := DecimalSeparator;
  669. If (Digits < 4) And (Result[Precision + 5] = '0') Then
  670. Begin
  671. system.Delete(Result, Precision + 5, 1);
  672. If (Digits < 3) And (Result[Precision + 5] = '0') Then
  673. Begin
  674. system.Delete(Result, Precision + 5, 1);
  675. If (Digits < 2) And (Result[Precision + 5] = '0') Then
  676. Begin
  677. system.Delete(Result, Precision + 5, 1);
  678. If (Digits < 1) And (Result[Precision + 5] = '0') Then system.Delete(Result, Precision + 3, 3);
  679. End;
  680. End;
  681. End;
  682. If Result[1] = ' ' Then
  683. System.Delete(Result, 1, 1);
  684. End;
  685. ffFixed:
  686. Begin
  687. If Digits = -1 Then Digits := 2
  688. Else If Digits > 15 Then Digits := 15;
  689. Str(Value:0:Digits, Result);
  690. If Result[1] = ' ' Then
  691. System.Delete(Result, 1, 1);
  692. P := Pos('.', Result);
  693. If P <> 0 Then Result[P] := DecimalSeparator;
  694. End;
  695. ffNumber:
  696. Begin
  697. If Digits = -1 Then Digits := 2
  698. Else If Digits > 15 Then Digits := 15;
  699. Str(Value:0:Digits, Result);
  700. If Result[1] = ' ' Then System.Delete(Result, 1, 1);
  701. P := Pos('.', Result);
  702. If P <> 0 Then Result[P] := DecimalSeparator;
  703. Dec(P, 3);
  704. While (P > 1) Do
  705. Begin
  706. If Result[P - 1] <> '-' Then Insert(ThousandSeparator, Result, P);
  707. Dec(P, 3);
  708. End;
  709. End;
  710. ffCurrency:
  711. Begin
  712. If Value < 0 Then
  713. Begin
  714. Negative := True;
  715. Value := -Value;
  716. End
  717. Else Negative := False;
  718. If Digits = -1 Then Digits := CurrencyDecimals
  719. Else If Digits > 15 Then Digits := 15;
  720. Str(Value:0:Digits, Result);
  721. If Result[1] = ' ' Then System.Delete(Result, 1, 1);
  722. P := Pos('.', Result);
  723. If P <> 0 Then Result[P] := DecimalSeparator;
  724. Dec(P, 3);
  725. While (P > 1) Do
  726. Begin
  727. Insert(ThousandSeparator, Result, P);
  728. Dec(P, 3);
  729. End;
  730. If Not Negative Then
  731. Begin
  732. Case CurrencyFormat Of
  733. 0: Result := CurrencyString + Result;
  734. 1: Result := Result + CurrencyString;
  735. 2: Result := CurrencyString + ' ' + Result;
  736. 3: Result := Result + ' ' + CurrencyString;
  737. End
  738. End
  739. Else
  740. Begin
  741. Case NegCurrFormat Of
  742. 0: Result := '(' + CurrencyString + Result + ')';
  743. 1: Result := '-' + CurrencyString + Result;
  744. 2: Result := CurrencyString + '-' + Result;
  745. 3: Result := CurrencyString + Result + '-';
  746. 4: Result := '(' + Result + CurrencyString + ')';
  747. 5: Result := '-' + Result + CurrencyString;
  748. 6: Result := Result + '-' + CurrencyString;
  749. 7: Result := Result + CurrencyString + '-';
  750. 8: Result := '-' + Result + ' ' + CurrencyString;
  751. 9: Result := '-' + CurrencyString + ' ' + Result;
  752. 10: Result := CurrencyString + ' ' + Result + '-';
  753. End;
  754. End;
  755. End;
  756. End;
  757. End;
  758. {==============================================================================}
  759. { extra functions }
  760. {==============================================================================}
  761. { LeftStr returns Count left-most characters from S }
  762. function LeftStr(const S: string; Count: integer): string;
  763. begin
  764. result := Copy(S, 1, Count);
  765. end ;
  766. { RightStr returns Count right-most characters from S }
  767. function RightStr(const S: string; Count: integer): string;
  768. begin
  769. result := Copy(S, 1 + Length(S) - Count, Count);
  770. end;
  771. { BCDToInt converts the BCD value Value to an integer }
  772. function BCDToInt(Value: integer): integer;
  773. var i, j: integer;
  774. begin
  775. result := 0;
  776. j := 1;
  777. for i := 0 to SizeOf(Value) shr 1 - 1 do begin
  778. result := result + j * (Value and 15);
  779. j := j * 10;
  780. Value := Value shr 4;
  781. end ;
  782. end ;
  783. {
  784. Case Translation Tables
  785. Can be used in internationalization support.
  786. Although these tables can be obtained through system calls
  787. it is better to not use those, since most implementation are not 100%
  788. WARNING:
  789. before modifying a translation table make sure that the current codepage
  790. of the OS corresponds to the one you make changes to
  791. }
  792. const
  793. { upper case translation table for character set 850 }
  794. CP850UCT: array[128..255] of char =
  795. ('€', 'š', '�', '¶', 'Ž', '¶', '�', '€', 'Ò', 'Ó', 'Ô', 'Ø', '×', 'Þ', 'Ž', '�',
  796. '�', '’', '’', 'â', '™', 'ã', 'ê', 'ë', 'Y', '™', 'š', '�', 'œ', '�', 'ž', 'Ÿ',
  797. 'µ', 'Ö', 'à', 'é', '¥', '¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯',
  798. '°', '±', '²', '³', '´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿',
  799. 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Ç', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï',
  800. 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', '×', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß',
  801. 'à', 'á', 'â', 'ã', 'å', 'å', 'æ', 'í', 'è', 'é', 'ê', 'ë', 'í', 'í', 'î', 'ï',
  802. 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ');
  803. { lower case translation table for character set 850 }
  804. CP850LCT: array[128..255] of char =
  805. ('‡', '�', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '�', '„', '†',
  806. '‚', '‘', '‘', '“', '”', '•', '–', '—', '˜', '”', '�', '›', 'œ', '›', 'ž', 'Ÿ',
  807. ' ', '¡', '¢', '£', '¤', '¤', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯',
  808. '°', '±', '²', '³', '´', ' ', 'ƒ', '…', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿',
  809. 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Æ', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï',
  810. 'Ð', 'Ñ', 'ˆ', '‰', 'Š', 'Õ', '¡', 'Œ', '‹', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', '�', 'ß',
  811. '¢', 'á', '“', '•', 'ä', 'ä', 'æ', 'í', 'è', '£', '–', '—', 'ì', 'ì', 'î', 'ï',
  812. 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ');
  813. { upper case translation table for character set ISO 8859/1 Latin 1 }
  814. CPISO88591UCT: array[192..255] of char =
  815. ( #192, #193, #194, #195, #196, #197, #198, #199,
  816. #200, #201, #202, #203, #204, #205, #206, #207,
  817. #208, #209, #210, #211, #212, #213, #214, #215,
  818. #216, #217, #218, #219, #220, #221, #222, #223,
  819. #192, #193, #194, #195, #196, #197, #198, #199,
  820. #200, #201, #202, #203, #204, #205, #206, #207,
  821. #208, #209, #210, #211, #212, #213, #214, #247,
  822. #216, #217, #218, #219, #220, #221, #222, #89 );
  823. { lower case translation table for character set ISO 8859/1 Latin 1 }
  824. CPISO88591LCT: array[192..255] of char =
  825. ( #224, #225, #226, #227, #228, #229, #230, #231,
  826. #232, #233, #234, #235, #236, #237, #238, #239,
  827. #240, #241, #242, #243, #244, #245, #246, #215,
  828. #248, #249, #250, #251, #252, #253, #254, #223,
  829. #224, #225, #226, #227, #228, #229, #230, #231,
  830. #232, #233, #234, #235, #236, #237, #238, #239,
  831. #240, #241, #242, #243, #244, #245, #246, #247,
  832. #248, #249, #250, #251, #252, #253, #254, #255 );
  833. {
  834. $Log$
  835. Revision 1.15 1999-04-04 10:19:07 peter
  836. * format support for ansistring (from mailinglist)
  837. * fixed length checking in Trim()
  838. Revision 1.14 1999/03/01 12:40:06 michael
  839. changed delete to system.delete
  840. Revision 1.13 1999/02/28 13:17:35 michael
  841. + Added internationalization support and more format functions
  842. Revision 1.12 1999/02/24 15:56:29 michael
  843. + Small fixes. Moved getlocaltime to system-dependent files
  844. Revision 1.11 1999/02/10 22:15:12 michael
  845. + Changed to ansistrings
  846. Revision 1.10 1998/12/15 22:43:09 peter
  847. * removed temp symbols
  848. Revision 1.9 1998/11/04 10:20:52 peter
  849. * ansistring fixes
  850. Revision 1.8 1998/10/02 13:57:38 michael
  851. Format error now causes exception
  852. Revision 1.7 1998/10/02 12:17:17 michael
  853. + Made sure it compiles with official 0.99.8
  854. Revision 1.6 1998/10/02 10:42:17 michael
  855. + Initial implementation of format
  856. Revision 1.5 1998/10/01 16:05:37 michael
  857. Added (empty) format function
  858. Revision 1.4 1998/09/17 12:39:52 michael
  859. + Further fixes from GertJan Schouten
  860. Revision 1.3 1998/09/16 14:34:37 pierre
  861. * go32v2 did not compile
  862. * wrong code in systr.inc corrected
  863. Revision 1.2 1998/09/16 08:28:42 michael
  864. Update from gertjan Schouten, plus small fix for linux
  865. Revision 1.1 1998/04/10 15:17:46 michael
  866. + Initial implementation; Donated by Gertjan Schouten
  867. His file was split into several files, to keep it a little bit structured.
  868. }