sysstr.inc 70 KB

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