sysstr.inc 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471
  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
  88. Count:=Count2
  89. else
  90. Count:=Count1;
  91. result := CompareMemRange(Pointer(S1),Pointer(S2), Count);
  92. if result=0 then
  93. result:=Count1-Count2;
  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
  102. i: cardinal;
  103. begin
  104. i := 0;
  105. result := 0;
  106. while (result=0) and (I<length) do
  107. begin
  108. result:=byte(P1^)-byte(P2^);
  109. P1:=pchar(P1)+1; // VP compat.
  110. P2:=pchar(P2)+1;
  111. i := i + 1;
  112. end ;
  113. end ;
  114. function CompareMem(P1, P2: Pointer; Length: cardinal): Boolean;
  115. var
  116. i: cardinal;
  117. begin
  118. Result:=True;
  119. I:=0;
  120. If (P1)<>(P2) then
  121. While Result and (i<Length) do
  122. begin
  123. Result:=PByte(P1)^=PByte(P2)^;
  124. Inc(I);
  125. Inc(pchar(P1));
  126. Inc(pchar(P2));
  127. end;
  128. end;
  129. { CompareText compares S1 and S2, the result is the based on
  130. substraction of the ascii values of characters in S1 and S2
  131. comparison is case-insensitive
  132. case result
  133. S1 < S2 < 0
  134. S1 > S2 > 0
  135. S1 = S2 = 0 }
  136. function CompareText(const S1, S2: string): integer;
  137. var
  138. i, count, count1, count2: integer; Chr1, Chr2: byte;
  139. begin
  140. result := 0;
  141. Count1 := Length(S1);
  142. Count2 := Length(S2);
  143. if (Count1>Count2) then
  144. Count := Count2
  145. else
  146. Count := Count1;
  147. i := 0;
  148. while (result=0) and (i<count) do
  149. begin
  150. inc (i);
  151. Chr1 := byte(s1[i]);
  152. Chr2 := byte(s2[i]);
  153. if Chr1 in [97..122] then
  154. dec(Chr1,32);
  155. if Chr2 in [97..122] then
  156. dec(Chr2,32);
  157. result := Chr1 - Chr2;
  158. end ;
  159. if (result = 0) then
  160. result:=(count1-count2);
  161. end;
  162. function SameText(const s1,s2:String):Boolean;
  163. begin
  164. Result:=CompareText(S1,S2)=0;
  165. end;
  166. {==============================================================================}
  167. { Ansi string functions }
  168. { these functions rely on the character set loaded by the OS }
  169. {==============================================================================}
  170. function AnsiUpperCase(const s: string): string;
  171. var len, i: integer;
  172. begin
  173. len := length(s);
  174. SetLength(result, len);
  175. for i := 1 to len do
  176. result[i] := UpperCaseTable[ord(s[i])];
  177. end ;
  178. function AnsiLowerCase(const s: string): string;
  179. var len, i: integer;
  180. begin
  181. len := length(s);
  182. SetLength(result, len);
  183. for i := 1 to len do
  184. result[i] := LowerCaseTable[ord(s[i])];
  185. end ;
  186. function AnsiCompareStr(const S1, S2: string): integer;
  187. Var I,L1,L2 : Longint;
  188. begin
  189. Result:=0;
  190. L1:=Length(S1);
  191. L2:=Length(S2);
  192. I:=1;
  193. While (Result=0) and ((I<=L1) and (I<=L2)) do
  194. begin
  195. Result:=Ord(S1[I])-Ord(S2[I]); //!! Must be replaced by ansi characters !!
  196. Inc(I);
  197. end;
  198. If Result=0 Then
  199. Result:=L1-L2;
  200. end;
  201. function AnsiCompareText(const S1, S2: string): integer;
  202. Var I,L1,L2 : Longint;
  203. begin
  204. Result:=0;
  205. L1:=Length(S1);
  206. L2:=Length(S2);
  207. I:=1;
  208. While (Result=0) and ((I<=L1) and (I<=L2)) do
  209. begin
  210. Result:=Ord(LowerCaseTable[Ord(S1[I])])-Ord(LowerCaseTable[Ord(S2[I])]); //!! Must be replaced by ansi characters !!
  211. Inc(I);
  212. end;
  213. If Result=0 Then
  214. Result:=L1-L2;
  215. end;
  216. function AnsiSameText(const s1,s2:String):Boolean;
  217. begin
  218. AnsiSameText:=AnsiCompareText(S1,S2)=0;
  219. end;
  220. function AnsiSameStr(const s1,s2:String):Boolean;
  221. begin
  222. AnsiSameStr:=AnsiCompareStr(S1,S2)=0;
  223. end;
  224. function AnsiStrComp(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. exit;
  232. end;
  233. If S2=Nil then
  234. begin
  235. Result:=1;
  236. exit;
  237. end;
  238. Repeat
  239. Result:=Ord(S1[0])-Ord(S2[0]); //!! Must be replaced by ansi characters !!
  240. Inc(S1);
  241. Inc(S2);
  242. Until (Result<>0) or ((S1[0]=#0) or (S2[0]=#0))
  243. end;
  244. function AnsiStrIComp(S1, S2: PChar): integer;
  245. begin
  246. Result:=0;
  247. If S1=Nil then
  248. begin
  249. If S2=Nil Then Exit;
  250. result:=-1;
  251. exit;
  252. end;
  253. If S2=Nil then
  254. begin
  255. Result:=1;
  256. exit;
  257. end;
  258. Repeat
  259. Result:=Ord(LowerCaseTable[Ord(S1[0])])-Ord(LowerCaseTable[Ord(S2[0])]); //!! Must be replaced by ansi characters !!
  260. Inc(S1);
  261. Inc(S2);
  262. Until (Result<>0) or ((S1[0]=#0) or (S2[0]=#0))
  263. end;
  264. function AnsiStrLComp(S1, S2: PChar; MaxLen: cardinal): integer;
  265. Var I : cardinal;
  266. begin
  267. Result:=0;
  268. If MaxLen=0 then exit;
  269. If S1=Nil then
  270. begin
  271. If S2=Nil Then Exit;
  272. result:=-1;
  273. exit;
  274. end;
  275. If S2=Nil then
  276. begin
  277. Result:=1;
  278. exit;
  279. end;
  280. I:=0;
  281. Repeat
  282. Result:=Ord(S1[0])-Ord(S2[0]); //!! Must be replaced by ansi characters !!
  283. Inc(S1);
  284. Inc(S2);
  285. Inc(I);
  286. Until (Result<>0) or ((S1[0]=#0) or (S2[0]=#0)) or (I=MaxLen)
  287. end ;
  288. function AnsiStrLIComp(S1, S2: PChar; MaxLen: cardinal): integer;
  289. Var I : cardinal;
  290. begin
  291. Result:=0;
  292. If MaxLen=0 then exit;
  293. If S1=Nil then
  294. begin
  295. If S2=Nil Then Exit;
  296. result:=-1;
  297. exit;
  298. end;
  299. If S2=Nil then
  300. begin
  301. Result:=1;
  302. exit;
  303. end;
  304. I:=0;
  305. Repeat
  306. Result:=Ord(LowerCaseTable[Ord(S1[0])])-Ord(LowerCaseTable[Ord(S2[0])]); //!! Must be replaced by ansi characters !!
  307. Inc(S1);
  308. Inc(S2);
  309. Inc(I);
  310. Until (Result<>0) or ((S1[0]=#0) or (S2[0]=#0)) or (I=MaxLen)
  311. end ;
  312. function AnsiStrLower(Str: PChar): PChar;
  313. begin
  314. result := Str;
  315. if Str <> Nil then begin
  316. while Str^ <> #0 do begin
  317. Str^ := LowerCaseTable[byte(Str^)];
  318. Str := Str + 1;
  319. end ;
  320. end ;
  321. end ;
  322. function AnsiStrUpper(Str: PChar): PChar;
  323. begin
  324. result := Str;
  325. if Str <> Nil then begin
  326. while Str^ <> #0 do begin
  327. Str^ := UpperCaseTable[byte(Str^)];
  328. Str := Str + 1;
  329. end ;
  330. end ;
  331. end ;
  332. function AnsiLastChar(const S: string): PChar;
  333. begin
  334. //!! No multibyte yet, so we return the last one.
  335. result:=StrEnd(Pchar(S));
  336. Dec(Result);
  337. end ;
  338. function AnsiStrLastChar(Str: PChar): PChar;
  339. begin
  340. //!! No multibyte yet, so we return the last one.
  341. result:=StrEnd(Str);
  342. Dec(Result);
  343. end ;
  344. {==============================================================================}
  345. { End of Ansi functions }
  346. {==============================================================================}
  347. { Trim returns a copy of S with blanks characters on the left and right stripped off }
  348. Const WhiteSpace = [' ',#10,#13,#9];
  349. function Trim(const S: string): string;
  350. var Ofs, Len: integer;
  351. begin
  352. len := Length(S);
  353. while (Len>0) and (S[Len] in WhiteSpace) do
  354. dec(Len);
  355. Ofs := 1;
  356. while (Ofs<=Len) and (S[Ofs] in WhiteSpace) do
  357. Inc(Ofs);
  358. result := Copy(S, Ofs, 1 + Len - Ofs);
  359. end ;
  360. { TrimLeft returns a copy of S with all blank characters on the left stripped off }
  361. function TrimLeft(const S: string): string;
  362. var i,l:integer;
  363. begin
  364. l := length(s);
  365. i := 1;
  366. while (i<=l) and (s[i] in whitespace) do
  367. inc(i);
  368. Result := copy(s, i, l);
  369. end ;
  370. { TrimRight returns a copy of S with all blank characters on the right stripped off }
  371. function TrimRight(const S: string): string;
  372. var l:integer;
  373. begin
  374. l := length(s);
  375. while (l>0) and (s[l] in whitespace) do
  376. dec(l);
  377. result := copy(s,1,l);
  378. end ;
  379. { QuotedStr returns S quoted left and right and every single quote in S
  380. replaced by two quotes }
  381. function QuotedStr(const S: string): string;
  382. begin
  383. result := AnsiQuotedStr(s, '''');
  384. end ;
  385. { AnsiQuotedStr returns S quoted left and right by Quote,
  386. and every single occurance of Quote replaced by two }
  387. function AnsiQuotedStr(const S: string; Quote: char): string;
  388. var i, j, count: integer;
  389. begin
  390. result := '' + Quote;
  391. count := length(s);
  392. i := 0;
  393. j := 0;
  394. while i < count do begin
  395. i := i + 1;
  396. if S[i] = Quote then begin
  397. result := result + copy(S, 1 + j, i - j) + Quote;
  398. j := i;
  399. end ;
  400. end ;
  401. if i <> j then
  402. result := result + copy(S, 1 + j, i - j);
  403. result := result + Quote;
  404. end ;
  405. { AnsiExtractQuotedStr returns a copy of Src with quote characters
  406. deleted to the left and right and double occurances
  407. of Quote replaced by a single Quote }
  408. function AnsiExtractQuotedStr(var Src: PChar; Quote: Char): string;
  409. var i: integer; P, Q,R: PChar;
  410. begin
  411. P := Src;
  412. Q := StrEnd(P);
  413. result:='';
  414. if P=Q then exit;
  415. if P^<>quote then exit;
  416. inc(p);
  417. setlength(result,(Q-P)+1);
  418. R:=@Result[1];
  419. i := 0;
  420. while P <> Q do
  421. begin
  422. R^:=P^;
  423. inc(R);
  424. if (P^ = Quote) then
  425. begin
  426. P := P + 1;
  427. if (p^ <> Quote) then
  428. begin
  429. dec(R);
  430. break;
  431. end;
  432. end;
  433. P := P + 1;
  434. end ;
  435. src:=p;
  436. SetLength(result, (R-pchar(@Result[1])));
  437. end ;
  438. { AdjustLineBreaks returns S with all CR characters not followed by LF
  439. replaced with CR/LF }
  440. // under Linux all CR characters or CR/LF combinations should be replaced with LF
  441. function AdjustLineBreaks(const S: string): string;
  442. begin
  443. Result:=AdjustLineBreaks(S,DefaultTextLineBreakStyle);
  444. end;
  445. function AdjustLineBreaks(const S: string; Style: TTextLineBreakStyle): string;
  446. var
  447. Source,Dest: PChar;
  448. DestLen: Integer;
  449. I,J,L: Longint;
  450. begin
  451. Source:=Pointer(S);
  452. L:=Length(S);
  453. DestLen:=L;
  454. I:=1;
  455. while (I<=L) do
  456. begin
  457. case S[i] of
  458. #10: if (Style=tlbsCRLF) then
  459. Inc(DestLen);
  460. #13: if (Style=tlbsCRLF) then
  461. if (I<L) and (S[i+1]=#10) then
  462. Inc(I)
  463. else
  464. Inc(DestLen)
  465. else if (I<L) and (S[I+1]=#10) then
  466. Dec(DestLen);
  467. end;
  468. Inc(I);
  469. end;
  470. if (DestLen=L) then
  471. Result:=S
  472. else
  473. begin
  474. SetLength(Result, DestLen);
  475. FillChar(Result[1],DestLen,0);
  476. Dest := Pointer(Result);
  477. J:=0;
  478. I:=0;
  479. While I<L do
  480. case Source[I] of
  481. #10: begin
  482. if Style=tlbsCRLF then
  483. begin
  484. Dest[j]:=#13;
  485. Inc(J);
  486. end;
  487. Dest[J] := #10;
  488. Inc(J);
  489. Inc(I);
  490. end;
  491. #13: begin
  492. if Style=tlbsCRLF then
  493. begin
  494. Dest[j] := #13;
  495. Inc(J);
  496. end;
  497. Dest[j]:=#10;
  498. Inc(J);
  499. Inc(I);
  500. if Source[I]=#10 then
  501. Inc(I);
  502. end;
  503. else
  504. Dest[j]:=Source[i];
  505. Inc(J);
  506. Inc(I);
  507. end;
  508. end;
  509. end;
  510. { IsValidIdent returns true if the first character of Ident is in:
  511. 'A' to 'Z', 'a' to 'z' or '_' and the following characters are
  512. on of: 'A' to 'Z', 'a' to 'z', '0'..'9' or '_' }
  513. function IsValidIdent(const Ident: string): boolean;
  514. var i, len: integer;
  515. begin
  516. result := false;
  517. len := length(Ident);
  518. if len <> 0 then begin
  519. result := Ident[1] in ['A'..'Z', 'a'..'z', '_'];
  520. i := 1;
  521. while (result) and (i < len) do begin
  522. i := i + 1;
  523. result := result and (Ident[i] in ['A'..'Z', 'a'..'z', '0'..'9', '_']);
  524. end ;
  525. end ;
  526. end ;
  527. { IntToStr returns a string representing the value of Value }
  528. function IntToStr(Value: integer): string;
  529. begin
  530. System.Str(Value, result);
  531. end ;
  532. {$IFNDEF VIRTUALPASCAL}
  533. function IntToStr(Value: int64): string;
  534. begin
  535. System.Str(Value, result);
  536. end ;
  537. {$ENDIF}
  538. function IntToStr(Value: QWord): string;
  539. begin
  540. System.Str(Value, result);
  541. end ;
  542. { IntToHex returns a string representing the hexadecimal value of Value }
  543. const
  544. HexDigits: array[0..15] of char = '0123456789ABCDEF';
  545. function IntToHex(Value: integer; Digits: integer): string;
  546. var i: integer;
  547. begin
  548. SetLength(result, digits);
  549. for i := 0 to digits - 1 do
  550. begin
  551. result[digits - i] := HexDigits[value and 15];
  552. value := value shr 4;
  553. end ;
  554. while value <> 0 do begin
  555. result := HexDigits[value and 15] + result;
  556. value := value shr 4;
  557. end;
  558. end ;
  559. {$IFNDEF VIRTUALPASCAL} // overloading
  560. function IntToHex(Value: int64; Digits: integer): string;
  561. var i: integer;
  562. begin
  563. SetLength(result, digits);
  564. for i := 0 to digits - 1 do
  565. begin
  566. result[digits - i] := HexDigits[value and 15];
  567. value := value shr 4;
  568. end ;
  569. while value <> 0 do begin
  570. result := HexDigits[value and 15] + result;
  571. value := value shr 4;
  572. end;
  573. end ;
  574. {$ENDIF}
  575. function TryStrToInt(const s: string; var i : integer) : boolean;
  576. var Error : word;
  577. begin
  578. Val(s, i, Error);
  579. TryStrToInt:=Error=0
  580. end;
  581. { StrToInt converts the string S to an integer value,
  582. if S does not represent a valid integer value EConvertError is raised }
  583. function StrToInt(const S: string): integer;
  584. {$IFDEF VIRTUALPASCAL}
  585. var Error: longint;
  586. {$ELSE}
  587. var Error: word;
  588. {$ENDIF}
  589. begin
  590. Val(S, result, Error);
  591. if Error <> 0 then raise EConvertError.createfmt(SInValidInteger,[S]);
  592. end ;
  593. function StrToInt64(const S: string): int64;
  594. {$IFDEF VIRTUALPASCAL}
  595. var Error: longint;
  596. {$ELSE}
  597. var Error: word;
  598. {$ENDIF}
  599. begin
  600. Val(S, result, Error);
  601. if Error <> 0 then raise EConvertError.createfmt(SInValidInteger,[S]);
  602. end;
  603. function TryStrToInt64(const s: string; var i : int64) : boolean;
  604. var Error : word;
  605. begin
  606. Val(s, i, Error);
  607. TryStrToInt64:=Error=0
  608. end;
  609. { StrToIntDef converts the string S to an integer value,
  610. Default is returned in case S does not represent a valid integer value }
  611. function StrToIntDef(const S: string; Default: integer): integer;
  612. {$IFDEF VIRTUALPASCAL}
  613. var Error: longint;
  614. {$ELSE}
  615. var Error: word;
  616. {$ENDIF}
  617. begin
  618. Val(S, result, Error);
  619. if Error <> 0 then result := Default;
  620. end ;
  621. { StrToIntDef converts the string S to an integer value,
  622. Default is returned in case S does not represent a valid integer value }
  623. function StrToInt64Def(const S: string; Default: int64): int64;
  624. {$IFDEF VIRTUALPASCAL}
  625. var Error: longint;
  626. {$ELSE}
  627. var Error: word;
  628. {$ENDIF}
  629. begin
  630. Val(S, result, Error);
  631. if Error <> 0 then result := Default;
  632. end ;
  633. { LoadStr returns the string resource Ident. }
  634. function LoadStr(Ident: integer): string;
  635. begin
  636. result:='';
  637. end ;
  638. { FmtLoadStr returns the string resource Ident and formats it accordingly }
  639. function FmtLoadStr(Ident: integer; const Args: array of const): string;
  640. begin
  641. result:='';
  642. end;
  643. Const
  644. feInvalidFormat = 1;
  645. feMissingArgument = 2;
  646. feInvalidArgIndex = 3;
  647. {$ifdef fmtdebug}
  648. Procedure Log (Const S: String);
  649. begin
  650. Writeln (S);
  651. end;
  652. {$endif}
  653. Procedure DoFormatError (ErrCode : Longint);
  654. Var
  655. S : String;
  656. begin
  657. //!! must be changed to contain format string...
  658. S:='';
  659. Case ErrCode of
  660. feInvalidFormat : raise EConvertError.Createfmt(SInvalidFormat,[s]);
  661. feMissingArgument : raise EConvertError.Createfmt(SArgumentMissing,[s]);
  662. feInvalidArgIndex : raise EConvertError.Createfmt(SInvalidArgIndex,[s]);
  663. end;
  664. end;
  665. Function Format (Const Fmt : String; const Args : Array of const) : String;
  666. Var ChPos,OldPos,ArgPos,DoArg,Len : Longint;
  667. Hs,ToAdd : String;
  668. Index,Width,Prec : Longint;
  669. Left : Boolean;
  670. Fchar : char;
  671. {$ifdef ver1_0}
  672. vl : int64;
  673. {$else}
  674. vq : qword;
  675. {$endif}
  676. {
  677. ReadFormat reads the format string. It returns the type character in
  678. uppercase, and sets index, Width, Prec to their correct values,
  679. or -1 if not set. It sets Left to true if left alignment was requested.
  680. In case of an error, DoFormatError is called.
  681. }
  682. Function ReadFormat : Char;
  683. Var Value : longint;
  684. Procedure ReadInteger;
  685. {$IFDEF VIRTUALPASCAL}
  686. var Code: longint;
  687. {$ELSE}
  688. var Code: word;
  689. {$ENDIF}
  690. begin
  691. If Value<>-1 then exit; // Was already read.
  692. OldPos:=chPos;
  693. While (Chpos<=Len) and
  694. (Pos(Fmt[chpos],'1234567890')<>0) do inc(chpos);
  695. If Chpos>len then
  696. DoFormatError(feInvalidFormat);
  697. If Fmt[Chpos]='*' then
  698. begin
  699. If (Chpos>OldPos) or (ArgPos>High(Args))
  700. or (Args[ArgPos].Vtype<>vtInteger) then
  701. DoFormatError(feInvalidFormat);
  702. Value:=Args[ArgPos].VInteger;
  703. Inc(ArgPos);
  704. Inc(chPos);
  705. end
  706. else
  707. begin
  708. If (OldPos<chPos) Then
  709. begin
  710. Val (Copy(Fmt,OldPos,ChPos-OldPos),value,code);
  711. // This should never happen !!
  712. If Code>0 then DoFormatError (feInvalidFormat);
  713. end
  714. else
  715. Value:=-1;
  716. end;
  717. end;
  718. Procedure ReadIndex;
  719. begin
  720. ReadInteger;
  721. If Fmt[ChPos]=':' then
  722. begin
  723. If Value=-1 then DoFormatError(feMissingArgument);
  724. Index:=Value;
  725. Value:=-1;
  726. Inc(Chpos);
  727. end;
  728. {$ifdef fmtdebug}
  729. Log ('Read index');
  730. {$endif}
  731. end;
  732. Procedure ReadLeft;
  733. begin
  734. If Fmt[chpos]='-' then
  735. begin
  736. left:=True;
  737. Inc(chpos);
  738. end
  739. else
  740. Left:=False;
  741. {$ifdef fmtdebug}
  742. Log ('Read Left');
  743. {$endif}
  744. end;
  745. Procedure ReadWidth;
  746. begin
  747. ReadInteger;
  748. If Value<>-1 then
  749. begin
  750. Width:=Value;
  751. Value:=-1;
  752. end;
  753. {$ifdef fmtdebug}
  754. Log ('Read width');
  755. {$endif}
  756. end;
  757. Procedure ReadPrec;
  758. begin
  759. If Fmt[chpos]='.' then
  760. begin
  761. inc(chpos);
  762. ReadInteger;
  763. If Value=-1 then
  764. Value:=0;
  765. prec:=Value;
  766. end;
  767. {$ifdef fmtdebug}
  768. Log ('Read precision');
  769. {$endif}
  770. end;
  771. begin
  772. {$ifdef fmtdebug}
  773. Log ('Start format');
  774. {$endif}
  775. Index:=-1;
  776. Width:=-1;
  777. Prec:=-1;
  778. Value:=-1;
  779. inc(chpos);
  780. If Fmt[Chpos]='%' then
  781. begin
  782. Result:='%';
  783. exit; // VP fix
  784. end;
  785. ReadIndex;
  786. ReadLeft;
  787. ReadWidth;
  788. ReadPrec;
  789. ReadFormat:=Upcase(Fmt[ChPos]);
  790. {$ifdef fmtdebug}
  791. Log ('End format');
  792. {$endif}
  793. end;
  794. {$ifdef fmtdebug}
  795. Procedure DumpFormat (C : char);
  796. begin
  797. Write ('Fmt : ',fmt:10);
  798. Write (' Index : ',Index:3);
  799. Write (' Left : ',left:5);
  800. Write (' Width : ',Width:3);
  801. Write (' Prec : ',prec:3);
  802. Writeln (' Type : ',C);
  803. end;
  804. {$endif}
  805. function Checkarg (AT : Longint;err:boolean):boolean;
  806. {
  807. Check if argument INDEX is of correct type (AT)
  808. If Index=-1, ArgPos is used, and argpos is augmented with 1
  809. DoArg is set to the argument that must be used.
  810. }
  811. begin
  812. result:=false;
  813. if Index=-1 then
  814. DoArg:=Argpos
  815. else
  816. DoArg:=Index;
  817. ArgPos:=DoArg+1;
  818. If (Doarg>High(Args)) or (Args[Doarg].Vtype<>AT) then
  819. begin
  820. if err then
  821. DoFormatError(feInvalidArgindex);
  822. dec(ArgPos);
  823. exit;
  824. end;
  825. result:=true;
  826. end;
  827. Const Zero = '000000000000000000000000000000000000000000000000000000000000000';
  828. begin
  829. Result:='';
  830. Len:=Length(Fmt);
  831. Chpos:=1;
  832. OldPos:=1;
  833. ArgPos:=0;
  834. While chpos<=len do
  835. begin
  836. While (ChPos<=Len) and (Fmt[chpos]<>'%') do
  837. inc(chpos);
  838. If ChPos>OldPos Then
  839. Result:=Result+Copy(Fmt,OldPos,Chpos-Oldpos);
  840. If ChPos<Len then
  841. begin
  842. FChar:=ReadFormat;
  843. {$ifdef fmtdebug}
  844. DumpFormat(FCHar);
  845. {$endif}
  846. Case FChar of
  847. 'D' : begin
  848. if Checkarg(vtinteger,false) then
  849. Str(Args[Doarg].VInteger,ToAdd)
  850. {$IFNDEF VIRTUALPASCAL}
  851. else if CheckArg(vtInt64,true) then
  852. Str(Args[DoArg].VInt64^,toadd)
  853. {$ENDIF}
  854. ;
  855. Width:=Abs(width);
  856. Index:=Prec-Length(ToAdd);
  857. If ToAdd[1]<>'-' then
  858. ToAdd:=StringOfChar('0',Index)+ToAdd
  859. else
  860. // + 1 to accomodate for - sign in length !!
  861. Insert(StringOfChar('0',Index+1),toadd,2);
  862. end;
  863. 'U' : begin
  864. if Checkarg(vtinteger,false) then
  865. Str(cardinal(Args[Doarg].VInteger),ToAdd)
  866. {$IFNDEF VIRTUALPASCAL}
  867. else if CheckArg(vtInt64,true) then
  868. Str(qword(Args[DoArg].VInt64^),toadd)
  869. {$ENDIF}
  870. ;
  871. Width:=Abs(width);
  872. Index:=Prec-Length(ToAdd);
  873. ToAdd:=StringOfChar('0',Index)+ToAdd
  874. end;
  875. 'E' : begin
  876. CheckArg(vtExtended,true);
  877. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffexponent,Prec,3);
  878. end;
  879. 'F' : begin
  880. CheckArg(vtExtended,true);
  881. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffFixed,9999,Prec);
  882. end;
  883. 'G' : begin
  884. CheckArg(vtExtended,true);
  885. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffGeneral,Prec,3);
  886. end;
  887. 'N' : begin
  888. CheckArg(vtExtended,true);
  889. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffNumber,9999,Prec);
  890. end;
  891. 'M' : begin
  892. CheckArg(vtExtended,true);
  893. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffCurrency,9999,Prec);
  894. end;
  895. 'S' : begin
  896. if CheckArg(vtString,false) then
  897. hs:=Args[doarg].VString^
  898. else
  899. if CheckArg(vtChar,false) then
  900. hs:=Args[doarg].VChar
  901. else
  902. if CheckArg(vtPChar,false) then
  903. hs:=Args[doarg].VPChar
  904. else
  905. {$ifndef VER1_0}
  906. if CheckArg(vtPWideChar,false) then
  907. hs:=WideString(Args[doarg].VPWideChar)
  908. else
  909. if CheckArg(vtWideChar,false) then
  910. hs:=WideString(Args[doarg].VWideChar)
  911. else
  912. if CheckArg(vtWidestring,false) then
  913. hs:=WideString(Args[doarg].VWideString)
  914. else
  915. {$endif VER1_0}
  916. if CheckArg(vtAnsiString,true) then
  917. hs:=ansistring(Args[doarg].VAnsiString);
  918. Index:=Length(hs);
  919. If (Prec<>-1) and (Index>Prec) then
  920. Index:=Prec;
  921. ToAdd:=Copy(hs,1,Index);
  922. end;
  923. 'P' : Begin
  924. CheckArg(vtpointer,true);
  925. ToAdd:=HexStr(ptrint(Args[DoArg].VPointer),sizeof(Ptrint)*2);
  926. // Insert ':'. Is this needed in 32 bit ? No it isn't.
  927. // Insert(':',ToAdd,5);
  928. end;
  929. 'X' : begin
  930. {$ifdef ver1_0}
  931. if Checkarg(vtinteger,false) then
  932. begin
  933. vl:=Args[Doarg].VInteger and int64($ffffffff);
  934. index:=16;
  935. end
  936. else
  937. begin
  938. CheckArg(vtInt64,true);
  939. vl:=Args[DoArg].VInt64^;
  940. index:=31;
  941. end;
  942. If Prec>index then
  943. ToAdd:=HexStr(vl,index)
  944. else
  945. begin
  946. // determine minimum needed number of hex digits.
  947. Index:=1;
  948. While (DWord(1 shl (Index*4))<=DWord(Args[DoArg].VInteger)) and (index<8) do
  949. inc(Index);
  950. If Index>Prec then
  951. Prec:=Index;
  952. ToAdd:=HexStr(int64(vl),Prec);
  953. end;
  954. {$else}
  955. if Checkarg(vtinteger,false) then
  956. begin
  957. vq:=Cardinal(Args[Doarg].VInteger);
  958. index:=16;
  959. end
  960. else
  961. begin
  962. CheckArg(vtInt64,true);
  963. vq:=Qword(Args[DoArg].VInt64^);
  964. index:=31;
  965. end;
  966. If Prec>index then
  967. ToAdd:=HexStr(vq,index)
  968. else
  969. begin
  970. // determine minimum needed number of hex digits.
  971. Index:=1;
  972. While (qWord(1) shl (Index*4)<=vq) and (index<16) do
  973. inc(Index);
  974. If Index>Prec then
  975. Prec:=Index;
  976. ToAdd:=HexStr(vq,Prec);
  977. end;
  978. {$endif}
  979. end;
  980. '%': ToAdd:='%';
  981. end;
  982. If Width<>-1 then
  983. If Length(ToAdd)<Width then
  984. If not Left then
  985. ToAdd:=Space(Width-Length(ToAdd))+ToAdd
  986. else
  987. ToAdd:=ToAdd+space(Width-Length(ToAdd));
  988. Result:=Result+ToAdd;
  989. end;
  990. inc(chpos);
  991. Oldpos:=chpos;
  992. end;
  993. end;
  994. Function FormatBuf (Var Buffer; BufLen : Cardinal;
  995. Const Fmt; fmtLen : Cardinal;
  996. Const Args : Array of const) : Cardinal;
  997. Var S,F : String;
  998. begin
  999. Setlength(F,fmtlen);
  1000. if fmtlen > 0 then
  1001. Move(fmt,F[1],fmtlen);
  1002. S:=Format (F,Args);
  1003. If Cardinal(Length(S))<Buflen then
  1004. Result:=Length(S)
  1005. else
  1006. Result:=Buflen;
  1007. Move(S[1],Buffer,Result);
  1008. end;
  1009. Procedure FmtStr(Var Res: String; Const Fmt : String; Const args: Array of const);
  1010. begin
  1011. Res:=Format(fmt,Args);
  1012. end;
  1013. Function StrFmt(Buffer,Fmt : PChar; Const args: Array of const) : Pchar;
  1014. begin
  1015. Buffer[FormatBuf(Buffer^,Maxint,Fmt^,strlen(fmt),args)]:=#0;
  1016. Result:=Buffer;
  1017. end;
  1018. Function StrLFmt(Buffer : PCHar; Maxlen : Cardinal;Fmt : PChar; Const args: Array of const) : Pchar;
  1019. begin
  1020. Buffer[FormatBuf(Buffer^,MaxLen,Fmt^,strlen(fmt),args)]:=#0;
  1021. Result:=Buffer;
  1022. end;
  1023. Function StrToFloat(Const S: String): Extended;
  1024. Begin
  1025. If Not TextToFloat(Pchar(S),Result) then
  1026. Raise EConvertError.createfmt(SInValidFLoat,[S]);
  1027. End;
  1028. function StrToFloatDef(const S: string; const Default: Extended): Extended;
  1029. begin
  1030. if not TextToFloat(PChar(S),Result,fvExtended) then
  1031. Result:=Default;
  1032. end;
  1033. Function TextToFloat(Buffer: PChar; Var Value: Extended): Boolean;
  1034. Var
  1035. E,P : Integer;
  1036. S : String;
  1037. Begin
  1038. S:=StrPas(Buffer);
  1039. P:=Pos(DecimalSeparator,S);
  1040. If (P<>0) Then
  1041. S[P] := '.';
  1042. Val(S,Value,E);
  1043. Result:=(E=0);
  1044. End;
  1045. Function TextToFloat(Buffer: PChar; Var Value; ValueType: TFloatValue): Boolean;
  1046. Var
  1047. E,P : Integer;
  1048. S : String;
  1049. C : Currency;
  1050. Ext : Extended;
  1051. Begin
  1052. S:=StrPas(Buffer);
  1053. P:=Pos(ThousandSeparator,S);
  1054. While (P<>0) do
  1055. begin
  1056. Delete(S,P,1);
  1057. P:=Pos(ThousandSeparator,S);
  1058. end;
  1059. P:=Pos(DecimalSeparator,S);
  1060. If (P<>0) Then
  1061. S[P] := '.';
  1062. case ValueType of
  1063. fvCurrency:
  1064. Val(S,Currency(Value),E);
  1065. fvExtended:
  1066. Val(S,Extended(Value),E);
  1067. fvDouble:
  1068. Val(S,Double(Value),E);
  1069. fvSingle:
  1070. Val(S,Single(Value),E);
  1071. fvComp:
  1072. Val(S,Comp(Value),E);
  1073. fvReal:
  1074. Val(S,Real(Value),E);
  1075. end;
  1076. Result:=(E=0);
  1077. End;
  1078. Function TryStrToFloat(Const S : String; Var Value: Single): Boolean;
  1079. Begin
  1080. Result := TextToFloat(PChar(S), Value, fvSingle);
  1081. End;
  1082. Function TryStrToFloat(Const S : String; Var Value: Double): Boolean;
  1083. Begin
  1084. Result := TextToFloat(PChar(S), Value, fvDouble);
  1085. End;
  1086. Function TryStrToFloat(Const S : String; Var Value: Extended): Boolean;
  1087. Begin
  1088. Result := TextToFloat(PChar(S), Value);
  1089. End;
  1090. Function FloatToStr(Value: Extended): String;
  1091. Begin
  1092. Result := FloatToStrF(Value, ffGeneral, 15, 0);
  1093. End;
  1094. Function FloatToText(Buffer: PChar; Value: Extended; format: TFloatFormat; Precision, Digits: Integer): Longint;
  1095. Var
  1096. Tmp: String[40];
  1097. Begin
  1098. Tmp := FloatToStrF(Value, format, Precision, Digits);
  1099. Result := Length(Tmp);
  1100. Move(Tmp[1], Buffer[0], Result);
  1101. End;
  1102. Function FloatToStrF(Value: Extended; format: TFloatFormat; Precision, Digits: Integer): String;
  1103. Var
  1104. P: Integer;
  1105. Negative, TooSmall, TooLarge: Boolean;
  1106. Begin
  1107. Case format Of
  1108. ffGeneral:
  1109. Begin
  1110. If (Precision = -1) Or (Precision > 15) Then Precision := 15;
  1111. TooSmall := (Abs(Value) < 0.00001) and (Value>0.0);
  1112. If Not TooSmall Then
  1113. Begin
  1114. Str(Value:0:999, Result);
  1115. P := Pos('.', Result);
  1116. Result[P] := DecimalSeparator;
  1117. TooLarge := P > Precision + 1;
  1118. End;
  1119. If TooSmall Or TooLarge Then
  1120. begin
  1121. Result := FloatToStrF(Value, ffExponent, Precision, Digits);
  1122. // Strip unneeded zeroes.
  1123. P:=Pos('E',result)-1;
  1124. If P<>-1 then
  1125. While (P>1) and (Result[P]='0') do
  1126. begin
  1127. system.Delete(Result,P,1);
  1128. Dec(P);
  1129. end;
  1130. end
  1131. else
  1132. begin
  1133. P := Length(Result);
  1134. While Result[P] = '0' Do Dec(P);
  1135. If Result[P] = DecimalSeparator Then Dec(P);
  1136. SetLength(Result, P);
  1137. end;
  1138. End;
  1139. ffExponent:
  1140. Begin
  1141. If (Precision = -1) Or (Precision > 15) Then Precision := 15;
  1142. Str(Value:Precision + 8, Result);
  1143. Result[3] := DecimalSeparator;
  1144. P:=4;
  1145. While (P>0) and (Digits < P) And (Result[Precision + 5] = '0') do
  1146. Begin
  1147. If P<>1 then
  1148. system.Delete(Result, Precision + 5, 1)
  1149. else
  1150. system.Delete(Result, Precision + 3, 3);
  1151. Dec(P);
  1152. end;
  1153. If Result[1] = ' ' Then
  1154. System.Delete(Result, 1, 1);
  1155. End;
  1156. ffFixed:
  1157. Begin
  1158. If Digits = -1 Then Digits := 2
  1159. Else If Digits > 18 Then Digits := 18;
  1160. Str(Value:0:Digits, Result);
  1161. If Result[1] = ' ' Then
  1162. System.Delete(Result, 1, 1);
  1163. P := Pos('.', Result);
  1164. If P <> 0 Then Result[P] := DecimalSeparator;
  1165. End;
  1166. ffNumber:
  1167. Begin
  1168. If Digits = -1 Then Digits := 2
  1169. Else If Digits > 15 Then Digits := 15;
  1170. Str(Value:0:Digits, Result);
  1171. If Result[1] = ' ' Then System.Delete(Result, 1, 1);
  1172. P := Pos('.', Result);
  1173. If P <> 0 Then
  1174. Result[P] := DecimalSeparator
  1175. else
  1176. P := Length(Result)+1;
  1177. Dec(P, 3);
  1178. While (P > 1) Do
  1179. Begin
  1180. If Result[P - 1] <> '-' Then Insert(ThousandSeparator, Result, P);
  1181. Dec(P, 3);
  1182. End;
  1183. End;
  1184. ffCurrency:
  1185. Begin
  1186. If Value < 0 Then
  1187. Begin
  1188. Negative := True;
  1189. Value := -Value;
  1190. End
  1191. Else Negative := False;
  1192. If Digits = -1 Then Digits := CurrencyDecimals
  1193. Else If Digits > 18 Then Digits := 18;
  1194. Str(Value:0:Digits, Result);
  1195. If Result[1] = ' ' Then System.Delete(Result, 1, 1);
  1196. P := Pos('.', Result);
  1197. If P <> 0 Then Result[P] := DecimalSeparator;
  1198. Dec(P, 3);
  1199. While (P > 1) Do
  1200. Begin
  1201. Insert(ThousandSeparator, Result, P);
  1202. Dec(P, 3);
  1203. End;
  1204. If Not Negative Then
  1205. Begin
  1206. Case CurrencyFormat Of
  1207. 0: Result := CurrencyString + Result;
  1208. 1: Result := Result + CurrencyString;
  1209. 2: Result := CurrencyString + ' ' + Result;
  1210. 3: Result := Result + ' ' + CurrencyString;
  1211. End
  1212. End
  1213. Else
  1214. Begin
  1215. Case NegCurrFormat Of
  1216. 0: Result := '(' + CurrencyString + Result + ')';
  1217. 1: Result := '-' + CurrencyString + Result;
  1218. 2: Result := CurrencyString + '-' + Result;
  1219. 3: Result := CurrencyString + Result + '-';
  1220. 4: Result := '(' + Result + CurrencyString + ')';
  1221. 5: Result := '-' + Result + CurrencyString;
  1222. 6: Result := Result + '-' + CurrencyString;
  1223. 7: Result := Result + CurrencyString + '-';
  1224. 8: Result := '-' + Result + ' ' + CurrencyString;
  1225. 9: Result := '-' + CurrencyString + ' ' + Result;
  1226. 10: Result := CurrencyString + ' ' + Result + '-';
  1227. End;
  1228. End;
  1229. End;
  1230. End;
  1231. End;
  1232. Function FloatToDateTime (Const Value : Extended) : TDateTime;
  1233. begin
  1234. If (Value<MinDateTime) or (Value>MaxDateTime) then
  1235. Raise EConvertError.CreateFmt (SInvalidDateTime,[Value]);
  1236. Result:=Value;
  1237. end;
  1238. function TryFloatToCurr(const Value: Extended; var AResult: Currency): Boolean;
  1239. begin
  1240. {$ifndef VER1_0}
  1241. Result:=(Value>=MinCurrency) and (Value<=MaxCurrency);
  1242. if Result then
  1243. AResult := Value;
  1244. {$else VER1_0}
  1245. Result:=false;
  1246. {$endif VER1_0}
  1247. end;
  1248. function FloatToCurr(const Value: Extended): Currency;
  1249. begin
  1250. if not TryFloatToCurr(Value, Result) then
  1251. Raise EConvertError.CreateFmt(SInvalidCurrency, [FloatToStr(Value)]);
  1252. end;
  1253. Function CurrToStr(Value: Currency): string;
  1254. begin
  1255. Result:=FloatToStrF(Value,ffNumber,15,2);
  1256. end;
  1257. function StrToCurr(const S: string): Currency;
  1258. begin
  1259. if not TextToFloat(PChar(S), Result, fvCurrency) then
  1260. Raise EConvertError.createfmt(SInValidFLoat,[S]);
  1261. end;
  1262. function StrToCurrDef(const S: string; Default : Currency): Currency;
  1263. begin
  1264. if not TextToFloat(PChar(S), Result, fvCurrency) then
  1265. Result:=Default;
  1266. end;
  1267. function StrToBool(const S: string): Boolean;
  1268. Var
  1269. Temp : String;
  1270. D : Double;
  1271. {$IFDEF VIRTUALPASCAL}
  1272. Code: longint;
  1273. {$ELSE}
  1274. Code: word;
  1275. {$ENDIF}
  1276. begin
  1277. Temp:=upcase(S);
  1278. Val(temp,D,code);
  1279. If Code=0 then
  1280. Result:=(D<>0.0)
  1281. else If Temp='TRUE' then
  1282. result:=true
  1283. else if Temp='FALSE' then
  1284. result:=false
  1285. else
  1286. Raise EConvertError.CreateFmt(SInvalidBoolean,[S]);
  1287. end;
  1288. function BoolToStr(B: Boolean): string;
  1289. begin
  1290. If B then
  1291. Result:='TRUE'
  1292. else
  1293. Result:='FALSE';
  1294. end;
  1295. Function FloatToTextFmt(Buffer: PChar; Value: Extended; format: PChar): Integer;
  1296. Var
  1297. Digits: String[40]; { String Of Digits }
  1298. Exponent: String[8]; { Exponent strin }
  1299. FmtStart, FmtStop: PChar; { Start And End Of relevant part }
  1300. { Of format String }
  1301. ExpFmt, ExpSize: Integer; { Type And Length Of }
  1302. { exponential format chosen }
  1303. Placehold: Array[1..4] Of Integer; { Number Of placeholders In All }
  1304. { four Sections }
  1305. thousand: Boolean; { thousand separators? }
  1306. UnexpectedDigits: Integer; { Number Of unexpected Digits that }
  1307. { have To be inserted before the }
  1308. { First placeholder. }
  1309. DigitExponent: Integer; { Exponent Of First digit In }
  1310. { Digits Array. }
  1311. { Find end of format section starting at P. False, if empty }
  1312. Function GetSectionEnd(Var P: PChar): Boolean;
  1313. Var
  1314. C: Char;
  1315. SQ, DQ: Boolean;
  1316. Begin
  1317. Result := False;
  1318. SQ := False;
  1319. DQ := False;
  1320. C := P[0];
  1321. While (C<>#0) And ((C<>';') Or SQ Or DQ) Do
  1322. Begin
  1323. Result := True;
  1324. Case C Of
  1325. #34: If Not SQ Then DQ := Not DQ;
  1326. #39: If Not DQ Then SQ := Not SQ;
  1327. End;
  1328. Inc(P);
  1329. C := P[0];
  1330. End;
  1331. End;
  1332. { Find start and end of format section to apply. If section doesn't exist,
  1333. use section 1. If section 2 is used, the sign of value is ignored. }
  1334. Procedure GetSectionRange(section: Integer);
  1335. Var
  1336. Sec: Array[1..3] Of PChar;
  1337. SecOk: Array[1..3] Of Boolean;
  1338. Begin
  1339. Sec[1] := format;
  1340. SecOk[1] := GetSectionEnd(Sec[1]);
  1341. If section > 1 Then
  1342. Begin
  1343. Sec[2] := Sec[1];
  1344. If Sec[2][0] <> #0 Then
  1345. Inc(Sec[2]);
  1346. SecOk[2] := GetSectionEnd(Sec[2]);
  1347. If section > 2 Then
  1348. Begin
  1349. Sec[3] := Sec[2];
  1350. If Sec[3][0] <> #0 Then
  1351. Inc(Sec[3]);
  1352. SecOk[3] := GetSectionEnd(Sec[3]);
  1353. End;
  1354. End;
  1355. If Not SecOk[1] Then
  1356. FmtStart := Nil
  1357. Else
  1358. Begin
  1359. If Not SecOk[section] Then
  1360. section := 1
  1361. Else If section = 2 Then
  1362. Value := -Value; { Remove sign }
  1363. If section = 1 Then FmtStart := format Else
  1364. Begin
  1365. FmtStart := Sec[section - 1];
  1366. Inc(FmtStart);
  1367. End;
  1368. FmtStop := Sec[section];
  1369. End;
  1370. End;
  1371. { Find format section ranging from FmtStart to FmtStop. }
  1372. Procedure GetFormatOptions;
  1373. Var
  1374. Fmt: PChar;
  1375. SQ, DQ: Boolean;
  1376. area: Integer;
  1377. Begin
  1378. SQ := False;
  1379. DQ := False;
  1380. Fmt := FmtStart;
  1381. ExpFmt := 0;
  1382. area := 1;
  1383. thousand := False;
  1384. Placehold[1] := 0;
  1385. Placehold[2] := 0;
  1386. Placehold[3] := 0;
  1387. Placehold[4] := 0;
  1388. While Fmt < FmtStop Do
  1389. Begin
  1390. Case Fmt[0] Of
  1391. #34:
  1392. Begin
  1393. If Not SQ Then
  1394. DQ := Not DQ;
  1395. Inc(Fmt);
  1396. End;
  1397. #39:
  1398. Begin
  1399. If Not DQ Then
  1400. SQ := Not SQ;
  1401. Inc(Fmt);
  1402. End;
  1403. Else
  1404. { This was 'if not SQ or DQ'. Looked wrong... }
  1405. If Not SQ Or DQ Then
  1406. Begin
  1407. Case Fmt[0] Of
  1408. '0':
  1409. Begin
  1410. Case area Of
  1411. 1:
  1412. area := 2;
  1413. 4:
  1414. Begin
  1415. area := 3;
  1416. Inc(Placehold[3], Placehold[4]);
  1417. Placehold[4] := 0;
  1418. End;
  1419. End;
  1420. Inc(Placehold[area]);
  1421. Inc(Fmt);
  1422. End;
  1423. '#':
  1424. Begin
  1425. If area=3 Then
  1426. area:=4;
  1427. Inc(Placehold[area]);
  1428. Inc(Fmt);
  1429. End;
  1430. '.':
  1431. Begin
  1432. If area<3 Then
  1433. area:=3;
  1434. Inc(Fmt);
  1435. End;
  1436. ',':
  1437. Begin
  1438. thousand := True;
  1439. Inc(Fmt);
  1440. End;
  1441. 'e', 'E':
  1442. If ExpFmt = 0 Then
  1443. Begin
  1444. If (Fmt[0]='E') Then
  1445. ExpFmt:=1
  1446. Else
  1447. ExpFmt := 3;
  1448. Inc(Fmt);
  1449. If (Fmt<FmtStop) Then
  1450. Begin
  1451. Case Fmt[0] Of
  1452. '+':
  1453. Begin
  1454. End;
  1455. '-':
  1456. Inc(ExpFmt);
  1457. Else
  1458. ExpFmt := 0;
  1459. End;
  1460. If ExpFmt <> 0 Then
  1461. Begin
  1462. Inc(Fmt);
  1463. ExpSize := 0;
  1464. While (Fmt<FmtStop) And
  1465. (ExpSize<4) And
  1466. (Fmt[0] In ['0'..'9']) Do
  1467. Begin
  1468. Inc(ExpSize);
  1469. Inc(Fmt);
  1470. End;
  1471. End;
  1472. End;
  1473. End
  1474. Else
  1475. Inc(Fmt);
  1476. Else { Case }
  1477. Inc(Fmt);
  1478. End; { Case }
  1479. End; { Begin }
  1480. End; { Case }
  1481. End; { While .. Begin }
  1482. End;
  1483. Procedure FloatToStr;
  1484. Var
  1485. I, J, Exp, Width, Decimals, DecimalPoint, len: Integer;
  1486. Begin
  1487. If ExpFmt = 0 Then
  1488. Begin
  1489. { Fixpoint }
  1490. Decimals:=Placehold[3]+Placehold[4];
  1491. Width:=Placehold[1]+Placehold[2]+Decimals;
  1492. If (Decimals=0) Then
  1493. Str(Value:Width:0,Digits)
  1494. Else
  1495. Str(Value:Width+1:Decimals,Digits);
  1496. len:=Length(Digits);
  1497. { Find the decimal point }
  1498. If (Decimals=0) Then
  1499. DecimalPoint:=len+1
  1500. Else
  1501. DecimalPoint:=len-Decimals;
  1502. { If value is very small, and no decimal places
  1503. are desired, remove the leading 0. }
  1504. If (Abs(Value) < 1) And (Placehold[2] = 0) Then
  1505. Begin
  1506. If (Placehold[1]=0) Then
  1507. Delete(Digits,DecimalPoint-1,1)
  1508. Else
  1509. Digits[DecimalPoint-1]:=' ';
  1510. End;
  1511. { Convert optional zeroes to spaces. }
  1512. I:=len;
  1513. J:=DecimalPoint+Placehold[3];
  1514. While (I>J) And (Digits[I]='0') Do
  1515. Begin
  1516. Digits[I] := ' ';
  1517. Dec(I);
  1518. End;
  1519. { If integer value and no obligatory decimal
  1520. places, remove decimal point. }
  1521. If (DecimalPoint < len) And (Digits[DecimalPoint + 1] = ' ') Then
  1522. Digits[DecimalPoint] := ' ';
  1523. { Convert spaces left from obligatory decimal point to zeroes. }
  1524. I:=DecimalPoint-Placehold[2];
  1525. While (I<DecimalPoint) And (Digits[I]=' ') Do
  1526. Begin
  1527. Digits[I] := '0';
  1528. Inc(I);
  1529. End;
  1530. Exp := 0;
  1531. End
  1532. Else
  1533. Begin
  1534. { Scientific: exactly <Width> Digits With <Precision> Decimals
  1535. And adjusted Exponent. }
  1536. If Placehold[1]+Placehold[2]=0 Then
  1537. Placehold[1]:=1;
  1538. Decimals := Placehold[3] + Placehold[4];
  1539. Width:=Placehold[1]+Placehold[2]+Decimals;
  1540. Str(Value:Width+8,Digits);
  1541. { Find and cut out exponent. Always the
  1542. last 6 characters in the string.
  1543. -> 0000E+0000 }
  1544. I:=Length(Digits)-5;
  1545. Val(Copy(Digits,I+1,5),Exp,J);
  1546. Exp:=Exp+1-(Placehold[1]+Placehold[2]);
  1547. Delete(Digits, I, 6);
  1548. { Str() always returns at least one digit after the decimal point.
  1549. If we don't want it, we have to remove it. }
  1550. If (Decimals=0) And (Placehold[1]+Placehold[2]<= 1) Then
  1551. Begin
  1552. If (Digits[4]>='5') Then
  1553. Begin
  1554. Inc(Digits[2]);
  1555. If (Digits[2]>'9') Then
  1556. Begin
  1557. Digits[2] := '1';
  1558. Inc(Exp);
  1559. End;
  1560. End;
  1561. Delete(Digits, 3, 2);
  1562. DecimalPoint := Length(Digits) + 1;
  1563. End
  1564. Else
  1565. Begin
  1566. { Move decimal point at the desired position }
  1567. Delete(Digits, 3, 1);
  1568. DecimalPoint:=2+Placehold[1]+Placehold[2];
  1569. If (Decimals<>0) Then
  1570. Insert('.',Digits,DecimalPoint);
  1571. End;
  1572. { Convert optional zeroes to spaces. }
  1573. I := Length(Digits);
  1574. J := DecimalPoint + Placehold[3];
  1575. While (I > J) And (Digits[I] = '0') Do
  1576. Begin
  1577. Digits[I] := ' ';
  1578. Dec(I);
  1579. End;
  1580. { If integer number and no obligatory decimal paces, remove decimal point }
  1581. If (DecimalPoint<Length(Digits)) And
  1582. (Digits[DecimalPoint+1]=' ') Then
  1583. Digits[DecimalPoint]:=' ';
  1584. If (Digits[1]=' ') Then
  1585. Begin
  1586. Delete(Digits, 1, 1);
  1587. Dec(DecimalPoint);
  1588. End;
  1589. { Calculate exponent string }
  1590. Str(Abs(Exp), Exponent);
  1591. While Length(Exponent)<ExpSize Do
  1592. Insert('0',Exponent,1);
  1593. If Exp >= 0 Then
  1594. Begin
  1595. If (ExpFmt In [1,3]) Then
  1596. Insert('+', Exponent, 1);
  1597. End
  1598. Else
  1599. Insert('-',Exponent,1);
  1600. If (ExpFmt<3) Then
  1601. Insert('E',Exponent,1)
  1602. Else
  1603. Insert('e',Exponent,1);
  1604. End;
  1605. DigitExponent:=DecimalPoint-2;
  1606. If (Digits[1]='-') Then
  1607. Dec(DigitExponent);
  1608. UnexpectedDigits:=DecimalPoint-1-(Placehold[1]+Placehold[2]);
  1609. End;
  1610. Function PutResult: LongInt;
  1611. Var
  1612. SQ, DQ: Boolean;
  1613. Fmt, Buf: PChar;
  1614. Dig, N: Integer;
  1615. Begin
  1616. SQ := False;
  1617. DQ := False;
  1618. Fmt := FmtStart;
  1619. Buf := Buffer;
  1620. Dig := 1;
  1621. While (Fmt<FmtStop) Do
  1622. Begin
  1623. //Write(Fmt[0]);
  1624. Case Fmt[0] Of
  1625. #34:
  1626. Begin
  1627. If Not SQ Then
  1628. DQ := Not DQ;
  1629. Inc(Fmt);
  1630. End;
  1631. #39:
  1632. Begin
  1633. If Not DQ Then
  1634. SQ := Not SQ;
  1635. Inc(Fmt);
  1636. End;
  1637. Else
  1638. If Not (SQ Or DQ) Then
  1639. Begin
  1640. Case Fmt[0] Of
  1641. '0', '#', '.':
  1642. Begin
  1643. If (Dig=1) And (UnexpectedDigits>0) Then
  1644. Begin
  1645. { Everything unexpected is written before the first digit }
  1646. For N := 1 To UnexpectedDigits Do
  1647. Begin
  1648. Buf[0] := Digits[N];
  1649. Inc(Buf);
  1650. If thousand And (Digits[N]<>'-') Then
  1651. Begin
  1652. If (DigitExponent Mod 3 = 0) And (DigitExponent>0) Then
  1653. Begin
  1654. Buf[0] := ThousandSeparator;
  1655. Inc(Buf);
  1656. End;
  1657. Dec(DigitExponent);
  1658. End;
  1659. End;
  1660. Inc(Dig, UnexpectedDigits);
  1661. End;
  1662. If (Digits[Dig]<>' ') Then
  1663. Begin
  1664. If (Digits[Dig]='.') Then
  1665. Buf[0] := DecimalSeparator
  1666. Else
  1667. Buf[0] := Digits[Dig];
  1668. Inc(Buf);
  1669. If thousand And (DigitExponent Mod 3 = 0) And (DigitExponent > 0) Then
  1670. Begin
  1671. Buf[0] := ThousandSeparator;
  1672. Inc(Buf);
  1673. End;
  1674. End;
  1675. Inc(Dig);
  1676. Dec(DigitExponent);
  1677. Inc(Fmt);
  1678. End;
  1679. 'e', 'E':
  1680. Begin
  1681. If ExpFmt <> 0 Then
  1682. Begin
  1683. Inc(Fmt);
  1684. If Fmt < FmtStop Then
  1685. Begin
  1686. If Fmt[0] In ['+', '-'] Then
  1687. Begin
  1688. Inc(Fmt, ExpSize);
  1689. For N:=1 To Length(Exponent) Do
  1690. Buf[N-1] := Exponent[N];
  1691. Inc(Buf,Length(Exponent));
  1692. ExpFmt:=0;
  1693. End;
  1694. Inc(Fmt);
  1695. End;
  1696. End
  1697. Else
  1698. Begin
  1699. { No legal exponential format.
  1700. Simply write the 'E' to the result. }
  1701. Buf[0] := Fmt[0];
  1702. Inc(Buf);
  1703. Inc(Fmt);
  1704. End;
  1705. End;
  1706. Else { Case }
  1707. { Usual character }
  1708. If (Fmt[0]<>',') Then
  1709. Begin
  1710. Buf[0] := Fmt[0];
  1711. Inc(Buf);
  1712. End;
  1713. Inc(Fmt);
  1714. End; { Case }
  1715. End
  1716. Else { IF }
  1717. Begin
  1718. { Character inside single or double quotes }
  1719. Buf[0] := Fmt[0];
  1720. Inc(Buf);
  1721. Inc(Fmt);
  1722. End;
  1723. End; { Case }
  1724. End; { While .. Begin }
  1725. Result:=PtrInt(Buf)-PtrInt(Buffer);
  1726. End;
  1727. Begin
  1728. If (Value>0) Then
  1729. GetSectionRange(1)
  1730. Else If (Value<0) Then
  1731. GetSectionRange(2)
  1732. Else
  1733. GetSectionRange(3);
  1734. If FmtStart = Nil Then
  1735. Begin
  1736. Result := FloatToText(Buffer, Value, ffGeneral, 15, 4);
  1737. End
  1738. Else
  1739. Begin
  1740. GetFormatOptions;
  1741. If (ExpFmt = 0) And (Abs(Value) >= 1E18) Then
  1742. Result := FloatToText(Buffer, Value, ffGeneral, 15, 4)
  1743. Else
  1744. Begin
  1745. FloatToStr;
  1746. Result := PutResult;
  1747. End;
  1748. End;
  1749. End;
  1750. Procedure FloatToDecimal(Var Result: TFloatRec; Value: Extended; Precision, Decimals : integer);
  1751. Var
  1752. Buffer: String[24];
  1753. Error, N: Integer;
  1754. Begin
  1755. Str(Value:23, Buffer);
  1756. Result.Negative := (Buffer[1] = '-');
  1757. Val(Copy(Buffer, 19, 5), Result.Exponent, Error);
  1758. Inc(Result. Exponent);
  1759. Result.Digits[0] := Buffer[2];
  1760. Move(Buffer[4], Result.Digits[1], 14);
  1761. If Decimals + Result.Exponent < Precision Then
  1762. N := Decimals + Result.Exponent
  1763. Else
  1764. N := Precision;
  1765. If N > 15 Then
  1766. N := 15;
  1767. If N = 0 Then
  1768. Begin
  1769. If Result.Digits[0] >= '5' Then
  1770. Begin
  1771. Result.Digits[0] := '1';
  1772. Result.Digits[1] := #0;
  1773. Inc(Result.Exponent);
  1774. End
  1775. Else
  1776. Result.Digits[0] := #0;
  1777. End
  1778. Else If N > 0 Then
  1779. Begin
  1780. If Result.Digits[N] >= '5' Then
  1781. Begin
  1782. Repeat
  1783. Result.Digits[N] := #0;
  1784. Dec(N);
  1785. Inc(Result.Digits[N]);
  1786. Until (N = 0) Or (Result.Digits[N] < ':');
  1787. If Result.Digits[0] = ':' Then
  1788. Begin
  1789. Result.Digits[0] := '1';
  1790. Inc(Result.Exponent);
  1791. End;
  1792. End
  1793. Else
  1794. Begin
  1795. Result.Digits[N] := '0';
  1796. While (Result.Digits[N] = '0') And (N > -1) Do
  1797. Begin
  1798. Result.Digits[N] := #0;
  1799. Dec(N);
  1800. End;
  1801. End;
  1802. End
  1803. Else
  1804. Result.Digits[0] := #0;
  1805. If Result.Digits[0] = #0 Then
  1806. Begin
  1807. Result.Exponent := 0;
  1808. Result.Negative := False;
  1809. End;
  1810. End;
  1811. Function FormatFloat(Const format: String; Value: Extended): String;
  1812. Var
  1813. Temp: ShortString;
  1814. buf : Array[0..1024] of char;
  1815. Begin
  1816. Buf[FloatToTextFmt(@Buf[0],Value,Pchar(Format))]:=#0;
  1817. Result:=StrPas(@Buf);
  1818. End;
  1819. {==============================================================================}
  1820. { extra functions }
  1821. {==============================================================================}
  1822. { LeftStr returns Count left-most characters from S }
  1823. function LeftStr(const S: string; Count: integer): string;
  1824. begin
  1825. result := Copy(S, 1, Count);
  1826. end ;
  1827. { RightStr returns Count right-most characters from S }
  1828. function RightStr(const S: string; Count: integer): string;
  1829. begin
  1830. If Count>Length(S) then
  1831. Count:=Length(S);
  1832. result := Copy(S, 1 + Length(S) - Count, Count);
  1833. end;
  1834. { BCDToInt converts the BCD value Value to an integer }
  1835. function BCDToInt(Value: integer): integer;
  1836. var i, j: integer;
  1837. begin
  1838. result := 0;
  1839. j := 1;
  1840. for i := 0 to SizeOf(Value) shr 1 - 1 do begin
  1841. result := result + j * (Value and 15);
  1842. j := j * 10;
  1843. Value := Value shr 4;
  1844. end ;
  1845. end ;
  1846. Function LastDelimiter(const Delimiters, S: string): Integer;
  1847. begin
  1848. Result:=Length(S);
  1849. While (Result>0) and (Pos(S[Result],Delimiters)=0) do
  1850. Dec(Result);
  1851. end;
  1852. Function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;
  1853. var
  1854. Srch,OldP,RemS: string; // Srch and Oldp can contain uppercase versions of S,OldPattern
  1855. P : Integer;
  1856. begin
  1857. Srch:=S;
  1858. OldP:=OldPattern;
  1859. if rfIgnoreCase in Flags then
  1860. begin
  1861. Srch:=UpperCase(Srch);
  1862. OldP:=UpperCase(OldP);
  1863. end;
  1864. RemS:=S;
  1865. Result:='';
  1866. while (Length(Srch)<>0) do
  1867. begin
  1868. P:=Pos(OldP, Srch);
  1869. if P=0 then
  1870. begin
  1871. Result:=Result+RemS;
  1872. Srch:='';
  1873. end
  1874. else
  1875. begin
  1876. Result:=Result+Copy(RemS,1,P-1)+NewPattern;
  1877. P:=P+Length(OldP);
  1878. RemS:=Copy(RemS,P,Length(RemS)-P+1);
  1879. if not (rfReplaceAll in Flags) then
  1880. begin
  1881. Result:=Result+RemS;
  1882. Srch:='';
  1883. end
  1884. else
  1885. Srch:=Copy(Srch,P,Length(Srch)-P+1);
  1886. end;
  1887. end;
  1888. end;
  1889. Function IsDelimiter(const Delimiters, S: string; Index: Integer): Boolean;
  1890. begin
  1891. Result:=False;
  1892. If (Index>0) and (Index<=Length(S)) then
  1893. Result:=Pos(S[Index],Delimiters)<>0; // Note we don't do MBCS yet
  1894. end;
  1895. Function ByteToCharLen(const S: string; MaxLen: Integer): Integer;
  1896. begin
  1897. Result:=Length(S);
  1898. If Result>MaxLen then
  1899. Result:=MaxLen;
  1900. end;
  1901. Function ByteToCharIndex(const S: string; Index: Integer): Integer;
  1902. begin
  1903. Result:=Index;
  1904. end;
  1905. Function CharToByteLen(const S: string; MaxLen: Integer): Integer;
  1906. begin
  1907. Result:=Length(S);
  1908. If Result>MaxLen then
  1909. Result:=MaxLen;
  1910. end;
  1911. Function CharToByteIndex(const S: string; Index: Integer): Integer;
  1912. begin
  1913. Result:=Index;
  1914. end;
  1915. Function ByteType(const S: string; Index: Integer): TMbcsByteType;
  1916. begin
  1917. Result:=mbSingleByte;
  1918. end;
  1919. Function StrByteType(Str: PChar; Index: Cardinal): TMbcsByteType;
  1920. begin
  1921. Result:=mbSingleByte;
  1922. end;
  1923. Function FindCmdLineSwitch(const Switch: string; const Chars: TSysCharSet;IgnoreCase: Boolean): Boolean;
  1924. Var
  1925. I,L : Integer;
  1926. S,T : String;
  1927. begin
  1928. Result:=False;
  1929. S:=Switch;
  1930. If IgnoreCase then
  1931. S:=UpperCase(S);
  1932. I:=ParamCount;
  1933. While (Not Result) and (I>0) do
  1934. begin
  1935. L:=Length(Paramstr(I));
  1936. If (L>0) and (ParamStr(I)[1] in Chars) then
  1937. begin
  1938. T:=Copy(ParamStr(I),2,L-1);
  1939. If IgnoreCase then
  1940. T:=UpperCase(T);
  1941. Result:=S=T;
  1942. end;
  1943. Dec(i);
  1944. end;
  1945. end;
  1946. Function FindCmdLineSwitch(const Switch: string; IgnoreCase: Boolean): Boolean;
  1947. begin
  1948. Result:=FindCmdLineSwitch(Switch,SwitchChars,IgnoreCase);
  1949. end;
  1950. Function FindCmdLineSwitch(const Switch: string): Boolean;
  1951. begin
  1952. Result:=FindCmdLineSwitch(Switch,SwitchChars,False);
  1953. end;
  1954. function WrapText(const Line, BreakStr: string; const BreakChars: TSysCharSet; MaxCol: Integer): string;
  1955. const
  1956. Quotes = ['''', '"'];
  1957. Var
  1958. L : String;
  1959. C,LQ,BC : Char;
  1960. P,BLen,Len : Integer;
  1961. HB,IBC : Boolean;
  1962. begin
  1963. Result:='';
  1964. L:=Line;
  1965. Blen:=Length(BreakStr);
  1966. If (BLen>0) then
  1967. BC:=BreakStr[1]
  1968. else
  1969. BC:=#0;
  1970. Len:=Length(L);
  1971. While (Len>0) do
  1972. begin
  1973. P:=1;
  1974. LQ:=#0;
  1975. HB:=False;
  1976. IBC:=False;
  1977. While ((P<=Len) and ((P<=MaxCol) or not IBC)) and ((LQ<>#0) or Not HB) do
  1978. begin
  1979. C:=L[P];
  1980. If (C=LQ) then
  1981. LQ:=#0
  1982. else If (C in Quotes) then
  1983. LQ:=C;
  1984. If (LQ<>#0) then
  1985. Inc(P)
  1986. else
  1987. begin
  1988. HB:=((C=BC) and (BreakStr=Copy(L,P,BLen)));
  1989. If HB then
  1990. Inc(P,Blen)
  1991. else
  1992. begin
  1993. If (P>MaxCol) then
  1994. IBC:=C in BreakChars;
  1995. Inc(P);
  1996. end;
  1997. end;
  1998. // Writeln('"',C,'" : IBC : ',IBC,' HB : ',HB,' LQ : ',LQ,' P>MaxCol : ',P>MaxCol);
  1999. end;
  2000. Result:=Result+Copy(L,1,P-1);
  2001. If Not HB then
  2002. Result:=Result+BreakStr;
  2003. Delete(L,1,P-1);
  2004. Len:=Length(L);
  2005. end;
  2006. end;
  2007. function WrapText(const Line: string; MaxCol: Integer): string;
  2008. begin
  2009. Result:=WrapText(Line,sLineBreak, [' ', '-', #9], MaxCol);
  2010. end;
  2011. {
  2012. Case Translation Tables
  2013. Can be used in internationalization support.
  2014. Although these tables can be obtained through system calls
  2015. it is better to not use those, since most implementation are not 100%
  2016. WARNING:
  2017. before modifying a translation table make sure that the current codepage
  2018. of the OS corresponds to the one you make changes to
  2019. }
  2020. const
  2021. { upper case translation table for character set 850 }
  2022. CP850UCT: array[128..255] of char =
  2023. ('€', 'š', '�', '¶', 'Ž', '¶', '�', '€', 'Ò', 'Ó', 'Ô', 'Ø', '×', 'Þ', 'Ž', '�',
  2024. '�', '’', '’', 'â', '™', 'ã', 'ê', 'ë', 'Y', '™', 'š', '�', 'œ', '�', 'ž', 'Ÿ',
  2025. 'µ', 'Ö', 'à', 'é', '¥', '¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯',
  2026. '°', '±', '²', '³', '´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿',
  2027. 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Ç', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï',
  2028. 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', '×', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß',
  2029. 'à', 'á', 'â', 'ã', 'å', 'å', 'æ', 'í', 'è', 'é', 'ê', 'ë', 'í', 'í', 'î', 'ï',
  2030. 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ');
  2031. { lower case translation table for character set 850 }
  2032. CP850LCT: array[128..255] of char =
  2033. ('‡', '�', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '�', '„', '†',
  2034. '‚', '‘', '‘', '“', '”', '•', '–', '—', '˜', '”', '�', '›', 'œ', '›', 'ž', 'Ÿ',
  2035. ' ', '¡', '¢', '£', '¤', '¤', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯',
  2036. '°', '±', '²', '³', '´', ' ', 'ƒ', '…', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿',
  2037. 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Æ', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï',
  2038. 'Ð', 'Ñ', 'ˆ', '‰', 'Š', 'Õ', '¡', 'Œ', '‹', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', '�', 'ß',
  2039. '¢', 'á', '“', '•', 'ä', 'ä', 'æ', 'í', 'è', '£', '–', '—', 'ì', 'ì', 'î', 'ï',
  2040. 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ');
  2041. { upper case translation table for character set ISO 8859/1 Latin 1 }
  2042. CPISO88591UCT: array[192..255] of char =
  2043. ( #192, #193, #194, #195, #196, #197, #198, #199,
  2044. #200, #201, #202, #203, #204, #205, #206, #207,
  2045. #208, #209, #210, #211, #212, #213, #214, #215,
  2046. #216, #217, #218, #219, #220, #221, #222, #223,
  2047. #192, #193, #194, #195, #196, #197, #198, #199,
  2048. #200, #201, #202, #203, #204, #205, #206, #207,
  2049. #208, #209, #210, #211, #212, #213, #214, #247,
  2050. #216, #217, #218, #219, #220, #221, #222, #89 );
  2051. { lower case translation table for character set ISO 8859/1 Latin 1 }
  2052. CPISO88591LCT: array[192..255] of char =
  2053. ( #224, #225, #226, #227, #228, #229, #230, #231,
  2054. #232, #233, #234, #235, #236, #237, #238, #239,
  2055. #240, #241, #242, #243, #244, #245, #246, #215,
  2056. #248, #249, #250, #251, #252, #253, #254, #223,
  2057. #224, #225, #226, #227, #228, #229, #230, #231,
  2058. #232, #233, #234, #235, #236, #237, #238, #239,
  2059. #240, #241, #242, #243, #244, #245, #246, #247,
  2060. #248, #249, #250, #251, #252, #253, #254, #255 );
  2061. {
  2062. $Log$
  2063. Revision 1.25 2005-01-16 17:53:27 michael
  2064. + Patch from Colin Western to implemenet TryStrToFLoat
  2065. Revision 1.24 2004/12/26 13:04:30 peter
  2066. * fix bugs 3477, 3478, 3479
  2067. Revision 1.23 2004/12/19 17:55:38 michael
  2068. + Implemented wraptext
  2069. Revision 1.22 2004/12/01 10:34:46 michael
  2070. + Patch from Pete: Dont support widestrings when compiled with 1.0.x and Add additional typecasts to Widestring for widechar/pwidechar
  2071. Revision 1.21 2004/11/30 20:56:27 michael
  2072. + Fix from Alexey Barkovoy for bug 3302
  2073. Revision 1.20 2004/11/22 05:53:44 marco
  2074. * fixed little 1.0.xism
  2075. Revision 1.19 2004/11/21 19:33:20 marco
  2076. * %x 64-bit support
  2077. Revision 1.18 2004/11/21 16:44:01 marco
  2078. * %u
  2079. Revision 1.17 2004/11/16 18:30:35 marco
  2080. * updated ansiexctractquotedstring (more delphi compat, both interface and code)
  2081. Revision 1.16 2004/08/30 18:00:12 michael
  2082. + Extra index check in IsDelimiter
  2083. Revision 1.15 2004/08/07 19:32:35 florian
  2084. * fixed CompareStr with a patch from Michalis Kamburelis
  2085. Revision 1.14 2004/08/07 16:56:28 florian
  2086. + TryStrToInt* added
  2087. Revision 1.13 2004/06/13 10:49:50 florian
  2088. * fixed some bootstrapping problems as well as some 64 bit stuff
  2089. Revision 1.12 2004/06/12 13:57:18 michael
  2090. + Enhanced FloatToStrF to 18 digits (Delphi compatibility, bug 3106
  2091. Revision 1.11 2004/06/12 13:23:17 michael
  2092. + Fixed currency<->string conversion support
  2093. Revision 1.10 2004/04/28 20:48:20 peter
  2094. * ordinal-pointer conversions fixed
  2095. Revision 1.9 2004/02/26 08:46:21 michael
  2096. + Added AnsiSameStr
  2097. Revision 1.8 2003/11/26 22:17:42 michael
  2098. + Merged fixbranch fixes, missing in main branch
  2099. Revision 1.7 2003/11/22 17:18:53 marco
  2100. * johill patch applied
  2101. Revision 1.6 2003/11/22 16:17:26 michael
  2102. + Small optimization in comparemem
  2103. Revision 1.5 2003/11/22 15:46:48 michael
  2104. + Patched CompareMem for case when length is 0
  2105. Revision 1.4 2003/11/09 13:37:42 michael
  2106. + Position specifier in format string affects all later specifiers
  2107. Revision 1.3 2003/11/03 09:42:28 marco
  2108. * Peter's Cardinal<->Longint fixes patch
  2109. Revision 1.2 2003/10/07 12:02:47 marco
  2110. * sametext and ansisametext added. (simple (ansi)comparetext wrappers)
  2111. Revision 1.1 2003/10/06 21:01:06 peter
  2112. * moved classes unit to rtl
  2113. Revision 1.26 2003/09/06 21:22:07 marco
  2114. * More objpas fixes
  2115. Revision 1.25 2002/12/23 23:26:08 florian
  2116. + addition to previous commit, forgot to save in the editor
  2117. Revision 1.23 2002/11/28 22:26:30 michael
  2118. + Fixed float<>string conversion routines
  2119. Revision 1.22 2002/11/28 20:29:26 michael
  2120. + made it compile again
  2121. Revision 1.21 2002/11/28 20:15:37 michael
  2122. + Fixed comparestr (merge from fix)
  2123. Revision 1.20 2002/09/15 17:50:35 peter
  2124. * Fixed AnsiStrComp crashes
  2125. Revision 1.1.2.16 2002/11/28 22:25:01 michael
  2126. + Fixed float<>string conversion routines
  2127. Revision 1.1.2.15 2002/11/28 20:24:11 michael
  2128. + merged some fixes from mainbranch
  2129. Revision 1.19 2002/09/07 16:01:22 peter
  2130. * old logs removed and tabs fixed
  2131. Revision 1.1.2.14 2002/11/28 20:13:10 michael
  2132. + Fixed comparestr
  2133. Revision 1.1.2.13 2002/10/29 23:41:06 michael
  2134. + Added lots of D4 functions
  2135. Revision 1.18 2002/09/02 06:07:16 michael
  2136. + Fix for formatbuf not applied correct
  2137. Revision 1.17 2002/08/29 10:04:48 michael
  2138. + Fix for bug report 2097 in formatbuf
  2139. Revision 1.16 2002/08/29 10:04:25 michael
  2140. + Fix for bug report 2097 in formatbuf
  2141. Revision 1.15 2002/07/06 12:14:03 daniel
  2142. - Changes from Strasbourg
  2143. Revision 1.14 2002/01/24 12:33:53 jonas
  2144. * adapted ranges of native types to int64 (e.g. high cardinal is no
  2145. longer longint($ffffffff), but just $fffffff in psystem)
  2146. * small additional fix in 64bit rangecheck code generation for 32 bit
  2147. processors
  2148. * adaption of ranges required the matching talgorithm used for selecting
  2149. which overloaded procedure to call to be adapted. It should now always
  2150. select the closest match for ordinal parameters.
  2151. + inttostr(qword) in sysstr.inc/sysstrh.inc
  2152. + abs(int64), sqr(int64), sqr(qword) in systemh.inc/generic.inc (previous
  2153. fixes were required to be able to add them)
  2154. * is_in_limit() moved from ncal to types unit, should always be used
  2155. instead of direct comparisons of low/high values of orddefs because
  2156. qword is a special case
  2157. }