sysstr.inc 75 KB

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