sysstr.inc 78 KB

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