sysstr.inc 71 KB

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