sysstr.inc 77 KB

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