sysstr.inc 77 KB

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