sysstr.inc 59 KB

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