sysstr.inc 71 KB

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