sysstr.inc 77 KB

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