sysstr.inc 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251
  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. UniqueString(Result);
  59. P:=Pchar(Result);
  60. for i := 1 to Length(Result) do
  61. begin
  62. if (P^ in ['a'..'z']) then P^ := char(byte(p^) - 32);
  63. Inc(P);
  64. end;
  65. end;
  66. { LowerCase returns a copy of S where all uppercase characters ( from A to Z )
  67. have been converted to lowercase }
  68. Function Lowercase(Const S : String) : String;
  69. Var
  70. i : Integer;
  71. P : PChar;
  72. begin
  73. Result := S;
  74. UniqueString(Result);
  75. P:=Pchar(Result);
  76. for i := 1 to Length(Result) do
  77. begin
  78. if (P^ in ['A'..'Z']) then P^ := char(byte(p^) + 32);
  79. Inc(P);
  80. end;
  81. end;
  82. { CompareStr compares S1 and S2, the result is the based on
  83. substraction of the ascii values of the characters in S1 and S2
  84. case result
  85. S1 < S2 < 0
  86. S1 > S2 > 0
  87. S1 = S2 = 0 }
  88. function CompareStr(const S1, S2: string): Integer;
  89. var count, count1, count2: integer;
  90. begin
  91. result := 0;
  92. Count1 := Length(S1);
  93. Count2 := Length(S2);
  94. if Count1>Count2 then
  95. Count:=Count2
  96. else
  97. Count:=Count1;
  98. result := CompareMemRange(Pointer(S1),Pointer(S2), Count);
  99. if result=0 then
  100. result:=Count1-Count2;
  101. end;
  102. { CompareMemRange returns the result of comparison of Length bytes at P1 and P2
  103. case result
  104. P1 < P2 < 0
  105. P1 > P2 > 0
  106. P1 = P2 = 0 }
  107. function CompareMemRange(P1, P2: Pointer; Length: cardinal): integer;
  108. var
  109. i: cardinal;
  110. begin
  111. i := 0;
  112. result := 0;
  113. while (result=0) and (I<length) do
  114. begin
  115. result:=byte(P1^)-byte(P2^);
  116. P1:=pchar(P1)+1; // VP compat.
  117. P2:=pchar(P2)+1;
  118. i := i + 1;
  119. end ;
  120. end ;
  121. function CompareMem(P1, P2: Pointer; Length: cardinal): Boolean;
  122. var
  123. i: cardinal;
  124. begin
  125. Result:=True;
  126. I:=0;
  127. If (P1)<>(P2) then
  128. While Result and (i<Length) do
  129. begin
  130. Result:=PByte(P1)^=PByte(P2)^;
  131. Inc(I);
  132. Inc(pchar(P1));
  133. Inc(pchar(P2));
  134. end;
  135. end;
  136. { CompareText compares S1 and S2, the result is the based on
  137. substraction of the ascii values of characters in S1 and S2
  138. comparison is case-insensitive
  139. case result
  140. S1 < S2 < 0
  141. S1 > S2 > 0
  142. S1 = S2 = 0 }
  143. function CompareText(const S1, S2: string): integer;
  144. var
  145. i, count, count1, count2: integer; Chr1, Chr2: byte;
  146. begin
  147. result := 0;
  148. Count1 := Length(S1);
  149. Count2 := Length(S2);
  150. if (Count1>Count2) then
  151. Count := Count2
  152. else
  153. Count := Count1;
  154. i := 0;
  155. while (result=0) and (i<count) do
  156. begin
  157. inc (i);
  158. Chr1 := byte(s1[i]);
  159. Chr2 := byte(s2[i]);
  160. if Chr1 in [97..122] then
  161. dec(Chr1,32);
  162. if Chr2 in [97..122] then
  163. dec(Chr2,32);
  164. result := Chr1 - Chr2;
  165. end ;
  166. if (result = 0) then
  167. result:=(count1-count2);
  168. end;
  169. function SameText(const s1,s2:String):Boolean;
  170. begin
  171. Result:=CompareText(S1,S2)=0;
  172. end;
  173. {==============================================================================}
  174. { Ansi string functions }
  175. { these functions rely on the character set loaded by the OS }
  176. {==============================================================================}
  177. function GenericAnsiUpperCase(const s: string): string;
  178. var
  179. len, i: integer;
  180. begin
  181. len := length(s);
  182. SetLength(result, len);
  183. for i := 1 to len do
  184. result[i] := UpperCaseTable[ord(s[i])];
  185. end;
  186. function GenericAnsiLowerCase(const s: string): string;
  187. var
  188. len, i: integer;
  189. begin
  190. len := length(s);
  191. SetLength(result, len);
  192. for i := 1 to len do
  193. result[i] := LowerCaseTable[ord(s[i])];
  194. end;
  195. function GenericAnsiCompareStr(const S1, S2: string): PtrInt;
  196. Var
  197. I,L1,L2 : SizeInt;
  198. begin
  199. Result:=0;
  200. L1:=Length(S1);
  201. L2:=Length(S2);
  202. I:=1;
  203. While (Result=0) and ((I<=L1) and (I<=L2)) do
  204. begin
  205. Result:=Ord(S1[I])-Ord(S2[I]); //!! Must be replaced by ansi characters !!
  206. Inc(I);
  207. end;
  208. If Result=0 Then
  209. Result:=L1-L2;
  210. end;
  211. function GenericAnsiCompareText(const S1, S2: string): PtrInt;
  212. Var
  213. I,L1,L2 : SizeInt;
  214. begin
  215. Result:=0;
  216. L1:=Length(S1);
  217. L2:=Length(S2);
  218. I:=1;
  219. While (Result=0) and ((I<=L1) and (I<=L2)) do
  220. begin
  221. Result:=Ord(LowerCaseTable[Ord(S1[I])])-Ord(LowerCaseTable[Ord(S2[I])]); //!! Must be replaced by ansi characters !!
  222. Inc(I);
  223. end;
  224. If Result=0 Then
  225. Result:=L1-L2;
  226. end;
  227. function AnsiSameText(const s1,s2:String):Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
  228. begin
  229. AnsiSameText:=AnsiCompareText(S1,S2)=0;
  230. end;
  231. function AnsiSameStr(const s1,s2:String):Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
  232. begin
  233. AnsiSameStr:=AnsiCompareStr(S1,S2)=0;
  234. end;
  235. function GenericAnsiStrComp(S1, S2: PChar): PtrInt;
  236. begin
  237. Result:=0;
  238. If S1=Nil then
  239. begin
  240. If S2=Nil Then Exit;
  241. result:=-1;
  242. exit;
  243. end;
  244. If S2=Nil then
  245. begin
  246. Result:=1;
  247. exit;
  248. end;
  249. Repeat
  250. Result:=Ord(S1[0])-Ord(S2[0]); //!! Must be replaced by ansi characters !!
  251. Inc(S1);
  252. Inc(S2);
  253. Until (Result<>0) or (S1^=#0) or (S2^=#0);
  254. if Result=0 then
  255. if s1=#0 then
  256. result:=1
  257. else
  258. result:=-1;
  259. end;
  260. function GenericAnsiStrIComp(S1, S2: PChar): PtrInt;
  261. begin
  262. Result:=0;
  263. If S1=Nil then
  264. begin
  265. If S2=Nil Then Exit;
  266. result:=-1;
  267. exit;
  268. end;
  269. If S2=Nil then
  270. begin
  271. Result:=1;
  272. exit;
  273. end;
  274. Repeat
  275. Result:=Ord(LowerCaseTable[Ord(S1[0])])-Ord(LowerCaseTable[Ord(S2[0])]); //!! Must be replaced by ansi characters !!
  276. Inc(S1);
  277. Inc(S2);
  278. Until (Result<>0) or ((S1[0]=#0) or (S2[0]=#0))
  279. end;
  280. function GenericAnsiStrLComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  281. Var I : cardinal;
  282. begin
  283. Result:=0;
  284. If MaxLen=0 then exit;
  285. If S1=Nil then
  286. begin
  287. If S2=Nil Then Exit;
  288. result:=-1;
  289. exit;
  290. end;
  291. If S2=Nil then
  292. begin
  293. Result:=1;
  294. exit;
  295. end;
  296. I:=0;
  297. Repeat
  298. Result:=Ord(S1[0])-Ord(S2[0]); //!! Must be replaced by ansi characters !!
  299. Inc(S1);
  300. Inc(S2);
  301. Inc(I);
  302. Until (Result<>0) or ((S1[0]=#0) or (S2[0]=#0)) or (I=MaxLen)
  303. end;
  304. function GenericAnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  305. Var I : cardinal;
  306. begin
  307. Result:=0;
  308. If MaxLen=0 then exit;
  309. If S1=Nil then
  310. begin
  311. If S2=Nil Then Exit;
  312. result:=-1;
  313. exit;
  314. end;
  315. If S2=Nil then
  316. begin
  317. Result:=1;
  318. exit;
  319. end;
  320. I:=0;
  321. Repeat
  322. Result:=Ord(LowerCaseTable[Ord(S1[0])])-Ord(LowerCaseTable[Ord(S2[0])]); //!! Must be replaced by ansi characters !!
  323. Inc(S1);
  324. Inc(S2);
  325. Inc(I);
  326. Until (Result<>0) or ((S1[0]=#0) or (S2[0]=#0)) or (I=MaxLen)
  327. end;
  328. function GenericAnsiStrLower(Str: PChar): PChar;
  329. begin
  330. result := Str;
  331. if Str <> Nil then begin
  332. while Str^ <> #0 do begin
  333. Str^ := LowerCaseTable[byte(Str^)];
  334. Str := Str + 1;
  335. end;
  336. end;
  337. end;
  338. function GenericAnsiStrUpper(Str: PChar): PChar;
  339. begin
  340. result := Str;
  341. if Str <> Nil then begin
  342. while Str^ <> #0 do begin
  343. Str^ := UpperCaseTable[byte(Str^)];
  344. Str := Str + 1;
  345. end ;
  346. end ;
  347. end ;
  348. function AnsiLastChar(const S: string): PChar;
  349. begin
  350. //!! No multibyte yet, so we return the last one.
  351. result:=StrEnd(Pchar(S));
  352. Dec(Result);
  353. end ;
  354. function AnsiStrLastChar(Str: PChar): PChar;
  355. begin
  356. //!! No multibyte yet, so we return the last one.
  357. result:=StrEnd(Str);
  358. Dec(Result);
  359. end ;
  360. function AnsiUpperCase(const s: string): string;{$ifdef SYSUTILSINLINE}inline;{$endif}
  361. begin
  362. result:=widestringmanager.UpperAnsiStringProc(s);
  363. end;
  364. function AnsiLowerCase(const s: string): string;{$ifdef SYSUTILSINLINE}inline;{$endif}
  365. begin
  366. result:=widestringmanager.LowerAnsiStringProc(s);
  367. end;
  368. function AnsiCompareStr(const S1, S2: string): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  369. begin
  370. result:=widestringmanager.CompareStrAnsiStringProc(s1,s2);
  371. end;
  372. function AnsiCompareText(const S1, S2: string): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  373. begin
  374. result:=widestringmanager.CompareTextAnsiStringProc(s1,s2);
  375. end;
  376. function AnsiStrComp(S1, S2: PChar): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  377. begin
  378. result:=widestringmanager.StrCompAnsiStringProc(s1,s2);
  379. end;
  380. function AnsiStrIComp(S1, S2: PChar): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  381. begin
  382. result:=widestringmanager.StrICompAnsiStringProc(s1,s2);
  383. end;
  384. function AnsiStrLComp(S1, S2: PChar; MaxLen: cardinal): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  385. begin
  386. result:=widestringmanager.StrLCompAnsiStringProc(s1,s2,maxlen);
  387. end;
  388. function AnsiStrLIComp(S1, S2: PChar; MaxLen: cardinal): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  389. begin
  390. result:=widestringmanager.StrLICompAnsiStringProc(s1,s2,maxlen);
  391. end;
  392. function AnsiStrLower(Str: PChar): PChar;{$ifdef SYSUTILSINLINE}inline;{$endif}
  393. begin
  394. result:=widestringmanager.StrLowerAnsiStringProc(Str);
  395. end;
  396. function AnsiStrUpper(Str: PChar): PChar;{$ifdef SYSUTILSINLINE}inline;{$endif}
  397. begin
  398. result:=widestringmanager.StrUpperAnsiStringProc(Str);
  399. end;
  400. {==============================================================================}
  401. { End of Ansi functions }
  402. {==============================================================================}
  403. { Trim returns a copy of S with blanks characters on the left and right stripped off }
  404. Const WhiteSpace = [#0..' '];
  405. function Trim(const S: string): string;
  406. var Ofs, Len: integer;
  407. begin
  408. len := Length(S);
  409. while (Len>0) and (S[Len] in WhiteSpace) do
  410. dec(Len);
  411. Ofs := 1;
  412. while (Ofs<=Len) and (S[Ofs] in WhiteSpace) do
  413. Inc(Ofs);
  414. result := Copy(S, Ofs, 1 + Len - Ofs);
  415. end ;
  416. { TrimLeft returns a copy of S with all blank characters on the left stripped off }
  417. function TrimLeft(const S: string): string;
  418. var i,l:integer;
  419. begin
  420. l := length(s);
  421. i := 1;
  422. while (i<=l) and (s[i] in whitespace) do
  423. inc(i);
  424. Result := copy(s, i, l);
  425. end ;
  426. { TrimRight returns a copy of S with all blank characters on the right stripped off }
  427. function TrimRight(const S: string): string;
  428. var l:integer;
  429. begin
  430. l := length(s);
  431. while (l>0) and (s[l] in whitespace) do
  432. dec(l);
  433. result := copy(s,1,l);
  434. end ;
  435. { QuotedStr returns S quoted left and right and every single quote in S
  436. replaced by two quotes }
  437. function QuotedStr(const S: string): string;
  438. begin
  439. result := AnsiQuotedStr(s, '''');
  440. end ;
  441. { AnsiQuotedStr returns S quoted left and right by Quote,
  442. and every single occurance of Quote replaced by two }
  443. function AnsiQuotedStr(const S: string; Quote: char): string;
  444. var i, j, count: integer;
  445. begin
  446. result := '' + Quote;
  447. count := length(s);
  448. i := 0;
  449. j := 0;
  450. while i < count do begin
  451. i := i + 1;
  452. if S[i] = Quote then begin
  453. result := result + copy(S, 1 + j, i - j) + Quote;
  454. j := i;
  455. end ;
  456. end ;
  457. if i <> j then
  458. result := result + copy(S, 1 + j, i - j);
  459. result := result + Quote;
  460. end ;
  461. { AnsiExtractQuotedStr returns a copy of Src with quote characters
  462. deleted to the left and right and double occurances
  463. of Quote replaced by a single Quote }
  464. function AnsiExtractQuotedStr(var Src: PChar; Quote: Char): string;
  465. var
  466. P,Q,R: PChar;
  467. begin
  468. P := Src;
  469. Q := StrEnd(P);
  470. result:='';
  471. if P=Q then exit;
  472. if P^<>quote then exit;
  473. inc(p);
  474. setlength(result,(Q-P)+1);
  475. R:=@Result[1];
  476. while P <> Q do
  477. begin
  478. R^:=P^;
  479. inc(R);
  480. if (P^ = Quote) then
  481. begin
  482. P := P + 1;
  483. if (p^ <> Quote) then
  484. begin
  485. dec(R);
  486. break;
  487. end;
  488. end;
  489. P := P + 1;
  490. end ;
  491. src:=p;
  492. SetLength(result, (R-pchar(@Result[1])));
  493. end ;
  494. { AdjustLineBreaks returns S with all CR characters not followed by LF
  495. replaced with CR/LF }
  496. // under Linux all CR characters or CR/LF combinations should be replaced with LF
  497. function AdjustLineBreaks(const S: string): string;
  498. begin
  499. Result:=AdjustLineBreaks(S,DefaultTextLineBreakStyle);
  500. end;
  501. function AdjustLineBreaks(const S: string; Style: TTextLineBreakStyle): string;
  502. var
  503. Source,Dest: PChar;
  504. DestLen: Integer;
  505. I,J,L: Longint;
  506. begin
  507. Source:=Pointer(S);
  508. L:=Length(S);
  509. DestLen:=L;
  510. I:=1;
  511. while (I<=L) do
  512. begin
  513. case S[i] of
  514. #10: if (Style=tlbsCRLF) then
  515. Inc(DestLen);
  516. #13: if (Style=tlbsCRLF) then
  517. if (I<L) and (S[i+1]=#10) then
  518. Inc(I)
  519. else
  520. Inc(DestLen)
  521. else if (I<L) and (S[I+1]=#10) then
  522. Dec(DestLen);
  523. end;
  524. Inc(I);
  525. end;
  526. if (DestLen=L) then
  527. Result:=S
  528. else
  529. begin
  530. SetLength(Result, DestLen);
  531. FillChar(Result[1],DestLen,0);
  532. Dest := Pointer(Result);
  533. J:=0;
  534. I:=0;
  535. While I<L do
  536. case Source[I] of
  537. #10: begin
  538. if Style=tlbsCRLF then
  539. begin
  540. Dest[j]:=#13;
  541. Inc(J);
  542. end;
  543. Dest[J] := #10;
  544. Inc(J);
  545. Inc(I);
  546. end;
  547. #13: begin
  548. if Style=tlbsCRLF then
  549. begin
  550. Dest[j] := #13;
  551. Inc(J);
  552. end;
  553. Dest[j]:=#10;
  554. Inc(J);
  555. Inc(I);
  556. if Source[I]=#10 then
  557. Inc(I);
  558. end;
  559. else
  560. Dest[j]:=Source[i];
  561. Inc(J);
  562. Inc(I);
  563. end;
  564. end;
  565. end;
  566. { IsValidIdent returns true if the first character of Ident is in:
  567. 'A' to 'Z', 'a' to 'z' or '_' and the following characters are
  568. on of: 'A' to 'Z', 'a' to 'z', '0'..'9' or '_' }
  569. function IsValidIdent(const Ident: string): boolean;
  570. var i, len: integer;
  571. begin
  572. result := false;
  573. len := length(Ident);
  574. if len <> 0 then begin
  575. result := Ident[1] in ['A'..'Z', 'a'..'z', '_'];
  576. i := 1;
  577. while (result) and (i < len) do begin
  578. i := i + 1;
  579. result := result and (Ident[i] in ['A'..'Z', 'a'..'z', '0'..'9', '_']);
  580. end ;
  581. end ;
  582. end ;
  583. { IntToStr returns a string representing the value of Value }
  584. function IntToStr(Value: integer): string;
  585. begin
  586. System.Str(Value, result);
  587. end ;
  588. function IntToStr(Value: int64): string;
  589. begin
  590. System.Str(Value, result);
  591. end ;
  592. function IntToStr(Value: QWord): string;
  593. begin
  594. System.Str(Value, result);
  595. end ;
  596. { IntToHex returns a string representing the hexadecimal value of Value }
  597. const
  598. HexDigits: array[0..15] of char = '0123456789ABCDEF';
  599. function IntToHex(Value: integer; Digits: integer): string;
  600. var i: integer;
  601. begin
  602. SetLength(result, digits);
  603. for i := 0 to digits - 1 do
  604. begin
  605. result[digits - i] := HexDigits[value and 15];
  606. value := value shr 4;
  607. end ;
  608. while value <> 0 do begin
  609. result := HexDigits[value and 15] + result;
  610. value := value shr 4;
  611. end;
  612. end ;
  613. function IntToHex(Value: int64; Digits: integer): string;
  614. var i: integer;
  615. begin
  616. SetLength(result, digits);
  617. for i := 0 to digits - 1 do
  618. begin
  619. result[digits - i] := HexDigits[value and 15];
  620. value := value shr 4;
  621. end ;
  622. while value <> 0 do begin
  623. result := HexDigits[value and 15] + result;
  624. value := value shr 4;
  625. end;
  626. end ;
  627. function TryStrToInt(const s: string; var i : integer) : boolean;
  628. var Error : word;
  629. begin
  630. Val(s, i, Error);
  631. TryStrToInt:=Error=0
  632. end;
  633. { StrToInt converts the string S to an integer value,
  634. if S does not represent a valid integer value EConvertError is raised }
  635. function StrToInt(const S: string): integer;
  636. var Error: word;
  637. begin
  638. Val(S, result, Error);
  639. if Error <> 0 then raise EConvertError.createfmt(SInvalidInteger,[S]);
  640. end ;
  641. function StrToInt64(const S: string): int64;
  642. var Error: word;
  643. begin
  644. Val(S, result, Error);
  645. if Error <> 0 then raise EConvertError.createfmt(SInvalidInteger,[S]);
  646. end;
  647. function TryStrToInt64(const s: string; var i : int64) : boolean;
  648. var Error : word;
  649. begin
  650. Val(s, i, Error);
  651. TryStrToInt64:=Error=0
  652. end;
  653. { StrToIntDef converts the string S to an integer value,
  654. Default is returned in case S does not represent a valid integer value }
  655. function StrToIntDef(const S: string; Default: integer): integer;
  656. var Error: word;
  657. begin
  658. Val(S, result, Error);
  659. if Error <> 0 then result := Default;
  660. end ;
  661. { StrToIntDef converts the string S to an integer value,
  662. Default is returned in case S does not represent a valid integer value }
  663. function StrToInt64Def(const S: string; Default: int64): int64;
  664. var Error: word;
  665. begin
  666. Val(S, result, Error);
  667. if Error <> 0 then result := Default;
  668. end ;
  669. { LoadStr returns the string resource Ident. }
  670. function LoadStr(Ident: integer): string;
  671. begin
  672. result:='';
  673. end ;
  674. { FmtLoadStr returns the string resource Ident and formats it accordingly }
  675. function FmtLoadStr(Ident: integer; const Args: array of const): string;
  676. begin
  677. result:='';
  678. end;
  679. Const
  680. feInvalidFormat = 1;
  681. feMissingArgument = 2;
  682. feInvalidArgIndex = 3;
  683. {$ifdef fmtdebug}
  684. Procedure Log (Const S: String);
  685. begin
  686. Writeln (S);
  687. end;
  688. {$endif}
  689. Procedure DoFormatError (ErrCode : Longint);
  690. Var
  691. S : String;
  692. begin
  693. //!! must be changed to contain format string...
  694. S:='';
  695. Case ErrCode of
  696. feInvalidFormat : raise EConvertError.Createfmt(SInvalidFormat,[s]);
  697. feMissingArgument : raise EConvertError.Createfmt(SArgumentMissing,[s]);
  698. feInvalidArgIndex : raise EConvertError.Createfmt(SInvalidArgIndex,[s]);
  699. end;
  700. end;
  701. { we've no templates, but with includes we can simulate this :) }
  702. {$macro on}
  703. {$define INFORMAT}
  704. {$define TFormatString:=ansistring}
  705. {$define TFormatChar:=char}
  706. Function Format (Const Fmt : AnsiString; const Args : Array of const) : AnsiString;
  707. {$i sysformt.inc}
  708. {$undef TFormatString}
  709. {$undef TFormatChar}
  710. {$undef INFORMAT}
  711. {$macro off}
  712. Function FormatBuf (Var Buffer; BufLen : Cardinal;
  713. Const Fmt; fmtLen : Cardinal;
  714. Const Args : Array of const) : Cardinal;
  715. Var S,F : String;
  716. begin
  717. Setlength(F,fmtlen);
  718. if fmtlen > 0 then
  719. Move(fmt,F[1],fmtlen);
  720. S:=Format (F,Args);
  721. If Cardinal(Length(S))<Buflen then
  722. Result:=Length(S)
  723. else
  724. Result:=Buflen;
  725. Move(S[1],Buffer,Result);
  726. end;
  727. Procedure FmtStr(Var Res: String; Const Fmt : String; Const args: Array of const);
  728. begin
  729. Res:=Format(fmt,Args);
  730. end;
  731. Function StrFmt(Buffer,Fmt : PChar; Const args: Array of const) : Pchar;
  732. begin
  733. Buffer[FormatBuf(Buffer^,Maxint,Fmt^,strlen(fmt),args)]:=#0;
  734. Result:=Buffer;
  735. end;
  736. Function StrLFmt(Buffer : PCHar; Maxlen : Cardinal;Fmt : PChar; Const args: Array of const) : Pchar;
  737. begin
  738. Buffer[FormatBuf(Buffer^,MaxLen,Fmt^,strlen(fmt),args)]:=#0;
  739. Result:=Buffer;
  740. end;
  741. Function StrToFloat(Const S: String): Extended;
  742. Begin
  743. If Not TextToFloat(Pchar(S),Result) then
  744. Raise EConvertError.createfmt(SInValidFLoat,[S]);
  745. End;
  746. function StrToFloatDef(const S: string; const Default: Extended): Extended;
  747. begin
  748. if not TextToFloat(PChar(S),Result,fvExtended) then
  749. Result:=Default;
  750. end;
  751. Function TextToFloat(Buffer: PChar; Var Value: Extended): Boolean;
  752. Var
  753. E,P : Integer;
  754. S : String;
  755. Begin
  756. S:=StrPas(Buffer);
  757. P:=Pos(DecimalSeparator,S);
  758. If (P<>0) Then
  759. S[P] := '.';
  760. Val(trim(S),Value,E);
  761. Result:=(E=0);
  762. End;
  763. Function TextToFloat(Buffer: PChar; Var Value; ValueType: TFloatValue): Boolean;
  764. Var
  765. E,P : Integer;
  766. S : String;
  767. Begin
  768. S:=StrPas(Buffer);
  769. P:=Pos(ThousandSeparator,S);
  770. While (P<>0) do
  771. begin
  772. Delete(S,P,1);
  773. P:=Pos(ThousandSeparator,S);
  774. end;
  775. P:=Pos(DecimalSeparator,S);
  776. If (P<>0) Then
  777. S[P] := '.';
  778. case ValueType of
  779. fvCurrency:
  780. Val(S,Currency(Value),E);
  781. fvExtended:
  782. Val(S,Extended(Value),E);
  783. fvDouble:
  784. Val(S,Double(Value),E);
  785. fvSingle:
  786. Val(S,Single(Value),E);
  787. fvComp:
  788. Val(S,Comp(Value),E);
  789. fvReal:
  790. Val(S,Real(Value),E);
  791. end;
  792. Result:=(E=0);
  793. End;
  794. Function TryStrToFloat(Const S : String; Var Value: Single): Boolean;
  795. Begin
  796. Result := TextToFloat(PChar(S), Value, fvSingle);
  797. End;
  798. Function TryStrToFloat(Const S : String; Var Value: Double): Boolean;
  799. Begin
  800. Result := TextToFloat(PChar(S), Value, fvDouble);
  801. End;
  802. {$ifdef FPC_HAS_TYPE_EXTENDED}
  803. Function TryStrToFloat(Const S : String; Var Value: Extended): Boolean;
  804. Begin
  805. Result := TextToFloat(PChar(S), Value);
  806. End;
  807. {$endif FPC_HAS_TYPE_EXTENDED}
  808. Function FloatToStr(Value: Extended): String;
  809. Begin
  810. Result := FloatToStrF(Value, ffGeneral, 15, 0);
  811. End;
  812. Function FloatToText(Buffer: PChar; Value: Extended; format: TFloatFormat; Precision, Digits: Integer): Longint;
  813. Var
  814. Tmp: String[40];
  815. Begin
  816. Tmp := FloatToStrF(Value, format, Precision, Digits);
  817. Result := Length(Tmp);
  818. Move(Tmp[1], Buffer[0], Result);
  819. End;
  820. Function FloatToStrF(Value: Extended; format: TFloatFormat; Precision, Digits: Integer): String;
  821. Var
  822. P: Integer;
  823. Negative, TooSmall, TooLarge: Boolean;
  824. Begin
  825. Case format Of
  826. ffGeneral:
  827. Begin
  828. If (Precision = -1) Or (Precision > 15) Then Precision := 15;
  829. TooSmall := (Abs(Value) < 0.00001) and (Value>0.0);
  830. If Not TooSmall Then
  831. Begin
  832. Str(Value:digits:precision, Result);
  833. P := Pos('.', Result);
  834. if P<>0 then
  835. Result[P] := DecimalSeparator;
  836. TooLarge := P > Precision + 1;
  837. End;
  838. If TooSmall Or TooLarge Then
  839. begin
  840. Result := FloatToStrF(Value, ffExponent, Precision, Digits);
  841. // Strip unneeded zeroes.
  842. P:=Pos('E',result)-1;
  843. If P<>-1 then
  844. While (P>1) and (Result[P]='0') do
  845. begin
  846. system.Delete(Result,P,1);
  847. Dec(P);
  848. end;
  849. end
  850. else if (P<>0) then // we have a decimalseparator
  851. begin
  852. P := Length(Result);
  853. While (P>0) and (Result[P] = '0') Do
  854. Dec(P);
  855. If (P>0) and (Result[P]=DecimalSeparator) Then
  856. Dec(P);
  857. SetLength(Result, P);
  858. end;
  859. End;
  860. ffExponent:
  861. Begin
  862. If (Precision = -1) Or (Precision > 15) Then Precision := 15;
  863. Str(Value:Precision + 8, Result);
  864. Result[3] := DecimalSeparator;
  865. P:=4;
  866. While (P>0) and (Digits < P) And (Result[Precision + 5] = '0') do
  867. Begin
  868. If P<>1 then
  869. system.Delete(Result, Precision + 5, 1)
  870. else
  871. system.Delete(Result, Precision + 3, 3);
  872. Dec(P);
  873. end;
  874. If Result[1] = ' ' Then
  875. System.Delete(Result, 1, 1);
  876. End;
  877. ffFixed:
  878. Begin
  879. If Digits = -1 Then Digits := 2
  880. Else If Digits > 18 Then Digits := 18;
  881. Str(Value:0:Digits, Result);
  882. If Result[1] = ' ' Then
  883. System.Delete(Result, 1, 1);
  884. P := Pos('.', Result);
  885. If P <> 0 Then Result[P] := DecimalSeparator;
  886. End;
  887. ffNumber:
  888. Begin
  889. If Digits = -1 Then Digits := 2
  890. Else If Digits > 15 Then Digits := 15;
  891. Str(Value:0:Digits, Result);
  892. If Result[1] = ' ' Then System.Delete(Result, 1, 1);
  893. P := Pos('.', Result);
  894. If P <> 0 Then
  895. Result[P] := DecimalSeparator
  896. else
  897. P := Length(Result)+1;
  898. Dec(P, 3);
  899. While (P > 1) Do
  900. Begin
  901. If Result[P - 1] <> '-' Then Insert(ThousandSeparator, Result, P);
  902. Dec(P, 3);
  903. End;
  904. End;
  905. ffCurrency:
  906. Begin
  907. If Value < 0 Then
  908. Begin
  909. Negative := True;
  910. Value := -Value;
  911. End
  912. Else Negative := False;
  913. If Digits = -1 Then Digits := CurrencyDecimals
  914. Else If Digits > 18 Then Digits := 18;
  915. Str(Value:0:Digits, Result);
  916. If Result[1] = ' ' Then System.Delete(Result, 1, 1);
  917. P := Pos('.', Result);
  918. If P <> 0 Then Result[P] := DecimalSeparator;
  919. Dec(P, 3);
  920. While (P > 1) Do
  921. Begin
  922. Insert(ThousandSeparator, Result, P);
  923. Dec(P, 3);
  924. End;
  925. If Not Negative Then
  926. Begin
  927. Case CurrencyFormat Of
  928. 0: Result := CurrencyString + Result;
  929. 1: Result := Result + CurrencyString;
  930. 2: Result := CurrencyString + ' ' + Result;
  931. 3: Result := Result + ' ' + CurrencyString;
  932. End
  933. End
  934. Else
  935. Begin
  936. Case NegCurrFormat Of
  937. 0: Result := '(' + CurrencyString + Result + ')';
  938. 1: Result := '-' + CurrencyString + Result;
  939. 2: Result := CurrencyString + '-' + Result;
  940. 3: Result := CurrencyString + Result + '-';
  941. 4: Result := '(' + Result + CurrencyString + ')';
  942. 5: Result := '-' + Result + CurrencyString;
  943. 6: Result := Result + '-' + CurrencyString;
  944. 7: Result := Result + CurrencyString + '-';
  945. 8: Result := '-' + Result + ' ' + CurrencyString;
  946. 9: Result := '-' + CurrencyString + ' ' + Result;
  947. 10: Result := CurrencyString + ' ' + Result + '-';
  948. End;
  949. End;
  950. End;
  951. End;
  952. End;
  953. Function FloatToDateTime (Const Value : Extended) : TDateTime;
  954. begin
  955. If (Value<MinDateTime) or (Value>MaxDateTime) then
  956. Raise EConvertError.CreateFmt (SInvalidDateTime,[Value]);
  957. Result:=Value;
  958. end;
  959. function TryFloatToCurr(const Value: Extended; var AResult: Currency): Boolean;
  960. begin
  961. Result:=(Value>=MinCurrency) and (Value<=MaxCurrency);
  962. if Result then
  963. AResult := Value;
  964. end;
  965. function FloatToCurr(const Value: Extended): Currency;
  966. begin
  967. if not TryFloatToCurr(Value, Result) then
  968. Raise EConvertError.CreateFmt(SInvalidCurrency, [FloatToStr(Value)]);
  969. end;
  970. Function CurrToStr(Value: Currency): string;
  971. begin
  972. Result:=FloatToStrF(Value,ffNumber,15,2);
  973. end;
  974. function StrToCurr(const S: string): Currency;
  975. begin
  976. if not TextToFloat(PChar(S), Result, fvCurrency) then
  977. Raise EConvertError.createfmt(SInValidFLoat,[S]);
  978. end;
  979. Function TryStrToCurr(Const S : String; Var Value: Currency): Boolean;
  980. Begin
  981. Result := TextToFloat(PChar(S), Value, fvCurrency);
  982. End;
  983. function StrToCurrDef(const S: string; Default : Currency): Currency;
  984. begin
  985. if not TextToFloat(PChar(S), Result, fvCurrency) then
  986. Result:=Default;
  987. end;
  988. function StrToBool(const S: string): Boolean;
  989. Var
  990. Temp : String;
  991. D : Double;
  992. Code: word;
  993. begin
  994. Temp:=upcase(S);
  995. Val(temp,D,code);
  996. If Code=0 then
  997. Result:=(D<>0.0)
  998. else If Temp='TRUE' then
  999. result:=true
  1000. else if Temp='FALSE' then
  1001. result:=false
  1002. else
  1003. Raise EConvertError.CreateFmt(SInvalidBoolean,[S]);
  1004. end;
  1005. function BoolToStr(B: Boolean): string;
  1006. begin
  1007. If B then
  1008. Result:='TRUE'
  1009. else
  1010. Result:='FALSE';
  1011. end;
  1012. Function FloatToTextFmt(Buffer: PChar; Value: Extended; format: PChar): Integer;
  1013. Var
  1014. Digits: String[40]; { String Of Digits }
  1015. Exponent: String[8]; { Exponent strin }
  1016. FmtStart, FmtStop: PChar; { Start And End Of relevant part }
  1017. { Of format String }
  1018. ExpFmt, ExpSize: Integer; { Type And Length Of }
  1019. { exponential format chosen }
  1020. Placehold: Array[1..4] Of Integer; { Number Of placeholders In All }
  1021. { four Sections }
  1022. thousand: Boolean; { thousand separators? }
  1023. UnexpectedDigits: Integer; { Number Of unexpected Digits that }
  1024. { have To be inserted before the }
  1025. { First placeholder. }
  1026. DigitExponent: Integer; { Exponent Of First digit In }
  1027. { Digits Array. }
  1028. { Find end of format section starting at P. False, if empty }
  1029. Function GetSectionEnd(Var P: PChar): Boolean;
  1030. Var
  1031. C: Char;
  1032. SQ, DQ: Boolean;
  1033. Begin
  1034. Result := False;
  1035. SQ := False;
  1036. DQ := False;
  1037. C := P[0];
  1038. While (C<>#0) And ((C<>';') Or SQ Or DQ) Do
  1039. Begin
  1040. Result := True;
  1041. Case C Of
  1042. #34: If Not SQ Then DQ := Not DQ;
  1043. #39: If Not DQ Then SQ := Not SQ;
  1044. End;
  1045. Inc(P);
  1046. C := P[0];
  1047. End;
  1048. End;
  1049. { Find start and end of format section to apply. If section doesn't exist,
  1050. use section 1. If section 2 is used, the sign of value is ignored. }
  1051. Procedure GetSectionRange(section: Integer);
  1052. Var
  1053. Sec: Array[1..3] Of PChar;
  1054. SecOk: Array[1..3] Of Boolean;
  1055. Begin
  1056. Sec[1] := format;
  1057. SecOk[1] := GetSectionEnd(Sec[1]);
  1058. If section > 1 Then
  1059. Begin
  1060. Sec[2] := Sec[1];
  1061. If Sec[2][0] <> #0 Then
  1062. Inc(Sec[2]);
  1063. SecOk[2] := GetSectionEnd(Sec[2]);
  1064. If section > 2 Then
  1065. Begin
  1066. Sec[3] := Sec[2];
  1067. If Sec[3][0] <> #0 Then
  1068. Inc(Sec[3]);
  1069. SecOk[3] := GetSectionEnd(Sec[3]);
  1070. End;
  1071. End;
  1072. If Not SecOk[1] Then
  1073. FmtStart := Nil
  1074. Else
  1075. Begin
  1076. If Not SecOk[section] Then
  1077. section := 1
  1078. Else If section = 2 Then
  1079. Value := -Value; { Remove sign }
  1080. If section = 1 Then FmtStart := format Else
  1081. Begin
  1082. FmtStart := Sec[section - 1];
  1083. Inc(FmtStart);
  1084. End;
  1085. FmtStop := Sec[section];
  1086. End;
  1087. End;
  1088. { Find format section ranging from FmtStart to FmtStop. }
  1089. Procedure GetFormatOptions;
  1090. Var
  1091. Fmt: PChar;
  1092. SQ, DQ: Boolean;
  1093. area: Integer;
  1094. Begin
  1095. SQ := False;
  1096. DQ := False;
  1097. Fmt := FmtStart;
  1098. ExpFmt := 0;
  1099. area := 1;
  1100. thousand := False;
  1101. Placehold[1] := 0;
  1102. Placehold[2] := 0;
  1103. Placehold[3] := 0;
  1104. Placehold[4] := 0;
  1105. While Fmt < FmtStop Do
  1106. Begin
  1107. Case Fmt[0] Of
  1108. #34:
  1109. Begin
  1110. If Not SQ Then
  1111. DQ := Not DQ;
  1112. Inc(Fmt);
  1113. End;
  1114. #39:
  1115. Begin
  1116. If Not DQ Then
  1117. SQ := Not SQ;
  1118. Inc(Fmt);
  1119. End;
  1120. Else
  1121. { This was 'if not SQ or DQ'. Looked wrong... }
  1122. If Not SQ Or DQ Then
  1123. Begin
  1124. Case Fmt[0] Of
  1125. '0':
  1126. Begin
  1127. Case area Of
  1128. 1:
  1129. area := 2;
  1130. 4:
  1131. Begin
  1132. area := 3;
  1133. Inc(Placehold[3], Placehold[4]);
  1134. Placehold[4] := 0;
  1135. End;
  1136. End;
  1137. Inc(Placehold[area]);
  1138. Inc(Fmt);
  1139. End;
  1140. '#':
  1141. Begin
  1142. If area=3 Then
  1143. area:=4;
  1144. Inc(Placehold[area]);
  1145. Inc(Fmt);
  1146. End;
  1147. '.':
  1148. Begin
  1149. If area<3 Then
  1150. area:=3;
  1151. Inc(Fmt);
  1152. End;
  1153. ',':
  1154. Begin
  1155. thousand := True;
  1156. Inc(Fmt);
  1157. End;
  1158. 'e', 'E':
  1159. If ExpFmt = 0 Then
  1160. Begin
  1161. If (Fmt[0]='E') Then
  1162. ExpFmt:=1
  1163. Else
  1164. ExpFmt := 3;
  1165. Inc(Fmt);
  1166. If (Fmt<FmtStop) Then
  1167. Begin
  1168. Case Fmt[0] Of
  1169. '+':
  1170. Begin
  1171. End;
  1172. '-':
  1173. Inc(ExpFmt);
  1174. Else
  1175. ExpFmt := 0;
  1176. End;
  1177. If ExpFmt <> 0 Then
  1178. Begin
  1179. Inc(Fmt);
  1180. ExpSize := 0;
  1181. While (Fmt<FmtStop) And
  1182. (ExpSize<4) And
  1183. (Fmt[0] In ['0'..'9']) Do
  1184. Begin
  1185. Inc(ExpSize);
  1186. Inc(Fmt);
  1187. End;
  1188. End;
  1189. End;
  1190. End
  1191. Else
  1192. Inc(Fmt);
  1193. Else { Case }
  1194. Inc(Fmt);
  1195. End; { Case }
  1196. End; { Begin }
  1197. End; { Case }
  1198. End; { While .. Begin }
  1199. End;
  1200. Procedure FloatToStr;
  1201. Var
  1202. I, J, Exp, Width, Decimals, DecimalPoint, len: Integer;
  1203. Begin
  1204. If ExpFmt = 0 Then
  1205. Begin
  1206. { Fixpoint }
  1207. Decimals:=Placehold[3]+Placehold[4];
  1208. Width:=Placehold[1]+Placehold[2]+Decimals;
  1209. If (Decimals=0) Then
  1210. Str(Value:Width:0,Digits)
  1211. Else
  1212. Str(Value:Width+1:Decimals,Digits);
  1213. len:=Length(Digits);
  1214. { Find the decimal point }
  1215. If (Decimals=0) Then
  1216. DecimalPoint:=len+1
  1217. Else
  1218. DecimalPoint:=len-Decimals;
  1219. { If value is very small, and no decimal places
  1220. are desired, remove the leading 0. }
  1221. If (Abs(Value) < 1) And (Placehold[2] = 0) Then
  1222. Begin
  1223. If (Placehold[1]=0) Then
  1224. Delete(Digits,DecimalPoint-1,1)
  1225. Else
  1226. Digits[DecimalPoint-1]:=' ';
  1227. End;
  1228. { Convert optional zeroes to spaces. }
  1229. I:=len;
  1230. J:=DecimalPoint+Placehold[3];
  1231. While (I>J) And (Digits[I]='0') Do
  1232. Begin
  1233. Digits[I] := ' ';
  1234. Dec(I);
  1235. End;
  1236. { If integer value and no obligatory decimal
  1237. places, remove decimal point. }
  1238. If (DecimalPoint < len) And (Digits[DecimalPoint + 1] = ' ') Then
  1239. Digits[DecimalPoint] := ' ';
  1240. { Convert spaces left from obligatory decimal point to zeroes. }
  1241. I:=DecimalPoint-Placehold[2];
  1242. While (I<DecimalPoint) And (Digits[I]=' ') Do
  1243. Begin
  1244. Digits[I] := '0';
  1245. Inc(I);
  1246. End;
  1247. Exp := 0;
  1248. End
  1249. Else
  1250. Begin
  1251. { Scientific: exactly <Width> Digits With <Precision> Decimals
  1252. And adjusted Exponent. }
  1253. If Placehold[1]+Placehold[2]=0 Then
  1254. Placehold[1]:=1;
  1255. Decimals := Placehold[3] + Placehold[4];
  1256. Width:=Placehold[1]+Placehold[2]+Decimals;
  1257. Str(Value:Width+8,Digits);
  1258. { Find and cut out exponent. Always the
  1259. last 6 characters in the string.
  1260. -> 0000E+0000 }
  1261. I:=Length(Digits)-5;
  1262. Val(Copy(Digits,I+1,5),Exp,J);
  1263. Exp:=Exp+1-(Placehold[1]+Placehold[2]);
  1264. Delete(Digits, I, 6);
  1265. { Str() always returns at least one digit after the decimal point.
  1266. If we don't want it, we have to remove it. }
  1267. If (Decimals=0) And (Placehold[1]+Placehold[2]<= 1) Then
  1268. Begin
  1269. If (Digits[4]>='5') Then
  1270. Begin
  1271. Inc(Digits[2]);
  1272. If (Digits[2]>'9') Then
  1273. Begin
  1274. Digits[2] := '1';
  1275. Inc(Exp);
  1276. End;
  1277. End;
  1278. Delete(Digits, 3, 2);
  1279. DecimalPoint := Length(Digits) + 1;
  1280. End
  1281. Else
  1282. Begin
  1283. { Move decimal point at the desired position }
  1284. Delete(Digits, 3, 1);
  1285. DecimalPoint:=2+Placehold[1]+Placehold[2];
  1286. If (Decimals<>0) Then
  1287. Insert('.',Digits,DecimalPoint);
  1288. End;
  1289. { Convert optional zeroes to spaces. }
  1290. I := Length(Digits);
  1291. J := DecimalPoint + Placehold[3];
  1292. While (I > J) And (Digits[I] = '0') Do
  1293. Begin
  1294. Digits[I] := ' ';
  1295. Dec(I);
  1296. End;
  1297. { If integer number and no obligatory decimal paces, remove decimal point }
  1298. If (DecimalPoint<Length(Digits)) And
  1299. (Digits[DecimalPoint+1]=' ') Then
  1300. Digits[DecimalPoint]:=' ';
  1301. If (Digits[1]=' ') Then
  1302. Begin
  1303. Delete(Digits, 1, 1);
  1304. Dec(DecimalPoint);
  1305. End;
  1306. { Calculate exponent string }
  1307. Str(Abs(Exp), Exponent);
  1308. While Length(Exponent)<ExpSize Do
  1309. Insert('0',Exponent,1);
  1310. If Exp >= 0 Then
  1311. Begin
  1312. If (ExpFmt In [1,3]) Then
  1313. Insert('+', Exponent, 1);
  1314. End
  1315. Else
  1316. Insert('-',Exponent,1);
  1317. If (ExpFmt<3) Then
  1318. Insert('E',Exponent,1)
  1319. Else
  1320. Insert('e',Exponent,1);
  1321. End;
  1322. DigitExponent:=DecimalPoint-2;
  1323. If (Digits[1]='-') Then
  1324. Dec(DigitExponent);
  1325. UnexpectedDigits:=DecimalPoint-1-(Placehold[1]+Placehold[2]);
  1326. End;
  1327. Function PutResult: LongInt;
  1328. Var
  1329. SQ, DQ: Boolean;
  1330. Fmt, Buf: PChar;
  1331. Dig, N: Integer;
  1332. Begin
  1333. SQ := False;
  1334. DQ := False;
  1335. Fmt := FmtStart;
  1336. Buf := Buffer;
  1337. Dig := 1;
  1338. While (Fmt<FmtStop) Do
  1339. Begin
  1340. //Write(Fmt[0]);
  1341. Case Fmt[0] Of
  1342. #34:
  1343. Begin
  1344. If Not SQ Then
  1345. DQ := Not DQ;
  1346. Inc(Fmt);
  1347. End;
  1348. #39:
  1349. Begin
  1350. If Not DQ Then
  1351. SQ := Not SQ;
  1352. Inc(Fmt);
  1353. End;
  1354. Else
  1355. If Not (SQ Or DQ) Then
  1356. Begin
  1357. Case Fmt[0] Of
  1358. '0', '#', '.':
  1359. Begin
  1360. If (Dig=1) And (UnexpectedDigits>0) Then
  1361. Begin
  1362. { Everything unexpected is written before the first digit }
  1363. For N := 1 To UnexpectedDigits Do
  1364. Begin
  1365. Buf[0] := Digits[N];
  1366. Inc(Buf);
  1367. If thousand And (Digits[N]<>'-') Then
  1368. Begin
  1369. If (DigitExponent Mod 3 = 0) And (DigitExponent>0) Then
  1370. Begin
  1371. Buf[0] := ThousandSeparator;
  1372. Inc(Buf);
  1373. End;
  1374. Dec(DigitExponent);
  1375. End;
  1376. End;
  1377. Inc(Dig, UnexpectedDigits);
  1378. End;
  1379. If (Digits[Dig]<>' ') Then
  1380. Begin
  1381. If (Digits[Dig]='.') Then
  1382. Buf[0] := DecimalSeparator
  1383. Else
  1384. Buf[0] := Digits[Dig];
  1385. Inc(Buf);
  1386. If thousand And (DigitExponent Mod 3 = 0) And (DigitExponent > 0) Then
  1387. Begin
  1388. Buf[0] := ThousandSeparator;
  1389. Inc(Buf);
  1390. End;
  1391. End;
  1392. Inc(Dig);
  1393. Dec(DigitExponent);
  1394. Inc(Fmt);
  1395. End;
  1396. 'e', 'E':
  1397. Begin
  1398. If ExpFmt <> 0 Then
  1399. Begin
  1400. Inc(Fmt);
  1401. If Fmt < FmtStop Then
  1402. Begin
  1403. If Fmt[0] In ['+', '-'] Then
  1404. Begin
  1405. Inc(Fmt, ExpSize);
  1406. For N:=1 To Length(Exponent) Do
  1407. Buf[N-1] := Exponent[N];
  1408. Inc(Buf,Length(Exponent));
  1409. ExpFmt:=0;
  1410. End;
  1411. Inc(Fmt);
  1412. End;
  1413. End
  1414. Else
  1415. Begin
  1416. { No legal exponential format.
  1417. Simply write the 'E' to the result. }
  1418. Buf[0] := Fmt[0];
  1419. Inc(Buf);
  1420. Inc(Fmt);
  1421. End;
  1422. End;
  1423. Else { Case }
  1424. { Usual character }
  1425. If (Fmt[0]<>',') Then
  1426. Begin
  1427. Buf[0] := Fmt[0];
  1428. Inc(Buf);
  1429. End;
  1430. Inc(Fmt);
  1431. End; { Case }
  1432. End
  1433. Else { IF }
  1434. Begin
  1435. { Character inside single or double quotes }
  1436. Buf[0] := Fmt[0];
  1437. Inc(Buf);
  1438. Inc(Fmt);
  1439. End;
  1440. End; { Case }
  1441. End; { While .. Begin }
  1442. Result:=PtrInt(Buf)-PtrInt(Buffer);
  1443. End;
  1444. Begin
  1445. If (Value>0) Then
  1446. GetSectionRange(1)
  1447. Else If (Value<0) Then
  1448. GetSectionRange(2)
  1449. Else
  1450. GetSectionRange(3);
  1451. If FmtStart = Nil Then
  1452. Begin
  1453. Result := FloatToText(Buffer, Value, ffGeneral, 15, 4);
  1454. End
  1455. Else
  1456. Begin
  1457. GetFormatOptions;
  1458. If (ExpFmt = 0) And (Abs(Value) >= 1E18) Then
  1459. Result := FloatToText(Buffer, Value, ffGeneral, 15, 4)
  1460. Else
  1461. Begin
  1462. FloatToStr;
  1463. Result := PutResult;
  1464. End;
  1465. End;
  1466. End;
  1467. Procedure FloatToDecimal(Var Result: TFloatRec; Value: Extended; Precision, Decimals : integer);
  1468. Var
  1469. Buffer: String[24];
  1470. Error, N: Integer;
  1471. Begin
  1472. Str(Value:23, Buffer);
  1473. Result.Negative := (Buffer[1] = '-');
  1474. Val(Copy(Buffer, 19, 5), Result.Exponent, Error);
  1475. Inc(Result. Exponent);
  1476. Result.Digits[0] := Buffer[2];
  1477. Move(Buffer[4], Result.Digits[1], 14);
  1478. If Decimals + Result.Exponent < Precision Then
  1479. N := Decimals + Result.Exponent
  1480. Else
  1481. N := Precision;
  1482. If N > 15 Then
  1483. N := 15;
  1484. If N = 0 Then
  1485. Begin
  1486. If Result.Digits[0] >= '5' Then
  1487. Begin
  1488. Result.Digits[0] := '1';
  1489. Result.Digits[1] := #0;
  1490. Inc(Result.Exponent);
  1491. End
  1492. Else
  1493. Result.Digits[0] := #0;
  1494. End
  1495. Else If N > 0 Then
  1496. Begin
  1497. If Result.Digits[N] >= '5' Then
  1498. Begin
  1499. Repeat
  1500. Result.Digits[N] := #0;
  1501. Dec(N);
  1502. Inc(Result.Digits[N]);
  1503. Until (N = 0) Or (Result.Digits[N] < ':');
  1504. If Result.Digits[0] = ':' Then
  1505. Begin
  1506. Result.Digits[0] := '1';
  1507. Inc(Result.Exponent);
  1508. End;
  1509. End
  1510. Else
  1511. Begin
  1512. Result.Digits[N] := '0';
  1513. While (Result.Digits[N] = '0') And (N > -1) Do
  1514. Begin
  1515. Result.Digits[N] := #0;
  1516. Dec(N);
  1517. End;
  1518. End;
  1519. End
  1520. Else
  1521. Result.Digits[0] := #0;
  1522. If Result.Digits[0] = #0 Then
  1523. Begin
  1524. Result.Exponent := 0;
  1525. Result.Negative := False;
  1526. End;
  1527. End;
  1528. Function FormatFloat(Const format: String; Value: Extended): String;
  1529. Var
  1530. buf : Array[0..1024] of char;
  1531. Begin
  1532. Buf[FloatToTextFmt(@Buf[0],Value,Pchar(Format))]:=#0;
  1533. Result:=StrPas(@Buf);
  1534. End;
  1535. function FormatCurr(const Format: string; Value: Currency): string;
  1536. begin
  1537. Result := FormatFloat(Format, Value);
  1538. end;
  1539. {==============================================================================}
  1540. { extra functions }
  1541. {==============================================================================}
  1542. { LeftStr returns Count left-most characters from S }
  1543. function LeftStr(const S: string; Count: integer): string;
  1544. begin
  1545. result := Copy(S, 1, Count);
  1546. end ;
  1547. { RightStr returns Count right-most characters from S }
  1548. function RightStr(const S: string; Count: integer): string;
  1549. begin
  1550. If Count>Length(S) then
  1551. Count:=Length(S);
  1552. result := Copy(S, 1 + Length(S) - Count, Count);
  1553. end;
  1554. { BCDToInt converts the BCD value Value to an integer }
  1555. function BCDToInt(Value: integer): integer;
  1556. var i, j: integer;
  1557. begin
  1558. result := 0;
  1559. j := 1;
  1560. for i := 0 to SizeOf(Value) shr 1 - 1 do begin
  1561. result := result + j * (Value and 15);
  1562. j := j * 10;
  1563. Value := Value shr 4;
  1564. end ;
  1565. end ;
  1566. Function LastDelimiter(const Delimiters, S: string): Integer;
  1567. begin
  1568. Result:=Length(S);
  1569. While (Result>0) and (Pos(S[Result],Delimiters)=0) do
  1570. Dec(Result);
  1571. end;
  1572. Function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;
  1573. var
  1574. Srch,OldP,RemS: string; // Srch and Oldp can contain uppercase versions of S,OldPattern
  1575. P : Integer;
  1576. begin
  1577. Srch:=S;
  1578. OldP:=OldPattern;
  1579. if rfIgnoreCase in Flags then
  1580. begin
  1581. Srch:=AnsiUpperCase(Srch);
  1582. OldP:=AnsiUpperCase(OldP);
  1583. end;
  1584. RemS:=S;
  1585. Result:='';
  1586. while (Length(Srch)<>0) do
  1587. begin
  1588. P:=AnsiPos(OldP, Srch);
  1589. if P=0 then
  1590. begin
  1591. Result:=Result+RemS;
  1592. Srch:='';
  1593. end
  1594. else
  1595. begin
  1596. Result:=Result+Copy(RemS,1,P-1)+NewPattern;
  1597. P:=P+Length(OldP);
  1598. RemS:=Copy(RemS,P,Length(RemS)-P+1);
  1599. if not (rfReplaceAll in Flags) then
  1600. begin
  1601. Result:=Result+RemS;
  1602. Srch:='';
  1603. end
  1604. else
  1605. Srch:=Copy(Srch,P,Length(Srch)-P+1);
  1606. end;
  1607. end;
  1608. end;
  1609. Function IsDelimiter(const Delimiters, S: string; Index: Integer): Boolean;
  1610. begin
  1611. Result:=False;
  1612. If (Index>0) and (Index<=Length(S)) then
  1613. Result:=Pos(S[Index],Delimiters)<>0; // Note we don't do MBCS yet
  1614. end;
  1615. Function ByteToCharLen(const S: string; MaxLen: Integer): Integer;
  1616. begin
  1617. Result:=Length(S);
  1618. If Result>MaxLen then
  1619. Result:=MaxLen;
  1620. end;
  1621. Function ByteToCharIndex(const S: string; Index: Integer): Integer;
  1622. begin
  1623. Result:=Index;
  1624. end;
  1625. Function CharToByteLen(const S: string; MaxLen: Integer): Integer;
  1626. begin
  1627. Result:=Length(S);
  1628. If Result>MaxLen then
  1629. Result:=MaxLen;
  1630. end;
  1631. Function CharToByteIndex(const S: string; Index: Integer): Integer;
  1632. begin
  1633. Result:=Index;
  1634. end;
  1635. Function ByteType(const S: string; Index: Integer): TMbcsByteType;
  1636. begin
  1637. Result:=mbSingleByte;
  1638. end;
  1639. Function StrByteType(Str: PChar; Index: Cardinal): TMbcsByteType;
  1640. begin
  1641. Result:=mbSingleByte;
  1642. end;
  1643. Function StrCharLength(const Str: PChar): Integer;
  1644. begin
  1645. result:=widestringmanager.CharLengthPCharProc(Str);
  1646. end;
  1647. Function FindCmdLineSwitch(const Switch: string; const Chars: TSysCharSet;IgnoreCase: Boolean): Boolean;
  1648. Var
  1649. I,L : Integer;
  1650. S,T : String;
  1651. begin
  1652. Result:=False;
  1653. S:=Switch;
  1654. If IgnoreCase then
  1655. S:=UpperCase(S);
  1656. I:=ParamCount;
  1657. While (Not Result) and (I>0) do
  1658. begin
  1659. L:=Length(Paramstr(I));
  1660. If (L>0) and (ParamStr(I)[1] in Chars) then
  1661. begin
  1662. T:=Copy(ParamStr(I),2,L-1);
  1663. If IgnoreCase then
  1664. T:=UpperCase(T);
  1665. Result:=S=T;
  1666. end;
  1667. Dec(i);
  1668. end;
  1669. end;
  1670. Function FindCmdLineSwitch(const Switch: string; IgnoreCase: Boolean): Boolean;
  1671. begin
  1672. Result:=FindCmdLineSwitch(Switch,SwitchChars,IgnoreCase);
  1673. end;
  1674. Function FindCmdLineSwitch(const Switch: string): Boolean;
  1675. begin
  1676. Result:=FindCmdLineSwitch(Switch,SwitchChars,False);
  1677. end;
  1678. function WrapText(const Line, BreakStr: string; const BreakChars: TSysCharSet; MaxCol: Integer): string;
  1679. const
  1680. Quotes = ['''', '"'];
  1681. Var
  1682. L : String;
  1683. C,LQ,BC : Char;
  1684. P,BLen,Len : Integer;
  1685. HB,IBC : Boolean;
  1686. begin
  1687. Result:='';
  1688. L:=Line;
  1689. Blen:=Length(BreakStr);
  1690. If (BLen>0) then
  1691. BC:=BreakStr[1]
  1692. else
  1693. BC:=#0;
  1694. Len:=Length(L);
  1695. While (Len>0) do
  1696. begin
  1697. P:=1;
  1698. LQ:=#0;
  1699. HB:=False;
  1700. IBC:=False;
  1701. While ((P<=Len) and ((P<=MaxCol) or not IBC)) and ((LQ<>#0) or Not HB) do
  1702. begin
  1703. C:=L[P];
  1704. If (C=LQ) then
  1705. LQ:=#0
  1706. else If (C in Quotes) then
  1707. LQ:=C;
  1708. If (LQ<>#0) then
  1709. Inc(P)
  1710. else
  1711. begin
  1712. HB:=((C=BC) and (BreakStr=Copy(L,P,BLen)));
  1713. If HB then
  1714. Inc(P,Blen)
  1715. else
  1716. begin
  1717. If (P>MaxCol) then
  1718. IBC:=C in BreakChars;
  1719. Inc(P);
  1720. end;
  1721. end;
  1722. // Writeln('"',C,'" : IBC : ',IBC,' HB : ',HB,' LQ : ',LQ,' P>MaxCol : ',P>MaxCol);
  1723. end;
  1724. Result:=Result+Copy(L,1,P-1);
  1725. If Not HB then
  1726. Result:=Result+BreakStr;
  1727. Delete(L,1,P-1);
  1728. Len:=Length(L);
  1729. end;
  1730. end;
  1731. function WrapText(const Line: string; MaxCol: Integer): string;
  1732. begin
  1733. Result:=WrapText(Line,sLineBreak, [' ', '-', #9], MaxCol);
  1734. end;
  1735. {
  1736. Case Translation Tables
  1737. Can be used in internationalization support.
  1738. Although these tables can be obtained through system calls
  1739. it is better to not use those, since most implementation are not 100%
  1740. WARNING:
  1741. before modifying a translation table make sure that the current codepage
  1742. of the OS corresponds to the one you make changes to
  1743. }
  1744. const
  1745. { upper case translation table for character set 850 }
  1746. CP850UCT: array[128..255] of char =
  1747. ('€', 'š', '�', '¶', 'Ž', '¶', '�', '€', 'Ò', 'Ó', 'Ô', 'Ø', '×', 'Þ', 'Ž', '�',
  1748. '�', '’', '’', 'â', '™', 'ã', 'ê', 'ë', 'Y', '™', 'š', '�', 'œ', '�', 'ž', 'Ÿ',
  1749. 'µ', 'Ö', 'à', 'é', '¥', '¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯',
  1750. '°', '±', '²', '³', '´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿',
  1751. 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Ç', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï',
  1752. 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', '×', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß',
  1753. 'à', 'á', 'â', 'ã', 'å', 'å', 'æ', 'í', 'è', 'é', 'ê', 'ë', 'í', 'í', 'î', 'ï',
  1754. 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ');
  1755. { lower case translation table for character set 850 }
  1756. CP850LCT: array[128..255] of char =
  1757. ('‡', '�', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '�', '„', '†',
  1758. '‚', '‘', '‘', '“', '”', '•', '–', '—', '˜', '”', '�', '›', 'œ', '›', 'ž', 'Ÿ',
  1759. ' ', '¡', '¢', '£', '¤', '¤', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯',
  1760. '°', '±', '²', '³', '´', ' ', 'ƒ', '…', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿',
  1761. 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Æ', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï',
  1762. 'Ð', 'Ñ', 'ˆ', '‰', 'Š', 'Õ', '¡', 'Œ', '‹', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', '�', 'ß',
  1763. '¢', 'á', '“', '•', 'ä', 'ä', 'æ', 'í', 'è', '£', '–', '—', 'ì', 'ì', 'î', 'ï',
  1764. 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ');
  1765. { upper case translation table for character set ISO 8859/1 Latin 1 }
  1766. CPISO88591UCT: array[192..255] of char =
  1767. ( #192, #193, #194, #195, #196, #197, #198, #199,
  1768. #200, #201, #202, #203, #204, #205, #206, #207,
  1769. #208, #209, #210, #211, #212, #213, #214, #215,
  1770. #216, #217, #218, #219, #220, #221, #222, #223,
  1771. #192, #193, #194, #195, #196, #197, #198, #199,
  1772. #200, #201, #202, #203, #204, #205, #206, #207,
  1773. #208, #209, #210, #211, #212, #213, #214, #247,
  1774. #216, #217, #218, #219, #220, #221, #222, #89 );
  1775. { lower case translation table for character set ISO 8859/1 Latin 1 }
  1776. CPISO88591LCT: array[192..255] of char =
  1777. ( #224, #225, #226, #227, #228, #229, #230, #231,
  1778. #232, #233, #234, #235, #236, #237, #238, #239,
  1779. #240, #241, #242, #243, #244, #245, #246, #215,
  1780. #248, #249, #250, #251, #252, #253, #254, #223,
  1781. #224, #225, #226, #227, #228, #229, #230, #231,
  1782. #232, #233, #234, #235, #236, #237, #238, #239,
  1783. #240, #241, #242, #243, #244, #245, #246, #247,
  1784. #248, #249, #250, #251, #252, #253, #254, #255 );
  1785. function sscanf(const s: string; const fmt : string;const Pointers : array of Pointer) : Integer;
  1786. var
  1787. i,j,n,m : SizeInt;
  1788. s1 : string;
  1789. function GetInt(unsigned : boolean=false) : Integer;
  1790. begin
  1791. s1 := '';
  1792. while (s[n] = ' ') and (Length(s) > n) do
  1793. inc(n);
  1794. { read sign }
  1795. if (Length(s)>= n) and (s[n] in ['+', '-']) then
  1796. begin
  1797. { don't accept - when reading unsigned }
  1798. if unsigned and (s[n]='-') then
  1799. begin
  1800. result:=length(s1);
  1801. exit;
  1802. end
  1803. else
  1804. begin
  1805. s1:=s1+s[n];
  1806. inc(n);
  1807. end;
  1808. end;
  1809. { read numbers }
  1810. while (s[n] in ['0'..'9'])
  1811. and (Length(s) >= n) do
  1812. begin
  1813. s1 := s1+s[n];
  1814. inc(n);
  1815. end;
  1816. Result := Length(s1);
  1817. end;
  1818. function GetFloat : Integer;
  1819. begin
  1820. s1 := '';
  1821. while (s[n] = ' ') and (Length(s) > n) do
  1822. inc(n);
  1823. while (s[n] in ['0'..'9', '+', '-', '.', 'e', 'E'])
  1824. and (Length(s) >= n) do
  1825. begin
  1826. s1 := s1+s[n];
  1827. inc(n);
  1828. end;
  1829. Result := Length(s1);
  1830. end;
  1831. function GetString : Integer;
  1832. begin
  1833. s1 := '';
  1834. while (s[n] = ' ') and (Length(s) > n) do
  1835. inc(n);
  1836. while (s[n] <> ' ') and (Length(s) >= n) do
  1837. begin
  1838. s1 := s1+s[n];
  1839. inc(n);
  1840. end;
  1841. Result := Length(s1);
  1842. end;
  1843. function ScanStr(c : Char) : Boolean;
  1844. begin
  1845. while (s[n] <> c) and (Length(s) > n) do
  1846. inc(n);
  1847. inc(n);
  1848. If (n <= Length(s)) then
  1849. Result := True
  1850. else
  1851. Result := False;
  1852. end;
  1853. function GetFmt : Integer;
  1854. begin
  1855. Result := -1;
  1856. while true do
  1857. begin
  1858. while (fmt[m] = ' ') and (Length(fmt) > m) do
  1859. inc(m);
  1860. if (m >= Length(fmt)) then
  1861. break;
  1862. if (fmt[m] = '%') then
  1863. begin
  1864. inc(m);
  1865. case fmt[m] of
  1866. 'd':
  1867. Result:=vtInteger;
  1868. 'f':
  1869. Result:=vtExtended;
  1870. 's':
  1871. Result:=vtString;
  1872. 'c':
  1873. Result:=vtChar;
  1874. else
  1875. raise EFormatError.CreateFmt(SInvalidFormat,[fmt]);
  1876. end;
  1877. inc(m);
  1878. break;
  1879. end;
  1880. if not(ScanStr(fmt[m])) then
  1881. break;
  1882. inc(m);
  1883. end;
  1884. end;
  1885. begin
  1886. n := 1;
  1887. m := 1;
  1888. Result := 0;
  1889. for i:=0 to High(Pointers) do
  1890. begin
  1891. j := GetFmt;
  1892. case j of
  1893. vtInteger :
  1894. begin
  1895. if GetInt>0 then
  1896. begin
  1897. plongint(Pointers[i])^:=StrToInt(s1);
  1898. inc(Result);
  1899. end
  1900. else
  1901. break;
  1902. end;
  1903. vtchar :
  1904. begin
  1905. if Length(s)>n then
  1906. begin
  1907. pchar(Pointers[i])^:=s[n];
  1908. inc(n);
  1909. inc(Result);
  1910. end
  1911. else
  1912. break;
  1913. end;
  1914. vtExtended :
  1915. begin
  1916. if GetFloat>0 then
  1917. begin
  1918. pextended(Pointers[i])^:=StrToFloat(s1);
  1919. inc(Result);
  1920. end
  1921. else
  1922. break;
  1923. end;
  1924. vtString :
  1925. begin
  1926. if GetString > 0 then
  1927. begin
  1928. pansistring(Pointers[i])^:=s1;
  1929. inc(Result);
  1930. end
  1931. else
  1932. break;
  1933. end;
  1934. else
  1935. break;
  1936. end;
  1937. end;
  1938. end;