sysstr.inc 73 KB

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