sysstr.inc 73 KB

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