sysstr.inc 59 KB

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