sysstr.inc 69 KB

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