sysstr.inc 72 KB

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