sysstr.inc 71 KB

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