sysstr.inc 75 KB

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