sysstr.inc 74 KB

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