sysstr.inc 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  1. {
  2. *********************************************************************
  3. $Id$
  4. Copyright (C) 1997, 1998 Gertjan Schouten
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. *********************************************************************
  17. System Utilities For Free Pascal
  18. }
  19. { NewStr creates a new PString and assigns S to it
  20. if length(s) = 0 NewStr returns Nil }
  21. function NewStr(const S: string): PString;
  22. begin
  23. if (S='') then
  24. Result:=nil
  25. else
  26. begin
  27. getmem(Result,length(s)+1);
  28. if (Result<>nil) then
  29. Result^:=s;
  30. end;
  31. end;
  32. { DisposeStr frees the memory occupied by S }
  33. procedure DisposeStr(S: PString);
  34. begin
  35. if S <> Nil then
  36. begin
  37. Freemem(S,Length(S^)+1);
  38. S:=nil;
  39. end;
  40. end;
  41. { AssignStr assigns S to P^ }
  42. procedure AssignStr(var P: PString; const S: string);
  43. begin
  44. P^ := s;
  45. end ;
  46. { AppendStr appends S to Dest }
  47. procedure AppendStr(var Dest: String; const S: string);
  48. begin
  49. Dest := Dest + S;
  50. end ;
  51. { UpperCase returns a copy of S where all lowercase characters ( from a to z )
  52. have been converted to uppercase }
  53. function UpperCase(const S: string): string;
  54. var i: integer;
  55. begin
  56. result := S;
  57. i := Length(S);
  58. while i <> 0 do begin
  59. if (result[i] in ['a'..'z']) then result[i] := char(byte(result[i]) - 32);
  60. Dec(i);
  61. end;
  62. end;
  63. { LowerCase returns a copy of S where all uppercase characters ( from A to Z )
  64. have been converted to lowercase }
  65. function LowerCase(const S: string): string;
  66. var i: integer;
  67. begin
  68. result := S;
  69. i := Length(result);
  70. while i <> 0 do begin
  71. if (result[i] in ['A'..'Z']) then result[i] := char(byte(result[i]) + 32);
  72. dec(i);
  73. end;
  74. end;
  75. { CompareStr compares S1 and S2, the result is the based on
  76. substraction of the ascii values of the characters in S1 and S2
  77. case result
  78. S1 < S2 < 0
  79. S1 > S2 > 0
  80. S1 = S2 = 0 }
  81. function CompareStr(const S1, S2: string): Integer;
  82. var count, count1, count2: integer;
  83. begin
  84. result := 0;
  85. Count1 := Length(S1);
  86. Count2 := Length(S2);
  87. if Count1 > Count2 then Count := Count2
  88. else Count := Count1;
  89. result := CompareMem(Pointer(S1),Pointer(S2), Count);
  90. if (result = 0) and (Count1 <> Count2) then begin
  91. if Count1 > Count2 then result := ord(s1[Count1 + 1])
  92. else result := -ord(s2[Count2 + 1]);
  93. end ;
  94. end ;
  95. { CompareMem returns the result of comparison of Length bytes at P1 and P2
  96. case result
  97. P1 < P2 < 0
  98. P1 > P2 > 0
  99. P1 = P2 = 0 }
  100. function CompareMem(P1, P2: Pointer; Length: cardinal): integer;
  101. var i: integer;
  102. begin
  103. i := 0;
  104. result := 0;
  105. while (result = 0) and (i < length) do begin
  106. result := byte(P1^) - byte(P2^);
  107. P1 := P1 + 1;
  108. P2 := P2 + 1;
  109. i := i + 1;
  110. end ;
  111. end ;
  112. { CompareText compares S1 and S2, the result is the based on
  113. substraction of the ascii values of characters in S1 and S2
  114. comparison is case-insensitive
  115. case result
  116. S1 < S2 < 0
  117. S1 > S2 > 0
  118. S1 = S2 = 0 }
  119. function CompareText(const S1, S2: string): integer;
  120. var i, count, count1, count2: integer; Chr1, Chr2: byte;
  121. begin
  122. result := 0;
  123. Count1 := Length(S1);
  124. Count2 := Length(S2);
  125. if Count1 > Count2 then Count := Count2
  126. else Count := Count1;
  127. i := 0;
  128. while (result = 0) and (i < count) do begin
  129. inc (i);
  130. Chr1 := byte(s1[i]);
  131. Chr2 := byte(s2[i]);
  132. if Chr1 in [97..122] then dec(Chr1,32);
  133. if Chr2 in [97..122] then dec(Chr2,32);
  134. result := Chr1 - Chr2;
  135. end ;
  136. if (result = 0) then
  137. result:=(count1-count2);
  138. end ;
  139. {==============================================================================}
  140. { Ansi string functions }
  141. { these functions rely on the character set loaded by the OS }
  142. {==============================================================================}
  143. function AnsiUpperCase(const s: string): string;
  144. var len, i: integer;
  145. begin
  146. len := length(s);
  147. SetLength(result, len);
  148. for i := 1 to len do
  149. result[i] := UpperCaseTable[ord(s[i])];
  150. end ;
  151. function AnsiLowerCase(const s: string): string;
  152. var len, i: integer;
  153. begin
  154. len := length(s);
  155. SetLength(result, len);
  156. for i := 1 to len do
  157. result[i] := LowerCaseTable[ord(s[i])];
  158. end ;
  159. function AnsiCompareStr(const S1, S2: string): integer;
  160. Var I,L1,L2 : Longint;
  161. begin
  162. Result:=0;
  163. L1:=Length(S1);
  164. L2:=Length(S2);
  165. I:=1;
  166. While (Result=0) and ((I<=L1) and (I<=L2)) do
  167. begin
  168. Result:=Ord(S1[I])-Ord(S2[I]); //!! Must be replaced by ansi characters !!
  169. Inc(I);
  170. end;
  171. If Result=0 Then
  172. Result:=L1-L2;
  173. end;
  174. function AnsiCompareText(const S1, S2: string): integer;
  175. Var I,L1,L2 : Longint;
  176. begin
  177. Result:=0;
  178. L1:=Length(S1);
  179. L2:=Length(S2);
  180. I:=1;
  181. While (Result=0) and ((I<=L1) and (I<=L2)) do
  182. begin
  183. Result:=Ord(LowerCaseTable[Ord(S1[I])])-Ord(LowerCaseTable[Ord(S2[I])]); //!! Must be replaced by ansi characters !!
  184. Inc(I);
  185. end;
  186. If Result=0 Then
  187. Result:=L1-L2;
  188. end;
  189. function AnsiStrComp(S1, S2: PChar): integer;
  190. begin
  191. Result:=0;
  192. If S1=Nil then
  193. begin
  194. If S2=Nil Then Exit;
  195. result:=-1;
  196. end;
  197. If S2=Nil then
  198. begin
  199. Result:=1;
  200. exit;
  201. end;
  202. Repeat
  203. Result:=Ord(S1[0])-Ord(S2[0]); //!! Must be replaced by ansi characters !!
  204. Inc(S1);
  205. Inc(S2);
  206. Until (Result<>0) or ((S1[0]=#0) or (S2[0]=#0))
  207. end;
  208. function AnsiStrIComp(S1, S2: PChar): integer;
  209. begin
  210. Result:=0;
  211. If S1=Nil then
  212. begin
  213. If S2=Nil Then Exit;
  214. result:=-1;
  215. end;
  216. If S2=Nil then
  217. begin
  218. Result:=1;
  219. exit;
  220. end;
  221. Repeat
  222. Result:=Ord(LowerCaseTable[Ord(S1[0])])-Ord(LowerCaseTable[Ord(S2[0])]); //!! Must be replaced by ansi characters !!
  223. Inc(S1);
  224. Inc(S2);
  225. Until (Result<>0) or ((S1[0]=#0) or (S2[0]=#0))
  226. end;
  227. function AnsiStrLComp(S1, S2: PChar; MaxLen: cardinal): integer;
  228. Var I : longint;
  229. begin
  230. Result:=0;
  231. If MaxLen=0 then exit;
  232. If S1=Nil then
  233. begin
  234. If S2=Nil Then Exit;
  235. result:=-1;
  236. end;
  237. If S2=Nil then
  238. begin
  239. Result:=1;
  240. exit;
  241. end;
  242. I:=0;
  243. Repeat
  244. Result:=Ord(S1[0])-Ord(S2[0]); //!! Must be replaced by ansi characters !!
  245. Inc(S1);
  246. Inc(S2);
  247. Inc(I);
  248. Until (Result<>0) or ((S1[0]=#0) or (S2[0]=#0)) or (I=MaxLen)
  249. end ;
  250. function AnsiStrLIComp(S1, S2: PChar; MaxLen: cardinal): integer;
  251. Var I : longint;
  252. begin
  253. Result:=0;
  254. If MaxLen=0 then exit;
  255. If S1=Nil then
  256. begin
  257. If S2=Nil Then Exit;
  258. result:=-1;
  259. end;
  260. If S2=Nil then
  261. begin
  262. Result:=1;
  263. exit;
  264. end;
  265. I:=0;
  266. Repeat
  267. Result:=Ord(LowerCaseTable[Ord(S1[0])])-Ord(LowerCaseTable[Ord(S2[0])]); //!! Must be replaced by ansi characters !!
  268. Inc(S1);
  269. Inc(S2);
  270. Inc(I);
  271. Until (Result<>0) or ((S1[0]=#0) or (S2[0]=#0)) or (I=MaxLen)
  272. end ;
  273. function AnsiStrLower(Str: PChar): PChar;
  274. begin
  275. result := Str;
  276. if Str <> Nil then begin
  277. while Str^ <> #0 do begin
  278. Str^ := LowerCaseTable[byte(Str^)];
  279. Str := Str + 1;
  280. end ;
  281. end ;
  282. end ;
  283. function AnsiStrUpper(Str: PChar): PChar;
  284. begin
  285. result := Str;
  286. if Str <> Nil then begin
  287. while Str^ <> #0 do begin
  288. Str^ := UpperCaseTable[byte(Str^)];
  289. Str := Str + 1;
  290. end ;
  291. end ;
  292. end ;
  293. function AnsiLastChar(const S: string): PChar;
  294. begin
  295. //!! No multibyte yet, so we return the last one.
  296. result:=StrEnd(Pchar(S));
  297. Dec(Result);
  298. end ;
  299. function AnsiStrLastChar(Str: PChar): PChar;
  300. begin
  301. //!! No multibyte yet, so we return the last one.
  302. result:=StrEnd(Str);
  303. Dec(Result);
  304. end ;
  305. {==============================================================================}
  306. { End of Ansi functions }
  307. {==============================================================================}
  308. { Trim returns a copy of S with blanks characters on the left and right stripped off }
  309. Const WhiteSpace = [' ',#10,#13,#9];
  310. function Trim(const S: string): string;
  311. var Ofs, Len: integer;
  312. begin
  313. len := Length(S);
  314. while (Len>0) and (S[Len] in WhiteSpace) do
  315. dec(Len);
  316. Ofs := 1;
  317. while (Ofs<=Len) and (S[Ofs] in WhiteSpace) do
  318. Inc(Ofs);
  319. result := Copy(S, Ofs, 1 + Len - Ofs);
  320. end ;
  321. { TrimLeft returns a copy of S with all blank characters on the left stripped off }
  322. function TrimLeft(const S: string): string;
  323. var i,l:integer;
  324. begin
  325. l := length(s);
  326. i := 1;
  327. while (i<=l) and (s[i] in whitespace) do
  328. inc(i);
  329. Result := copy(s, i, l);
  330. end ;
  331. { TrimRight returns a copy of S with all blank characters on the right stripped off }
  332. function TrimRight(const S: string): string;
  333. var l:integer;
  334. begin
  335. l := length(s);
  336. while (l>0) and (s[l] in whitespace) do
  337. dec(l);
  338. result := copy(s,1,l);
  339. end ;
  340. { QuotedStr returns S quoted left and right and every single quote in S
  341. replaced by two quotes }
  342. function QuotedStr(const S: string): string;
  343. begin
  344. result := AnsiQuotedStr(s, '''');
  345. end ;
  346. { AnsiQuotedStr returns S quoted left and right by Quote,
  347. and every single occurance of Quote replaced by two }
  348. function AnsiQuotedStr(const S: string; Quote: char): string;
  349. var i, j, count: integer;
  350. begin
  351. result := '' + Quote;
  352. count := length(s);
  353. i := 0;
  354. j := 0;
  355. while i < count do begin
  356. i := i + 1;
  357. if S[i] = Quote then begin
  358. result := result + copy(S, 1 + j, i - j) + Quote;
  359. j := i;
  360. end ;
  361. end ;
  362. if i <> j then
  363. result := result + copy(S, 1 + j, i - j);
  364. result := result + Quote;
  365. end ;
  366. { AnsiExtractQuotedStr returns a copy of Src with quote characters
  367. deleted to the left and right and double occurances
  368. of Quote replaced by a single Quote }
  369. function AnsiExtractQuotedStr(Const Src: PChar; Quote: Char): string;
  370. var i: integer; P, Q: PChar;
  371. begin
  372. P := Src;
  373. if Src^ = Quote then P := P + 1;
  374. Q := StrEnd(P);
  375. if PChar(Q - 1)^ = Quote then Q := Q - 1;
  376. SetLength(result, Q - P);
  377. i := 0;
  378. while P <> Q do begin
  379. i := i + 1;
  380. result[i] := P^;
  381. if (P^ = Quote) and (PChar(P + 1)^ = Quote) then
  382. P := P + 1;
  383. P := P + 1;
  384. end ;
  385. SetLength(result, i);
  386. end ;
  387. { AdjustLineBreaks returns S with all CR characters not followed by LF
  388. replaced with CR/LF }
  389. // under Linux all CR characters or CR/LF combinations should be replaced with LF
  390. function AdjustLineBreaks(const S: string): string;
  391. var i, j, count: integer;
  392. begin
  393. result := '';
  394. i := 0;
  395. j := 0;
  396. count := Length(S);
  397. while i < count do begin
  398. i := i + 1;
  399. {$ifndef linux}
  400. if (S[i] = #13) and ((i = count) or (S[i + 1] <> #10)) then
  401. begin
  402. result := result + Copy(S, 1 + j, i - j) + #10;
  403. j := i;
  404. end;
  405. {$else}
  406. If S[i]=#13 then
  407. begin
  408. Result:= Result+Copy(S,J+1,i-j-1)+#10;
  409. If I<>Count Then
  410. If S[I+1]=#10 then inc(i);
  411. J :=I;
  412. end;
  413. {$endif}
  414. end ;
  415. if j <> i then
  416. result := result + copy(S, 1 + j, i - j);
  417. end ;
  418. { IsValidIdent returns true if the first character of Ident is in:
  419. 'A' to 'Z', 'a' to 'z' or '_' and the following characters are
  420. on of: 'A' to 'Z', 'a' to 'z', '0'..'9' or '_' }
  421. function IsValidIdent(const Ident: string): boolean;
  422. var i, len: integer;
  423. begin
  424. result := false;
  425. len := length(Ident);
  426. if len <> 0 then begin
  427. result := Ident[1] in ['A'..'Z', 'a'..'z', '_'];
  428. i := 1;
  429. while (result) and (i < len) do begin
  430. i := i + 1;
  431. result := result and (Ident[i] in ['A'..'Z', 'a'..'z', '0'..'9', '_']);
  432. end ;
  433. end ;
  434. end ;
  435. { IntToStr returns a string representing the value of Value }
  436. function IntToStr(Value: integer): string;
  437. begin
  438. System.Str(Value, result);
  439. end ;
  440. { IntToHex returns a string representing the hexadecimal value of Value }
  441. const
  442. HexDigits: array[0..15] of char = '0123456789ABCDEF';
  443. function IntToHex(Value: integer; Digits: integer): string;
  444. var i: integer;
  445. begin
  446. SetLength(result, digits);
  447. for i := 0 to digits - 1 do begin
  448. result[digits - i] := HexDigits[value and 15];
  449. value := value shr 4;
  450. end ;
  451. end ;
  452. { StrToInt converts the string S to an integer value,
  453. if S does not represent a valid integer value EConvertError is raised }
  454. function StrToInt(const S: string): integer;
  455. var Error: word;
  456. begin
  457. Val(S, result, Error);
  458. if Error <> 0 then raise EConvertError.createfmt(SInValidInteger,[S]);
  459. end ;
  460. { StrToIntDef converts the string S to an integer value,
  461. Default is returned in case S does not represent a valid integer value }
  462. function StrToIntDef(const S: string; Default: integer): integer;
  463. var Error: word;
  464. begin
  465. Val(S, result, Error);
  466. if Error <> 0 then result := Default;
  467. end ;
  468. { LoadStr returns the string resource Ident. }
  469. function LoadStr(Ident: integer): string;
  470. begin
  471. result:='';
  472. end ;
  473. { FmtLoadStr returns the string resource Ident and formats it accordingly }
  474. function FmtLoadStr(Ident: integer; const Args: array of const): string;
  475. begin
  476. result:='';
  477. end;
  478. Const
  479. feInvalidFormat = 1;
  480. feMissingArgument = 2;
  481. feInvalidArgIndex = 3;
  482. Procedure Log (Const S: String);
  483. begin
  484. {$ifdef debug}
  485. Writeln (S);
  486. {$endif}
  487. end;
  488. Procedure DoFormatError (ErrCode : Longint);
  489. Var S : String;
  490. begin
  491. //!! must be changed to contain format string...
  492. S:='';
  493. Case ErrCode of
  494. feInvalidFormat : raise EConvertError.Createfmt(SInvalidFormat,[s]);
  495. feMissingArgument : raise EConvertError.Createfmt(SArgumentMissing,[s]);
  496. feInvalidArgIndex : raise EConvertError.Createfmt(SInvalidArgIndex,[s]);
  497. end;
  498. end;
  499. Function Format (Const Fmt : String; const Args : Array of const) : String;
  500. Var ChPos,OldPos,ArgPos,DoArg,Len : Longint;
  501. Hs,ToAdd : String;
  502. Index,Width,Prec : Longint;
  503. Left : Boolean;
  504. Fchar : char;
  505. {
  506. ReadFormat reads the format string. It returns the type character in
  507. uppercase, and sets index, Width, Prec to their correct values,
  508. or -1 if not set. It sets Left to true if left alignment was requested.
  509. In case of an error, DoFormatError is called.
  510. }
  511. Function ReadFormat : Char;
  512. Var Value : longint;
  513. Procedure ReadInteger;
  514. Var Code : Word;
  515. begin
  516. If Value<>-1 then exit; // Was already read.
  517. OldPos:=chPos;
  518. While (Chpos<=Len) and
  519. (Pos(Fmt[chpos],'1234567890')<>0) do inc(chpos);
  520. If Chpos>len then
  521. 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);
  639. Chpos:=1;
  640. OldPos:=1;
  641. ArgPos:=0;
  642. While chpos<=len do
  643. begin
  644. While (ChPos<=Len) and (Fmt[chpos]<>'%') do
  645. 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>15 then
  709. ToAdd:=HexStr(Args[Doarg].VInteger,15)
  710. else
  711. begin
  712. // determine minimum needed number of hex digits.
  713. Index:=1;
  714. While (DWord(1 shl (Index*4))<DWord(Args[DoArg].VInteger)) and (index<8) 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 StrToFloat(Value: string): Extended;
  764. var Error: word;
  765. begin
  766. Val(Value, result, Error);
  767. if Error <> 0 then raise
  768. EConvertError.createfmt(SInValidFLoat,[Value]);
  769. end ;
  770. Function FloatToStr(Value: Extended): String;
  771. Begin
  772. Result := FloatToStrF(Value, ffGeneral, 15, 0);
  773. End;
  774. Function FloatToText(Buffer: PChar; Value: Extended; format: TFloatFormat; Precision, Digits: Integer): Longint;
  775. Var
  776. Tmp: String[40];
  777. Begin
  778. Tmp := FloatToStrF(Value, format, Precision, Digits);
  779. Result := Length(Tmp);
  780. Move(Tmp[1], Buffer[0], Result);
  781. End;
  782. Function FloatToStrF(Value: Extended; format: TFloatFormat; Precision, Digits: Integer): String;
  783. Var
  784. P: Integer;
  785. Negative, TooSmall, TooLarge: Boolean;
  786. Begin
  787. Case format Of
  788. ffGeneral:
  789. Begin
  790. If (Precision = -1) Or (Precision > 15) Then Precision := 15;
  791. TooSmall := (Abs(Value) < 0.00001) and (Value>0.0);
  792. If Not TooSmall Then
  793. Begin
  794. Str(Value:0:999, Result);
  795. P := Pos('.', Result);
  796. Result[P] := DecimalSeparator;
  797. TooLarge := P > Precision + 1;
  798. End;
  799. If TooSmall Or TooLarge Then
  800. begin
  801. Result := FloatToStrF(Value, ffExponent, Precision, Digits);
  802. // Strip unneeded zeroes.
  803. P:=Pos('E',result)-1;
  804. If P<>-1 then
  805. While (P>1) and (Result[P]='0') do
  806. begin
  807. system.Delete(Result,P,1);
  808. Dec(P);
  809. end;
  810. end
  811. else
  812. begin
  813. P := Length(Result);
  814. While Result[P] = '0' Do Dec(P);
  815. If Result[P] = DecimalSeparator Then Dec(P);
  816. SetLength(Result, P);
  817. end;
  818. End;
  819. ffExponent:
  820. Begin
  821. If (Precision = -1) Or (Precision > 15) Then Precision := 15;
  822. Str(Value:Precision + 8, Result);
  823. Result[3] := DecimalSeparator;
  824. P:=4;
  825. While (P>0) and (Digits < P) And (Result[Precision + 5] = '0') do
  826. Begin
  827. If P<>1 then
  828. system.Delete(Result, Precision + 5, 1)
  829. else
  830. system.Delete(Result, Precision + 3, 3);
  831. Dec(P);
  832. end;
  833. If Result[1] = ' ' Then
  834. System.Delete(Result, 1, 1);
  835. End;
  836. ffFixed:
  837. Begin
  838. If Digits = -1 Then Digits := 2
  839. Else If Digits > 15 Then Digits := 15;
  840. Str(Value:0:Digits, Result);
  841. If Result[1] = ' ' Then
  842. System.Delete(Result, 1, 1);
  843. P := Pos('.', Result);
  844. If P <> 0 Then Result[P] := DecimalSeparator;
  845. End;
  846. ffNumber:
  847. Begin
  848. If Digits = -1 Then Digits := 2
  849. Else If Digits > 15 Then Digits := 15;
  850. Str(Value:0:Digits, Result);
  851. If Result[1] = ' ' Then System.Delete(Result, 1, 1);
  852. P := Pos('.', Result);
  853. If P <> 0 Then Result[P] := DecimalSeparator;
  854. Dec(P, 3);
  855. While (P > 1) Do
  856. Begin
  857. If Result[P - 1] <> '-' Then Insert(ThousandSeparator, Result, P);
  858. Dec(P, 3);
  859. End;
  860. End;
  861. ffCurrency:
  862. Begin
  863. If Value < 0 Then
  864. Begin
  865. Negative := True;
  866. Value := -Value;
  867. End
  868. Else Negative := False;
  869. If Digits = -1 Then Digits := CurrencyDecimals
  870. Else If Digits > 18 Then Digits := 18;
  871. Str(Value:0:Digits, Result);
  872. If Result[1] = ' ' Then System.Delete(Result, 1, 1);
  873. P := Pos('.', Result);
  874. If P <> 0 Then Result[P] := DecimalSeparator;
  875. Dec(P, 3);
  876. While (P > 1) Do
  877. Begin
  878. Insert(ThousandSeparator, Result, P);
  879. Dec(P, 3);
  880. End;
  881. If Not Negative Then
  882. Begin
  883. Case CurrencyFormat Of
  884. 0: Result := CurrencyString + Result;
  885. 1: Result := Result + CurrencyString;
  886. 2: Result := CurrencyString + ' ' + Result;
  887. 3: Result := Result + ' ' + CurrencyString;
  888. End
  889. End
  890. Else
  891. Begin
  892. Case NegCurrFormat Of
  893. 0: Result := '(' + CurrencyString + Result + ')';
  894. 1: Result := '-' + CurrencyString + Result;
  895. 2: Result := CurrencyString + '-' + Result;
  896. 3: Result := CurrencyString + Result + '-';
  897. 4: Result := '(' + Result + CurrencyString + ')';
  898. 5: Result := '-' + Result + CurrencyString;
  899. 6: Result := Result + '-' + CurrencyString;
  900. 7: Result := Result + CurrencyString + '-';
  901. 8: Result := '-' + Result + ' ' + CurrencyString;
  902. 9: Result := '-' + CurrencyString + ' ' + Result;
  903. 10: Result := CurrencyString + ' ' + Result + '-';
  904. End;
  905. End;
  906. End;
  907. End;
  908. End;
  909. {==============================================================================}
  910. { extra functions }
  911. {==============================================================================}
  912. { LeftStr returns Count left-most characters from S }
  913. function LeftStr(const S: string; Count: integer): string;
  914. begin
  915. result := Copy(S, 1, Count);
  916. end ;
  917. { RightStr returns Count right-most characters from S }
  918. function RightStr(const S: string; Count: integer): string;
  919. begin
  920. If Count>Length(S) then
  921. Count:=Length(S);
  922. result := Copy(S, 1 + Length(S) - Count, Count);
  923. end;
  924. { BCDToInt converts the BCD value Value to an integer }
  925. function BCDToInt(Value: integer): integer;
  926. var i, j: integer;
  927. begin
  928. result := 0;
  929. j := 1;
  930. for i := 0 to SizeOf(Value) shr 1 - 1 do begin
  931. result := result + j * (Value and 15);
  932. j := j * 10;
  933. Value := Value shr 4;
  934. end ;
  935. end ;
  936. {
  937. Case Translation Tables
  938. Can be used in internationalization support.
  939. Although these tables can be obtained through system calls
  940. it is better to not use those, since most implementation are not 100%
  941. WARNING:
  942. before modifying a translation table make sure that the current codepage
  943. of the OS corresponds to the one you make changes to
  944. }
  945. const
  946. { upper case translation table for character set 850 }
  947. CP850UCT: array[128..255] of char =
  948. ('€', 'š', '�', '¶', 'Ž', '¶', '�', '€', 'Ò', 'Ó', 'Ô', 'Ø', '×', 'Þ', 'Ž', '�',
  949. '�', '’', '’', 'â', '™', 'ã', 'ê', 'ë', 'Y', '™', 'š', '�', 'œ', '�', 'ž', 'Ÿ',
  950. 'µ', 'Ö', 'à', 'é', '¥', '¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯',
  951. '°', '±', '²', '³', '´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿',
  952. 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Ç', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï',
  953. 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', '×', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß',
  954. 'à', 'á', 'â', 'ã', 'å', 'å', 'æ', 'í', 'è', 'é', 'ê', 'ë', 'í', 'í', 'î', 'ï',
  955. 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ');
  956. { lower case translation table for character set 850 }
  957. CP850LCT: array[128..255] of char =
  958. ('‡', '�', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '�', '„', '†',
  959. '‚', '‘', '‘', '“', '”', '•', '–', '—', '˜', '”', '�', '›', 'œ', '›', 'ž', 'Ÿ',
  960. ' ', '¡', '¢', '£', '¤', '¤', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯',
  961. '°', '±', '²', '³', '´', ' ', 'ƒ', '…', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿',
  962. 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Æ', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï',
  963. 'Ð', 'Ñ', 'ˆ', '‰', 'Š', 'Õ', '¡', 'Œ', '‹', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', '�', 'ß',
  964. '¢', 'á', '“', '•', 'ä', 'ä', 'æ', 'í', 'è', '£', '–', '—', 'ì', 'ì', 'î', 'ï',
  965. 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ');
  966. { upper case translation table for character set ISO 8859/1 Latin 1 }
  967. CPISO88591UCT: array[192..255] of char =
  968. ( #192, #193, #194, #195, #196, #197, #198, #199,
  969. #200, #201, #202, #203, #204, #205, #206, #207,
  970. #208, #209, #210, #211, #212, #213, #214, #215,
  971. #216, #217, #218, #219, #220, #221, #222, #223,
  972. #192, #193, #194, #195, #196, #197, #198, #199,
  973. #200, #201, #202, #203, #204, #205, #206, #207,
  974. #208, #209, #210, #211, #212, #213, #214, #247,
  975. #216, #217, #218, #219, #220, #221, #222, #89 );
  976. { lower case translation table for character set ISO 8859/1 Latin 1 }
  977. CPISO88591LCT: array[192..255] of char =
  978. ( #224, #225, #226, #227, #228, #229, #230, #231,
  979. #232, #233, #234, #235, #236, #237, #238, #239,
  980. #240, #241, #242, #243, #244, #245, #246, #215,
  981. #248, #249, #250, #251, #252, #253, #254, #223,
  982. #224, #225, #226, #227, #228, #229, #230, #231,
  983. #232, #233, #234, #235, #236, #237, #238, #239,
  984. #240, #241, #242, #243, #244, #245, #246, #247,
  985. #248, #249, #250, #251, #252, #253, #254, #255 );
  986. {
  987. $Log$
  988. Revision 1.32 2000-04-03 06:40:37 michael
  989. * TRim(right|Left) more Delphi compatible
  990. Revision 1.31 2000/02/09 16:59:33 peter
  991. * truncated log
  992. Revision 1.30 2000/02/01 12:53:23 peter
  993. * fixed rangecheck error in format()
  994. Revision 1.29 1999/11/06 14:41:31 peter
  995. * truncated log
  996. Revision 1.28 1999/10/12 19:16:27 florian
  997. * bug 645 fixed: format('%x',...) should writes unsigned hexadecimals, also
  998. prec fixed: max. value in delphi is 15 (and not 32)
  999. Revision 1.27 1999/10/03 19:42:40 peter
  1000. * fixed comparetext
  1001. Revision 1.26 1999/09/04 20:48:34 florian
  1002. * format('%g',[0.0]) returned long format string, fixed
  1003. Revision 1.25 1999/08/25 13:13:58 michael
  1004. fixed Formaterror, added missing raise
  1005. Revision 1.24 1999/08/16 22:38:53 peter
  1006. * fixed newstr/disposestr
  1007. }