sysstr.inc 71 KB

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