sysstr.inc 71 KB

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