sysstr.inc 74 KB

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