sysstr.inc 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885
  1. {
  2. *********************************************************************
  3. Copyright (C) 1997, 1998 Gertjan Schouten
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. *********************************************************************
  16. System Utilities For Free Pascal
  17. }
  18. { NewStr creates a new PString and assigns S to it
  19. if length(s) = 0 NewStr returns Nil }
  20. function NewStr(const S: string): PString;
  21. begin
  22. if (S='') then
  23. Result:=nil
  24. else
  25. begin
  26. new(result);
  27. if (Result<>nil) then
  28. Result^:=s;
  29. end;
  30. end;
  31. { DisposeStr frees the memory occupied by S }
  32. procedure DisposeStr(S: PString);
  33. begin
  34. if S <> Nil then
  35. begin
  36. dispose(s);
  37. S:=nil;
  38. end;
  39. end;
  40. { AssignStr assigns S to P^ }
  41. procedure AssignStr(var P: PString; const S: string);
  42. begin
  43. P^ := s;
  44. end ;
  45. { AppendStr appends S to Dest }
  46. procedure AppendStr(var Dest: String; const S: string);
  47. begin
  48. Dest := Dest + S;
  49. end ;
  50. { UpperCase returns a copy of S where all lowercase characters ( from a to z )
  51. have been converted to uppercase }
  52. Function UpperCase(Const S : String) : String;
  53. Var
  54. i : Integer;
  55. P : PChar;
  56. begin
  57. Result := S;
  58. if not assigned(pointer(result)) then exit;
  59. UniqueString(Result);
  60. P:=Pchar(pointer(Result));
  61. for i := 1 to Length(Result) do
  62. begin
  63. if (P^ in ['a'..'z']) then P^ := char(byte(p^) - 32);
  64. Inc(P);
  65. end;
  66. end;
  67. { LowerCase returns a copy of S where all uppercase characters ( from A to Z )
  68. have been converted to lowercase }
  69. Function Lowercase(Const S : String) : String;
  70. Var
  71. i : Integer;
  72. P : PChar;
  73. begin
  74. Result := S;
  75. if not assigned(pointer(result)) then exit;
  76. UniqueString(Result);
  77. P:=Pchar(pointer(Result));
  78. for i := 1 to Length(Result) do
  79. begin
  80. if (P^ in ['A'..'Z']) then P^ := char(byte(p^) + 32);
  81. Inc(P);
  82. end;
  83. end;
  84. function LowerCase(const V: variant): string; overload;{$ifdef SYSUTILSINLINE}inline;{$endif}
  85. begin
  86. result:=LowerCase(ansistring(V));
  87. end;
  88. { CompareStr compares S1 and S2, the result is the based on
  89. substraction of the ascii values of the characters in S1 and S2
  90. case result
  91. S1 < S2 < 0
  92. S1 > S2 > 0
  93. S1 = S2 = 0 }
  94. function CompareStr(const S1, S2: string): Integer;
  95. var count, count1, count2: integer;
  96. begin
  97. result := 0;
  98. Count1 := Length(S1);
  99. Count2 := Length(S2);
  100. if Count1>Count2 then
  101. Count:=Count2
  102. else
  103. Count:=Count1;
  104. result := CompareMemRange(Pointer(S1),Pointer(S2), Count);
  105. if result=0 then
  106. result:=Count1-Count2;
  107. end;
  108. { CompareMemRange returns the result of comparison of Length bytes at P1 and P2
  109. case result
  110. P1 < P2 < 0
  111. P1 > P2 > 0
  112. P1 = P2 = 0 }
  113. function CompareMemRange(P1, P2: Pointer; Length: cardinal): integer;
  114. var
  115. i: cardinal;
  116. begin
  117. i := 0;
  118. result := 0;
  119. while (result=0) and (I<length) do
  120. begin
  121. result:=byte(P1^)-byte(P2^);
  122. P1:=pchar(P1)+1; // VP compat.
  123. P2:=pchar(P2)+1;
  124. i := i + 1;
  125. end ;
  126. end ;
  127. function CompareMem(P1, P2: Pointer; Length: cardinal): Boolean;
  128. var
  129. i: cardinal;
  130. begin
  131. Result:=True;
  132. I:=0;
  133. If (P1)<>(P2) then
  134. While Result and (i<Length) do
  135. begin
  136. Result:=PByte(P1)^=PByte(P2)^;
  137. Inc(I);
  138. Inc(pchar(P1));
  139. Inc(pchar(P2));
  140. end;
  141. end;
  142. { CompareText compares S1 and S2, the result is the based on
  143. substraction of the ascii values of characters in S1 and S2
  144. comparison is case-insensitive
  145. case result
  146. S1 < S2 < 0
  147. S1 > S2 > 0
  148. S1 = S2 = 0 }
  149. function CompareText(const S1, S2: string): integer;
  150. var
  151. i, count, count1, count2: integer; Chr1, Chr2: byte;
  152. P1, P2: PChar;
  153. begin
  154. Count1 := Length(S1);
  155. Count2 := Length(S2);
  156. if (Count1>Count2) then
  157. Count := Count2
  158. else
  159. Count := Count1;
  160. P1 := @S1[1];
  161. P2 := @S2[1];
  162. i := 0;
  163. while i < Count do
  164. begin
  165. Chr1 := byte(p1^);
  166. Chr2 := byte(p2^);
  167. if Chr1 <> Chr2 then
  168. begin
  169. if Chr1 in [97..122] then
  170. dec(Chr1,32);
  171. if Chr2 in [97..122] then
  172. dec(Chr2,32);
  173. if Chr1 <> Chr2 then
  174. Break;
  175. end;
  176. Inc(P1); Inc(P2); Inc(I);
  177. end;
  178. if i < Count then
  179. result := Chr1-Chr2
  180. else
  181. result := count1-count2;
  182. end;
  183. function SameText(const s1,s2:String):Boolean;
  184. begin
  185. Result:=CompareText(S1,S2)=0;
  186. end;
  187. {$ifndef FPC_NOGENERICANSIROUTINES}
  188. {==============================================================================}
  189. { Ansi string functions }
  190. { these functions rely on the character set loaded by the OS }
  191. {==============================================================================}
  192. type
  193. TCaseTranslationTable = array[0..255] of char;
  194. var
  195. { Tables with upper and lowercase forms of character sets.
  196. MUST be initialized with the correct code-pages }
  197. UpperCaseTable: TCaseTranslationTable;
  198. LowerCaseTable: TCaseTranslationTable;
  199. function GenericAnsiUpperCase(const s: string): string;
  200. var
  201. len, i: integer;
  202. begin
  203. len := length(s);
  204. SetLength(result, len);
  205. for i := 1 to len do
  206. result[i] := UpperCaseTable[ord(s[i])];
  207. end;
  208. function GenericAnsiLowerCase(const s: string): string;
  209. var
  210. len, i: integer;
  211. begin
  212. len := length(s);
  213. SetLength(result, len);
  214. for i := 1 to len do
  215. result[i] := LowerCaseTable[ord(s[i])];
  216. end;
  217. function GenericAnsiCompareStr(const S1, S2: string): PtrInt;
  218. Var
  219. I,L1,L2 : SizeInt;
  220. begin
  221. Result:=0;
  222. L1:=Length(S1);
  223. L2:=Length(S2);
  224. I:=1;
  225. While (Result=0) and ((I<=L1) and (I<=L2)) do
  226. begin
  227. Result:=Ord(S1[I])-Ord(S2[I]); //!! Must be replaced by ansi characters !!
  228. Inc(I);
  229. end;
  230. If Result=0 Then
  231. Result:=L1-L2;
  232. end;
  233. function GenericAnsiCompareText(const S1, S2: string): PtrInt;
  234. Var
  235. I,L1,L2 : SizeInt;
  236. begin
  237. Result:=0;
  238. L1:=Length(S1);
  239. L2:=Length(S2);
  240. I:=1;
  241. While (Result=0) and ((I<=L1) and (I<=L2)) do
  242. begin
  243. Result:=Ord(LowerCaseTable[Ord(S1[I])])-Ord(LowerCaseTable[Ord(S2[I])]); //!! Must be replaced by ansi characters !!
  244. Inc(I);
  245. end;
  246. If Result=0 Then
  247. Result:=L1-L2;
  248. end;
  249. function GenericAnsiStrComp(S1, S2: PChar): PtrInt;
  250. begin
  251. Result:=0;
  252. If S1=Nil then
  253. begin
  254. If S2=Nil Then Exit;
  255. result:=-1;
  256. exit;
  257. end;
  258. If S2=Nil then
  259. begin
  260. Result:=1;
  261. exit;
  262. end;
  263. Repeat
  264. Result:=Ord(S1^)-Ord(S2^); //!! Must be replaced by ansi characters !!
  265. Inc(S1);
  266. Inc(S2);
  267. Until (Result<>0) or (S1^=#0) or (S2^=#0);
  268. if (Result=0) and (S1^<>S2^) then // loop ended because exactly one has #0
  269. if S1^=#0 then // shorter string is smaller
  270. result:=-1
  271. else
  272. result:=1;
  273. end;
  274. function GenericAnsiStrIComp(S1, S2: PChar): PtrInt;
  275. begin
  276. Result:=0;
  277. If S1=Nil then
  278. begin
  279. If S2=Nil Then Exit;
  280. result:=-1;
  281. exit;
  282. end;
  283. If S2=Nil then
  284. begin
  285. Result:=1;
  286. exit;
  287. end;
  288. Repeat
  289. Result:=Ord(LowerCaseTable[Ord(S1[0])])-Ord(LowerCaseTable[Ord(S2[0])]); //!! Must be replaced by ansi characters !!
  290. Inc(S1);
  291. Inc(S2);
  292. Until (Result<>0) or ((S1[0]=#0) or (S2[0]=#0));
  293. if (Result=0) and (s1[0]<>s2[0]) then //length(s1)<>length(s2)
  294. if s1[0]=#0 then
  295. Result:=-1 //s1 shorter than s2
  296. else
  297. Result:=1; //s1 longer than s2
  298. end;
  299. function GenericAnsiStrLComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  300. Var I : cardinal;
  301. begin
  302. Result:=0;
  303. If MaxLen=0 then exit;
  304. If S1=Nil then
  305. begin
  306. If S2=Nil Then Exit;
  307. result:=-1;
  308. exit;
  309. end;
  310. If S2=Nil then
  311. begin
  312. Result:=1;
  313. exit;
  314. end;
  315. I:=0;
  316. Repeat
  317. Result:=Ord(S1[0])-Ord(S2[0]); //!! Must be replaced by ansi characters !!
  318. Inc(S1);
  319. Inc(S2);
  320. Inc(I);
  321. Until (Result<>0) or (I=MaxLen)
  322. end;
  323. function GenericAnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  324. Var I : cardinal;
  325. begin
  326. Result:=0;
  327. If MaxLen=0 then exit;
  328. If S1=Nil then
  329. begin
  330. If S2=Nil Then Exit;
  331. result:=-1;
  332. exit;
  333. end;
  334. If S2=Nil then
  335. begin
  336. Result:=1;
  337. exit;
  338. end;
  339. I:=0;
  340. Repeat
  341. Result:=Ord(LowerCaseTable[Ord(S1[0])])-Ord(LowerCaseTable[Ord(S2[0])]); //!! Must be replaced by ansi characters !!
  342. Inc(S1);
  343. Inc(S2);
  344. Inc(I);
  345. Until (Result<>0) or (I=MaxLen)
  346. end;
  347. function GenericAnsiStrLower(Str: PChar): PChar;
  348. begin
  349. result := Str;
  350. if Str <> Nil then begin
  351. while Str^ <> #0 do begin
  352. Str^ := LowerCaseTable[byte(Str^)];
  353. Str := Str + 1;
  354. end;
  355. end;
  356. end;
  357. function GenericAnsiStrUpper(Str: PChar): PChar;
  358. begin
  359. result := Str;
  360. if Str <> Nil then begin
  361. while Str^ <> #0 do begin
  362. Str^ := UpperCaseTable[byte(Str^)];
  363. Str := Str + 1;
  364. end ;
  365. end ;
  366. end ;
  367. {$endif FPC_NOGENERICANSIROUTINES}
  368. function AnsiSameText(const s1,s2:String):Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
  369. begin
  370. AnsiSameText:=AnsiCompareText(S1,S2)=0;
  371. end;
  372. function AnsiSameStr(const s1,s2:String):Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
  373. begin
  374. AnsiSameStr:=AnsiCompareStr(S1,S2)=0;
  375. end;
  376. function AnsiLastChar(const S: string): PChar;
  377. begin
  378. //!! No multibyte yet, so we return the last one.
  379. result:=StrEnd(Pchar(pointer(S))); // strend checks for nil
  380. Dec(Result);
  381. end ;
  382. function AnsiStrLastChar(Str: PChar): PChar;
  383. begin
  384. //!! No multibyte yet, so we return the last one.
  385. result:=StrEnd(Str);
  386. Dec(Result);
  387. end ;
  388. function AnsiUpperCase(const s: string): string;{$ifdef SYSUTILSINLINE}inline;{$endif}
  389. begin
  390. result:=widestringmanager.UpperAnsiStringProc(s);
  391. end;
  392. function AnsiLowerCase(const s: string): string;{$ifdef SYSUTILSINLINE}inline;{$endif}
  393. begin
  394. result:=widestringmanager.LowerAnsiStringProc(s);
  395. end;
  396. function AnsiCompareStr(const S1, S2: string): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  397. begin
  398. result:=widestringmanager.CompareStrAnsiStringProc(s1,s2);
  399. end;
  400. function AnsiCompareText(const S1, S2: string): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  401. begin
  402. result:=widestringmanager.CompareTextAnsiStringProc(s1,s2);
  403. end;
  404. function AnsiStrComp(S1, S2: PChar): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  405. begin
  406. result:=widestringmanager.StrCompAnsiStringProc(s1,s2);
  407. end;
  408. function AnsiStrIComp(S1, S2: PChar): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  409. begin
  410. result:=widestringmanager.StrICompAnsiStringProc(s1,s2);
  411. end;
  412. function AnsiStrLComp(S1, S2: PChar; MaxLen: cardinal): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  413. begin
  414. result:=widestringmanager.StrLCompAnsiStringProc(s1,s2,maxlen);
  415. end;
  416. function AnsiStrLIComp(S1, S2: PChar; MaxLen: cardinal): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  417. begin
  418. result:=widestringmanager.StrLICompAnsiStringProc(s1,s2,maxlen);
  419. end;
  420. function AnsiStrLower(Str: PChar): PChar;{$ifdef SYSUTILSINLINE}inline;{$endif}
  421. begin
  422. result:=widestringmanager.StrLowerAnsiStringProc(Str);
  423. end;
  424. function AnsiStrUpper(Str: PChar): PChar;{$ifdef SYSUTILSINLINE}inline;{$endif}
  425. begin
  426. result:=widestringmanager.StrUpperAnsiStringProc(Str);
  427. end;
  428. {==============================================================================}
  429. { End of Ansi functions }
  430. {==============================================================================}
  431. { Trim returns a copy of S with blanks characters on the left and right stripped off }
  432. Const WhiteSpace = [#0..' '];
  433. function Trim(const S: string): string;
  434. var Ofs, Len: integer;
  435. begin
  436. len := Length(S);
  437. while (Len>0) and (S[Len] in WhiteSpace) do
  438. dec(Len);
  439. Ofs := 1;
  440. while (Ofs<=Len) and (S[Ofs] in WhiteSpace) do
  441. Inc(Ofs);
  442. result := Copy(S, Ofs, 1 + Len - Ofs);
  443. end ;
  444. { TrimLeft returns a copy of S with all blank characters on the left stripped off }
  445. function TrimLeft(const S: string): string;
  446. var i,l:integer;
  447. begin
  448. l := length(s);
  449. i := 1;
  450. while (i<=l) and (s[i] in whitespace) do
  451. inc(i);
  452. Result := copy(s, i, l);
  453. end ;
  454. { TrimRight returns a copy of S with all blank characters on the right stripped off }
  455. function TrimRight(const S: string): string;
  456. var l:integer;
  457. begin
  458. l := length(s);
  459. while (l>0) and (s[l] in whitespace) do
  460. dec(l);
  461. result := copy(s,1,l);
  462. end ;
  463. { QuotedStr returns S quoted left and right and every single quote in S
  464. replaced by two quotes }
  465. function QuotedStr(const S: string): string;
  466. begin
  467. result := AnsiQuotedStr(s, '''');
  468. end ;
  469. { AnsiQuotedStr returns S quoted left and right by Quote,
  470. and every single occurance of Quote replaced by two }
  471. function AnsiQuotedStr(const S: string; Quote: char): string;
  472. var i, j, count: integer;
  473. begin
  474. result := '' + Quote;
  475. count := length(s);
  476. i := 0;
  477. j := 0;
  478. while i < count do begin
  479. i := i + 1;
  480. if S[i] = Quote then begin
  481. result := result + copy(S, 1 + j, i - j) + Quote;
  482. j := i;
  483. end ;
  484. end ;
  485. if i <> j then
  486. result := result + copy(S, 1 + j, i - j);
  487. result := result + Quote;
  488. end ;
  489. { AnsiExtractQuotedStr returns a copy of Src with quote characters
  490. deleted to the left and right and double occurances
  491. of Quote replaced by a single Quote }
  492. function AnsiExtractQuotedStr(var Src: PChar; Quote: Char): string;
  493. var
  494. P,Q,R: PChar;
  495. begin
  496. P := Src;
  497. Q := StrEnd(P);
  498. result:='';
  499. if P=Q then exit;
  500. if P^<>quote then exit;
  501. inc(p);
  502. setlength(result,(Q-P)+1);
  503. R:=@Result[1];
  504. while P <> Q do
  505. begin
  506. R^:=P^;
  507. inc(R);
  508. if (P^ = Quote) then
  509. begin
  510. P := P + 1;
  511. if (p^ <> Quote) then
  512. begin
  513. dec(R);
  514. break;
  515. end;
  516. end;
  517. P := P + 1;
  518. end ;
  519. src:=p;
  520. SetLength(result, (R-pchar(@Result[1])));
  521. end ;
  522. { AdjustLineBreaks returns S with all CR characters not followed by LF
  523. replaced with CR/LF }
  524. // under Linux all CR characters or CR/LF combinations should be replaced with LF
  525. function AdjustLineBreaks(const S: string): string;
  526. begin
  527. Result:=AdjustLineBreaks(S,DefaultTextLineBreakStyle);
  528. end;
  529. function AdjustLineBreaks(const S: string; Style: TTextLineBreakStyle): string;
  530. var
  531. Source,Dest: PChar;
  532. DestLen: Integer;
  533. I,J,L: Longint;
  534. begin
  535. Source:=Pointer(S);
  536. L:=Length(S);
  537. DestLen:=L;
  538. I:=1;
  539. while (I<=L) do
  540. begin
  541. case S[i] of
  542. #10: if (Style=tlbsCRLF) then
  543. Inc(DestLen);
  544. #13: if (Style=tlbsCRLF) then
  545. if (I<L) and (S[i+1]=#10) then
  546. Inc(I)
  547. else
  548. Inc(DestLen)
  549. else if (I<L) and (S[I+1]=#10) then
  550. Dec(DestLen);
  551. end;
  552. Inc(I);
  553. end;
  554. if (DestLen=L) then
  555. Result:=S
  556. else
  557. begin
  558. SetLength(Result, DestLen);
  559. FillChar(Result[1],DestLen,0);
  560. Dest := Pointer(Result);
  561. J:=0;
  562. I:=0;
  563. While I<L do
  564. case Source[I] of
  565. #10: begin
  566. if Style=tlbsCRLF then
  567. begin
  568. Dest[j]:=#13;
  569. Inc(J);
  570. end;
  571. Dest[J] := #10;
  572. Inc(J);
  573. Inc(I);
  574. end;
  575. #13: begin
  576. if Style=tlbsCRLF then
  577. begin
  578. Dest[j] := #13;
  579. Inc(J);
  580. end;
  581. Dest[j]:=#10;
  582. Inc(J);
  583. Inc(I);
  584. if Source[I]=#10 then
  585. Inc(I);
  586. end;
  587. else
  588. Dest[j]:=Source[i];
  589. Inc(J);
  590. Inc(I);
  591. end;
  592. end;
  593. end;
  594. { IsValidIdent returns true if the first character of Ident is in:
  595. 'A' to 'Z', 'a' to 'z' or '_' and the following characters are
  596. on of: 'A' to 'Z', 'a' to 'z', '0'..'9' or '_' }
  597. function IsValidIdent(const Ident: string): boolean;
  598. var i, len: integer;
  599. begin
  600. result := false;
  601. len := length(Ident);
  602. if len <> 0 then begin
  603. result := Ident[1] in ['A'..'Z', 'a'..'z', '_'];
  604. i := 1;
  605. while (result) and (i < len) do begin
  606. i := i + 1;
  607. result := result and (Ident[i] in ['A'..'Z', 'a'..'z', '0'..'9', '_']);
  608. end ;
  609. end ;
  610. end ;
  611. { IntToStr returns a string representing the value of Value }
  612. function IntToStr(Value: integer): string;
  613. begin
  614. System.Str(Value, result);
  615. end ;
  616. function IntToStr(Value: int64): string;
  617. begin
  618. System.Str(Value, result);
  619. end ;
  620. function IntToStr(Value: QWord): string;
  621. begin
  622. System.Str(Value, result);
  623. end ;
  624. { IntToHex returns a string representing the hexadecimal value of Value }
  625. const
  626. HexDigits: array[0..15] of char = '0123456789ABCDEF';
  627. function IntToHex(Value: integer; Digits: integer): string;
  628. var i: integer;
  629. begin
  630. SetLength(result, digits);
  631. for i := 0 to digits - 1 do
  632. begin
  633. result[digits - i] := HexDigits[value and 15];
  634. value := value shr 4;
  635. end ;
  636. while value <> 0 do begin
  637. result := HexDigits[value and 15] + result;
  638. value := value shr 4;
  639. end;
  640. end ;
  641. function IntToHex(Value: int64; Digits: integer): string;
  642. var i: integer;
  643. begin
  644. SetLength(result, digits);
  645. for i := 0 to digits - 1 do
  646. begin
  647. result[digits - i] := HexDigits[value and 15];
  648. value := value shr 4;
  649. end ;
  650. while value <> 0 do begin
  651. result := HexDigits[value and 15] + result;
  652. value := value shr 4;
  653. end;
  654. end ;
  655. function IntToHex(Value: QWord; Digits: integer): string;
  656. begin
  657. result:=IntToHex(Int64(Value),Digits);
  658. end;
  659. function TryStrToInt(const s: string; var i : integer) : boolean;
  660. var Error : word;
  661. begin
  662. Val(s, i, Error);
  663. TryStrToInt:=Error=0
  664. end;
  665. { StrToInt converts the string S to an integer value,
  666. if S does not represent a valid integer value EConvertError is raised }
  667. function StrToInt(const S: string): integer;
  668. var Error: word;
  669. begin
  670. Val(S, result, Error);
  671. if Error <> 0 then raise EConvertError.createfmt(SInvalidInteger,[S]);
  672. end ;
  673. function StrToInt64(const S: string): int64;
  674. var Error: word;
  675. begin
  676. Val(S, result, Error);
  677. if Error <> 0 then raise EConvertError.createfmt(SInvalidInteger,[S]);
  678. end;
  679. function TryStrToInt64(const s: string; var i : int64) : boolean;
  680. var Error : word;
  681. begin
  682. Val(s, i, Error);
  683. TryStrToInt64:=Error=0
  684. end;
  685. function StrToQWord(const s: string): QWord;
  686. var Error: word;
  687. begin
  688. Val(S, result, Error);
  689. if Error <> 0 then raise EConvertError.createfmt(SInvalidInteger,[S]);
  690. end;
  691. function TryStrToQWord(const s: string; var Q: QWord): boolean;
  692. var Error : word;
  693. begin
  694. Val(s, Q, Error);
  695. TryStrToQWord:=Error=0
  696. end;
  697. { StrToIntDef converts the string S to an integer value,
  698. Default is returned in case S does not represent a valid integer value }
  699. function StrToIntDef(const S: string; Default: integer): integer;
  700. var Error: word;
  701. begin
  702. Val(S, result, Error);
  703. if Error <> 0 then result := Default;
  704. end ;
  705. { StrToInt64Def converts the string S to an int64 value,
  706. Default is returned in case S does not represent a valid int64 value }
  707. function StrToInt64Def(const S: string; Default: int64): int64;
  708. var Error: word;
  709. begin
  710. Val(S, result, Error);
  711. if Error <> 0 then result := Default;
  712. end ;
  713. { StrToQWordDef converts the string S to an QWord value,
  714. Default is returned in case S does not represent a valid QWord value }
  715. function StrToQWordDef(const S: string; Default: QWord): QWord;
  716. var Error: word;
  717. begin
  718. Val(S, result, Error);
  719. if Error <> 0 then result := Default;
  720. end;
  721. { LoadStr returns the string resource Ident. }
  722. function LoadStr(Ident: integer): string;
  723. begin
  724. result:='';
  725. end ;
  726. { FmtLoadStr returns the string resource Ident and formats it accordingly }
  727. function FmtLoadStr(Ident: integer; const Args: array of const): string;
  728. begin
  729. result:='';
  730. end;
  731. Const
  732. feInvalidFormat = 1;
  733. feMissingArgument = 2;
  734. feInvalidArgIndex = 3;
  735. {$ifdef fmtdebug}
  736. Procedure Log (Const S: String);
  737. begin
  738. Writeln (S);
  739. end;
  740. {$endif}
  741. Procedure DoFormatError (ErrCode : Longint);
  742. Var
  743. S : String;
  744. begin
  745. //!! must be changed to contain format string...
  746. S:='';
  747. Case ErrCode of
  748. feInvalidFormat : raise EConvertError.Createfmt(SInvalidFormat,[s]);
  749. feMissingArgument : raise EConvertError.Createfmt(SArgumentMissing,[s]);
  750. feInvalidArgIndex : raise EConvertError.Createfmt(SInvalidArgIndex,[s]);
  751. end;
  752. end;
  753. { we've no templates, but with includes we can simulate this :) }
  754. {$macro on}
  755. {$define INFORMAT}
  756. {$define TFormatString:=ansistring}
  757. {$define TFormatChar:=char}
  758. Function Format (Const Fmt : AnsiString; const Args : Array of const; const FormatSettings: TFormatSettings) : AnsiString;
  759. {$i sysformt.inc}
  760. {$undef TFormatString}
  761. {$undef TFormatChar}
  762. {$undef INFORMAT}
  763. {$macro off}
  764. Function Format (Const Fmt : AnsiString; const Args : Array of const) : AnsiString;
  765. begin
  766. Result:=Format(Fmt,Args,DefaultFormatSettings);
  767. end;
  768. Function FormatBuf (Var Buffer; BufLen : Cardinal; Const Fmt; fmtLen : Cardinal; Const Args : Array of const; Const FormatSettings: TFormatSettings) : Cardinal;
  769. Var S,F : String;
  770. begin
  771. Setlength(F,fmtlen);
  772. if fmtlen > 0 then
  773. Move(fmt,F[1],fmtlen);
  774. S:=Format (F,Args,FormatSettings);
  775. If Cardinal(Length(S))<Buflen then
  776. Result:=Length(S)
  777. else
  778. Result:=Buflen;
  779. Move(S[1],Buffer,Result);
  780. end;
  781. Function FormatBuf (Var Buffer; BufLen : Cardinal;
  782. Const Fmt; fmtLen : Cardinal;
  783. Const Args : Array of const) : Cardinal;
  784. begin
  785. Result:=FormatBuf(Buffer,BufLen,Fmt,FmtLen,Args,DefaultFormatSettings);
  786. end;
  787. Procedure FmtStr(Var Res: string; const Fmt : string; Const args: Array of const; Const FormatSettings: TFormatSettings);
  788. begin
  789. Res:=Format(fmt,Args,FormatSettings);
  790. end;
  791. Procedure FmtStr(Var Res: String; Const Fmt : String; Const args: Array of const);
  792. begin
  793. FmtStr(Res,Fmt,Args,DefaultFormatSettings);
  794. end;
  795. Function StrFmt(Buffer,Fmt : PChar; Const args: Array of const) : Pchar;
  796. begin
  797. Result:=StrFmt(Buffer,Fmt,Args,DefaultFormatSettings);
  798. end;
  799. Function StrFmt(Buffer,Fmt : PChar; Const Args: Array of const; Const FormatSettings: TFormatSettings): PChar;
  800. begin
  801. Buffer[FormatBuf(Buffer^,Maxint,Fmt^,strlen(fmt),args,FormatSettings)]:=#0;
  802. Result:=Buffer;
  803. end;
  804. Function StrLFmt(Buffer : PCHar; Maxlen : Cardinal;Fmt : PChar; Const args: Array of const) : Pchar;
  805. begin
  806. Result:=StrLFmt(Buffer,MaxLen,Fmt,Args,DefaultFormatSettings);
  807. end;
  808. Function StrLFmt(Buffer : PCHar; Maxlen : Cardinal;Fmt : PChar; Const args: Array of const; Const FormatSettings: TFormatSettings) : Pchar;
  809. begin
  810. Buffer[FormatBuf(Buffer^,MaxLen,Fmt^,strlen(fmt),args,FormatSettings)]:=#0;
  811. Result:=Buffer;
  812. end;
  813. {$ifndef FPUNONE}
  814. Function StrToFloat(Const S: String): Extended;
  815. begin
  816. Result:=StrToFloat(S,DefaultFormatSettings);
  817. end;
  818. Function StrToFloat(Const S : String; Const FormatSettings: TFormatSettings) : Extended;
  819. Begin // texttofloat handles NIL properly
  820. If Not TextToFloat(Pchar(pointer(S)),Result,FormatSettings) then
  821. Raise EConvertError.createfmt(SInValidFLoat,[S]);
  822. End;
  823. function StrToFloatDef(const S: string; const Default: Extended): Extended;
  824. begin
  825. Result:=StrToFloatDef(S,Default,DefaultFormatSettings);
  826. end;
  827. Function StrToFloatDef(Const S: String; Const Default: Extended; Const FormatSettings: TFormatSettings): Extended;
  828. begin
  829. if not TextToFloat(PChar(pointer(S)),Result,fvExtended,FormatSettings) then
  830. Result:=Default;
  831. end;
  832. Function TextToFloat(Buffer: PChar; Var Value: Extended; Const FormatSettings: TFormatSettings): Boolean;
  833. Var
  834. E,P : Integer;
  835. S : String;
  836. Begin
  837. S:=StrPas(Buffer);
  838. P:=Pos(FormatSettings.DecimalSeparator,S);
  839. If (P<>0) Then
  840. S[P] := '.';
  841. Val(trim(S),Value,E);
  842. Result:=(E=0);
  843. End;
  844. Function TextToFloat(Buffer: PChar; Var Value: Extended): Boolean;
  845. begin
  846. Result:=TextToFloat(Buffer,Value,DefaultFormatSettings);
  847. end;
  848. Function TextToFloat(Buffer: PChar; Var Value; ValueType: TFloatValue): Boolean;
  849. begin
  850. Result:=TextToFloat(Buffer,Value,ValueType,DefaultFormatSettings);
  851. end;
  852. Function TextToFloat(Buffer: PChar; Var Value; ValueType: TFloatValue; Const FormatSettings: TFormatSettings): Boolean;
  853. Var
  854. E,P : Integer;
  855. S : String;
  856. {$ifndef FPC_HAS_STR_CURRENCY}
  857. TempValue: extended;
  858. {$endif FPC_HAS_STR_CURRENCY}
  859. Begin
  860. S:=StrPas(Buffer);
  861. P:=Pos(FormatSettings.ThousandSeparator,S);
  862. While (P<>0) do
  863. begin
  864. Delete(S,P,1);
  865. P:=Pos(FormatSettings.ThousandSeparator,S);
  866. end;
  867. P:=Pos(FormatSettings.DecimalSeparator,S);
  868. If (P<>0) Then
  869. S[P] := '.';
  870. case ValueType of
  871. fvCurrency:
  872. {$ifdef FPC_HAS_STR_CURRENCY}
  873. Val(S,Currency(Value),E);
  874. {$else FPC_HAS_STR_CURRENCY}
  875. begin
  876. // needed for platforms where Currency = Int64
  877. Val(S,TempValue,E);
  878. Currency(Value) := TempValue;
  879. end;
  880. {$endif FPC_HAS_STR_CURRENCY}
  881. fvExtended:
  882. Val(S,Extended(Value),E);
  883. fvDouble:
  884. Val(S,Double(Value),E);
  885. fvSingle:
  886. Val(S,Single(Value),E);
  887. fvComp:
  888. Val(S,Comp(Value),E);
  889. fvReal:
  890. Val(S,Real(Value),E);
  891. end;
  892. Result:=(E=0);
  893. End;
  894. Function TryStrToFloat(Const S : String; Var Value: Single): Boolean;
  895. begin
  896. Result:=TryStrToFloat(S,Value,DefaultFormatSettings);
  897. end;
  898. Function TryStrToFloat(Const S : String; Var Value: Single; Const FormatSettings: TFormatSettings): Boolean;
  899. Begin
  900. Result := TextToFloat(PChar(pointer(S)), Value, fvSingle,FormatSettings);
  901. End;
  902. Function TryStrToFloat(Const S : String; Var Value: Double): Boolean;
  903. begin
  904. Result:=TryStrToFloat(S,Value,DefaultFormatSettings);
  905. end;
  906. Function TryStrToFloat(Const S : String; Var Value: Double; Const FormatSettings: TFormatSettings): Boolean;
  907. Begin
  908. Result := TextToFloat(PChar(pointer(S)), Value, fvDouble,FormatSettings);
  909. End;
  910. {$ifdef FPC_HAS_TYPE_EXTENDED}
  911. Function TryStrToFloat(Const S : String; Var Value: Extended): Boolean;
  912. begin
  913. Result:=TryStrToFloat(S,Value,DefaultFormatSettings);
  914. end;
  915. Function TryStrToFloat(Const S : String; Var Value: Extended; Const FormatSettings: TFormatSettings): Boolean;
  916. Begin
  917. Result := TextToFloat(PChar(pointer(S)), Value,FormatSettings);
  918. End;
  919. {$endif FPC_HAS_TYPE_EXTENDED}
  920. const
  921. {$ifdef FPC_HAS_TYPE_EXTENDED}
  922. maxdigits = 17;
  923. {$else}
  924. maxdigits = 15;
  925. {$endif}
  926. Function FloatToStrFIntl(const Value; format: TFloatFormat; Precision, Digits: Integer; ValueType: TFloatValue; Const FormatSettings: TFormatSettings): String;
  927. Var
  928. P: Integer;
  929. Negative, TooSmall, TooLarge: Boolean;
  930. DS: Char;
  931. Begin
  932. DS:=FormatSettings.DecimalSeparator;
  933. Case format Of
  934. ffGeneral:
  935. Begin
  936. case ValueType of
  937. fvCurrency:
  938. begin
  939. If (Precision = -1) Or (Precision > 19) Then Precision := 19;
  940. TooSmall:=False;
  941. end;
  942. else
  943. begin
  944. If (Precision = -1) Or (Precision > maxdigits) Then Precision := maxdigits;
  945. TooSmall := (Abs(Extended(Value)) < 0.00001) and (Extended(Value)<>0.0);
  946. end;
  947. end;
  948. If Not TooSmall Then
  949. Begin
  950. case ValueType of
  951. fvDouble:
  952. Str(Double(Extended(Value)):0:precision, Result);
  953. fvSingle:
  954. Str(Single(Extended(Value)):0:precision, Result);
  955. fvCurrency:
  956. {$ifdef FPC_HAS_STR_CURRENCY}
  957. Str(Currency(Value):0:precision, Result);
  958. {$else}
  959. Str(Extended(Currency(Value)):0:precision, Result);
  960. {$endif FPC_HAS_STR_CURRENCY}
  961. else
  962. Str(Extended(Value):0:precision, Result);
  963. end;
  964. P := Pos('.', Result);
  965. if P<>0 then
  966. Result[P] := DS;
  967. TooLarge :=(P > Precision + 1) or (Pos('E', Result)<>0);
  968. End;
  969. If TooSmall Or TooLarge Then
  970. begin
  971. Result := FloatToStrFIntl(Value, ffExponent, Precision, Digits, ValueType,FormatSettings);
  972. // Strip unneeded zeroes.
  973. P:=Pos('E',result)-1;
  974. If P<>-1 then
  975. begin
  976. { delete superfluous +? }
  977. if result[p+2]='+' then
  978. system.Delete(Result,P+2,1);
  979. While (P>1) and (Result[P]='0') do
  980. begin
  981. system.Delete(Result,P,1);
  982. Dec(P);
  983. end;
  984. If (P>0) and (Result[P]=DS) Then
  985. begin
  986. system.Delete(Result,P,1);
  987. Dec(P);
  988. end;
  989. end;
  990. end
  991. else if (P<>0) then // we have a decimalseparator
  992. begin
  993. { it seems that in this unit "precision" must mean "number of }
  994. { significant digits" rather than "number of digits after the }
  995. { decimal point" (as it does in the system unit) -> adjust }
  996. { (precision+1 to count the decimal point character) }
  997. if Result[1] = '-' then
  998. Inc(Precision);
  999. if (Length(Result) > Precision + 1) and
  1000. (Precision + 1 > P) then
  1001. begin
  1002. P := Precision + 1;
  1003. SetLength(Result,P);
  1004. end;
  1005. P := Length(Result);
  1006. While (P>0) and (Result[P] = '0') Do
  1007. Dec(P);
  1008. If (P>0) and (Result[P]=DS) Then
  1009. Dec(P);
  1010. SetLength(Result, P);
  1011. end;
  1012. End;
  1013. ffExponent:
  1014. Begin
  1015. If (Precision = -1) Or (Precision > maxdigits) Then Precision := maxdigits;
  1016. case ValueType of
  1017. fvDouble:
  1018. Str(Double(Extended(Value)):Precision+7, Result);
  1019. fvSingle:
  1020. Str(Single(Extended(Value)):Precision+6, Result);
  1021. fvCurrency:
  1022. {$ifdef FPC_HAS_STR_CURRENCY}
  1023. Str(Currency(Value):Precision+6, Result);
  1024. {$else}
  1025. Str(Extended(Currency(Value)):Precision+8, Result);
  1026. {$endif FPC_HAS_STR_CURRENCY}
  1027. else
  1028. Str(Extended(Value):Precision+8, Result);
  1029. end;
  1030. { Delete leading spaces }
  1031. while Result[1] = ' ' do
  1032. System.Delete(Result, 1, 1);
  1033. if Result[1] = '-' then
  1034. Result[3] := DS
  1035. else
  1036. Result[2] := DS;
  1037. P:=Pos('E',Result);
  1038. if P <> 0 then
  1039. begin
  1040. Inc(P, 2);
  1041. if Digits > 4 then
  1042. Digits:=4;
  1043. Digits:=Length(Result) - P - Digits + 1;
  1044. if Digits < 0 then
  1045. insert(copy('0000',1,-Digits),Result,P)
  1046. else
  1047. while (Digits > 0) and (Result[P] = '0') do
  1048. begin
  1049. System.Delete(Result, P, 1);
  1050. if P > Length(Result) then
  1051. begin
  1052. System.Delete(Result, P - 2, 2);
  1053. break;
  1054. end;
  1055. Dec(Digits);
  1056. end;
  1057. end;
  1058. End;
  1059. ffFixed:
  1060. Begin
  1061. If Digits = -1 Then Digits := 2
  1062. Else If Digits > 18 Then Digits := 18;
  1063. case ValueType of
  1064. fvDouble:
  1065. Str(Double(Extended(Value)):0:Digits, Result);
  1066. fvSingle:
  1067. Str(Single(Extended(Value)):0:Digits, Result);
  1068. fvCurrency:
  1069. {$ifdef FPC_HAS_STR_CURRENCY}
  1070. Str(Currency(Value):0:Digits, Result);
  1071. {$else}
  1072. Str(Extended(Currency(Value)):0:Digits, Result);
  1073. {$endif FPC_HAS_STR_CURRENCY}
  1074. else
  1075. Str(Extended(Value):0:Digits, Result);
  1076. end;
  1077. If Result[1] = ' ' Then
  1078. System.Delete(Result, 1, 1);
  1079. P := Pos('.', Result);
  1080. If P <> 0 Then Result[P] := DS;
  1081. End;
  1082. ffNumber:
  1083. Begin
  1084. If Digits = -1 Then Digits := 2
  1085. Else If Digits > maxdigits Then Digits := maxdigits;
  1086. case ValueType of
  1087. fvDouble:
  1088. Str(Double(Extended(Value)):0:Digits, Result);
  1089. fvSingle:
  1090. Str(Single(Extended(Value)):0:Digits, Result);
  1091. fvCurrency:
  1092. {$ifdef FPC_HAS_STR_CURRENCY}
  1093. Str(Currency(Value):0:Digits, Result);
  1094. {$else}
  1095. Str(Extended(Currency(Value)):0:Digits, Result);
  1096. {$endif FPC_HAS_STR_CURRENCY}
  1097. else
  1098. Str(Extended(Value):0:Digits, Result);
  1099. end;
  1100. If Result[1] = ' ' Then System.Delete(Result, 1, 1);
  1101. P := Pos('.', Result);
  1102. If P <> 0 Then
  1103. Result[P] := DS
  1104. else
  1105. P := Length(Result)+1;
  1106. Dec(P, 3);
  1107. While (P > 1) Do
  1108. Begin
  1109. If Result[P - 1] <> '-' Then Insert(FormatSettings.ThousandSeparator, Result, P);
  1110. Dec(P, 3);
  1111. End;
  1112. End;
  1113. ffCurrency:
  1114. Begin
  1115. If Digits = -1 Then Digits := FormatSettings.CurrencyDecimals
  1116. Else If Digits > 18 Then Digits := 18;
  1117. case ValueType of
  1118. fvDouble:
  1119. Str(Double(Extended(Value)):0:Digits, Result);
  1120. fvSingle:
  1121. Str(Single(Extended(Value)):0:Digits, Result);
  1122. fvCurrency:
  1123. {$ifdef FPC_HAS_STR_CURRENCY}
  1124. Str(Currency(Value):0:Digits, Result);
  1125. {$else}
  1126. Str(Extended(Currency(Value)):0:Digits, Result);
  1127. {$endif FPC_HAS_STR_CURRENCY}
  1128. else
  1129. Str(Extended(Value):0:Digits, Result);
  1130. end;
  1131. Negative:=Result[1] = '-';
  1132. if Negative then
  1133. System.Delete(Result, 1, 1);
  1134. P := Pos('.', Result);
  1135. If P <> 0 Then Result[P] := DS;
  1136. Dec(P, 3);
  1137. While (P > 1) Do
  1138. Begin
  1139. Insert(FormatSettings.ThousandSeparator, Result, P);
  1140. Dec(P, 3);
  1141. End;
  1142. If Not Negative Then
  1143. Begin
  1144. Case FormatSettings.CurrencyFormat Of
  1145. 0: Result := FormatSettings.CurrencyString + Result;
  1146. 1: Result := Result + FormatSettings.CurrencyString;
  1147. 2: Result := FormatSettings.CurrencyString + ' ' + Result;
  1148. 3: Result := Result + ' ' + FormatSettings.CurrencyString;
  1149. End
  1150. End
  1151. Else
  1152. Begin
  1153. Case NegCurrFormat Of
  1154. 0: Result := '(' + FormatSettings.CurrencyString + Result + ')';
  1155. 1: Result := '-' + FormatSettings.CurrencyString + Result;
  1156. 2: Result := FormatSettings.CurrencyString + '-' + Result;
  1157. 3: Result := FormatSettings.CurrencyString + Result + '-';
  1158. 4: Result := '(' + Result + FormatSettings.CurrencyString + ')';
  1159. 5: Result := '-' + Result + FormatSettings.CurrencyString;
  1160. 6: Result := Result + '-' + FormatSettings.CurrencyString;
  1161. 7: Result := Result + FormatSettings.CurrencyString + '-';
  1162. 8: Result := '-' + Result + ' ' + FormatSettings.CurrencyString;
  1163. 9: Result := '-' + FormatSettings.CurrencyString + ' ' + Result;
  1164. 10: Result := FormatSettings.CurrencyString + ' ' + Result + '-';
  1165. End;
  1166. End;
  1167. End;
  1168. End;
  1169. End;
  1170. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1171. Function FloatToStr(Value: Extended; Const FormatSettings: TFormatSettings): String;
  1172. Begin
  1173. Result := FloatToStrFIntl(Value, ffGeneral, 15, 0, fvExtended,FormatSettings);
  1174. End;
  1175. Function FloatToStr(Value: Extended): String;
  1176. begin
  1177. Result:=FloatToStr(Value,DefaultFormatSettings);
  1178. end;
  1179. {$endif FPC_HAS_TYPE_EXTENDED}
  1180. Function FloatToStr(Value: Currency; Const FormatSettings: TFormatSettings): String;
  1181. Begin
  1182. Result := FloatToStrFIntl(Value, ffGeneral, 15, 0, fvCurrency,FormatSettings);
  1183. End;
  1184. Function FloatToStr(Value: Currency): String;
  1185. begin
  1186. Result:=FloatToStr(Value,DefaultFormatSettings);
  1187. end;
  1188. Function FloatToStr(Value: Double; Const FormatSettings: TFormatSettings): String;
  1189. var
  1190. e: Extended;
  1191. Begin
  1192. e := Value;
  1193. Result := FloatToStrFIntl(e, ffGeneral, 15, 0, fvDouble,FormatSettings);
  1194. End;
  1195. Function FloatToStr(Value: Double): String;
  1196. begin
  1197. Result:=FloatToStr(Value,DefaultFormatSettings);
  1198. end;
  1199. Function FloatToStr(Value: Single; Const FormatSettings: TFormatSettings): String;
  1200. var
  1201. e: Extended;
  1202. Begin
  1203. e := Value;
  1204. Result := FloatToStrFIntl(e, ffGeneral, 15, 0, fvSingle,FormatSettings);
  1205. End;
  1206. Function FloatToStr(Value: Single): String;
  1207. begin
  1208. Result:=FloatToStr(Value,DefaultFormatSettings);
  1209. end;
  1210. Function FloatToStr(Value: Comp; Const FormatSettings: TFormatSettings): String;
  1211. var
  1212. e: Extended;
  1213. Begin
  1214. e := Value;
  1215. Result := FloatToStrFIntl(e, ffGeneral, 15, 0, fvComp,FormatSettings);
  1216. End;
  1217. Function FloatToStr(Value: Comp): String;
  1218. begin
  1219. Result:=FloatToStr(Value,DefaultFormatSettings);
  1220. end;
  1221. {$ifndef FPC_COMP_IS_INT64}
  1222. Function FloatToStr(Value: Int64): String;
  1223. begin
  1224. Result:=FloatToStr(Value,DefaultFormatSettings);
  1225. end;
  1226. Function FloatToStr(Value: Int64; Const FormatSettings: TFormatSettings): String;
  1227. var
  1228. e: Extended;
  1229. Begin
  1230. e := Comp(Value);
  1231. Result := FloatToStrFIntl(e, ffGeneral, 15, 0, fvComp,FormatSettings);
  1232. End;
  1233. {$endif FPC_COMP_IS_INT64}
  1234. Function FloatToText(Buffer: PChar; Value: Extended; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): Longint;
  1235. Var
  1236. Tmp: String[40];
  1237. Begin
  1238. Tmp := FloatToStrF(Value, format, Precision, Digits,FormatSettings);
  1239. Result := Length(Tmp);
  1240. Move(Tmp[1], Buffer[0], Result);
  1241. End;
  1242. Function FloatToText(Buffer: PChar; Value: Extended; format: TFloatFormat; Precision, Digits: Integer): Longint;
  1243. begin
  1244. Result:=FloatToText(Buffer,Value,Format,Precision,Digits,DefaultFormatSettings);
  1245. end;
  1246. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1247. Function FloatToStrF(Value: Extended; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1248. begin
  1249. Result := FloatToStrFIntl(value,format,precision,digits,fvExtended,FormatSettings);
  1250. end;
  1251. Function FloatToStrF(Value: Extended; format: TFloatFormat; Precision, Digits: Integer): String;
  1252. begin
  1253. Result:=FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
  1254. end;
  1255. {$endif}
  1256. Function FloatToStrF(Value: Currency; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1257. begin
  1258. Result := FloatToStrFIntl(value,format,precision,digits,fvCurrency,FormatSettings);
  1259. end;
  1260. Function FloatToStrF(Value: Currency; format: TFloatFormat; Precision, Digits: Integer): String;
  1261. begin
  1262. Result:=FloatToStrF(Value,format,Precision,Digits,DefaultFormatSettings);
  1263. end;
  1264. Function FloatToStrF(Value: Double; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1265. var
  1266. e: Extended;
  1267. begin
  1268. e := Value;
  1269. result := FloatToStrFIntl(e,format,precision,digits,fvDouble,FormatSettings);
  1270. end;
  1271. Function FloatToStrF(Value: Double; format: TFloatFormat; Precision, Digits: Integer): String;
  1272. begin
  1273. Result:= FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
  1274. end;
  1275. Function FloatToStrF(Value: Single; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1276. var
  1277. e: Extended;
  1278. begin
  1279. e:=Value;
  1280. result := FloatToStrFIntl(e,format,precision,digits,fvSingle,FormatSettings);
  1281. end;
  1282. Function FloatToStrF(Value: Single; format: TFloatFormat; Precision, Digits: Integer): String;
  1283. begin
  1284. Result:= FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
  1285. end;
  1286. Function FloatToStrF(Value: Comp; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1287. var
  1288. e: Extended;
  1289. begin
  1290. e := Value;
  1291. Result := FloatToStrFIntl(e,format,precision,digits,fvComp,FormatSettings);
  1292. end;
  1293. Function FloatToStrF(Value: Comp; format: TFloatFormat; Precision, Digits: Integer): String;
  1294. begin
  1295. Result:=FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
  1296. end;
  1297. {$ifndef FPC_COMP_IS_INT64}
  1298. Function FloatToStrF(Value: Int64; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1299. var
  1300. e: Extended;
  1301. begin
  1302. e := Comp(Value);
  1303. result := FloatToStrFIntl(e,format,precision,digits,fvComp,FormatSettings);
  1304. end;
  1305. Function FloatToStrF(Value: Int64; format: TFloatFormat; Precision, Digits: Integer): String;
  1306. begin
  1307. Result:=FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
  1308. end;
  1309. {$endif FPC_COMP_IS_INT64}
  1310. Function CurrToStrF(Value: Currency; Format: TFloatFormat; Digits: Integer; Const FormatSettings: TFormatSettings): string;
  1311. begin
  1312. result:=FloatToStrF(Value,Format,19,Digits,FormatSettings);
  1313. end;
  1314. Function CurrToStrF(Value: Currency; Format: TFloatFormat; Digits: Integer): string;
  1315. begin
  1316. Result:=CurrToStrF(Value,Format,Digits,DefaultFormatSettings);
  1317. end;
  1318. Function FloatToDateTime (Const Value : Extended) : TDateTime;
  1319. begin
  1320. If (Value<MinDateTime) or (Value>MaxDateTime) then
  1321. Raise EConvertError.CreateFmt (SInvalidDateTime,[Value]);
  1322. Result:=Value;
  1323. end;
  1324. function TryFloatToCurr(const Value: Extended; var AResult: Currency): Boolean;
  1325. begin
  1326. Result:=(Value>=MinCurrency) and (Value<=MaxCurrency);
  1327. if Result then
  1328. AResult := Value;
  1329. end;
  1330. function FloatToCurr(const Value: Extended): Currency;
  1331. begin
  1332. if not TryFloatToCurr(Value, Result) then
  1333. Raise EConvertError.CreateFmt(SInvalidCurrency, [FloatToStr(Value)]);
  1334. end;
  1335. Function CurrToStr(Value: Currency): string;
  1336. begin
  1337. Result:=FloatToStrF(Value,ffGeneral,-1,0);
  1338. end;
  1339. function StrToCurr(const S: string): Currency;
  1340. begin
  1341. if not TextToFloat(PChar(pointer(S)), Result, fvCurrency) then
  1342. Raise EConvertError.createfmt(SInValidFLoat,[S]);
  1343. end;
  1344. Function TryStrToCurr(Const S : String; Var Value: Currency): Boolean;
  1345. Begin
  1346. Result := TextToFloat(PChar(pointer(S)), Value, fvCurrency);
  1347. End;
  1348. function StrToCurrDef(const S: string; Default : Currency): Currency;
  1349. begin
  1350. if not TextToFloat(PChar(pointer(S)), Result, fvCurrency) then
  1351. Result:=Default;
  1352. end;
  1353. {$endif FPUNONE}
  1354. function AnsiDequotedStr(const S: string; AQuote: Char): string;
  1355. var p : pchar;
  1356. begin
  1357. p:=pchar(pointer(s)); // work around CONST. Ansiextract is safe for nil
  1358. result:=AnsiExtractquotedStr(p,AQuote);
  1359. if result='' Then
  1360. result:=s;
  1361. end;
  1362. function StrToBool(const S: string): Boolean;
  1363. begin
  1364. if not(TryStrToBool(S,Result)) then
  1365. Raise EConvertError.CreateFmt(SInvalidBoolean,[S]);
  1366. end;
  1367. function BoolToStr(B: Boolean;UseBoolStrs:Boolean=False): string;
  1368. procedure CheckStrs;
  1369. begin
  1370. If Length(TrueBoolStrs)=0 then
  1371. begin
  1372. SetLength(TrueBoolStrs,1);
  1373. TrueBoolStrs[0]:='True';
  1374. end;
  1375. If Length(FalseBoolStrs)=0 then
  1376. begin
  1377. SetLength(FalseBoolStrs,1);
  1378. FalseBoolStrs[0]:='False';
  1379. end;
  1380. end;
  1381. begin
  1382. if UseBoolStrs Then
  1383. begin
  1384. CheckStrs;
  1385. if B then
  1386. Result:=TrueBoolStrs[0]
  1387. else
  1388. Result:=FalseBoolStrs[0];
  1389. end
  1390. else
  1391. If B then
  1392. Result:='-1'
  1393. else
  1394. Result:='0';
  1395. end;
  1396. function StrToBoolDef(const S: string; Default: Boolean): Boolean;
  1397. begin
  1398. if not(TryStrToBool(S,Result)) then
  1399. Result:=Default;
  1400. end;
  1401. function TryStrToBool(const S: string; out Value: Boolean): Boolean;
  1402. Var
  1403. Temp : String;
  1404. {$ifdef FPUNONE}
  1405. D : Longint;
  1406. {$else}
  1407. D : Double;
  1408. {$endif}
  1409. Code: word;
  1410. begin
  1411. Temp:=upcase(S);
  1412. Val(temp,D,code);
  1413. Result:=true;
  1414. If Code=0 then
  1415. {$ifdef FPUNONE}
  1416. Value:=(D<>0)
  1417. {$else}
  1418. Value:=(D<>0.0)
  1419. {$endif}
  1420. else If Temp='TRUE' then
  1421. Value:=true
  1422. else if Temp='FALSE' then
  1423. Value:=false
  1424. else
  1425. Result:=false;
  1426. end;
  1427. {$ifndef FPUNONE}
  1428. Function FloatToTextFmt(Buffer: PChar; Value: Extended; format: PChar): Integer;
  1429. begin
  1430. Result:=FloatToTextFmt(Buffer,Value,Format,DefaultFormatSettings);
  1431. end;
  1432. Function FloatToTextFmt(Buffer: PChar; Value: Extended; format: PChar;FormatSettings : TFormatSettings): Integer;
  1433. Var
  1434. Digits: String[40]; { String Of Digits }
  1435. Exponent: String[8]; { Exponent strin }
  1436. FmtStart, FmtStop: PChar; { Start And End Of relevant part }
  1437. { Of format String }
  1438. ExpFmt, ExpSize: Integer; { Type And Length Of }
  1439. { exponential format chosen }
  1440. Placehold: Array[1..4] Of Integer; { Number Of placeholders In All }
  1441. { four Sections }
  1442. thousand: Boolean; { thousand separators? }
  1443. UnexpectedDigits: Integer; { Number Of unexpected Digits that }
  1444. { have To be inserted before the }
  1445. { First placeholder. }
  1446. DigitExponent: Integer; { Exponent Of First digit In }
  1447. { Digits Array. }
  1448. { Find end of format section starting at P. False, if empty }
  1449. Function GetSectionEnd(Var P: PChar): Boolean;
  1450. Var
  1451. C: Char;
  1452. SQ, DQ: Boolean;
  1453. Begin
  1454. Result := False;
  1455. SQ := False;
  1456. DQ := False;
  1457. C := P[0];
  1458. While (C<>#0) And ((C<>';') Or SQ Or DQ) Do
  1459. Begin
  1460. Result := True;
  1461. Case C Of
  1462. #34: If Not SQ Then DQ := Not DQ;
  1463. #39: If Not DQ Then SQ := Not SQ;
  1464. End;
  1465. Inc(P);
  1466. C := P[0];
  1467. End;
  1468. End;
  1469. { Find start and end of format section to apply. If section doesn't exist,
  1470. use section 1. If section 2 is used, the sign of value is ignored. }
  1471. Procedure GetSectionRange(section: Integer);
  1472. Var
  1473. Sec: Array[1..3] Of PChar;
  1474. SecOk: Array[1..3] Of Boolean;
  1475. Begin
  1476. Sec[1] := format;
  1477. SecOk[1] := GetSectionEnd(Sec[1]);
  1478. If section > 1 Then
  1479. Begin
  1480. Sec[2] := Sec[1];
  1481. If Sec[2][0] <> #0 Then
  1482. Inc(Sec[2]);
  1483. SecOk[2] := GetSectionEnd(Sec[2]);
  1484. If section > 2 Then
  1485. Begin
  1486. Sec[3] := Sec[2];
  1487. If Sec[3][0] <> #0 Then
  1488. Inc(Sec[3]);
  1489. SecOk[3] := GetSectionEnd(Sec[3]);
  1490. End;
  1491. End;
  1492. If Not SecOk[1] Then
  1493. FmtStart := Nil
  1494. Else
  1495. Begin
  1496. If Not SecOk[section] Then
  1497. section := 1
  1498. Else If section = 2 Then
  1499. Value := -Value; { Remove sign }
  1500. If section = 1 Then FmtStart := format Else
  1501. Begin
  1502. FmtStart := Sec[section - 1];
  1503. Inc(FmtStart);
  1504. End;
  1505. FmtStop := Sec[section];
  1506. End;
  1507. End;
  1508. { Find format section ranging from FmtStart to FmtStop. }
  1509. Procedure GetFormatOptions;
  1510. Var
  1511. Fmt: PChar;
  1512. SQ, DQ: Boolean;
  1513. area: Integer;
  1514. Begin
  1515. SQ := False;
  1516. DQ := False;
  1517. Fmt := FmtStart;
  1518. ExpFmt := 0;
  1519. area := 1;
  1520. thousand := False;
  1521. Placehold[1] := 0;
  1522. Placehold[2] := 0;
  1523. Placehold[3] := 0;
  1524. Placehold[4] := 0;
  1525. While Fmt < FmtStop Do
  1526. Begin
  1527. Case Fmt[0] Of
  1528. #34:
  1529. Begin
  1530. If Not SQ Then
  1531. DQ := Not DQ;
  1532. Inc(Fmt);
  1533. End;
  1534. #39:
  1535. Begin
  1536. If Not DQ Then
  1537. SQ := Not SQ;
  1538. Inc(Fmt);
  1539. End;
  1540. Else
  1541. { if not in quotes, then interpret}
  1542. If Not (SQ Or DQ) Then
  1543. Begin
  1544. Case Fmt[0] Of
  1545. '0':
  1546. Begin
  1547. Case area Of
  1548. 1:
  1549. area := 2;
  1550. 4:
  1551. Begin
  1552. area := 3;
  1553. Inc(Placehold[3], Placehold[4]);
  1554. Placehold[4] := 0;
  1555. End;
  1556. End;
  1557. Inc(Placehold[area]);
  1558. Inc(Fmt);
  1559. End;
  1560. '#':
  1561. Begin
  1562. If area=3 Then
  1563. area:=4;
  1564. Inc(Placehold[area]);
  1565. Inc(Fmt);
  1566. End;
  1567. '.':
  1568. Begin
  1569. If area<3 Then
  1570. area:=3;
  1571. Inc(Fmt);
  1572. End;
  1573. ',':
  1574. Begin
  1575. thousand := True;
  1576. Inc(Fmt);
  1577. End;
  1578. 'e', 'E':
  1579. If ExpFmt = 0 Then
  1580. Begin
  1581. If (Fmt[0]='E') Then
  1582. ExpFmt:=1
  1583. Else
  1584. ExpFmt := 3;
  1585. Inc(Fmt);
  1586. If (Fmt<FmtStop) Then
  1587. Begin
  1588. Case Fmt[0] Of
  1589. '+':
  1590. Begin
  1591. End;
  1592. '-':
  1593. Inc(ExpFmt);
  1594. Else
  1595. ExpFmt := 0;
  1596. End;
  1597. If ExpFmt <> 0 Then
  1598. Begin
  1599. Inc(Fmt);
  1600. ExpSize := 0;
  1601. While (Fmt<FmtStop) And
  1602. (ExpSize<4) And
  1603. (Fmt[0] In ['0'..'9']) Do
  1604. Begin
  1605. Inc(ExpSize);
  1606. Inc(Fmt);
  1607. End;
  1608. End;
  1609. End;
  1610. End
  1611. Else
  1612. Inc(Fmt);
  1613. Else { Case }
  1614. Inc(Fmt);
  1615. End; { Case }
  1616. End { Begin }
  1617. Else
  1618. Inc(Fmt);
  1619. End; { Case }
  1620. End; { While .. Begin }
  1621. End;
  1622. Procedure FloatToStr;
  1623. Var
  1624. I, J, Exp, Width, Decimals, DecimalPoint, len: Integer;
  1625. Begin
  1626. If ExpFmt = 0 Then
  1627. Begin
  1628. { Fixpoint }
  1629. Decimals:=Placehold[3]+Placehold[4];
  1630. Width:=Placehold[1]+Placehold[2]+Decimals;
  1631. If (Decimals=0) Then
  1632. Str(Value:Width:0,Digits)
  1633. Else
  1634. Str(Value:Width+1:Decimals,Digits);
  1635. len:=Length(Digits);
  1636. { Find the decimal point }
  1637. If (Decimals=0) Then
  1638. DecimalPoint:=len+1
  1639. Else
  1640. DecimalPoint:=len-Decimals;
  1641. { If value is very small, and no decimal places
  1642. are desired, remove the leading 0. }
  1643. If (Abs(Value) < 1) And (Placehold[2] = 0) Then
  1644. Begin
  1645. If (Placehold[1]=0) Then
  1646. Delete(Digits,DecimalPoint-1,1)
  1647. Else
  1648. Digits[DecimalPoint-1]:=' ';
  1649. End;
  1650. { Convert optional zeroes to spaces. }
  1651. I:=len;
  1652. J:=DecimalPoint+Placehold[3];
  1653. While (I>J) And (Digits[I]='0') Do
  1654. Begin
  1655. Digits[I] := ' ';
  1656. Dec(I);
  1657. End;
  1658. { If integer value and no obligatory decimal
  1659. places, remove decimal point. }
  1660. If (DecimalPoint < len) And (Digits[DecimalPoint + 1] = ' ') Then
  1661. Digits[DecimalPoint] := ' ';
  1662. { Convert spaces left from obligatory decimal point to zeroes. }
  1663. I:=DecimalPoint-Placehold[2];
  1664. While (I<DecimalPoint) And (Digits[I]=' ') Do
  1665. Begin
  1666. Digits[I] := '0';
  1667. Inc(I);
  1668. End;
  1669. Exp := 0;
  1670. End
  1671. Else
  1672. Begin
  1673. { Scientific: exactly <Width> Digits With <Precision> Decimals
  1674. And adjusted Exponent. }
  1675. If Placehold[1]+Placehold[2]=0 Then
  1676. Placehold[1]:=1;
  1677. Decimals := Placehold[3] + Placehold[4];
  1678. Width:=Placehold[1]+Placehold[2]+Decimals;
  1679. { depending on the maximally supported precision, the exponent field }
  1680. { is longer/shorter }
  1681. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1682. Str(Value:Width+8,Digits);
  1683. {$else FPC_HAS_TYPE_EXTENDED}
  1684. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1685. Str(Value:Width+7,Digits);
  1686. {$else FPC_HAS_TYPE_DOUBLE}
  1687. Str(Value:Width+6,Digits);
  1688. {$endif FPC_HAS_TYPE_DOUBLE}
  1689. {$endif FPC_HAS_TYPE_EXTENDED}
  1690. { Find and cut out exponent. Always the
  1691. last 6 characters in the string.
  1692. -> 0000E+0000
  1693. *** No, not always the last 6 characters, this depends on
  1694. the maximally supported precision (JM)}
  1695. I:=Pos('E',Digits);
  1696. Val(Copy(Digits,I+1,255),Exp,J);
  1697. Exp:=Exp+1-(Placehold[1]+Placehold[2]);
  1698. Delete(Digits, I, 255);
  1699. { Str() always returns at least one digit after the decimal point.
  1700. If we don't want it, we have to remove it. }
  1701. If (Decimals=0) And (Placehold[1]+Placehold[2]<= 1) Then
  1702. Begin
  1703. If (Digits[4]>='5') Then
  1704. Begin
  1705. Inc(Digits[2]);
  1706. If (Digits[2]>'9') Then
  1707. Begin
  1708. Digits[2] := '1';
  1709. Inc(Exp);
  1710. End;
  1711. End;
  1712. Delete(Digits, 3, 2);
  1713. DecimalPoint := Length(Digits) + 1;
  1714. End
  1715. Else
  1716. Begin
  1717. { Move decimal point at the desired position }
  1718. Delete(Digits, 3, 1);
  1719. DecimalPoint:=2+Placehold[1]+Placehold[2];
  1720. If (Decimals<>0) Then
  1721. Insert('.',Digits,DecimalPoint);
  1722. End;
  1723. { Convert optional zeroes to spaces. }
  1724. I := Length(Digits);
  1725. J := DecimalPoint + Placehold[3];
  1726. While (I > J) And (Digits[I] = '0') Do
  1727. Begin
  1728. Digits[I] := ' ';
  1729. Dec(I);
  1730. End;
  1731. { If integer number and no obligatory decimal paces, remove decimal point }
  1732. If (DecimalPoint<Length(Digits)) And
  1733. (Digits[DecimalPoint+1]=' ') Then
  1734. Digits[DecimalPoint]:=' ';
  1735. If (Digits[1]=' ') Then
  1736. Begin
  1737. Delete(Digits, 1, 1);
  1738. Dec(DecimalPoint);
  1739. End;
  1740. { Calculate exponent string }
  1741. Str(Abs(Exp), Exponent);
  1742. While Length(Exponent)<ExpSize Do
  1743. Insert('0',Exponent,1);
  1744. If Exp >= 0 Then
  1745. Begin
  1746. If (ExpFmt In [1,3]) Then
  1747. Insert('+', Exponent, 1);
  1748. End
  1749. Else
  1750. Insert('-',Exponent,1);
  1751. If (ExpFmt<3) Then
  1752. Insert('E',Exponent,1)
  1753. Else
  1754. Insert('e',Exponent,1);
  1755. End;
  1756. DigitExponent:=DecimalPoint-2;
  1757. If (Digits[1]='-') Then
  1758. Dec(DigitExponent);
  1759. UnexpectedDigits:=DecimalPoint-1-(Placehold[1]+Placehold[2]);
  1760. End;
  1761. Function PutResult: LongInt;
  1762. Var
  1763. SQ, DQ: Boolean;
  1764. Fmt, Buf: PChar;
  1765. Dig, N: Integer;
  1766. Begin
  1767. SQ := False;
  1768. DQ := False;
  1769. Fmt := FmtStart;
  1770. Buf := Buffer;
  1771. Dig := 1;
  1772. While (Fmt<FmtStop) Do
  1773. Begin
  1774. //Write(Fmt[0]);
  1775. Case Fmt[0] Of
  1776. #34:
  1777. Begin
  1778. If Not SQ Then
  1779. DQ := Not DQ;
  1780. Inc(Fmt);
  1781. End;
  1782. #39:
  1783. Begin
  1784. If Not DQ Then
  1785. SQ := Not SQ;
  1786. Inc(Fmt);
  1787. End;
  1788. Else
  1789. If Not (SQ Or DQ) Then
  1790. Begin
  1791. Case Fmt[0] Of
  1792. '0', '#', '.':
  1793. Begin
  1794. If (Dig=1) And (UnexpectedDigits>0) Then
  1795. Begin
  1796. { Everything unexpected is written before the first digit }
  1797. For N := 1 To UnexpectedDigits Do
  1798. Begin
  1799. Buf[0] := Digits[N];
  1800. Inc(Buf);
  1801. If thousand And (Digits[N]<>'-') Then
  1802. Begin
  1803. If (DigitExponent Mod 3 = 0) And (DigitExponent>0) Then
  1804. Begin
  1805. Buf[0] := FormatSettings.ThousandSeparator;
  1806. Inc(Buf);
  1807. End;
  1808. Dec(DigitExponent);
  1809. End;
  1810. End;
  1811. Inc(Dig, UnexpectedDigits);
  1812. End;
  1813. If (Digits[Dig]<>' ') Then
  1814. Begin
  1815. If (Digits[Dig]='.') Then
  1816. Buf[0] := FormatSettings.DecimalSeparator
  1817. Else
  1818. Buf[0] := Digits[Dig];
  1819. Inc(Buf);
  1820. If thousand And (DigitExponent Mod 3 = 0) And (DigitExponent > 0) Then
  1821. Begin
  1822. Buf[0] := FormatSettings.ThousandSeparator;
  1823. Inc(Buf);
  1824. End;
  1825. End;
  1826. Inc(Dig);
  1827. Dec(DigitExponent);
  1828. Inc(Fmt);
  1829. End;
  1830. 'e', 'E':
  1831. Begin
  1832. If ExpFmt <> 0 Then
  1833. Begin
  1834. Inc(Fmt);
  1835. If Fmt < FmtStop Then
  1836. Begin
  1837. If Fmt[0] In ['+', '-'] Then
  1838. Begin
  1839. Inc(Fmt, ExpSize);
  1840. For N:=1 To Length(Exponent) Do
  1841. Buf[N-1] := Exponent[N];
  1842. Inc(Buf,Length(Exponent));
  1843. ExpFmt:=0;
  1844. End;
  1845. Inc(Fmt);
  1846. End;
  1847. End
  1848. Else
  1849. Begin
  1850. { No legal exponential format.
  1851. Simply write the 'E' to the result. }
  1852. Buf[0] := Fmt[0];
  1853. Inc(Buf);
  1854. Inc(Fmt);
  1855. End;
  1856. End;
  1857. Else { Case }
  1858. { Usual character }
  1859. If (Fmt[0]<>',') Then
  1860. Begin
  1861. Buf[0] := Fmt[0];
  1862. Inc(Buf);
  1863. End;
  1864. Inc(Fmt);
  1865. End; { Case }
  1866. End
  1867. Else { IF }
  1868. Begin
  1869. { Character inside single or double quotes }
  1870. Buf[0] := Fmt[0];
  1871. Inc(Buf);
  1872. Inc(Fmt);
  1873. End;
  1874. End; { Case }
  1875. End; { While .. Begin }
  1876. Result:=PtrUInt(Buf)-PtrUInt(Buffer);
  1877. End;
  1878. Begin
  1879. If (Value>0) Then
  1880. GetSectionRange(1)
  1881. Else If (Value<0) Then
  1882. GetSectionRange(2)
  1883. Else
  1884. GetSectionRange(3);
  1885. If FmtStart = Nil Then
  1886. Begin
  1887. Result := FloatToText(Buffer, Value, ffGeneral, 15, 4, FormatSettings);
  1888. End
  1889. Else
  1890. Begin
  1891. GetFormatOptions;
  1892. If (ExpFmt = 0) And (Abs(Value) >= 1E18) Then
  1893. Result := FloatToText(Buffer, Value, ffGeneral, 15, 4, FormatSettings)
  1894. Else
  1895. Begin
  1896. FloatToStr;
  1897. Result := PutResult;
  1898. End;
  1899. End;
  1900. End;
  1901. Procedure FloatToDecimal(Out Result: TFloatRec; const Value; ValueType: TFloatValue; Precision, Decimals : integer);
  1902. var
  1903. Buffer: String[254]; //Though str func returns only 25 chars, this might change in the future
  1904. Error, N, L, Start, C: Integer;
  1905. GotNonZeroBeforeDot, BeforeDot : boolean;
  1906. begin
  1907. case ValueType of
  1908. fvExtended:
  1909. Str(Extended(Value):25, Buffer);
  1910. fvDouble,
  1911. fvReal:
  1912. Str(Double(Value):23, Buffer);
  1913. fvSingle:
  1914. Str(Single(Value):16, Buffer);
  1915. fvCurrency:
  1916. Str(Currency(Value):25, Buffer);
  1917. fvComp:
  1918. Str(Currency(Value):23, Buffer);
  1919. end;
  1920. N := 1;
  1921. L := Byte(Buffer[0]);
  1922. while Buffer[N]=' ' do
  1923. Inc(N);
  1924. Result.Negative := (Buffer[N] = '-');
  1925. if Result.Negative then
  1926. Inc(N);
  1927. Start := N; //Start of digits
  1928. Result.Exponent := 0; BeforeDot := true;
  1929. GotNonZeroBeforeDot := false;
  1930. while (L>=N) and (Buffer[N]<>'E') do
  1931. begin
  1932. if Buffer[N]='.' then
  1933. BeforeDot := false
  1934. else
  1935. begin
  1936. if BeforeDot then
  1937. begin // Currently this is always 1 char
  1938. Inc(Result.Exponent);
  1939. Result.Digits[N-Start] := Buffer[N];
  1940. if Buffer[N] <> '0' then
  1941. GotNonZeroBeforeDot := true;
  1942. end
  1943. else
  1944. Result.Digits[N-Start-1] := Buffer[N]
  1945. end;
  1946. Inc(N);
  1947. end;
  1948. Inc(N); // Pass through 'E'
  1949. if N<=L then
  1950. begin
  1951. Val(Copy(Buffer, N, L-N+1), C, Error); // Get exponent after 'E'
  1952. Inc(Result.Exponent, C);
  1953. end;
  1954. // Calculate number of digits we have from str
  1955. if BeforeDot then
  1956. N := N - Start - 1
  1957. else
  1958. N := N - Start - 2;
  1959. L := SizeOf(Result.Digits);
  1960. if N<L then
  1961. FillChar(Result.Digits[N], L-N, '0'); //Zero remaining space
  1962. if Decimals + Result.Exponent < Precision Then //After this it is the same as in FloatToDecimal
  1963. N := Decimals + Result.Exponent
  1964. Else
  1965. N := Precision;
  1966. if N >= L Then
  1967. N := L-1;
  1968. if N = 0 Then
  1969. begin
  1970. if Result.Digits[0] >= '5' Then
  1971. begin
  1972. Result.Digits[0] := '1';
  1973. Result.Digits[1] := #0;
  1974. Inc(Result.Exponent);
  1975. end
  1976. Else
  1977. Result.Digits[0] := #0;
  1978. end //N=0
  1979. Else if N > 0 Then
  1980. begin
  1981. if Result.Digits[N] >= '5' Then
  1982. begin
  1983. Repeat
  1984. Result.Digits[N] := #0;
  1985. Dec(N);
  1986. Inc(Result.Digits[N]);
  1987. Until (N = 0) Or (Result.Digits[N] < ':');
  1988. If Result.Digits[0] = ':' Then
  1989. begin
  1990. Result.Digits[0] := '1';
  1991. Inc(Result.Exponent);
  1992. end;
  1993. end
  1994. Else
  1995. begin
  1996. Result.Digits[N] := '0';
  1997. While (N > -1) And (Result.Digits[N] = '0') Do
  1998. begin
  1999. Result.Digits[N] := #0;
  2000. Dec(N);
  2001. end;
  2002. end;
  2003. end //N>0
  2004. Else
  2005. Result.Digits[0] := #0;
  2006. if (Result.Digits[0] = #0) and
  2007. not GotNonZeroBeforeDot then
  2008. begin
  2009. Result.Exponent := 0;
  2010. Result.Negative := False;
  2011. end;
  2012. end;
  2013. Procedure FloatToDecimal(Out Result: TFloatRec; Value: Extended; Precision, Decimals : integer);
  2014. begin
  2015. FloatToDecimal(Result,Value,fvExtended,Precision,Decimals);
  2016. end;
  2017. Function FormatFloat(Const Format : String; Value : Extended; Const FormatSettings: TFormatSettings) : String;
  2018. Var
  2019. buf : Array[0..1024] of char;
  2020. Begin // not changed to pchar(pointer(). Possibly not safe
  2021. Buf[FloatToTextFmt(@Buf[0],Value,Pchar(Format),FormatSettings)]:=#0;
  2022. Result:=StrPas(@Buf[0]);
  2023. End;
  2024. Function FormatFloat(Const format: String; Value: Extended): String;
  2025. begin
  2026. Result:=FormatFloat(Format,Value,DefaultFormatSettings);
  2027. end;
  2028. Function FormatCurr(const Format: string; Value: Currency; Const FormatSettings: TFormatSettings): string;
  2029. begin
  2030. Result := FormatFloat(Format, Value,FormatSettings);
  2031. end;
  2032. function FormatCurr(const Format: string; Value: Currency): string;
  2033. begin
  2034. Result:=FormatCurr(Format,Value,DefaultFormatSettings);
  2035. end;
  2036. {$endif}
  2037. {==============================================================================}
  2038. { extra functions }
  2039. {==============================================================================}
  2040. { LeftStr returns Count left-most characters from S }
  2041. function LeftStr(const S: string; Count: integer): string;
  2042. begin
  2043. result := Copy(S, 1, Count);
  2044. end ;
  2045. { RightStr returns Count right-most characters from S }
  2046. function RightStr(const S: string; Count: integer): string;
  2047. begin
  2048. If Count>Length(S) then
  2049. Count:=Length(S);
  2050. result := Copy(S, 1 + Length(S) - Count, Count);
  2051. end;
  2052. { BCDToInt converts the BCD value Value to an integer }
  2053. function BCDToInt(Value: integer): integer;
  2054. var i, j: integer;
  2055. begin
  2056. result := 0;
  2057. j := 1;
  2058. for i := 0 to SizeOf(Value) shr 1 - 1 do begin
  2059. result := result + j * (Value and 15);
  2060. j := j * 10;
  2061. Value := Value shr 4;
  2062. end ;
  2063. end ;
  2064. Function LastDelimiter(const Delimiters, S: string): Integer;
  2065. var
  2066. chs: TSysCharSet;
  2067. I: LongInt;
  2068. begin
  2069. chs := [];
  2070. for I := 1 to Length(Delimiters) do
  2071. Include(chs, Delimiters[I]);
  2072. Result:=Length(S);
  2073. While (Result>0) and not (S[Result] in chs) do
  2074. Dec(Result);
  2075. end;
  2076. Function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;
  2077. var
  2078. Srch,OldP,RemS: string; // Srch and Oldp can contain uppercase versions of S,OldPattern
  2079. P : Integer;
  2080. begin
  2081. Srch:=S;
  2082. OldP:=OldPattern;
  2083. if rfIgnoreCase in Flags then
  2084. begin
  2085. Srch:=AnsiUpperCase(Srch);
  2086. OldP:=AnsiUpperCase(OldP);
  2087. end;
  2088. RemS:=S;
  2089. Result:='';
  2090. while (Length(Srch)<>0) do
  2091. begin
  2092. P:=AnsiPos(OldP, Srch);
  2093. if P=0 then
  2094. begin
  2095. Result:=Result+RemS;
  2096. Srch:='';
  2097. end
  2098. else
  2099. begin
  2100. Result:=Result+Copy(RemS,1,P-1)+NewPattern;
  2101. P:=P+Length(OldP);
  2102. RemS:=Copy(RemS,P,Length(RemS)-P+1);
  2103. if not (rfReplaceAll in Flags) then
  2104. begin
  2105. Result:=Result+RemS;
  2106. Srch:='';
  2107. end
  2108. else
  2109. Srch:=Copy(Srch,P,Length(Srch)-P+1);
  2110. end;
  2111. end;
  2112. end;
  2113. Function IsDelimiter(const Delimiters, S: string; Index: Integer): Boolean;
  2114. begin
  2115. Result:=False;
  2116. If (Index>0) and (Index<=Length(S)) then
  2117. Result:=Pos(S[Index],Delimiters)<>0; // Note we don't do MBCS yet
  2118. end;
  2119. Function ByteToCharLen(const S: string; MaxLen: Integer): Integer;
  2120. begin
  2121. Result:=Length(S);
  2122. If Result>MaxLen then
  2123. Result:=MaxLen;
  2124. end;
  2125. Function ByteToCharIndex(const S: string; Index: Integer): Integer;
  2126. begin
  2127. Result:=Index;
  2128. end;
  2129. Function CharToByteLen(const S: string; MaxLen: Integer): Integer;
  2130. begin
  2131. Result:=Length(S);
  2132. If Result>MaxLen then
  2133. Result:=MaxLen;
  2134. end;
  2135. Function CharToByteIndex(const S: string; Index: Integer): Integer;
  2136. begin
  2137. Result:=Index;
  2138. end;
  2139. Function ByteType(const S: string; Index: Integer): TMbcsByteType;
  2140. begin
  2141. Result:=mbSingleByte;
  2142. end;
  2143. Function StrByteType(Str: PChar; Index: Cardinal): TMbcsByteType;
  2144. begin
  2145. Result:=mbSingleByte;
  2146. end;
  2147. Function StrCharLength(const Str: PChar): Integer;
  2148. begin
  2149. result:=widestringmanager.CharLengthPCharProc(Str);
  2150. end;
  2151. function StrNextChar(const Str: PChar): PChar;
  2152. begin
  2153. result:=Str+StrCharLength(Str);
  2154. end;
  2155. Function FindCmdLineSwitch(const Switch: string; const Chars: TSysCharSet;IgnoreCase: Boolean): Boolean;
  2156. Var
  2157. I,L : Integer;
  2158. S,T : String;
  2159. begin
  2160. Result:=False;
  2161. S:=Switch;
  2162. If IgnoreCase then
  2163. S:=UpperCase(S);
  2164. I:=ParamCount;
  2165. While (Not Result) and (I>0) do
  2166. begin
  2167. L:=Length(Paramstr(I));
  2168. If (L>0) and (ParamStr(I)[1] in Chars) then
  2169. begin
  2170. T:=Copy(ParamStr(I),2,L-1);
  2171. If IgnoreCase then
  2172. T:=UpperCase(T);
  2173. Result:=S=T;
  2174. end;
  2175. Dec(i);
  2176. end;
  2177. end;
  2178. Function FindCmdLineSwitch(const Switch: string; IgnoreCase: Boolean): Boolean;
  2179. begin
  2180. Result:=FindCmdLineSwitch(Switch,SwitchChars,IgnoreCase);
  2181. end;
  2182. Function FindCmdLineSwitch(const Switch: string): Boolean;
  2183. begin
  2184. Result:=FindCmdLineSwitch(Switch,SwitchChars,False);
  2185. end;
  2186. function WrapText(const Line, BreakStr: string; const BreakChars: TSysCharSet; MaxCol: Integer): string;
  2187. const
  2188. Quotes = ['''', '"'];
  2189. Var
  2190. L : String;
  2191. C,LQ,BC : Char;
  2192. P,BLen,Len : Integer;
  2193. HB,IBC : Boolean;
  2194. begin
  2195. Result:='';
  2196. L:=Line;
  2197. Blen:=Length(BreakStr);
  2198. If (BLen>0) then
  2199. BC:=BreakStr[1]
  2200. else
  2201. BC:=#0;
  2202. Len:=Length(L);
  2203. While (Len>0) do
  2204. begin
  2205. P:=1;
  2206. LQ:=#0;
  2207. HB:=False;
  2208. IBC:=False;
  2209. While ((P<=Len) and ((P<=MaxCol) or not IBC)) and ((LQ<>#0) or Not HB) do
  2210. begin
  2211. C:=L[P];
  2212. If (C=LQ) then
  2213. LQ:=#0
  2214. else If (C in Quotes) then
  2215. LQ:=C;
  2216. If (LQ<>#0) then
  2217. Inc(P)
  2218. else
  2219. begin
  2220. HB:=((C=BC) and (BreakStr=Copy(L,P,BLen)));
  2221. If HB then
  2222. Inc(P,Blen)
  2223. else
  2224. begin
  2225. If (P>MaxCol) then
  2226. IBC:=C in BreakChars;
  2227. Inc(P);
  2228. end;
  2229. end;
  2230. // Writeln('"',C,'" : IBC : ',IBC,' HB : ',HB,' LQ : ',LQ,' P>MaxCol : ',P>MaxCol);
  2231. end;
  2232. Result:=Result+Copy(L,1,P-1);
  2233. If Not HB then
  2234. Result:=Result+BreakStr;
  2235. Delete(L,1,P-1);
  2236. Len:=Length(L);
  2237. end;
  2238. end;
  2239. function WrapText(const Line: string; MaxCol: Integer): string;
  2240. begin
  2241. Result:=WrapText(Line,sLineBreak, [' ', '-', #9], MaxCol);
  2242. end;
  2243. {$ifndef FPC_NOGENERICANSIROUTINES}
  2244. {
  2245. Case Translation Tables
  2246. Can be used in internationalization support.
  2247. Although these tables can be obtained through system calls
  2248. cd it is better to not use those, since most implementation are not 100%
  2249. WARNING:
  2250. before modifying a translation table make sure that the current codepage
  2251. of the OS corresponds to the one you make changes to
  2252. }
  2253. const
  2254. { upper case translation table for character set 850 }
  2255. CP850UCT: array[128..255] of char =
  2256. (#128,#154,#144,#182,#142,#182,#143,#128,#210,#211,#212,#216,#215,#222,#142,#143,
  2257. #144,#146,#146,#226,#153,#227,#234,#235,'Y',#153,#154,#157,#156,#157,#158,#159,
  2258. #181,#214,#224,#233,#165,#165,#166,#167,#168,#169,#170,#171,#172,#173,#174,#175,
  2259. #176,#177,#178,#179,#180,#181,#182,#183,#184,#185,#186,#187,#188,#189,#190,#191,
  2260. #192,#193,#194,#195,#196,#197,#199,#199,#200,#201,#202,#203,#204,#205,#206,#207,
  2261. #208,#209,#210,#211,#212,#213,#214,#215,#216,#217,#218,#219,#220,#221,#222,#223,
  2262. #224,#225,#226,#227,#229,#229,#230,#237,#232,#233,#234,#235,#237,#237,#238,#239,
  2263. #240,#241,#242,#243,#244,#245,#246,#247,#248,#249,#250,#251,#252,#253,#254,#255);
  2264. { lower case translation table for character set 850 }
  2265. CP850LCT: array[128..255] of char =
  2266. (#135,#129,#130,#131,#132,#133,#134,#135,#136,#137,#138,#139,#140,#141,#132,#134,
  2267. #130,#145,#145,#147,#148,#149,#150,#151,#152,#148,#129,#155,#156,#155,#158,#159,
  2268. #160,#161,#162,#163,#164,#164,#166,#167,#168,#169,#170,#171,#172,#173,#174,#175,
  2269. #176,#177,#178,#179,#180,#160,#131,#133,#184,#185,#186,#187,#188,#189,#190,#191,
  2270. #192,#193,#194,#195,#196,#197,#198,#198,#200,#201,#202,#203,#204,#205,#206,#207,
  2271. #208,#209,#136,#137,#138,#213,#161,#140,#139,#217,#218,#219,#220,#221,#141,#223,
  2272. #162,#225,#147,#149,#228,#228,#230,#237,#232,#163,#150,#151,#236,#236,#238,#239,
  2273. #240,#241,#242,#243,#244,#245,#246,#247,#248,#249,#250,#251,#252,#253,#254,#255);
  2274. { upper case translation table for character set ISO 8859/1 Latin 1 }
  2275. CPISO88591UCT: array[192..255] of char =
  2276. ( #192, #193, #194, #195, #196, #197, #198, #199,
  2277. #200, #201, #202, #203, #204, #205, #206, #207,
  2278. #208, #209, #210, #211, #212, #213, #214, #215,
  2279. #216, #217, #218, #219, #220, #221, #222, #223,
  2280. #192, #193, #194, #195, #196, #197, #198, #199,
  2281. #200, #201, #202, #203, #204, #205, #206, #207,
  2282. #208, #209, #210, #211, #212, #213, #214, #247,
  2283. #216, #217, #218, #219, #220, #221, #222, #89 );
  2284. { lower case translation table for character set ISO 8859/1 Latin 1 }
  2285. CPISO88591LCT: array[192..255] of char =
  2286. ( #224, #225, #226, #227, #228, #229, #230, #231,
  2287. #232, #233, #234, #235, #236, #237, #238, #239,
  2288. #240, #241, #242, #243, #244, #245, #246, #215,
  2289. #248, #249, #250, #251, #252, #253, #254, #223,
  2290. #224, #225, #226, #227, #228, #229, #230, #231,
  2291. #232, #233, #234, #235, #236, #237, #238, #239,
  2292. #240, #241, #242, #243, #244, #245, #246, #247,
  2293. #248, #249, #250, #251, #252, #253, #254, #255 );
  2294. {$endif FPC_NOGENERICANSIROUTINES}
  2295. function sscanf(const s: string; const fmt : string;const Pointers : array of Pointer) : Integer;
  2296. var
  2297. i,j,n,m : SizeInt;
  2298. s1 : string;
  2299. function GetInt(unsigned : boolean=false) : Integer;
  2300. begin
  2301. s1 := '';
  2302. while (Length(s) > n) and (s[n] = ' ') do
  2303. inc(n);
  2304. { read sign }
  2305. if (Length(s)>= n) and (s[n] in ['+', '-']) then
  2306. begin
  2307. { don't accept - when reading unsigned }
  2308. if unsigned and (s[n]='-') then
  2309. begin
  2310. result:=length(s1);
  2311. exit;
  2312. end
  2313. else
  2314. begin
  2315. s1:=s1+s[n];
  2316. inc(n);
  2317. end;
  2318. end;
  2319. { read numbers }
  2320. while (Length(s) >= n) and
  2321. (s[n] in ['0'..'9']) do
  2322. begin
  2323. s1 := s1+s[n];
  2324. inc(n);
  2325. end;
  2326. Result := Length(s1);
  2327. end;
  2328. function GetFloat : Integer;
  2329. begin
  2330. s1 := '';
  2331. while (Length(s) > n) and (s[n] = ' ') do
  2332. inc(n);
  2333. while (Length(s) >= n) and
  2334. (s[n] in ['0'..'9', '+', '-', '.', 'e', 'E']) do
  2335. begin
  2336. s1 := s1+s[n];
  2337. inc(n);
  2338. end;
  2339. Result := Length(s1);
  2340. end;
  2341. function GetString : Integer;
  2342. begin
  2343. s1 := '';
  2344. while (Length(s) > n) and (s[n] = ' ') do
  2345. inc(n);
  2346. while (Length(s) >= n) and (s[n] <> ' ')do
  2347. begin
  2348. s1 := s1+s[n];
  2349. inc(n);
  2350. end;
  2351. Result := Length(s1);
  2352. end;
  2353. function ScanStr(c : Char) : Boolean;
  2354. begin
  2355. while (Length(s) > n) and (s[n] <> c) do
  2356. inc(n);
  2357. inc(n);
  2358. If (n <= Length(s)) then
  2359. Result := True
  2360. else
  2361. Result := False;
  2362. end;
  2363. function GetFmt : Integer;
  2364. begin
  2365. Result := -1;
  2366. while true do
  2367. begin
  2368. while (Length(fmt) > m) and (fmt[m] = ' ') do
  2369. inc(m);
  2370. if (m >= Length(fmt)) then
  2371. break;
  2372. if (fmt[m] = '%') then
  2373. begin
  2374. inc(m);
  2375. case fmt[m] of
  2376. 'd':
  2377. Result:=vtInteger;
  2378. {$ifndef FPUNONE}
  2379. 'f':
  2380. Result:=vtExtended;
  2381. {$endif}
  2382. 's':
  2383. Result:=vtString;
  2384. 'c':
  2385. Result:=vtChar;
  2386. else
  2387. raise EFormatError.CreateFmt(SInvalidFormat,[fmt]);
  2388. end;
  2389. inc(m);
  2390. break;
  2391. end;
  2392. if not(ScanStr(fmt[m])) then
  2393. break;
  2394. inc(m);
  2395. end;
  2396. end;
  2397. begin
  2398. n := 1;
  2399. m := 1;
  2400. Result := 0;
  2401. for i:=0 to High(Pointers) do
  2402. begin
  2403. j := GetFmt;
  2404. case j of
  2405. vtInteger :
  2406. begin
  2407. if GetInt>0 then
  2408. begin
  2409. pLongint(Pointers[i])^:=StrToInt(s1);
  2410. inc(Result);
  2411. end
  2412. else
  2413. break;
  2414. end;
  2415. vtchar :
  2416. begin
  2417. if Length(s)>n then
  2418. begin
  2419. pchar(Pointers[i])^:=s[n];
  2420. inc(n);
  2421. inc(Result);
  2422. end
  2423. else
  2424. break;
  2425. end;
  2426. {$ifndef FPUNONE}
  2427. vtExtended :
  2428. begin
  2429. if GetFloat>0 then
  2430. begin
  2431. pextended(Pointers[i])^:=StrToFloat(s1);
  2432. inc(Result);
  2433. end
  2434. else
  2435. break;
  2436. end;
  2437. {$endif}
  2438. vtString :
  2439. begin
  2440. if GetString > 0 then
  2441. begin
  2442. pansistring(Pointers[i])^:=s1;
  2443. inc(Result);
  2444. end
  2445. else
  2446. break;
  2447. end;
  2448. else
  2449. break;
  2450. end;
  2451. end;
  2452. end;