2
0

sysstr.inc 71 KB

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