sysstr.inc 72 KB

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