sysstr.inc 72 KB

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