sysstr.inc 28 KB

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