sysstr.inc 31 KB

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