sysstr.inc 71 KB

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