sysstr.inc 59 KB

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