sysstr.inc 56 KB

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