sysstr.inc 70 KB

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