sysstr.inc 59 KB

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