sysstr.inc 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256
  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 CurrToStrF(Value: Currency; Format: TFloatFormat; Digits: Integer): string;
  954. begin
  955. result:=FloatToStrF(Value,Format,19,Digits);
  956. end;
  957. Function FloatToDateTime (Const Value : Extended) : TDateTime;
  958. begin
  959. If (Value<MinDateTime) or (Value>MaxDateTime) then
  960. Raise EConvertError.CreateFmt (SInvalidDateTime,[Value]);
  961. Result:=Value;
  962. end;
  963. function TryFloatToCurr(const Value: Extended; var AResult: Currency): Boolean;
  964. begin
  965. Result:=(Value>=MinCurrency) and (Value<=MaxCurrency);
  966. if Result then
  967. AResult := Value;
  968. end;
  969. function FloatToCurr(const Value: Extended): Currency;
  970. begin
  971. if not TryFloatToCurr(Value, Result) then
  972. Raise EConvertError.CreateFmt(SInvalidCurrency, [FloatToStr(Value)]);
  973. end;
  974. Function CurrToStr(Value: Currency): string;
  975. begin
  976. Result:=FloatToStrF(Value,ffNumber,15,2);
  977. end;
  978. function StrToCurr(const S: string): Currency;
  979. begin
  980. if not TextToFloat(PChar(S), Result, fvCurrency) then
  981. Raise EConvertError.createfmt(SInValidFLoat,[S]);
  982. end;
  983. Function TryStrToCurr(Const S : String; Var Value: Currency): Boolean;
  984. Begin
  985. Result := TextToFloat(PChar(S), Value, fvCurrency);
  986. End;
  987. function StrToCurrDef(const S: string; Default : Currency): Currency;
  988. begin
  989. if not TextToFloat(PChar(S), Result, fvCurrency) then
  990. Result:=Default;
  991. end;
  992. function StrToBool(const S: string): Boolean;
  993. Var
  994. Temp : String;
  995. D : Double;
  996. Code: word;
  997. begin
  998. Temp:=upcase(S);
  999. Val(temp,D,code);
  1000. If Code=0 then
  1001. Result:=(D<>0.0)
  1002. else If Temp='TRUE' then
  1003. result:=true
  1004. else if Temp='FALSE' then
  1005. result:=false
  1006. else
  1007. Raise EConvertError.CreateFmt(SInvalidBoolean,[S]);
  1008. end;
  1009. function BoolToStr(B: Boolean): string;
  1010. begin
  1011. If B then
  1012. Result:='TRUE'
  1013. else
  1014. Result:='FALSE';
  1015. end;
  1016. Function FloatToTextFmt(Buffer: PChar; Value: Extended; format: PChar): Integer;
  1017. Var
  1018. Digits: String[40]; { String Of Digits }
  1019. Exponent: String[8]; { Exponent strin }
  1020. FmtStart, FmtStop: PChar; { Start And End Of relevant part }
  1021. { Of format String }
  1022. ExpFmt, ExpSize: Integer; { Type And Length Of }
  1023. { exponential format chosen }
  1024. Placehold: Array[1..4] Of Integer; { Number Of placeholders In All }
  1025. { four Sections }
  1026. thousand: Boolean; { thousand separators? }
  1027. UnexpectedDigits: Integer; { Number Of unexpected Digits that }
  1028. { have To be inserted before the }
  1029. { First placeholder. }
  1030. DigitExponent: Integer; { Exponent Of First digit In }
  1031. { Digits Array. }
  1032. { Find end of format section starting at P. False, if empty }
  1033. Function GetSectionEnd(Var P: PChar): Boolean;
  1034. Var
  1035. C: Char;
  1036. SQ, DQ: Boolean;
  1037. Begin
  1038. Result := False;
  1039. SQ := False;
  1040. DQ := False;
  1041. C := P[0];
  1042. While (C<>#0) And ((C<>';') Or SQ Or DQ) Do
  1043. Begin
  1044. Result := True;
  1045. Case C Of
  1046. #34: If Not SQ Then DQ := Not DQ;
  1047. #39: If Not DQ Then SQ := Not SQ;
  1048. End;
  1049. Inc(P);
  1050. C := P[0];
  1051. End;
  1052. End;
  1053. { Find start and end of format section to apply. If section doesn't exist,
  1054. use section 1. If section 2 is used, the sign of value is ignored. }
  1055. Procedure GetSectionRange(section: Integer);
  1056. Var
  1057. Sec: Array[1..3] Of PChar;
  1058. SecOk: Array[1..3] Of Boolean;
  1059. Begin
  1060. Sec[1] := format;
  1061. SecOk[1] := GetSectionEnd(Sec[1]);
  1062. If section > 1 Then
  1063. Begin
  1064. Sec[2] := Sec[1];
  1065. If Sec[2][0] <> #0 Then
  1066. Inc(Sec[2]);
  1067. SecOk[2] := GetSectionEnd(Sec[2]);
  1068. If section > 2 Then
  1069. Begin
  1070. Sec[3] := Sec[2];
  1071. If Sec[3][0] <> #0 Then
  1072. Inc(Sec[3]);
  1073. SecOk[3] := GetSectionEnd(Sec[3]);
  1074. End;
  1075. End;
  1076. If Not SecOk[1] Then
  1077. FmtStart := Nil
  1078. Else
  1079. Begin
  1080. If Not SecOk[section] Then
  1081. section := 1
  1082. Else If section = 2 Then
  1083. Value := -Value; { Remove sign }
  1084. If section = 1 Then FmtStart := format Else
  1085. Begin
  1086. FmtStart := Sec[section - 1];
  1087. Inc(FmtStart);
  1088. End;
  1089. FmtStop := Sec[section];
  1090. End;
  1091. End;
  1092. { Find format section ranging from FmtStart to FmtStop. }
  1093. Procedure GetFormatOptions;
  1094. Var
  1095. Fmt: PChar;
  1096. SQ, DQ: Boolean;
  1097. area: Integer;
  1098. Begin
  1099. SQ := False;
  1100. DQ := False;
  1101. Fmt := FmtStart;
  1102. ExpFmt := 0;
  1103. area := 1;
  1104. thousand := False;
  1105. Placehold[1] := 0;
  1106. Placehold[2] := 0;
  1107. Placehold[3] := 0;
  1108. Placehold[4] := 0;
  1109. While Fmt < FmtStop Do
  1110. Begin
  1111. Case Fmt[0] Of
  1112. #34:
  1113. Begin
  1114. If Not SQ Then
  1115. DQ := Not DQ;
  1116. Inc(Fmt);
  1117. End;
  1118. #39:
  1119. Begin
  1120. If Not DQ Then
  1121. SQ := Not SQ;
  1122. Inc(Fmt);
  1123. End;
  1124. Else
  1125. { This was 'if not SQ or DQ'. Looked wrong... }
  1126. If Not SQ Or DQ Then
  1127. Begin
  1128. Case Fmt[0] Of
  1129. '0':
  1130. Begin
  1131. Case area Of
  1132. 1:
  1133. area := 2;
  1134. 4:
  1135. Begin
  1136. area := 3;
  1137. Inc(Placehold[3], Placehold[4]);
  1138. Placehold[4] := 0;
  1139. End;
  1140. End;
  1141. Inc(Placehold[area]);
  1142. Inc(Fmt);
  1143. End;
  1144. '#':
  1145. Begin
  1146. If area=3 Then
  1147. area:=4;
  1148. Inc(Placehold[area]);
  1149. Inc(Fmt);
  1150. End;
  1151. '.':
  1152. Begin
  1153. If area<3 Then
  1154. area:=3;
  1155. Inc(Fmt);
  1156. End;
  1157. ',':
  1158. Begin
  1159. thousand := True;
  1160. Inc(Fmt);
  1161. End;
  1162. 'e', 'E':
  1163. If ExpFmt = 0 Then
  1164. Begin
  1165. If (Fmt[0]='E') Then
  1166. ExpFmt:=1
  1167. Else
  1168. ExpFmt := 3;
  1169. Inc(Fmt);
  1170. If (Fmt<FmtStop) Then
  1171. Begin
  1172. Case Fmt[0] Of
  1173. '+':
  1174. Begin
  1175. End;
  1176. '-':
  1177. Inc(ExpFmt);
  1178. Else
  1179. ExpFmt := 0;
  1180. End;
  1181. If ExpFmt <> 0 Then
  1182. Begin
  1183. Inc(Fmt);
  1184. ExpSize := 0;
  1185. While (Fmt<FmtStop) And
  1186. (ExpSize<4) And
  1187. (Fmt[0] In ['0'..'9']) Do
  1188. Begin
  1189. Inc(ExpSize);
  1190. Inc(Fmt);
  1191. End;
  1192. End;
  1193. End;
  1194. End
  1195. Else
  1196. Inc(Fmt);
  1197. Else { Case }
  1198. Inc(Fmt);
  1199. End; { Case }
  1200. End; { Begin }
  1201. End; { Case }
  1202. End; { While .. Begin }
  1203. End;
  1204. Procedure FloatToStr;
  1205. Var
  1206. I, J, Exp, Width, Decimals, DecimalPoint, len: Integer;
  1207. Begin
  1208. If ExpFmt = 0 Then
  1209. Begin
  1210. { Fixpoint }
  1211. Decimals:=Placehold[3]+Placehold[4];
  1212. Width:=Placehold[1]+Placehold[2]+Decimals;
  1213. If (Decimals=0) Then
  1214. Str(Value:Width:0,Digits)
  1215. Else
  1216. Str(Value:Width+1:Decimals,Digits);
  1217. len:=Length(Digits);
  1218. { Find the decimal point }
  1219. If (Decimals=0) Then
  1220. DecimalPoint:=len+1
  1221. Else
  1222. DecimalPoint:=len-Decimals;
  1223. { If value is very small, and no decimal places
  1224. are desired, remove the leading 0. }
  1225. If (Abs(Value) < 1) And (Placehold[2] = 0) Then
  1226. Begin
  1227. If (Placehold[1]=0) Then
  1228. Delete(Digits,DecimalPoint-1,1)
  1229. Else
  1230. Digits[DecimalPoint-1]:=' ';
  1231. End;
  1232. { Convert optional zeroes to spaces. }
  1233. I:=len;
  1234. J:=DecimalPoint+Placehold[3];
  1235. While (I>J) And (Digits[I]='0') Do
  1236. Begin
  1237. Digits[I] := ' ';
  1238. Dec(I);
  1239. End;
  1240. { If integer value and no obligatory decimal
  1241. places, remove decimal point. }
  1242. If (DecimalPoint < len) And (Digits[DecimalPoint + 1] = ' ') Then
  1243. Digits[DecimalPoint] := ' ';
  1244. { Convert spaces left from obligatory decimal point to zeroes. }
  1245. I:=DecimalPoint-Placehold[2];
  1246. While (I<DecimalPoint) And (Digits[I]=' ') Do
  1247. Begin
  1248. Digits[I] := '0';
  1249. Inc(I);
  1250. End;
  1251. Exp := 0;
  1252. End
  1253. Else
  1254. Begin
  1255. { Scientific: exactly <Width> Digits With <Precision> Decimals
  1256. And adjusted Exponent. }
  1257. If Placehold[1]+Placehold[2]=0 Then
  1258. Placehold[1]:=1;
  1259. Decimals := Placehold[3] + Placehold[4];
  1260. Width:=Placehold[1]+Placehold[2]+Decimals;
  1261. Str(Value:Width+8,Digits);
  1262. { Find and cut out exponent. Always the
  1263. last 6 characters in the string.
  1264. -> 0000E+0000 }
  1265. I:=Length(Digits)-5;
  1266. Val(Copy(Digits,I+1,5),Exp,J);
  1267. Exp:=Exp+1-(Placehold[1]+Placehold[2]);
  1268. Delete(Digits, I, 6);
  1269. { Str() always returns at least one digit after the decimal point.
  1270. If we don't want it, we have to remove it. }
  1271. If (Decimals=0) And (Placehold[1]+Placehold[2]<= 1) Then
  1272. Begin
  1273. If (Digits[4]>='5') Then
  1274. Begin
  1275. Inc(Digits[2]);
  1276. If (Digits[2]>'9') Then
  1277. Begin
  1278. Digits[2] := '1';
  1279. Inc(Exp);
  1280. End;
  1281. End;
  1282. Delete(Digits, 3, 2);
  1283. DecimalPoint := Length(Digits) + 1;
  1284. End
  1285. Else
  1286. Begin
  1287. { Move decimal point at the desired position }
  1288. Delete(Digits, 3, 1);
  1289. DecimalPoint:=2+Placehold[1]+Placehold[2];
  1290. If (Decimals<>0) Then
  1291. Insert('.',Digits,DecimalPoint);
  1292. End;
  1293. { Convert optional zeroes to spaces. }
  1294. I := Length(Digits);
  1295. J := DecimalPoint + Placehold[3];
  1296. While (I > J) And (Digits[I] = '0') Do
  1297. Begin
  1298. Digits[I] := ' ';
  1299. Dec(I);
  1300. End;
  1301. { If integer number and no obligatory decimal paces, remove decimal point }
  1302. If (DecimalPoint<Length(Digits)) And
  1303. (Digits[DecimalPoint+1]=' ') Then
  1304. Digits[DecimalPoint]:=' ';
  1305. If (Digits[1]=' ') Then
  1306. Begin
  1307. Delete(Digits, 1, 1);
  1308. Dec(DecimalPoint);
  1309. End;
  1310. { Calculate exponent string }
  1311. Str(Abs(Exp), Exponent);
  1312. While Length(Exponent)<ExpSize Do
  1313. Insert('0',Exponent,1);
  1314. If Exp >= 0 Then
  1315. Begin
  1316. If (ExpFmt In [1,3]) Then
  1317. Insert('+', Exponent, 1);
  1318. End
  1319. Else
  1320. Insert('-',Exponent,1);
  1321. If (ExpFmt<3) Then
  1322. Insert('E',Exponent,1)
  1323. Else
  1324. Insert('e',Exponent,1);
  1325. End;
  1326. DigitExponent:=DecimalPoint-2;
  1327. If (Digits[1]='-') Then
  1328. Dec(DigitExponent);
  1329. UnexpectedDigits:=DecimalPoint-1-(Placehold[1]+Placehold[2]);
  1330. End;
  1331. Function PutResult: LongInt;
  1332. Var
  1333. SQ, DQ: Boolean;
  1334. Fmt, Buf: PChar;
  1335. Dig, N: Integer;
  1336. Begin
  1337. SQ := False;
  1338. DQ := False;
  1339. Fmt := FmtStart;
  1340. Buf := Buffer;
  1341. Dig := 1;
  1342. While (Fmt<FmtStop) Do
  1343. Begin
  1344. //Write(Fmt[0]);
  1345. Case Fmt[0] Of
  1346. #34:
  1347. Begin
  1348. If Not SQ Then
  1349. DQ := Not DQ;
  1350. Inc(Fmt);
  1351. End;
  1352. #39:
  1353. Begin
  1354. If Not DQ Then
  1355. SQ := Not SQ;
  1356. Inc(Fmt);
  1357. End;
  1358. Else
  1359. If Not (SQ Or DQ) Then
  1360. Begin
  1361. Case Fmt[0] Of
  1362. '0', '#', '.':
  1363. Begin
  1364. If (Dig=1) And (UnexpectedDigits>0) Then
  1365. Begin
  1366. { Everything unexpected is written before the first digit }
  1367. For N := 1 To UnexpectedDigits Do
  1368. Begin
  1369. Buf[0] := Digits[N];
  1370. Inc(Buf);
  1371. If thousand And (Digits[N]<>'-') Then
  1372. Begin
  1373. If (DigitExponent Mod 3 = 0) And (DigitExponent>0) Then
  1374. Begin
  1375. Buf[0] := ThousandSeparator;
  1376. Inc(Buf);
  1377. End;
  1378. Dec(DigitExponent);
  1379. End;
  1380. End;
  1381. Inc(Dig, UnexpectedDigits);
  1382. End;
  1383. If (Digits[Dig]<>' ') Then
  1384. Begin
  1385. If (Digits[Dig]='.') Then
  1386. Buf[0] := DecimalSeparator
  1387. Else
  1388. Buf[0] := Digits[Dig];
  1389. Inc(Buf);
  1390. If thousand And (DigitExponent Mod 3 = 0) And (DigitExponent > 0) Then
  1391. Begin
  1392. Buf[0] := ThousandSeparator;
  1393. Inc(Buf);
  1394. End;
  1395. End;
  1396. Inc(Dig);
  1397. Dec(DigitExponent);
  1398. Inc(Fmt);
  1399. End;
  1400. 'e', 'E':
  1401. Begin
  1402. If ExpFmt <> 0 Then
  1403. Begin
  1404. Inc(Fmt);
  1405. If Fmt < FmtStop Then
  1406. Begin
  1407. If Fmt[0] In ['+', '-'] Then
  1408. Begin
  1409. Inc(Fmt, ExpSize);
  1410. For N:=1 To Length(Exponent) Do
  1411. Buf[N-1] := Exponent[N];
  1412. Inc(Buf,Length(Exponent));
  1413. ExpFmt:=0;
  1414. End;
  1415. Inc(Fmt);
  1416. End;
  1417. End
  1418. Else
  1419. Begin
  1420. { No legal exponential format.
  1421. Simply write the 'E' to the result. }
  1422. Buf[0] := Fmt[0];
  1423. Inc(Buf);
  1424. Inc(Fmt);
  1425. End;
  1426. End;
  1427. Else { Case }
  1428. { Usual character }
  1429. If (Fmt[0]<>',') Then
  1430. Begin
  1431. Buf[0] := Fmt[0];
  1432. Inc(Buf);
  1433. End;
  1434. Inc(Fmt);
  1435. End; { Case }
  1436. End
  1437. Else { IF }
  1438. Begin
  1439. { Character inside single or double quotes }
  1440. Buf[0] := Fmt[0];
  1441. Inc(Buf);
  1442. Inc(Fmt);
  1443. End;
  1444. End; { Case }
  1445. End; { While .. Begin }
  1446. Result:=PtrInt(Buf)-PtrInt(Buffer);
  1447. End;
  1448. Begin
  1449. If (Value>0) Then
  1450. GetSectionRange(1)
  1451. Else If (Value<0) Then
  1452. GetSectionRange(2)
  1453. Else
  1454. GetSectionRange(3);
  1455. If FmtStart = Nil Then
  1456. Begin
  1457. Result := FloatToText(Buffer, Value, ffGeneral, 15, 4);
  1458. End
  1459. Else
  1460. Begin
  1461. GetFormatOptions;
  1462. If (ExpFmt = 0) And (Abs(Value) >= 1E18) Then
  1463. Result := FloatToText(Buffer, Value, ffGeneral, 15, 4)
  1464. Else
  1465. Begin
  1466. FloatToStr;
  1467. Result := PutResult;
  1468. End;
  1469. End;
  1470. End;
  1471. Procedure FloatToDecimal(Var Result: TFloatRec; Value: Extended; Precision, Decimals : integer);
  1472. Var
  1473. Buffer: String[24];
  1474. Error, N: Integer;
  1475. Begin
  1476. Str(Value:23, Buffer);
  1477. Result.Negative := (Buffer[1] = '-');
  1478. Val(Copy(Buffer, 19, 5), Result.Exponent, Error);
  1479. Inc(Result. Exponent);
  1480. Result.Digits[0] := Buffer[2];
  1481. Move(Buffer[4], Result.Digits[1], 14);
  1482. If Decimals + Result.Exponent < Precision Then
  1483. N := Decimals + Result.Exponent
  1484. Else
  1485. N := Precision;
  1486. If N > 15 Then
  1487. N := 15;
  1488. If N = 0 Then
  1489. Begin
  1490. If Result.Digits[0] >= '5' Then
  1491. Begin
  1492. Result.Digits[0] := '1';
  1493. Result.Digits[1] := #0;
  1494. Inc(Result.Exponent);
  1495. End
  1496. Else
  1497. Result.Digits[0] := #0;
  1498. End
  1499. Else If N > 0 Then
  1500. Begin
  1501. If Result.Digits[N] >= '5' Then
  1502. Begin
  1503. Repeat
  1504. Result.Digits[N] := #0;
  1505. Dec(N);
  1506. Inc(Result.Digits[N]);
  1507. Until (N = 0) Or (Result.Digits[N] < ':');
  1508. If Result.Digits[0] = ':' Then
  1509. Begin
  1510. Result.Digits[0] := '1';
  1511. Inc(Result.Exponent);
  1512. End;
  1513. End
  1514. Else
  1515. Begin
  1516. Result.Digits[N] := '0';
  1517. While (Result.Digits[N] = '0') And (N > -1) Do
  1518. Begin
  1519. Result.Digits[N] := #0;
  1520. Dec(N);
  1521. End;
  1522. End;
  1523. End
  1524. Else
  1525. Result.Digits[0] := #0;
  1526. If Result.Digits[0] = #0 Then
  1527. Begin
  1528. Result.Exponent := 0;
  1529. Result.Negative := False;
  1530. End;
  1531. End;
  1532. Function FormatFloat(Const format: String; Value: Extended): String;
  1533. Var
  1534. buf : Array[0..1024] of char;
  1535. Begin
  1536. Buf[FloatToTextFmt(@Buf[0],Value,Pchar(Format))]:=#0;
  1537. Result:=StrPas(@Buf);
  1538. End;
  1539. function FormatCurr(const Format: string; Value: Currency): string;
  1540. begin
  1541. Result := FormatFloat(Format, Value);
  1542. end;
  1543. {==============================================================================}
  1544. { extra functions }
  1545. {==============================================================================}
  1546. { LeftStr returns Count left-most characters from S }
  1547. function LeftStr(const S: string; Count: integer): string;
  1548. begin
  1549. result := Copy(S, 1, Count);
  1550. end ;
  1551. { RightStr returns Count right-most characters from S }
  1552. function RightStr(const S: string; Count: integer): string;
  1553. begin
  1554. If Count>Length(S) then
  1555. Count:=Length(S);
  1556. result := Copy(S, 1 + Length(S) - Count, Count);
  1557. end;
  1558. { BCDToInt converts the BCD value Value to an integer }
  1559. function BCDToInt(Value: integer): integer;
  1560. var i, j: integer;
  1561. begin
  1562. result := 0;
  1563. j := 1;
  1564. for i := 0 to SizeOf(Value) shr 1 - 1 do begin
  1565. result := result + j * (Value and 15);
  1566. j := j * 10;
  1567. Value := Value shr 4;
  1568. end ;
  1569. end ;
  1570. Function LastDelimiter(const Delimiters, S: string): Integer;
  1571. begin
  1572. Result:=Length(S);
  1573. While (Result>0) and (Pos(S[Result],Delimiters)=0) do
  1574. Dec(Result);
  1575. end;
  1576. Function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;
  1577. var
  1578. Srch,OldP,RemS: string; // Srch and Oldp can contain uppercase versions of S,OldPattern
  1579. P : Integer;
  1580. begin
  1581. Srch:=S;
  1582. OldP:=OldPattern;
  1583. if rfIgnoreCase in Flags then
  1584. begin
  1585. Srch:=AnsiUpperCase(Srch);
  1586. OldP:=AnsiUpperCase(OldP);
  1587. end;
  1588. RemS:=S;
  1589. Result:='';
  1590. while (Length(Srch)<>0) do
  1591. begin
  1592. P:=AnsiPos(OldP, Srch);
  1593. if P=0 then
  1594. begin
  1595. Result:=Result+RemS;
  1596. Srch:='';
  1597. end
  1598. else
  1599. begin
  1600. Result:=Result+Copy(RemS,1,P-1)+NewPattern;
  1601. P:=P+Length(OldP);
  1602. RemS:=Copy(RemS,P,Length(RemS)-P+1);
  1603. if not (rfReplaceAll in Flags) then
  1604. begin
  1605. Result:=Result+RemS;
  1606. Srch:='';
  1607. end
  1608. else
  1609. Srch:=Copy(Srch,P,Length(Srch)-P+1);
  1610. end;
  1611. end;
  1612. end;
  1613. Function IsDelimiter(const Delimiters, S: string; Index: Integer): Boolean;
  1614. begin
  1615. Result:=False;
  1616. If (Index>0) and (Index<=Length(S)) then
  1617. Result:=Pos(S[Index],Delimiters)<>0; // Note we don't do MBCS yet
  1618. end;
  1619. Function ByteToCharLen(const S: string; MaxLen: Integer): Integer;
  1620. begin
  1621. Result:=Length(S);
  1622. If Result>MaxLen then
  1623. Result:=MaxLen;
  1624. end;
  1625. Function ByteToCharIndex(const S: string; Index: Integer): Integer;
  1626. begin
  1627. Result:=Index;
  1628. end;
  1629. Function CharToByteLen(const S: string; MaxLen: Integer): Integer;
  1630. begin
  1631. Result:=Length(S);
  1632. If Result>MaxLen then
  1633. Result:=MaxLen;
  1634. end;
  1635. Function CharToByteIndex(const S: string; Index: Integer): Integer;
  1636. begin
  1637. Result:=Index;
  1638. end;
  1639. Function ByteType(const S: string; Index: Integer): TMbcsByteType;
  1640. begin
  1641. Result:=mbSingleByte;
  1642. end;
  1643. Function StrByteType(Str: PChar; Index: Cardinal): TMbcsByteType;
  1644. begin
  1645. Result:=mbSingleByte;
  1646. end;
  1647. Function StrCharLength(const Str: PChar): Integer;
  1648. begin
  1649. result:=widestringmanager.CharLengthPCharProc(Str);
  1650. end;
  1651. Function FindCmdLineSwitch(const Switch: string; const Chars: TSysCharSet;IgnoreCase: Boolean): Boolean;
  1652. Var
  1653. I,L : Integer;
  1654. S,T : String;
  1655. begin
  1656. Result:=False;
  1657. S:=Switch;
  1658. If IgnoreCase then
  1659. S:=UpperCase(S);
  1660. I:=ParamCount;
  1661. While (Not Result) and (I>0) do
  1662. begin
  1663. L:=Length(Paramstr(I));
  1664. If (L>0) and (ParamStr(I)[1] in Chars) then
  1665. begin
  1666. T:=Copy(ParamStr(I),2,L-1);
  1667. If IgnoreCase then
  1668. T:=UpperCase(T);
  1669. Result:=S=T;
  1670. end;
  1671. Dec(i);
  1672. end;
  1673. end;
  1674. Function FindCmdLineSwitch(const Switch: string; IgnoreCase: Boolean): Boolean;
  1675. begin
  1676. Result:=FindCmdLineSwitch(Switch,SwitchChars,IgnoreCase);
  1677. end;
  1678. Function FindCmdLineSwitch(const Switch: string): Boolean;
  1679. begin
  1680. Result:=FindCmdLineSwitch(Switch,SwitchChars,False);
  1681. end;
  1682. function WrapText(const Line, BreakStr: string; const BreakChars: TSysCharSet; MaxCol: Integer): string;
  1683. const
  1684. Quotes = ['''', '"'];
  1685. Var
  1686. L : String;
  1687. C,LQ,BC : Char;
  1688. P,BLen,Len : Integer;
  1689. HB,IBC : Boolean;
  1690. begin
  1691. Result:='';
  1692. L:=Line;
  1693. Blen:=Length(BreakStr);
  1694. If (BLen>0) then
  1695. BC:=BreakStr[1]
  1696. else
  1697. BC:=#0;
  1698. Len:=Length(L);
  1699. While (Len>0) do
  1700. begin
  1701. P:=1;
  1702. LQ:=#0;
  1703. HB:=False;
  1704. IBC:=False;
  1705. While ((P<=Len) and ((P<=MaxCol) or not IBC)) and ((LQ<>#0) or Not HB) do
  1706. begin
  1707. C:=L[P];
  1708. If (C=LQ) then
  1709. LQ:=#0
  1710. else If (C in Quotes) then
  1711. LQ:=C;
  1712. If (LQ<>#0) then
  1713. Inc(P)
  1714. else
  1715. begin
  1716. HB:=((C=BC) and (BreakStr=Copy(L,P,BLen)));
  1717. If HB then
  1718. Inc(P,Blen)
  1719. else
  1720. begin
  1721. If (P>MaxCol) then
  1722. IBC:=C in BreakChars;
  1723. Inc(P);
  1724. end;
  1725. end;
  1726. // Writeln('"',C,'" : IBC : ',IBC,' HB : ',HB,' LQ : ',LQ,' P>MaxCol : ',P>MaxCol);
  1727. end;
  1728. Result:=Result+Copy(L,1,P-1);
  1729. If Not HB then
  1730. Result:=Result+BreakStr;
  1731. Delete(L,1,P-1);
  1732. Len:=Length(L);
  1733. end;
  1734. end;
  1735. function WrapText(const Line: string; MaxCol: Integer): string;
  1736. begin
  1737. Result:=WrapText(Line,sLineBreak, [' ', '-', #9], MaxCol);
  1738. end;
  1739. {
  1740. Case Translation Tables
  1741. Can be used in internationalization support.
  1742. Although these tables can be obtained through system calls
  1743. it is better to not use those, since most implementation are not 100%
  1744. WARNING:
  1745. before modifying a translation table make sure that the current codepage
  1746. of the OS corresponds to the one you make changes to
  1747. }
  1748. const
  1749. { upper case translation table for character set 850 }
  1750. CP850UCT: array[128..255] of char =
  1751. ('€', 'š', '�', '¶', 'Ž', '¶', '�', '€', 'Ò', 'Ó', 'Ô', 'Ø', '×', 'Þ', 'Ž', '�',
  1752. '�', '’', '’', 'â', '™', 'ã', 'ê', 'ë', 'Y', '™', 'š', '�', 'œ', '�', 'ž', 'Ÿ',
  1753. 'µ', 'Ö', 'à', 'é', '¥', '¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯',
  1754. '°', '±', '²', '³', '´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿',
  1755. 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Ç', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï',
  1756. 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', '×', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß',
  1757. 'à', 'á', 'â', 'ã', 'å', 'å', 'æ', 'í', 'è', 'é', 'ê', 'ë', 'í', 'í', 'î', 'ï',
  1758. 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ');
  1759. { lower case translation table for character set 850 }
  1760. CP850LCT: array[128..255] of char =
  1761. ('‡', '�', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '�', '„', '†',
  1762. '‚', '‘', '‘', '“', '”', '•', '–', '—', '˜', '”', '�', '›', 'œ', '›', 'ž', 'Ÿ',
  1763. ' ', '¡', '¢', '£', '¤', '¤', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯',
  1764. '°', '±', '²', '³', '´', ' ', 'ƒ', '…', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿',
  1765. 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Æ', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï',
  1766. 'Ð', 'Ñ', 'ˆ', '‰', 'Š', 'Õ', '¡', 'Œ', '‹', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', '�', 'ß',
  1767. '¢', 'á', '“', '•', 'ä', 'ä', 'æ', 'í', 'è', '£', '–', '—', 'ì', 'ì', 'î', 'ï',
  1768. 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ');
  1769. { upper case translation table for character set ISO 8859/1 Latin 1 }
  1770. CPISO88591UCT: array[192..255] of char =
  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, #215,
  1774. #216, #217, #218, #219, #220, #221, #222, #223,
  1775. #192, #193, #194, #195, #196, #197, #198, #199,
  1776. #200, #201, #202, #203, #204, #205, #206, #207,
  1777. #208, #209, #210, #211, #212, #213, #214, #247,
  1778. #216, #217, #218, #219, #220, #221, #222, #89 );
  1779. { lower case translation table for character set ISO 8859/1 Latin 1 }
  1780. CPISO88591LCT: array[192..255] of char =
  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, #215,
  1784. #248, #249, #250, #251, #252, #253, #254, #223,
  1785. #224, #225, #226, #227, #228, #229, #230, #231,
  1786. #232, #233, #234, #235, #236, #237, #238, #239,
  1787. #240, #241, #242, #243, #244, #245, #246, #247,
  1788. #248, #249, #250, #251, #252, #253, #254, #255 );
  1789. function sscanf(const s: string; const fmt : string;const Pointers : array of Pointer) : Integer;
  1790. var
  1791. i,j,n,m : SizeInt;
  1792. s1 : string;
  1793. function GetInt(unsigned : boolean=false) : Integer;
  1794. begin
  1795. s1 := '';
  1796. while (s[n] = ' ') and (Length(s) > n) do
  1797. inc(n);
  1798. { read sign }
  1799. if (Length(s)>= n) and (s[n] in ['+', '-']) then
  1800. begin
  1801. { don't accept - when reading unsigned }
  1802. if unsigned and (s[n]='-') then
  1803. begin
  1804. result:=length(s1);
  1805. exit;
  1806. end
  1807. else
  1808. begin
  1809. s1:=s1+s[n];
  1810. inc(n);
  1811. end;
  1812. end;
  1813. { read numbers }
  1814. while (s[n] in ['0'..'9'])
  1815. and (Length(s) >= n) do
  1816. begin
  1817. s1 := s1+s[n];
  1818. inc(n);
  1819. end;
  1820. Result := Length(s1);
  1821. end;
  1822. function GetFloat : Integer;
  1823. begin
  1824. s1 := '';
  1825. while (s[n] = ' ') and (Length(s) > n) do
  1826. inc(n);
  1827. while (s[n] in ['0'..'9', '+', '-', '.', 'e', 'E'])
  1828. and (Length(s) >= n) do
  1829. begin
  1830. s1 := s1+s[n];
  1831. inc(n);
  1832. end;
  1833. Result := Length(s1);
  1834. end;
  1835. function GetString : Integer;
  1836. begin
  1837. s1 := '';
  1838. while (s[n] = ' ') and (Length(s) > n) do
  1839. inc(n);
  1840. while (s[n] <> ' ') and (Length(s) >= n) do
  1841. begin
  1842. s1 := s1+s[n];
  1843. inc(n);
  1844. end;
  1845. Result := Length(s1);
  1846. end;
  1847. function ScanStr(c : Char) : Boolean;
  1848. begin
  1849. while (s[n] <> c) and (Length(s) > n) do
  1850. inc(n);
  1851. inc(n);
  1852. If (n <= Length(s)) then
  1853. Result := True
  1854. else
  1855. Result := False;
  1856. end;
  1857. function GetFmt : Integer;
  1858. begin
  1859. Result := -1;
  1860. while true do
  1861. begin
  1862. while (fmt[m] = ' ') and (Length(fmt) > m) do
  1863. inc(m);
  1864. if (m >= Length(fmt)) then
  1865. break;
  1866. if (fmt[m] = '%') then
  1867. begin
  1868. inc(m);
  1869. case fmt[m] of
  1870. 'd':
  1871. Result:=vtInteger;
  1872. 'f':
  1873. Result:=vtExtended;
  1874. 's':
  1875. Result:=vtString;
  1876. 'c':
  1877. Result:=vtChar;
  1878. else
  1879. raise EFormatError.CreateFmt(SInvalidFormat,[fmt]);
  1880. end;
  1881. inc(m);
  1882. break;
  1883. end;
  1884. if not(ScanStr(fmt[m])) then
  1885. break;
  1886. inc(m);
  1887. end;
  1888. end;
  1889. begin
  1890. n := 1;
  1891. m := 1;
  1892. Result := 0;
  1893. for i:=0 to High(Pointers) do
  1894. begin
  1895. j := GetFmt;
  1896. case j of
  1897. vtInteger :
  1898. begin
  1899. if GetInt>0 then
  1900. begin
  1901. plongint(Pointers[i])^:=StrToInt(s1);
  1902. inc(Result);
  1903. end
  1904. else
  1905. break;
  1906. end;
  1907. vtchar :
  1908. begin
  1909. if Length(s)>n then
  1910. begin
  1911. pchar(Pointers[i])^:=s[n];
  1912. inc(n);
  1913. inc(Result);
  1914. end
  1915. else
  1916. break;
  1917. end;
  1918. vtExtended :
  1919. begin
  1920. if GetFloat>0 then
  1921. begin
  1922. pextended(Pointers[i])^:=StrToFloat(s1);
  1923. inc(Result);
  1924. end
  1925. else
  1926. break;
  1927. end;
  1928. vtString :
  1929. begin
  1930. if GetString > 0 then
  1931. begin
  1932. pansistring(Pointers[i])^:=s1;
  1933. inc(Result);
  1934. end
  1935. else
  1936. break;
  1937. end;
  1938. else
  1939. break;
  1940. end;
  1941. end;
  1942. end;