sysstr.inc 72 KB

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