sysstr.inc 71 KB

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