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