sysstr.inc 34 KB

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