sysstr.inc 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959
  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. FUNCTION NewStr (Const S: ShortString): PShortString;
  32. VAR P: PShortString;
  33. BEGIN
  34. If (S = '') Then
  35. P := Nil
  36. Else
  37. Begin { Return nil }
  38. GetMem(P, Length(S) + 1); { Allocate memory }
  39. If (P<>Nil) Then P^ := S; { Hold string }
  40. End;
  41. NewStr := P; { Return result }
  42. END;
  43. { DisposeStr frees the memory occupied by S }
  44. procedure DisposeStr(S: PString);
  45. begin
  46. if S <> Nil then
  47. begin
  48. dispose(s);
  49. S:=nil;
  50. end;
  51. end;
  52. PROCEDURE DisposeStr (S: PShortString);
  53. BEGIN
  54. If (S <> Nil) Then FreeMem(S, Length(S^) + 1); { Release memory }
  55. END;
  56. { AssignStr assigns S to P^ }
  57. procedure AssignStr(var P: PString; const S: string);
  58. begin
  59. P^ := s;
  60. end ;
  61. { AppendStr appends S to Dest }
  62. procedure AppendStr(var Dest: String; const S: string);
  63. begin
  64. Dest := Dest + S;
  65. end ;
  66. { UpperCase returns a copy of S where all lowercase characters ( from a to z )
  67. have been converted to uppercase }
  68. Function UpperCase(Const S : String) : String;
  69. Var
  70. i : Integer;
  71. P : PChar;
  72. begin
  73. Result := S;
  74. if not assigned(pointer(result)) then exit;
  75. UniqueString(Result);
  76. P:=Pchar(pointer(Result));
  77. for i := 1 to Length(Result) do
  78. begin
  79. if (P^ in ['a'..'z']) then P^ := char(byte(p^) - 32);
  80. Inc(P);
  81. end;
  82. end;
  83. { LowerCase returns a copy of S where all uppercase characters ( from A to Z )
  84. have been converted to lowercase }
  85. Function Lowercase(Const S : String) : String;
  86. Var
  87. i : Integer;
  88. P : PChar;
  89. begin
  90. Result := S;
  91. if not assigned(pointer(result)) then exit;
  92. UniqueString(Result);
  93. P:=Pchar(pointer(Result));
  94. for i := 1 to Length(Result) do
  95. begin
  96. if (P^ in ['A'..'Z']) then P^ := char(byte(p^) + 32);
  97. Inc(P);
  98. end;
  99. end;
  100. function LowerCase(const V: variant): string; overload;{$ifdef SYSUTILSINLINE}inline;{$endif}
  101. begin
  102. result:=LowerCase(ansistring(V));
  103. end;
  104. { CompareStr compares S1 and S2, the result is the based on
  105. substraction of the ascii values of the characters in S1 and S2
  106. case result
  107. S1 < S2 < 0
  108. S1 > S2 > 0
  109. S1 = S2 = 0 }
  110. function CompareStr(const S1, S2: string): Integer;
  111. var count, count1, count2: integer;
  112. begin
  113. result := 0;
  114. Count1 := Length(S1);
  115. Count2 := Length(S2);
  116. if Count1>Count2 then
  117. Count:=Count2
  118. else
  119. Count:=Count1;
  120. result := CompareMemRange(Pointer(S1),Pointer(S2), Count);
  121. if result=0 then
  122. result:=Count1-Count2;
  123. end;
  124. { CompareMemRange returns the result of comparison of Length bytes at P1 and P2
  125. case result
  126. P1 < P2 < 0
  127. P1 > P2 > 0
  128. P1 = P2 = 0 }
  129. function CompareMemRange(P1, P2: Pointer; Length: cardinal): integer;
  130. var
  131. i: cardinal;
  132. begin
  133. i := 0;
  134. result := 0;
  135. while (result=0) and (I<length) do
  136. begin
  137. result:=byte(P1^)-byte(P2^);
  138. P1:=pchar(P1)+1; // VP compat.
  139. P2:=pchar(P2)+1;
  140. i := i + 1;
  141. end ;
  142. end ;
  143. function CompareMem(P1, P2: Pointer; Length: cardinal): Boolean;
  144. var
  145. i: cardinal;
  146. begin
  147. Result:=True;
  148. I:=0;
  149. If (P1)<>(P2) then
  150. While Result and (i<Length) do
  151. begin
  152. Result:=PByte(P1)^=PByte(P2)^;
  153. Inc(I);
  154. Inc(pchar(P1));
  155. Inc(pchar(P2));
  156. end;
  157. end;
  158. { CompareText compares S1 and S2, the result is the based on
  159. substraction of the ascii values of characters in S1 and S2
  160. comparison is case-insensitive
  161. case result
  162. S1 < S2 < 0
  163. S1 > S2 > 0
  164. S1 = S2 = 0 }
  165. function CompareText(const S1, S2: string): integer;
  166. var
  167. i, count, count1, count2: integer; Chr1, Chr2: byte;
  168. P1, P2: PChar;
  169. begin
  170. Count1 := Length(S1);
  171. Count2 := Length(S2);
  172. if (Count1>Count2) then
  173. Count := Count2
  174. else
  175. Count := Count1;
  176. i := 0;
  177. if count>0 then
  178. begin
  179. P1 := @S1[1];
  180. P2 := @S2[1];
  181. while i < Count do
  182. begin
  183. Chr1 := byte(p1^);
  184. Chr2 := byte(p2^);
  185. if Chr1 <> Chr2 then
  186. begin
  187. if Chr1 in [97..122] then
  188. dec(Chr1,32);
  189. if Chr2 in [97..122] then
  190. dec(Chr2,32);
  191. if Chr1 <> Chr2 then
  192. Break;
  193. end;
  194. Inc(P1); Inc(P2); Inc(I);
  195. end;
  196. end;
  197. if i < Count then
  198. result := Chr1-Chr2
  199. else
  200. result := count1-count2;
  201. end;
  202. function SameText(const s1,s2:String):Boolean;
  203. begin
  204. Result:=CompareText(S1,S2)=0;
  205. end;
  206. {$ifndef FPC_NOGENERICANSIROUTINES}
  207. {==============================================================================}
  208. { Ansi string functions }
  209. { these functions rely on the character set loaded by the OS }
  210. {==============================================================================}
  211. type
  212. TCaseTranslationTable = array[0..255] of char;
  213. var
  214. { Tables with upper and lowercase forms of character sets.
  215. MUST be initialized with the correct code-pages }
  216. UpperCaseTable: TCaseTranslationTable;
  217. LowerCaseTable: TCaseTranslationTable;
  218. function GenericAnsiUpperCase(const s: string): string;
  219. var
  220. len, i: integer;
  221. begin
  222. len := length(s);
  223. SetLength(result, len);
  224. for i := 1 to len do
  225. result[i] := UpperCaseTable[ord(s[i])];
  226. end;
  227. function GenericAnsiLowerCase(const s: string): string;
  228. var
  229. len, i: integer;
  230. begin
  231. len := length(s);
  232. SetLength(result, len);
  233. for i := 1 to len do
  234. result[i] := LowerCaseTable[ord(s[i])];
  235. end;
  236. function GenericAnsiCompareStr(const S1, S2: string): PtrInt;
  237. Var
  238. I,L1,L2 : SizeInt;
  239. begin
  240. Result:=0;
  241. L1:=Length(S1);
  242. L2:=Length(S2);
  243. I:=1;
  244. While (Result=0) and ((I<=L1) and (I<=L2)) do
  245. begin
  246. Result:=Ord(S1[I])-Ord(S2[I]); //!! Must be replaced by ansi characters !!
  247. Inc(I);
  248. end;
  249. If Result=0 Then
  250. Result:=L1-L2;
  251. end;
  252. function GenericAnsiCompareText(const S1, S2: string): PtrInt;
  253. Var
  254. I,L1,L2 : SizeInt;
  255. begin
  256. Result:=0;
  257. L1:=Length(S1);
  258. L2:=Length(S2);
  259. I:=1;
  260. While (Result=0) and ((I<=L1) and (I<=L2)) do
  261. begin
  262. Result:=Ord(LowerCaseTable[Ord(S1[I])])-Ord(LowerCaseTable[Ord(S2[I])]); //!! Must be replaced by ansi characters !!
  263. Inc(I);
  264. end;
  265. If Result=0 Then
  266. Result:=L1-L2;
  267. end;
  268. function GenericAnsiStrComp(S1, S2: PChar): PtrInt;
  269. begin
  270. Result:=0;
  271. If S1=Nil then
  272. begin
  273. If S2=Nil Then Exit;
  274. result:=-1;
  275. exit;
  276. end;
  277. If S2=Nil then
  278. begin
  279. Result:=1;
  280. exit;
  281. end;
  282. While (Result=0) and (S1^<>#0) and (S2^<>#0) do begin
  283. Result:=Ord(S1^)-Ord(S2^); //!! Must be replaced by ansi characters !!
  284. Inc(S1);
  285. Inc(S2);
  286. end;
  287. if (Result=0) and (S1^<>S2^) then // loop ended because exactly one has #0
  288. if S1^=#0 then // shorter string is smaller
  289. result:=-1
  290. else
  291. result:=1;
  292. end;
  293. function GenericAnsiStrIComp(S1, S2: PChar): PtrInt;
  294. begin
  295. Result:=0;
  296. If S1=Nil then
  297. begin
  298. If S2=Nil Then Exit;
  299. result:=-1;
  300. exit;
  301. end;
  302. If S2=Nil then
  303. begin
  304. Result:=1;
  305. exit;
  306. end;
  307. While (Result=0) and (S1^<>#0) and (S2^<>#0) do begin
  308. Result:=Ord(LowerCaseTable[Ord(S1[0])])-Ord(LowerCaseTable[Ord(S2[0])]); //!! Must be replaced by ansi characters !!
  309. Inc(S1);
  310. Inc(S2);
  311. end;
  312. if (Result=0) and (s1[0]<>s2[0]) then //length(s1)<>length(s2)
  313. if s1[0]=#0 then
  314. Result:=-1 //s1 shorter than s2
  315. else
  316. Result:=1; //s1 longer than s2
  317. end;
  318. function GenericAnsiStrLComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  319. Var I : cardinal;
  320. begin
  321. Result:=0;
  322. If MaxLen=0 then exit;
  323. If S1=Nil then
  324. begin
  325. If S2=Nil Then Exit;
  326. result:=-1;
  327. exit;
  328. end;
  329. If S2=Nil then
  330. begin
  331. Result:=1;
  332. exit;
  333. end;
  334. I:=0;
  335. Repeat
  336. Result:=Ord(S1[0])-Ord(S2[0]); //!! Must be replaced by ansi characters !!
  337. Inc(S1);
  338. Inc(S2);
  339. Inc(I);
  340. Until (Result<>0) or (I=MaxLen)
  341. end;
  342. function GenericAnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  343. Var I : cardinal;
  344. begin
  345. Result:=0;
  346. If MaxLen=0 then exit;
  347. If S1=Nil then
  348. begin
  349. If S2=Nil Then Exit;
  350. result:=-1;
  351. exit;
  352. end;
  353. If S2=Nil then
  354. begin
  355. Result:=1;
  356. exit;
  357. end;
  358. I:=0;
  359. Repeat
  360. Result:=Ord(LowerCaseTable[Ord(S1[0])])-Ord(LowerCaseTable[Ord(S2[0])]); //!! Must be replaced by ansi characters !!
  361. Inc(S1);
  362. Inc(S2);
  363. Inc(I);
  364. Until (Result<>0) or (I=MaxLen)
  365. end;
  366. function GenericAnsiStrLower(Str: PChar): PChar;
  367. begin
  368. result := Str;
  369. if Str <> Nil then begin
  370. while Str^ <> #0 do begin
  371. Str^ := LowerCaseTable[byte(Str^)];
  372. Str := Str + 1;
  373. end;
  374. end;
  375. end;
  376. function GenericAnsiStrUpper(Str: PChar): PChar;
  377. begin
  378. result := Str;
  379. if Str <> Nil then begin
  380. while Str^ <> #0 do begin
  381. Str^ := UpperCaseTable[byte(Str^)];
  382. Str := Str + 1;
  383. end ;
  384. end ;
  385. end ;
  386. {$endif FPC_NOGENERICANSIROUTINES}
  387. function AnsiSameText(const s1,s2:String):Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
  388. begin
  389. AnsiSameText:=AnsiCompareText(S1,S2)=0;
  390. end;
  391. function AnsiSameStr(const s1,s2:String):Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
  392. begin
  393. AnsiSameStr:=AnsiCompareStr(S1,S2)=0;
  394. end;
  395. function AnsiLastChar(const S: string): PChar;
  396. begin
  397. //!! No multibyte yet, so we return the last one.
  398. result:=StrEnd(Pchar(pointer(S))); // strend checks for nil
  399. Dec(Result);
  400. end ;
  401. function AnsiStrLastChar(Str: PChar): PChar;
  402. begin
  403. //!! No multibyte yet, so we return the last one.
  404. result:=StrEnd(Str);
  405. Dec(Result);
  406. end ;
  407. function AnsiUpperCase(const s: string): string;{$ifdef SYSUTILSINLINE}inline;{$endif}
  408. begin
  409. result:=widestringmanager.UpperAnsiStringProc(s);
  410. end;
  411. function AnsiLowerCase(const s: string): string;{$ifdef SYSUTILSINLINE}inline;{$endif}
  412. begin
  413. result:=widestringmanager.LowerAnsiStringProc(s);
  414. end;
  415. function AnsiCompareStr(const S1, S2: string): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  416. begin
  417. result:=widestringmanager.CompareStrAnsiStringProc(s1,s2);
  418. end;
  419. function AnsiCompareText(const S1, S2: string): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  420. begin
  421. result:=widestringmanager.CompareTextAnsiStringProc(s1,s2);
  422. end;
  423. function AnsiStrComp(S1, S2: PChar): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  424. begin
  425. result:=widestringmanager.StrCompAnsiStringProc(s1,s2);
  426. end;
  427. function AnsiStrIComp(S1, S2: PChar): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  428. begin
  429. result:=widestringmanager.StrICompAnsiStringProc(s1,s2);
  430. end;
  431. function AnsiStrLComp(S1, S2: PChar; MaxLen: cardinal): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  432. begin
  433. result:=widestringmanager.StrLCompAnsiStringProc(s1,s2,maxlen);
  434. end;
  435. function AnsiStrLIComp(S1, S2: PChar; MaxLen: cardinal): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  436. begin
  437. result:=widestringmanager.StrLICompAnsiStringProc(s1,s2,maxlen);
  438. end;
  439. function AnsiStrLower(Str: PChar): PChar;{$ifdef SYSUTILSINLINE}inline;{$endif}
  440. begin
  441. result:=widestringmanager.StrLowerAnsiStringProc(Str);
  442. end;
  443. function AnsiStrUpper(Str: PChar): PChar;{$ifdef SYSUTILSINLINE}inline;{$endif}
  444. begin
  445. result:=widestringmanager.StrUpperAnsiStringProc(Str);
  446. end;
  447. {==============================================================================}
  448. { End of Ansi functions }
  449. {==============================================================================}
  450. { Trim returns a copy of S with blanks characters on the left and right stripped off }
  451. Const WhiteSpace = [#0..' '];
  452. function Trim(const S: string): string;
  453. var Ofs, Len: integer;
  454. begin
  455. len := Length(S);
  456. while (Len>0) and (S[Len] in WhiteSpace) do
  457. dec(Len);
  458. Ofs := 1;
  459. while (Ofs<=Len) and (S[Ofs] in WhiteSpace) do
  460. Inc(Ofs);
  461. result := Copy(S, Ofs, 1 + Len - Ofs);
  462. end ;
  463. { TrimLeft returns a copy of S with all blank characters on the left stripped off }
  464. function TrimLeft(const S: string): string;
  465. var i,l:integer;
  466. begin
  467. l := length(s);
  468. i := 1;
  469. while (i<=l) and (s[i] in whitespace) do
  470. inc(i);
  471. Result := copy(s, i, l);
  472. end ;
  473. { TrimRight returns a copy of S with all blank characters on the right stripped off }
  474. function TrimRight(const S: string): string;
  475. var l:integer;
  476. begin
  477. l := length(s);
  478. while (l>0) and (s[l] in whitespace) do
  479. dec(l);
  480. result := copy(s,1,l);
  481. end ;
  482. { QuotedStr returns S quoted left and right and every single quote in S
  483. replaced by two quotes }
  484. function QuotedStr(const S: string): string;
  485. begin
  486. result := AnsiQuotedStr(s, '''');
  487. end ;
  488. { AnsiQuotedStr returns S quoted left and right by Quote,
  489. and every single occurance of Quote replaced by two }
  490. function AnsiQuotedStr(const S: string; Quote: char): string;
  491. var i, j, count: integer;
  492. begin
  493. result := '' + Quote;
  494. count := length(s);
  495. i := 0;
  496. j := 0;
  497. while i < count do begin
  498. i := i + 1;
  499. if S[i] = Quote then begin
  500. result := result + copy(S, 1 + j, i - j) + Quote;
  501. j := i;
  502. end ;
  503. end ;
  504. if i <> j then
  505. result := result + copy(S, 1 + j, i - j);
  506. result := result + Quote;
  507. end ;
  508. { AnsiExtractQuotedStr returns a copy of Src with quote characters
  509. deleted to the left and right and double occurances
  510. of Quote replaced by a single Quote }
  511. function AnsiExtractQuotedStr(var Src: PChar; Quote: Char): string;
  512. var
  513. P,Q,R: PChar;
  514. begin
  515. P := Src;
  516. Q := StrEnd(P);
  517. result:='';
  518. if P=Q then exit;
  519. if P^<>quote then exit;
  520. inc(p);
  521. setlength(result,(Q-P)+1);
  522. R:=@Result[1];
  523. while P <> Q do
  524. begin
  525. R^:=P^;
  526. inc(R);
  527. if (P^ = Quote) then
  528. begin
  529. P := P + 1;
  530. if (p^ <> Quote) then
  531. begin
  532. dec(R);
  533. break;
  534. end;
  535. end;
  536. P := P + 1;
  537. end ;
  538. src:=p;
  539. SetLength(result, (R-pchar(@Result[1])));
  540. end ;
  541. { AdjustLineBreaks returns S with all CR characters not followed by LF
  542. replaced with CR/LF }
  543. // under Linux all CR characters or CR/LF combinations should be replaced with LF
  544. function AdjustLineBreaks(const S: string): string;
  545. begin
  546. Result:=AdjustLineBreaks(S,DefaultTextLineBreakStyle);
  547. end;
  548. function AdjustLineBreaks(const S: string; Style: TTextLineBreakStyle): string;
  549. var
  550. Source,Dest: PChar;
  551. DestLen: Integer;
  552. I,J,L: Longint;
  553. begin
  554. Source:=Pointer(S);
  555. L:=Length(S);
  556. DestLen:=L;
  557. I:=1;
  558. while (I<=L) do
  559. begin
  560. case S[i] of
  561. #10: if (Style=tlbsCRLF) then
  562. Inc(DestLen);
  563. #13: if (Style=tlbsCRLF) then
  564. if (I<L) and (S[i+1]=#10) then
  565. Inc(I)
  566. else
  567. Inc(DestLen)
  568. else if (I<L) and (S[I+1]=#10) then
  569. Dec(DestLen);
  570. end;
  571. Inc(I);
  572. end;
  573. if (DestLen=L) then
  574. Result:=S
  575. else
  576. begin
  577. SetLength(Result, DestLen);
  578. FillChar(Result[1],DestLen,0);
  579. Dest := Pointer(Result);
  580. J:=0;
  581. I:=0;
  582. While I<L do
  583. case Source[I] of
  584. #10: begin
  585. if Style=tlbsCRLF then
  586. begin
  587. Dest[j]:=#13;
  588. Inc(J);
  589. end;
  590. Dest[J] := #10;
  591. Inc(J);
  592. Inc(I);
  593. end;
  594. #13: begin
  595. if Style=tlbsCRLF then
  596. begin
  597. Dest[j] := #13;
  598. Inc(J);
  599. end;
  600. Dest[j]:=#10;
  601. Inc(J);
  602. Inc(I);
  603. if Source[I]=#10 then
  604. Inc(I);
  605. end;
  606. else
  607. Dest[j]:=Source[i];
  608. Inc(J);
  609. Inc(I);
  610. end;
  611. end;
  612. end;
  613. { IsValidIdent returns true if the first character of Ident is in:
  614. 'A' to 'Z', 'a' to 'z' or '_' and the following characters are
  615. on of: 'A' to 'Z', 'a' to 'z', '0'..'9' or '_' }
  616. function IsValidIdent(const Ident: string): boolean;
  617. var i, len: integer;
  618. begin
  619. result := false;
  620. len := length(Ident);
  621. if len <> 0 then begin
  622. result := Ident[1] in ['A'..'Z', 'a'..'z', '_'];
  623. i := 1;
  624. while (result) and (i < len) do begin
  625. i := i + 1;
  626. result := result and (Ident[i] in ['A'..'Z', 'a'..'z', '0'..'9', '_']);
  627. end ;
  628. end ;
  629. end ;
  630. { IntToStr returns a string representing the value of Value }
  631. function IntToStr(Value: integer): string;
  632. begin
  633. System.Str(Value, result);
  634. end ;
  635. function IntToStr(Value: int64): string;
  636. begin
  637. System.Str(Value, result);
  638. end ;
  639. function IntToStr(Value: QWord): string;
  640. begin
  641. System.Str(Value, result);
  642. end ;
  643. { IntToHex returns a string representing the hexadecimal value of Value }
  644. const
  645. HexDigits: array[0..15] of char = '0123456789ABCDEF';
  646. function IntToHex(Value: integer; Digits: integer): string;
  647. var i: integer;
  648. begin
  649. SetLength(result, digits);
  650. for i := 0 to digits - 1 do
  651. begin
  652. result[digits - i] := HexDigits[value and 15];
  653. value := value shr 4;
  654. end ;
  655. while value <> 0 do begin
  656. result := HexDigits[value and 15] + result;
  657. value := value shr 4;
  658. end;
  659. end ;
  660. function IntToHex(Value: int64; Digits: integer): string;
  661. var i: integer;
  662. begin
  663. SetLength(result, digits);
  664. for i := 0 to digits - 1 do
  665. begin
  666. result[digits - i] := HexDigits[value and 15];
  667. value := value shr 4;
  668. end ;
  669. while value <> 0 do begin
  670. result := HexDigits[value and 15] + result;
  671. value := value shr 4;
  672. end;
  673. end ;
  674. function IntToHex(Value: QWord; Digits: integer): string;
  675. begin
  676. result:=IntToHex(Int64(Value),Digits);
  677. end;
  678. function TryStrToInt(const s: string; out i : integer) : boolean;
  679. var Error : word;
  680. begin
  681. Val(s, i, Error);
  682. TryStrToInt:=Error=0
  683. end;
  684. { StrToInt converts the string S to an integer value,
  685. if S does not represent a valid integer value EConvertError is raised }
  686. function StrToInt(const S: string): integer;
  687. var Error: word;
  688. begin
  689. Val(S, result, Error);
  690. if Error <> 0 then raise EConvertError.createfmt(SInvalidInteger,[S]);
  691. end ;
  692. function StrToInt64(const S: string): int64;
  693. var Error: word;
  694. begin
  695. Val(S, result, Error);
  696. if Error <> 0 then raise EConvertError.createfmt(SInvalidInteger,[S]);
  697. end;
  698. function TryStrToInt64(const s: string; Out i : int64) : boolean;
  699. var Error : word;
  700. begin
  701. Val(s, i, Error);
  702. TryStrToInt64:=Error=0
  703. end;
  704. function StrToQWord(const s: string): QWord;
  705. var Error: word;
  706. begin
  707. Val(S, result, Error);
  708. if Error <> 0 then raise EConvertError.createfmt(SInvalidInteger,[S]);
  709. end;
  710. function TryStrToQWord(const s: string; Out Q: QWord): boolean;
  711. var Error : word;
  712. begin
  713. Val(s, Q, Error);
  714. TryStrToQWord:=Error=0
  715. end;
  716. { StrToIntDef converts the string S to an integer value,
  717. Default is returned in case S does not represent a valid integer value }
  718. function StrToIntDef(const S: string; Default: integer): integer;
  719. var Error: word;
  720. begin
  721. Val(S, result, Error);
  722. if Error <> 0 then result := Default;
  723. end ;
  724. { StrToInt64Def converts the string S to an int64 value,
  725. Default is returned in case S does not represent a valid int64 value }
  726. function StrToInt64Def(const S: string; Default: int64): int64;
  727. var Error: word;
  728. begin
  729. Val(S, result, Error);
  730. if Error <> 0 then result := Default;
  731. end ;
  732. { StrToQWordDef converts the string S to an QWord value,
  733. Default is returned in case S does not represent a valid QWord value }
  734. function StrToQWordDef(const S: string; Default: QWord): QWord;
  735. var Error: word;
  736. begin
  737. Val(S, result, Error);
  738. if Error <> 0 then result := Default;
  739. end;
  740. { LoadStr returns the string resource Ident. }
  741. function LoadStr(Ident: integer): string;
  742. begin
  743. result:='';
  744. end ;
  745. { FmtLoadStr returns the string resource Ident and formats it accordingly }
  746. function FmtLoadStr(Ident: integer; const Args: array of const): string;
  747. begin
  748. result:='';
  749. end;
  750. Const
  751. feInvalidFormat = 1;
  752. feMissingArgument = 2;
  753. feInvalidArgIndex = 3;
  754. {$ifdef fmtdebug}
  755. Procedure Log (Const S: String);
  756. begin
  757. Writeln (S);
  758. end;
  759. {$endif}
  760. Procedure DoFormatError (ErrCode : Longint);
  761. Var
  762. S : String;
  763. begin
  764. //!! must be changed to contain format string...
  765. S:='';
  766. Case ErrCode of
  767. feInvalidFormat : raise EConvertError.Createfmt(SInvalidFormat,[s]);
  768. feMissingArgument : raise EConvertError.Createfmt(SArgumentMissing,[s]);
  769. feInvalidArgIndex : raise EConvertError.Createfmt(SInvalidArgIndex,[s]);
  770. end;
  771. end;
  772. { we've no templates, but with includes we can simulate this :) }
  773. {$macro on}
  774. {$define INFORMAT}
  775. {$define TFormatString:=ansistring}
  776. {$define TFormatChar:=char}
  777. Function Format (Const Fmt : AnsiString; const Args : Array of const; const FormatSettings: TFormatSettings) : AnsiString;
  778. {$i sysformt.inc}
  779. {$undef TFormatString}
  780. {$undef TFormatChar}
  781. {$undef INFORMAT}
  782. {$macro off}
  783. Function Format (Const Fmt : AnsiString; const Args : Array of const) : AnsiString;
  784. begin
  785. Result:=Format(Fmt,Args,DefaultFormatSettings);
  786. end;
  787. Function FormatBuf (Var Buffer; BufLen : Cardinal; Const Fmt; fmtLen : Cardinal; Const Args : Array of const; Const FormatSettings: TFormatSettings) : Cardinal;
  788. Var S,F : String;
  789. begin
  790. Setlength(F,fmtlen);
  791. if fmtlen > 0 then
  792. Move(fmt,F[1],fmtlen);
  793. S:=Format (F,Args,FormatSettings);
  794. If Cardinal(Length(S))<Buflen then
  795. Result:=Length(S)
  796. else
  797. Result:=Buflen;
  798. Move(S[1],Buffer,Result);
  799. end;
  800. Function FormatBuf (Var Buffer; BufLen : Cardinal;
  801. Const Fmt; fmtLen : Cardinal;
  802. Const Args : Array of const) : Cardinal;
  803. begin
  804. Result:=FormatBuf(Buffer,BufLen,Fmt,FmtLen,Args,DefaultFormatSettings);
  805. end;
  806. Procedure FmtStr(Var Res: string; const Fmt : string; Const args: Array of const; Const FormatSettings: TFormatSettings);
  807. begin
  808. Res:=Format(fmt,Args,FormatSettings);
  809. end;
  810. Procedure FmtStr(Var Res: String; Const Fmt : String; Const args: Array of const);
  811. begin
  812. FmtStr(Res,Fmt,Args,DefaultFormatSettings);
  813. end;
  814. Function StrFmt(Buffer,Fmt : PChar; Const args: Array of const) : Pchar;
  815. begin
  816. Result:=StrFmt(Buffer,Fmt,Args,DefaultFormatSettings);
  817. end;
  818. Function StrFmt(Buffer,Fmt : PChar; Const Args: Array of const; Const FormatSettings: TFormatSettings): PChar;
  819. begin
  820. Buffer[FormatBuf(Buffer^,Maxint,Fmt^,strlen(fmt),args,FormatSettings)]:=#0;
  821. Result:=Buffer;
  822. end;
  823. Function StrLFmt(Buffer : PCHar; Maxlen : Cardinal;Fmt : PChar; Const args: Array of const) : Pchar;
  824. begin
  825. Result:=StrLFmt(Buffer,MaxLen,Fmt,Args,DefaultFormatSettings);
  826. end;
  827. Function StrLFmt(Buffer : PCHar; Maxlen : Cardinal;Fmt : PChar; Const args: Array of const; Const FormatSettings: TFormatSettings) : Pchar;
  828. begin
  829. Buffer[FormatBuf(Buffer^,MaxLen,Fmt^,strlen(fmt),args,FormatSettings)]:=#0;
  830. Result:=Buffer;
  831. end;
  832. {$ifndef FPUNONE}
  833. Function StrToFloat(Const S: String): Extended;
  834. begin
  835. Result:=StrToFloat(S,DefaultFormatSettings);
  836. end;
  837. Function StrToFloat(Const S : String; Const FormatSettings: TFormatSettings) : Extended;
  838. Begin // texttofloat handles NIL properly
  839. If Not TextToFloat(Pchar(pointer(S)),Result,FormatSettings) then
  840. Raise EConvertError.createfmt(SInValidFLoat,[S]);
  841. End;
  842. function StrToFloatDef(const S: string; const Default: Extended): Extended;
  843. begin
  844. Result:=StrToFloatDef(S,Default,DefaultFormatSettings);
  845. end;
  846. Function StrToFloatDef(Const S: String; Const Default: Extended; Const FormatSettings: TFormatSettings): Extended;
  847. begin
  848. if not TextToFloat(PChar(pointer(S)),Result,fvExtended,FormatSettings) then
  849. Result:=Default;
  850. end;
  851. Function TextToFloat(Buffer: PChar; Out Value: Extended; Const FormatSettings: TFormatSettings): Boolean;
  852. Var
  853. E,P : Integer;
  854. S : String;
  855. Begin
  856. S:=StrPas(Buffer);
  857. //ThousandSeparator not allowed as by Delphi specs
  858. if (FormatSettings.ThousandSeparator <> FormatSettings.DecimalSeparator) and
  859. (Pos(FormatSettings.ThousandSeparator, S) <> 0) then
  860. begin
  861. Result := False;
  862. Exit;
  863. end;
  864. if (FormatSettings.DecimalSeparator <> '.') and
  865. (Pos('.', S) <>0) then
  866. begin
  867. Result := False;
  868. Exit;
  869. end;
  870. P:=Pos(FormatSettings.DecimalSeparator,S);
  871. If (P<>0) Then
  872. S[P] := '.';
  873. Val(trim(S),Value,E);
  874. Result:=(E=0);
  875. End;
  876. Function TextToFloat(Buffer: PChar; Out Value: Extended): Boolean;
  877. begin
  878. Result:=TextToFloat(Buffer,Value,DefaultFormatSettings);
  879. end;
  880. Function TextToFloat(Buffer: PChar; Out Value; ValueType: TFloatValue): Boolean;
  881. begin
  882. Result:=TextToFloat(Buffer,Value,ValueType,DefaultFormatSettings);
  883. end;
  884. Function TextToFloat(Buffer: PChar; Out Value; ValueType: TFloatValue; Const FormatSettings: TFormatSettings): Boolean;
  885. Var
  886. E,P : Integer;
  887. S : String;
  888. {$ifndef FPC_HAS_STR_CURRENCY}
  889. TempValue: extended;
  890. {$endif FPC_HAS_STR_CURRENCY}
  891. Begin
  892. S:=StrPas(Buffer);
  893. //ThousandSeparator not allowed as by Delphi specs
  894. if (FormatSettings.ThousandSeparator <> FormatSettings.DecimalSeparator) and
  895. (Pos(FormatSettings.ThousandSeparator, S) <> 0) then
  896. begin
  897. Result := False;
  898. Exit;
  899. end;
  900. if (FormatSettings.DecimalSeparator <> '.') and
  901. (Pos('.', S) <>0) then
  902. begin
  903. Result := False;
  904. Exit;
  905. end;
  906. P:=Pos(FormatSettings.DecimalSeparator,S);
  907. If (P<>0) Then
  908. S[P] := '.';
  909. case ValueType of
  910. fvCurrency:
  911. {$ifdef FPC_HAS_STR_CURRENCY}
  912. Val(S,Currency(Value),E);
  913. {$else FPC_HAS_STR_CURRENCY}
  914. begin
  915. // needed for platforms where Currency = Int64
  916. Val(S,TempValue,E);
  917. Currency(Value) := TempValue;
  918. end;
  919. {$endif FPC_HAS_STR_CURRENCY}
  920. fvExtended:
  921. Val(S,Extended(Value),E);
  922. fvDouble:
  923. Val(S,Double(Value),E);
  924. fvSingle:
  925. Val(S,Single(Value),E);
  926. fvComp:
  927. Val(S,Comp(Value),E);
  928. fvReal:
  929. Val(S,Real(Value),E);
  930. end;
  931. Result:=(E=0);
  932. End;
  933. Function TryStrToFloat(Const S : String; Out Value: Single): Boolean;
  934. begin
  935. Result:=TryStrToFloat(S,Value,DefaultFormatSettings);
  936. end;
  937. Function TryStrToFloat(Const S : String; Out Value: Single; Const FormatSettings: TFormatSettings): Boolean;
  938. Begin
  939. Result := TextToFloat(PChar(pointer(S)), Value, fvSingle,FormatSettings);
  940. End;
  941. Function TryStrToFloat(Const S : String; Out Value: Double): Boolean;
  942. begin
  943. Result:=TryStrToFloat(S,Value,DefaultFormatSettings);
  944. end;
  945. Function TryStrToFloat(Const S : String; Out Value: Double; Const FormatSettings: TFormatSettings): Boolean;
  946. Begin
  947. Result := TextToFloat(PChar(pointer(S)), Value, fvDouble,FormatSettings);
  948. End;
  949. {$ifdef FPC_HAS_TYPE_EXTENDED}
  950. Function TryStrToFloat(Const S : String; Out Value: Extended): Boolean;
  951. begin
  952. Result:=TryStrToFloat(S,Value,DefaultFormatSettings);
  953. end;
  954. Function TryStrToFloat(Const S : String; Out Value: Extended; Const FormatSettings: TFormatSettings): Boolean;
  955. Begin
  956. Result := TextToFloat(PChar(pointer(S)), Value,FormatSettings);
  957. End;
  958. {$endif FPC_HAS_TYPE_EXTENDED}
  959. const
  960. {$ifdef FPC_HAS_TYPE_EXTENDED}
  961. maxdigits = 17;
  962. {$else}
  963. maxdigits = 15;
  964. {$endif}
  965. Function FloatToStrFIntl(const Value; format: TFloatFormat; Precision, Digits: Integer; ValueType: TFloatValue; Const FormatSettings: TFormatSettings): String;
  966. Var
  967. P: Integer;
  968. Negative, TooSmall, TooLarge: Boolean;
  969. DS: Char;
  970. Begin
  971. DS:=FormatSettings.DecimalSeparator;
  972. Case format Of
  973. ffGeneral:
  974. Begin
  975. case ValueType of
  976. fvCurrency:
  977. begin
  978. If (Precision = -1) Or (Precision > 19) Then Precision := 19;
  979. TooSmall:=False;
  980. end;
  981. else
  982. begin
  983. If (Precision = -1) Or (Precision > maxdigits) Then Precision := maxdigits;
  984. TooSmall := (Abs(Extended(Value)) < 0.00001) and (Extended(Value)<>0.0);
  985. end;
  986. end;
  987. If Not TooSmall Then
  988. Begin
  989. case ValueType of
  990. fvDouble:
  991. Str(Double(Extended(Value)):0:precision, Result);
  992. fvSingle:
  993. Str(Single(Extended(Value)):0:precision, Result);
  994. fvCurrency:
  995. {$ifdef FPC_HAS_STR_CURRENCY}
  996. Str(Currency(Value):0:precision, Result);
  997. {$else}
  998. Str(Extended(Currency(Value)):0:precision, Result);
  999. {$endif FPC_HAS_STR_CURRENCY}
  1000. else
  1001. Str(Extended(Value):0:precision, Result);
  1002. end;
  1003. P := Pos('.', Result);
  1004. if P<>0 then
  1005. Result[P] := DS;
  1006. TooLarge :=(P > Precision + 1) or (Pos('E', Result)<>0);
  1007. End;
  1008. If TooSmall Or TooLarge Then
  1009. begin
  1010. Result := FloatToStrFIntl(Value, ffExponent, Precision, Digits, ValueType,FormatSettings);
  1011. // Strip unneeded zeroes.
  1012. P:=Pos('E',result)-1;
  1013. If P<>-1 then
  1014. begin
  1015. { delete superfluous +? }
  1016. if result[p+2]='+' then
  1017. system.Delete(Result,P+2,1);
  1018. While (P>1) and (Result[P]='0') do
  1019. begin
  1020. system.Delete(Result,P,1);
  1021. Dec(P);
  1022. end;
  1023. If (P>0) and (Result[P]=DS) Then
  1024. begin
  1025. system.Delete(Result,P,1);
  1026. Dec(P);
  1027. end;
  1028. end;
  1029. end
  1030. else if (P<>0) then // we have a decimalseparator
  1031. begin
  1032. { it seems that in this unit "precision" must mean "number of }
  1033. { significant digits" rather than "number of digits after the }
  1034. { decimal point" (as it does in the system unit) -> adjust }
  1035. { (precision+1 to count the decimal point character) }
  1036. if Result[1] = '-' then
  1037. Inc(Precision);
  1038. if (Length(Result) > Precision + 1) and
  1039. (Precision + 1 > P) then
  1040. begin
  1041. P := Precision + 1;
  1042. SetLength(Result,P);
  1043. end;
  1044. P := Length(Result);
  1045. While (P>0) and (Result[P] = '0') Do
  1046. Dec(P);
  1047. If (P>0) and (Result[P]=DS) Then
  1048. Dec(P);
  1049. SetLength(Result, P);
  1050. end;
  1051. End;
  1052. ffExponent:
  1053. Begin
  1054. If (Precision = -1) Or (Precision > maxdigits) Then Precision := maxdigits;
  1055. case ValueType of
  1056. fvDouble:
  1057. Str(Double(Extended(Value)):Precision+7, Result);
  1058. fvSingle:
  1059. Str(Single(Extended(Value)):Precision+6, Result);
  1060. fvCurrency:
  1061. {$ifdef FPC_HAS_STR_CURRENCY}
  1062. Str(Currency(Value):Precision+6, Result);
  1063. {$else}
  1064. Str(Extended(Currency(Value)):Precision+8, Result);
  1065. {$endif FPC_HAS_STR_CURRENCY}
  1066. else
  1067. Str(Extended(Value):Precision+8, Result);
  1068. end;
  1069. { Delete leading spaces }
  1070. while Result[1] = ' ' do
  1071. System.Delete(Result, 1, 1);
  1072. if Result[1] = '-' then
  1073. Result[3] := DS
  1074. else
  1075. Result[2] := DS;
  1076. P:=Pos('E',Result);
  1077. if P <> 0 then
  1078. begin
  1079. Inc(P, 2);
  1080. if Digits > 4 then
  1081. Digits:=4;
  1082. Digits:=Length(Result) - P - Digits + 1;
  1083. if Digits < 0 then
  1084. insert(copy('0000',1,-Digits),Result,P)
  1085. else
  1086. while (Digits > 0) and (Result[P] = '0') do
  1087. begin
  1088. System.Delete(Result, P, 1);
  1089. if P > Length(Result) then
  1090. begin
  1091. System.Delete(Result, P - 2, 2);
  1092. break;
  1093. end;
  1094. Dec(Digits);
  1095. end;
  1096. end;
  1097. End;
  1098. ffFixed:
  1099. Begin
  1100. If Digits = -1 Then Digits := 2
  1101. Else If Digits > 18 Then Digits := 18;
  1102. case ValueType of
  1103. fvDouble:
  1104. Str(Double(Extended(Value)):0:Digits, Result);
  1105. fvSingle:
  1106. Str(Single(Extended(Value)):0:Digits, Result);
  1107. fvCurrency:
  1108. {$ifdef FPC_HAS_STR_CURRENCY}
  1109. Str(Currency(Value):0:Digits, Result);
  1110. {$else}
  1111. Str(Extended(Currency(Value)):0:Digits, Result);
  1112. {$endif FPC_HAS_STR_CURRENCY}
  1113. else
  1114. Str(Extended(Value):0:Digits, Result);
  1115. end;
  1116. If Result[1] = ' ' Then
  1117. System.Delete(Result, 1, 1);
  1118. P := Pos('.', Result);
  1119. If P <> 0 Then Result[P] := DS;
  1120. End;
  1121. ffNumber:
  1122. Begin
  1123. If Digits = -1 Then Digits := 2
  1124. Else If Digits > maxdigits Then Digits := maxdigits;
  1125. case ValueType of
  1126. fvDouble:
  1127. Str(Double(Extended(Value)):0:Digits, Result);
  1128. fvSingle:
  1129. Str(Single(Extended(Value)):0:Digits, Result);
  1130. fvCurrency:
  1131. {$ifdef FPC_HAS_STR_CURRENCY}
  1132. Str(Currency(Value):0:Digits, Result);
  1133. {$else}
  1134. Str(Extended(Currency(Value)):0:Digits, Result);
  1135. {$endif FPC_HAS_STR_CURRENCY}
  1136. else
  1137. Str(Extended(Value):0:Digits, Result);
  1138. end;
  1139. If Result[1] = ' ' Then System.Delete(Result, 1, 1);
  1140. P := Pos('.', Result);
  1141. If P <> 0 Then
  1142. Result[P] := DS
  1143. else
  1144. P := Length(Result)+1;
  1145. Dec(P, 3);
  1146. While (P > 1) Do
  1147. Begin
  1148. If (Result[P - 1] <> '-') And (FormatSettings.ThousandSeparator <> #0) Then
  1149. Insert(FormatSettings.ThousandSeparator, Result, P);
  1150. Dec(P, 3);
  1151. End;
  1152. End;
  1153. ffCurrency:
  1154. Begin
  1155. If Digits = -1 Then Digits := FormatSettings.CurrencyDecimals
  1156. Else If Digits > 18 Then Digits := 18;
  1157. case ValueType of
  1158. fvDouble:
  1159. Str(Double(Extended(Value)):0:Digits, Result);
  1160. fvSingle:
  1161. Str(Single(Extended(Value)):0:Digits, Result);
  1162. fvCurrency:
  1163. {$ifdef FPC_HAS_STR_CURRENCY}
  1164. Str(Currency(Value):0:Digits, Result);
  1165. {$else}
  1166. Str(Extended(Currency(Value)):0:Digits, Result);
  1167. {$endif FPC_HAS_STR_CURRENCY}
  1168. else
  1169. Str(Extended(Value):0:Digits, Result);
  1170. end;
  1171. Negative:=Result[1] = '-';
  1172. if Negative then
  1173. System.Delete(Result, 1, 1);
  1174. P := Pos('.', Result);
  1175. If P <> 0 Then Result[P] := DS;
  1176. Dec(P, 3);
  1177. While (P > 1) Do
  1178. Begin
  1179. If FormatSettings.ThousandSeparator<>#0 Then
  1180. Insert(FormatSettings.ThousandSeparator, Result, P);
  1181. Dec(P, 3);
  1182. End;
  1183. If Not Negative Then
  1184. Begin
  1185. Case FormatSettings.CurrencyFormat Of
  1186. 0: Result := FormatSettings.CurrencyString + Result;
  1187. 1: Result := Result + FormatSettings.CurrencyString;
  1188. 2: Result := FormatSettings.CurrencyString + ' ' + Result;
  1189. 3: Result := Result + ' ' + FormatSettings.CurrencyString;
  1190. End
  1191. End
  1192. Else
  1193. Begin
  1194. Case NegCurrFormat Of
  1195. 0: Result := '(' + FormatSettings.CurrencyString + Result + ')';
  1196. 1: Result := '-' + FormatSettings.CurrencyString + Result;
  1197. 2: Result := FormatSettings.CurrencyString + '-' + Result;
  1198. 3: Result := FormatSettings.CurrencyString + Result + '-';
  1199. 4: Result := '(' + Result + FormatSettings.CurrencyString + ')';
  1200. 5: Result := '-' + Result + FormatSettings.CurrencyString;
  1201. 6: Result := Result + '-' + FormatSettings.CurrencyString;
  1202. 7: Result := Result + FormatSettings.CurrencyString + '-';
  1203. 8: Result := '-' + Result + ' ' + FormatSettings.CurrencyString;
  1204. 9: Result := '-' + FormatSettings.CurrencyString + ' ' + Result;
  1205. 10: Result := FormatSettings.CurrencyString + ' ' + Result + '-';
  1206. End;
  1207. End;
  1208. End;
  1209. End;
  1210. End;
  1211. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1212. Function FloatToStr(Value: Extended; Const FormatSettings: TFormatSettings): String;
  1213. Begin
  1214. Result := FloatToStrFIntl(Value, ffGeneral, 15, 0, fvExtended,FormatSettings);
  1215. End;
  1216. Function FloatToStr(Value: Extended): String;
  1217. begin
  1218. Result:=FloatToStr(Value,DefaultFormatSettings);
  1219. end;
  1220. {$endif FPC_HAS_TYPE_EXTENDED}
  1221. Function FloatToStr(Value: Currency; Const FormatSettings: TFormatSettings): String;
  1222. Begin
  1223. Result := FloatToStrFIntl(Value, ffGeneral, 15, 0, fvCurrency,FormatSettings);
  1224. End;
  1225. Function FloatToStr(Value: Currency): String;
  1226. begin
  1227. Result:=FloatToStr(Value,DefaultFormatSettings);
  1228. end;
  1229. Function FloatToStr(Value: Double; Const FormatSettings: TFormatSettings): String;
  1230. var
  1231. e: Extended;
  1232. Begin
  1233. e := Value;
  1234. Result := FloatToStrFIntl(e, ffGeneral, 15, 0, fvDouble,FormatSettings);
  1235. End;
  1236. Function FloatToStr(Value: Double): String;
  1237. begin
  1238. Result:=FloatToStr(Value,DefaultFormatSettings);
  1239. end;
  1240. Function FloatToStr(Value: Single; Const FormatSettings: TFormatSettings): String;
  1241. var
  1242. e: Extended;
  1243. Begin
  1244. e := Value;
  1245. Result := FloatToStrFIntl(e, ffGeneral, 15, 0, fvSingle,FormatSettings);
  1246. End;
  1247. Function FloatToStr(Value: Single): String;
  1248. begin
  1249. Result:=FloatToStr(Value,DefaultFormatSettings);
  1250. end;
  1251. Function FloatToStr(Value: Comp; Const FormatSettings: TFormatSettings): String;
  1252. var
  1253. e: Extended;
  1254. Begin
  1255. e := Value;
  1256. Result := FloatToStrFIntl(e, ffGeneral, 15, 0, fvComp,FormatSettings);
  1257. End;
  1258. Function FloatToStr(Value: Comp): String;
  1259. begin
  1260. Result:=FloatToStr(Value,DefaultFormatSettings);
  1261. end;
  1262. {$ifndef FPC_COMP_IS_INT64}
  1263. Function FloatToStr(Value: Int64): String;
  1264. begin
  1265. Result:=FloatToStr(Value,DefaultFormatSettings);
  1266. end;
  1267. Function FloatToStr(Value: Int64; Const FormatSettings: TFormatSettings): String;
  1268. var
  1269. e: Extended;
  1270. Begin
  1271. e := Comp(Value);
  1272. Result := FloatToStrFIntl(e, ffGeneral, 15, 0, fvComp,FormatSettings);
  1273. End;
  1274. {$endif FPC_COMP_IS_INT64}
  1275. Function FloatToText(Buffer: PChar; Value: Extended; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): Longint;
  1276. Var
  1277. Tmp: String[40];
  1278. Begin
  1279. Tmp := FloatToStrF(Value, format, Precision, Digits,FormatSettings);
  1280. Result := Length(Tmp);
  1281. Move(Tmp[1], Buffer[0], Result);
  1282. End;
  1283. Function FloatToText(Buffer: PChar; Value: Extended; format: TFloatFormat; Precision, Digits: Integer): Longint;
  1284. begin
  1285. Result:=FloatToText(Buffer,Value,Format,Precision,Digits,DefaultFormatSettings);
  1286. end;
  1287. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1288. Function FloatToStrF(Value: Extended; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1289. begin
  1290. Result := FloatToStrFIntl(value,format,precision,digits,fvExtended,FormatSettings);
  1291. end;
  1292. Function FloatToStrF(Value: Extended; format: TFloatFormat; Precision, Digits: Integer): String;
  1293. begin
  1294. Result:=FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
  1295. end;
  1296. {$endif}
  1297. Function FloatToStrF(Value: Currency; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1298. begin
  1299. Result := FloatToStrFIntl(value,format,precision,digits,fvCurrency,FormatSettings);
  1300. end;
  1301. Function FloatToStrF(Value: Currency; format: TFloatFormat; Precision, Digits: Integer): String;
  1302. begin
  1303. Result:=FloatToStrF(Value,format,Precision,Digits,DefaultFormatSettings);
  1304. end;
  1305. Function FloatToStrF(Value: Double; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1306. var
  1307. e: Extended;
  1308. begin
  1309. e := Value;
  1310. result := FloatToStrFIntl(e,format,precision,digits,fvDouble,FormatSettings);
  1311. end;
  1312. Function FloatToStrF(Value: Double; format: TFloatFormat; Precision, Digits: Integer): String;
  1313. begin
  1314. Result:= FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
  1315. end;
  1316. Function FloatToStrF(Value: Single; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1317. var
  1318. e: Extended;
  1319. begin
  1320. e:=Value;
  1321. result := FloatToStrFIntl(e,format,precision,digits,fvSingle,FormatSettings);
  1322. end;
  1323. Function FloatToStrF(Value: Single; format: TFloatFormat; Precision, Digits: Integer): String;
  1324. begin
  1325. Result:= FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
  1326. end;
  1327. Function FloatToStrF(Value: Comp; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1328. var
  1329. e: Extended;
  1330. begin
  1331. e := Value;
  1332. Result := FloatToStrFIntl(e,format,precision,digits,fvComp,FormatSettings);
  1333. end;
  1334. Function FloatToStrF(Value: Comp; format: TFloatFormat; Precision, Digits: Integer): String;
  1335. begin
  1336. Result:=FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
  1337. end;
  1338. {$ifndef FPC_COMP_IS_INT64}
  1339. Function FloatToStrF(Value: Int64; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1340. var
  1341. e: Extended;
  1342. begin
  1343. e := Comp(Value);
  1344. result := FloatToStrFIntl(e,format,precision,digits,fvComp,FormatSettings);
  1345. end;
  1346. Function FloatToStrF(Value: Int64; format: TFloatFormat; Precision, Digits: Integer): String;
  1347. begin
  1348. Result:=FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
  1349. end;
  1350. {$endif FPC_COMP_IS_INT64}
  1351. Function CurrToStrF(Value: Currency; Format: TFloatFormat; Digits: Integer; Const FormatSettings: TFormatSettings): string;
  1352. begin
  1353. result:=FloatToStrF(Value,Format,19,Digits,FormatSettings);
  1354. end;
  1355. Function CurrToStrF(Value: Currency; Format: TFloatFormat; Digits: Integer): string;
  1356. begin
  1357. Result:=CurrToStrF(Value,Format,Digits,DefaultFormatSettings);
  1358. end;
  1359. Function FloatToDateTime (Const Value : Extended) : TDateTime;
  1360. begin
  1361. If (Value<MinDateTime) or (Value>MaxDateTime) then
  1362. Raise EConvertError.CreateFmt (SInvalidDateTime,[Value]);
  1363. Result:=Value;
  1364. end;
  1365. function TryFloatToCurr(const Value: Extended; var AResult: Currency): Boolean;
  1366. begin
  1367. Result:=(Value>=MinCurrency) and (Value<=MaxCurrency);
  1368. if Result then
  1369. AResult := Value;
  1370. end;
  1371. function FloatToCurr(const Value: Extended): Currency;
  1372. begin
  1373. if not TryFloatToCurr(Value, Result) then
  1374. Raise EConvertError.CreateFmt(SInvalidCurrency, [FloatToStr(Value)]);
  1375. end;
  1376. Function CurrToStr(Value: Currency): string;
  1377. begin
  1378. Result:=FloatToStrF(Value,ffGeneral,-1,0);
  1379. end;
  1380. function StrToCurr(const S: string): Currency;
  1381. begin
  1382. if not TextToFloat(PChar(pointer(S)), Result, fvCurrency) then
  1383. Raise EConvertError.createfmt(SInValidFLoat,[S]);
  1384. end;
  1385. Function TryStrToCurr(Const S : String; Out Value: Currency): Boolean;
  1386. Begin
  1387. Result := TextToFloat(PChar(pointer(S)), Value, fvCurrency);
  1388. End;
  1389. function StrToCurrDef(const S: string; Default : Currency): Currency;
  1390. begin
  1391. if not TextToFloat(PChar(pointer(S)), Result, fvCurrency) then
  1392. Result:=Default;
  1393. end;
  1394. {$endif FPUNONE}
  1395. function AnsiDequotedStr(const S: string; AQuote: Char): string;
  1396. var p : pchar;
  1397. begin
  1398. p:=pchar(pointer(s)); // work around CONST. Ansiextract is safe for nil
  1399. result:=AnsiExtractquotedStr(p,AQuote);
  1400. if result='' Then
  1401. result:=s;
  1402. end;
  1403. function StrToBool(const S: string): Boolean;
  1404. begin
  1405. if not(TryStrToBool(S,Result)) then
  1406. Raise EConvertError.CreateFmt(SInvalidBoolean,[S]);
  1407. end;
  1408. function BoolToStr(B: Boolean;UseBoolStrs:Boolean=False): string;
  1409. procedure CheckStrs;
  1410. begin
  1411. If Length(TrueBoolStrs)=0 then
  1412. begin
  1413. SetLength(TrueBoolStrs,1);
  1414. TrueBoolStrs[0]:='True';
  1415. end;
  1416. If Length(FalseBoolStrs)=0 then
  1417. begin
  1418. SetLength(FalseBoolStrs,1);
  1419. FalseBoolStrs[0]:='False';
  1420. end;
  1421. end;
  1422. begin
  1423. if UseBoolStrs Then
  1424. begin
  1425. CheckStrs;
  1426. if B then
  1427. Result:=TrueBoolStrs[0]
  1428. else
  1429. Result:=FalseBoolStrs[0];
  1430. end
  1431. else
  1432. If B then
  1433. Result:='-1'
  1434. else
  1435. Result:='0';
  1436. end;
  1437. // from textmode IDE util funcs.
  1438. function BoolToStr(B: boolean; const TrueS, FalseS: string): string;
  1439. begin
  1440. if B then Result:=TrueS else BoolToStr:=FalseS;
  1441. end;
  1442. function StrToBoolDef(const S: string; Default: Boolean): Boolean;
  1443. begin
  1444. if not(TryStrToBool(S,Result)) then
  1445. Result:=Default;
  1446. end;
  1447. function TryStrToBool(const S: string; out Value: Boolean): Boolean;
  1448. Var
  1449. Temp : String;
  1450. {$ifdef FPUNONE}
  1451. D : Longint;
  1452. {$else}
  1453. D : Double;
  1454. {$endif}
  1455. Code: word;
  1456. begin
  1457. Temp:=upcase(S);
  1458. Val(temp,D,code);
  1459. Result:=true;
  1460. If Code=0 then
  1461. {$ifdef FPUNONE}
  1462. Value:=(D<>0)
  1463. {$else}
  1464. Value:=(D<>0.0)
  1465. {$endif}
  1466. else If Temp='TRUE' then
  1467. Value:=true
  1468. else if Temp='FALSE' then
  1469. Value:=false
  1470. else
  1471. Result:=false;
  1472. end;
  1473. {$ifndef FPUNONE}
  1474. Function FloatToTextFmt(Buffer: PChar; Value: Extended; format: PChar): Integer;
  1475. begin
  1476. Result:=FloatToTextFmt(Buffer,Value,Format,DefaultFormatSettings);
  1477. end;
  1478. Function FloatToTextFmt(Buffer: PChar; Value: Extended; format: PChar;FormatSettings : TFormatSettings): Integer;
  1479. Var
  1480. Digits: String[40]; { String Of Digits }
  1481. Exponent: String[8]; { Exponent strin }
  1482. FmtStart, FmtStop: PChar; { Start And End Of relevant part }
  1483. { Of format String }
  1484. ExpFmt, ExpSize: Integer; { Type And Length Of }
  1485. { exponential format chosen }
  1486. Placehold: Array[1..4] Of Integer; { Number Of placeholders In All }
  1487. { four Sections }
  1488. thousand: Boolean; { thousand separators? }
  1489. UnexpectedDigits: Integer; { Number Of unexpected Digits that }
  1490. { have To be inserted before the }
  1491. { First placeholder. }
  1492. DigitExponent: Integer; { Exponent Of First digit In }
  1493. { Digits Array. }
  1494. { Find end of format section starting at P. False, if empty }
  1495. Function GetSectionEnd(Var P: PChar): Boolean;
  1496. Var
  1497. C: Char;
  1498. SQ, DQ: Boolean;
  1499. Begin
  1500. Result := False;
  1501. SQ := False;
  1502. DQ := False;
  1503. C := P[0];
  1504. While (C<>#0) And ((C<>';') Or SQ Or DQ) Do
  1505. Begin
  1506. Result := True;
  1507. Case C Of
  1508. #34: If Not SQ Then DQ := Not DQ;
  1509. #39: If Not DQ Then SQ := Not SQ;
  1510. End;
  1511. Inc(P);
  1512. C := P[0];
  1513. End;
  1514. End;
  1515. { Find start and end of format section to apply. If section doesn't exist,
  1516. use section 1. If section 2 is used, the sign of value is ignored. }
  1517. Procedure GetSectionRange(section: Integer);
  1518. Var
  1519. Sec: Array[1..3] Of PChar;
  1520. SecOk: Array[1..3] Of Boolean;
  1521. Begin
  1522. Sec[1] := format;
  1523. SecOk[1] := GetSectionEnd(Sec[1]);
  1524. If section > 1 Then
  1525. Begin
  1526. Sec[2] := Sec[1];
  1527. If Sec[2][0] <> #0 Then
  1528. Inc(Sec[2]);
  1529. SecOk[2] := GetSectionEnd(Sec[2]);
  1530. If section > 2 Then
  1531. Begin
  1532. Sec[3] := Sec[2];
  1533. If Sec[3][0] <> #0 Then
  1534. Inc(Sec[3]);
  1535. SecOk[3] := GetSectionEnd(Sec[3]);
  1536. End;
  1537. End;
  1538. If Not SecOk[1] Then
  1539. FmtStart := Nil
  1540. Else
  1541. Begin
  1542. If Not SecOk[section] Then
  1543. section := 1
  1544. Else If section = 2 Then
  1545. Value := -Value; { Remove sign }
  1546. If section = 1 Then FmtStart := format Else
  1547. Begin
  1548. FmtStart := Sec[section - 1];
  1549. Inc(FmtStart);
  1550. End;
  1551. FmtStop := Sec[section];
  1552. End;
  1553. End;
  1554. { Find format section ranging from FmtStart to FmtStop. }
  1555. Procedure GetFormatOptions;
  1556. Var
  1557. Fmt: PChar;
  1558. SQ, DQ: Boolean;
  1559. area: Integer;
  1560. Begin
  1561. SQ := False;
  1562. DQ := False;
  1563. Fmt := FmtStart;
  1564. ExpFmt := 0;
  1565. area := 1;
  1566. thousand := False;
  1567. Placehold[1] := 0;
  1568. Placehold[2] := 0;
  1569. Placehold[3] := 0;
  1570. Placehold[4] := 0;
  1571. While Fmt < FmtStop Do
  1572. Begin
  1573. Case Fmt[0] Of
  1574. #34:
  1575. Begin
  1576. If Not SQ Then
  1577. DQ := Not DQ;
  1578. Inc(Fmt);
  1579. End;
  1580. #39:
  1581. Begin
  1582. If Not DQ Then
  1583. SQ := Not SQ;
  1584. Inc(Fmt);
  1585. End;
  1586. Else
  1587. { if not in quotes, then interpret}
  1588. If Not (SQ Or DQ) Then
  1589. Begin
  1590. Case Fmt[0] Of
  1591. '0':
  1592. Begin
  1593. Case area Of
  1594. 1:
  1595. area := 2;
  1596. 4:
  1597. Begin
  1598. area := 3;
  1599. Inc(Placehold[3], Placehold[4]);
  1600. Placehold[4] := 0;
  1601. End;
  1602. End;
  1603. Inc(Placehold[area]);
  1604. Inc(Fmt);
  1605. End;
  1606. '#':
  1607. Begin
  1608. If area=3 Then
  1609. area:=4;
  1610. Inc(Placehold[area]);
  1611. Inc(Fmt);
  1612. End;
  1613. '.':
  1614. Begin
  1615. If area<3 Then
  1616. area:=3;
  1617. Inc(Fmt);
  1618. End;
  1619. ',':
  1620. Begin
  1621. thousand := DefaultFormatSettings.ThousandSeparator<>#0;
  1622. Inc(Fmt);
  1623. End;
  1624. 'e', 'E':
  1625. If ExpFmt = 0 Then
  1626. Begin
  1627. If (Fmt[0]='E') Then
  1628. ExpFmt:=1
  1629. Else
  1630. ExpFmt := 3;
  1631. Inc(Fmt);
  1632. If (Fmt<FmtStop) Then
  1633. Begin
  1634. Case Fmt[0] Of
  1635. '+':
  1636. Begin
  1637. End;
  1638. '-':
  1639. Inc(ExpFmt);
  1640. Else
  1641. ExpFmt := 0;
  1642. End;
  1643. If ExpFmt <> 0 Then
  1644. Begin
  1645. Inc(Fmt);
  1646. ExpSize := 0;
  1647. While (Fmt<FmtStop) And
  1648. (ExpSize<4) And
  1649. (Fmt[0] In ['0'..'9']) Do
  1650. Begin
  1651. Inc(ExpSize);
  1652. Inc(Fmt);
  1653. End;
  1654. End;
  1655. End;
  1656. End
  1657. Else
  1658. Inc(Fmt);
  1659. Else { Case }
  1660. Inc(Fmt);
  1661. End; { Case }
  1662. End { Begin }
  1663. Else
  1664. Inc(Fmt);
  1665. End; { Case }
  1666. End; { While .. Begin }
  1667. End;
  1668. Procedure FloatToStr;
  1669. Var
  1670. I, J, Exp, Width, Decimals, DecimalPoint, len: Integer;
  1671. Begin
  1672. If ExpFmt = 0 Then
  1673. Begin
  1674. { Fixpoint }
  1675. Decimals:=Placehold[3]+Placehold[4];
  1676. Width:=Placehold[1]+Placehold[2]+Decimals;
  1677. If (Decimals=0) Then
  1678. Str(Value:Width:0,Digits)
  1679. Else if Value>=0 then
  1680. Str(Value:Width+1:Decimals,Digits)
  1681. else
  1682. Str(Value:Width+2:Decimals,Digits);
  1683. len:=Length(Digits);
  1684. { Find the decimal point }
  1685. If (Decimals=0) Then
  1686. DecimalPoint:=len+1
  1687. Else
  1688. DecimalPoint:=len-Decimals;
  1689. { If value is very small, and no decimal places
  1690. are desired, remove the leading 0. }
  1691. If (Abs(Value) < 1) And (Placehold[2] = 0) Then
  1692. Begin
  1693. If (Placehold[1]=0) Then
  1694. Delete(Digits,DecimalPoint-1,1)
  1695. Else
  1696. Digits[DecimalPoint-1]:=' ';
  1697. End;
  1698. { Convert optional zeroes to spaces. }
  1699. I:=len;
  1700. J:=DecimalPoint+Placehold[3];
  1701. While (I>J) And (Digits[I]='0') Do
  1702. Begin
  1703. Digits[I] := ' ';
  1704. Dec(I);
  1705. End;
  1706. { If integer value and no obligatory decimal
  1707. places, remove decimal point. }
  1708. If (DecimalPoint < len) And (Digits[DecimalPoint + 1] = ' ') Then
  1709. Digits[DecimalPoint] := ' ';
  1710. { Convert spaces left from obligatory decimal point to zeroes.
  1711. MVC : If - sign is encountered, replace it too, and put at position 1}
  1712. I:=DecimalPoint-Placehold[2];
  1713. J:=0;
  1714. While (I<DecimalPoint) And (Digits[I] in [' ','-']) Do
  1715. Begin
  1716. If Digits[i]='-' then
  1717. J:=I;
  1718. Digits[I] := '0';
  1719. Inc(I);
  1720. End;
  1721. If (J<>0) then
  1722. Digits[1]:='-';
  1723. Exp := 0;
  1724. End
  1725. Else
  1726. Begin
  1727. { Scientific: exactly <Width> Digits With <Precision> Decimals
  1728. And adjusted Exponent. }
  1729. If Placehold[1]+Placehold[2]=0 Then
  1730. Placehold[1]:=1;
  1731. Decimals := Placehold[3] + Placehold[4];
  1732. Width:=Placehold[1]+Placehold[2]+Decimals;
  1733. { depending on the maximally supported precision, the exponent field }
  1734. { is longer/shorter }
  1735. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1736. Str(Value:Width+8,Digits);
  1737. {$else FPC_HAS_TYPE_EXTENDED}
  1738. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1739. Str(Value:Width+7,Digits);
  1740. {$else FPC_HAS_TYPE_DOUBLE}
  1741. Str(Value:Width+6,Digits);
  1742. {$endif FPC_HAS_TYPE_DOUBLE}
  1743. {$endif FPC_HAS_TYPE_EXTENDED}
  1744. { Find and cut out exponent. Always the
  1745. last 6 characters in the string.
  1746. -> 0000E+0000
  1747. *** No, not always the last 6 characters, this depends on
  1748. the maximally supported precision (JM)}
  1749. I:=Pos('E',Digits);
  1750. Val(Copy(Digits,I+1,255),Exp,J);
  1751. Exp:=Exp+1-(Placehold[1]+Placehold[2]);
  1752. Delete(Digits, I, 255);
  1753. { Str() always returns at least one digit after the decimal point.
  1754. If we don't want it, we have to remove it. }
  1755. If (Decimals=0) And (Placehold[1]+Placehold[2]<= 1) Then
  1756. Begin
  1757. If (Digits[4]>='5') Then
  1758. Begin
  1759. Inc(Digits[2]);
  1760. If (Digits[2]>'9') Then
  1761. Begin
  1762. Digits[2] := '1';
  1763. Inc(Exp);
  1764. End;
  1765. End;
  1766. Delete(Digits, 3, 2);
  1767. DecimalPoint := Length(Digits) + 1;
  1768. End
  1769. Else
  1770. Begin
  1771. { Move decimal point at the desired position }
  1772. Delete(Digits, 3, 1);
  1773. DecimalPoint:=2+Placehold[1]+Placehold[2];
  1774. If (Decimals<>0) Then
  1775. Insert('.',Digits,DecimalPoint);
  1776. End;
  1777. { Convert optional zeroes to spaces. }
  1778. I := Length(Digits);
  1779. J := DecimalPoint + Placehold[3];
  1780. While (I > J) And (Digits[I] = '0') Do
  1781. Begin
  1782. Digits[I] := ' ';
  1783. Dec(I);
  1784. End;
  1785. { If integer number and no obligatory decimal paces, remove decimal point }
  1786. If (DecimalPoint<Length(Digits)) And
  1787. (Digits[DecimalPoint+1]=' ') Then
  1788. Digits[DecimalPoint]:=' ';
  1789. If (Digits[1]=' ') Then
  1790. Begin
  1791. Delete(Digits, 1, 1);
  1792. Dec(DecimalPoint);
  1793. End;
  1794. { Calculate exponent string }
  1795. Str(Abs(Exp), Exponent);
  1796. While Length(Exponent)<ExpSize Do
  1797. Insert('0',Exponent,1);
  1798. If Exp >= 0 Then
  1799. Begin
  1800. If (ExpFmt In [1,3]) Then
  1801. Insert('+', Exponent, 1);
  1802. End
  1803. Else
  1804. Insert('-',Exponent,1);
  1805. If (ExpFmt<3) Then
  1806. Insert('E',Exponent,1)
  1807. Else
  1808. Insert('e',Exponent,1);
  1809. End;
  1810. DigitExponent:=DecimalPoint-2;
  1811. If (Digits[1]='-') Then
  1812. Dec(DigitExponent);
  1813. UnexpectedDigits:=DecimalPoint-1-(Placehold[1]+Placehold[2]);
  1814. End;
  1815. Function PutResult: LongInt;
  1816. Var
  1817. SQ, DQ: Boolean;
  1818. Fmt, Buf: PChar;
  1819. Dig, N: Integer;
  1820. Begin
  1821. SQ := False;
  1822. DQ := False;
  1823. Fmt := FmtStart;
  1824. Buf := Buffer;
  1825. Dig := 1;
  1826. While (Fmt<FmtStop) Do
  1827. Begin
  1828. //Write(Fmt[0]);
  1829. Case Fmt[0] Of
  1830. #34:
  1831. Begin
  1832. If Not SQ Then
  1833. DQ := Not DQ;
  1834. Inc(Fmt);
  1835. End;
  1836. #39:
  1837. Begin
  1838. If Not DQ Then
  1839. SQ := Not SQ;
  1840. Inc(Fmt);
  1841. End;
  1842. Else
  1843. If Not (SQ Or DQ) Then
  1844. Begin
  1845. Case Fmt[0] Of
  1846. '0', '#', '.':
  1847. Begin
  1848. If (Dig=1) And (UnexpectedDigits>0) Then
  1849. Begin
  1850. { Everything unexpected is written before the first digit }
  1851. For N := 1 To UnexpectedDigits Do
  1852. Begin
  1853. Buf[0] := Digits[N];
  1854. Inc(Buf);
  1855. If thousand And (Digits[N]<>'-') Then
  1856. Begin
  1857. If (DigitExponent Mod 3 = 0) And (DigitExponent>0) Then
  1858. Begin
  1859. Buf[0] := FormatSettings.ThousandSeparator;
  1860. Inc(Buf);
  1861. End;
  1862. Dec(DigitExponent);
  1863. End;
  1864. End;
  1865. Inc(Dig, UnexpectedDigits);
  1866. End;
  1867. If (Digits[Dig]<>' ') Then
  1868. Begin
  1869. If (Digits[Dig]='.') Then
  1870. Buf[0] := FormatSettings.DecimalSeparator
  1871. Else
  1872. Buf[0] := Digits[Dig];
  1873. Inc(Buf);
  1874. If thousand And (DigitExponent Mod 3 = 0) And (DigitExponent > 0) and (Digits[Dig]<>'-') Then
  1875. Begin
  1876. Buf[0] := FormatSettings.ThousandSeparator;
  1877. Inc(Buf);
  1878. End;
  1879. End;
  1880. Inc(Dig);
  1881. Dec(DigitExponent);
  1882. Inc(Fmt);
  1883. End;
  1884. 'e', 'E':
  1885. Begin
  1886. If ExpFmt <> 0 Then
  1887. Begin
  1888. Inc(Fmt);
  1889. If Fmt < FmtStop Then
  1890. Begin
  1891. If Fmt[0] In ['+', '-'] Then
  1892. Begin
  1893. Inc(Fmt, ExpSize);
  1894. For N:=1 To Length(Exponent) Do
  1895. Buf[N-1] := Exponent[N];
  1896. Inc(Buf,Length(Exponent));
  1897. ExpFmt:=0;
  1898. End;
  1899. Inc(Fmt);
  1900. End;
  1901. End
  1902. Else
  1903. Begin
  1904. { No legal exponential format.
  1905. Simply write the 'E' to the result. }
  1906. Buf[0] := Fmt[0];
  1907. Inc(Buf);
  1908. Inc(Fmt);
  1909. End;
  1910. End;
  1911. Else { Case }
  1912. { Usual character }
  1913. If (Fmt[0]<>',') Then
  1914. Begin
  1915. Buf[0] := Fmt[0];
  1916. Inc(Buf);
  1917. End;
  1918. Inc(Fmt);
  1919. End; { Case }
  1920. End
  1921. Else { IF }
  1922. Begin
  1923. { Character inside single or double quotes }
  1924. Buf[0] := Fmt[0];
  1925. Inc(Buf);
  1926. Inc(Fmt);
  1927. End;
  1928. End; { Case }
  1929. End; { While .. Begin }
  1930. Result:=PtrUInt(Buf)-PtrUInt(Buffer);
  1931. End;
  1932. Begin
  1933. If (Value>0) Then
  1934. GetSectionRange(1)
  1935. Else If (Value<0) Then
  1936. GetSectionRange(2)
  1937. Else
  1938. GetSectionRange(3);
  1939. If FmtStart = Nil Then
  1940. Begin
  1941. Result := FloatToText(Buffer, Value, ffGeneral, 15, 4, FormatSettings);
  1942. End
  1943. Else
  1944. Begin
  1945. GetFormatOptions;
  1946. If (ExpFmt = 0) And (Abs(Value) >= 1E18) Then
  1947. Result := FloatToText(Buffer, Value, ffGeneral, 15, 4, FormatSettings)
  1948. Else
  1949. Begin
  1950. FloatToStr;
  1951. Result := PutResult;
  1952. End;
  1953. End;
  1954. End;
  1955. Procedure FloatToDecimal(Out Result: TFloatRec; const Value; ValueType: TFloatValue; Precision, Decimals : integer);
  1956. var
  1957. Buffer: String[254]; //Though str func returns only 25 chars, this might change in the future
  1958. Error, N, L, Start, C: Integer;
  1959. GotNonZeroBeforeDot, BeforeDot : boolean;
  1960. begin
  1961. case ValueType of
  1962. fvExtended:
  1963. Str(Extended(Value):25, Buffer);
  1964. fvDouble,
  1965. fvReal:
  1966. Str(Double(Value):23, Buffer);
  1967. fvSingle:
  1968. Str(Single(Value):16, Buffer);
  1969. fvCurrency:
  1970. Str(Currency(Value):25, Buffer);
  1971. fvComp:
  1972. Str(Currency(Value):23, Buffer);
  1973. end;
  1974. N := 1;
  1975. L := Byte(Buffer[0]);
  1976. while Buffer[N]=' ' do
  1977. Inc(N);
  1978. Result.Negative := (Buffer[N] = '-');
  1979. if Result.Negative then
  1980. Inc(N);
  1981. Start := N; //Start of digits
  1982. Result.Exponent := 0; BeforeDot := true;
  1983. GotNonZeroBeforeDot := false;
  1984. while (L>=N) and (Buffer[N]<>'E') do
  1985. begin
  1986. if Buffer[N]='.' then
  1987. BeforeDot := false
  1988. else
  1989. begin
  1990. if BeforeDot then
  1991. begin // Currently this is always 1 char
  1992. Inc(Result.Exponent);
  1993. Result.Digits[N-Start] := Buffer[N];
  1994. if Buffer[N] <> '0' then
  1995. GotNonZeroBeforeDot := true;
  1996. end
  1997. else
  1998. Result.Digits[N-Start-1] := Buffer[N]
  1999. end;
  2000. Inc(N);
  2001. end;
  2002. Inc(N); // Pass through 'E'
  2003. if N<=L then
  2004. begin
  2005. Val(Copy(Buffer, N, L-N+1), C, Error); // Get exponent after 'E'
  2006. Inc(Result.Exponent, C);
  2007. end;
  2008. // Calculate number of digits we have from str
  2009. if BeforeDot then
  2010. N := N - Start - 1
  2011. else
  2012. N := N - Start - 2;
  2013. L := SizeOf(Result.Digits);
  2014. if N<L then
  2015. FillChar(Result.Digits[N], L-N, '0'); //Zero remaining space
  2016. if Decimals + Result.Exponent < Precision Then //After this it is the same as in FloatToDecimal
  2017. N := Decimals + Result.Exponent
  2018. Else
  2019. N := Precision;
  2020. if N >= L Then
  2021. N := L-1;
  2022. if N = 0 Then
  2023. begin
  2024. if Result.Digits[0] >= '5' Then
  2025. begin
  2026. Result.Digits[0] := '1';
  2027. Result.Digits[1] := #0;
  2028. Inc(Result.Exponent);
  2029. end
  2030. Else
  2031. Result.Digits[0] := #0;
  2032. end //N=0
  2033. Else if N > 0 Then
  2034. begin
  2035. if Result.Digits[N] >= '5' Then
  2036. begin
  2037. Repeat
  2038. Result.Digits[N] := #0;
  2039. Dec(N);
  2040. Inc(Result.Digits[N]);
  2041. Until (N = 0) Or (Result.Digits[N] < ':');
  2042. If Result.Digits[0] = ':' Then
  2043. begin
  2044. Result.Digits[0] := '1';
  2045. Inc(Result.Exponent);
  2046. end;
  2047. end
  2048. Else
  2049. begin
  2050. Result.Digits[N] := '0';
  2051. While (N > -1) And (Result.Digits[N] = '0') Do
  2052. begin
  2053. Result.Digits[N] := #0;
  2054. Dec(N);
  2055. end;
  2056. end;
  2057. end //N>0
  2058. Else
  2059. Result.Digits[0] := #0;
  2060. if (Result.Digits[0] = #0) and
  2061. not GotNonZeroBeforeDot then
  2062. begin
  2063. Result.Exponent := 0;
  2064. Result.Negative := False;
  2065. end;
  2066. end;
  2067. Procedure FloatToDecimal(Out Result: TFloatRec; Value: Extended; Precision, Decimals : integer);
  2068. begin
  2069. FloatToDecimal(Result,Value,fvExtended,Precision,Decimals);
  2070. end;
  2071. Function FormatFloat(Const Format : String; Value : Extended; Const FormatSettings: TFormatSettings) : String;
  2072. Var
  2073. buf : Array[0..1024] of char;
  2074. Begin // not changed to pchar(pointer(). Possibly not safe
  2075. Buf[FloatToTextFmt(@Buf[0],Value,Pchar(Format),FormatSettings)]:=#0;
  2076. Result:=StrPas(@Buf[0]);
  2077. End;
  2078. Function FormatFloat(Const format: String; Value: Extended): String;
  2079. begin
  2080. Result:=FormatFloat(Format,Value,DefaultFormatSettings);
  2081. end;
  2082. Function FormatCurr(const Format: string; Value: Currency; Const FormatSettings: TFormatSettings): string;
  2083. begin
  2084. Result := FormatFloat(Format, Value,FormatSettings);
  2085. end;
  2086. function FormatCurr(const Format: string; Value: Currency): string;
  2087. begin
  2088. Result:=FormatCurr(Format,Value,DefaultFormatSettings);
  2089. end;
  2090. {$endif}
  2091. {==============================================================================}
  2092. { extra functions }
  2093. {==============================================================================}
  2094. { LeftStr returns Count left-most characters from S }
  2095. function LeftStr(const S: string; Count: integer): string;
  2096. begin
  2097. result := Copy(S, 1, Count);
  2098. end ;
  2099. { RightStr returns Count right-most characters from S }
  2100. function RightStr(const S: string; Count: integer): string;
  2101. begin
  2102. If Count>Length(S) then
  2103. Count:=Length(S);
  2104. result := Copy(S, 1 + Length(S) - Count, Count);
  2105. end;
  2106. { BCDToInt converts the BCD value Value to an integer }
  2107. function BCDToInt(Value: integer): integer;
  2108. var i, j, digit: integer;
  2109. begin
  2110. result := 0;
  2111. j := 1;
  2112. for i := 0 to SizeOf(Value) shl 1 - 1 do begin
  2113. digit := Value and 15;
  2114. if digit > $9 then
  2115. begin
  2116. if i = 0 then
  2117. begin
  2118. if digit in [$B, $D] then j := -1
  2119. end
  2120. else raise EConvertError.createfmt(SInvalidBCD,[Value]);
  2121. end
  2122. else
  2123. begin
  2124. result := result + j * digit;
  2125. j := j * 10;
  2126. end ;
  2127. Value := Value shr 4;
  2128. end ;
  2129. end ;
  2130. Function LastDelimiter(const Delimiters, S: string): Integer;
  2131. var
  2132. chs: TSysCharSet;
  2133. I: LongInt;
  2134. begin
  2135. chs := [];
  2136. for I := 1 to Length(Delimiters) do
  2137. Include(chs, Delimiters[I]);
  2138. Result:=Length(S);
  2139. While (Result>0) and not (S[Result] in chs) do
  2140. Dec(Result);
  2141. end;
  2142. Function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;
  2143. var
  2144. Srch,OldP,RemS: string; // Srch and Oldp can contain uppercase versions of S,OldPattern
  2145. P : Integer;
  2146. begin
  2147. Srch:=S;
  2148. OldP:=OldPattern;
  2149. if rfIgnoreCase in Flags then
  2150. begin
  2151. Srch:=AnsiUpperCase(Srch);
  2152. OldP:=AnsiUpperCase(OldP);
  2153. end;
  2154. RemS:=S;
  2155. Result:='';
  2156. while (Length(Srch)<>0) do
  2157. begin
  2158. P:=AnsiPos(OldP, Srch);
  2159. if P=0 then
  2160. begin
  2161. Result:=Result+RemS;
  2162. Srch:='';
  2163. end
  2164. else
  2165. begin
  2166. Result:=Result+Copy(RemS,1,P-1)+NewPattern;
  2167. P:=P+Length(OldP);
  2168. RemS:=Copy(RemS,P,Length(RemS)-P+1);
  2169. if not (rfReplaceAll in Flags) then
  2170. begin
  2171. Result:=Result+RemS;
  2172. Srch:='';
  2173. end
  2174. else
  2175. Srch:=Copy(Srch,P,Length(Srch)-P+1);
  2176. end;
  2177. end;
  2178. end;
  2179. Function IsDelimiter(const Delimiters, S: string; Index: Integer): Boolean;
  2180. begin
  2181. Result:=False;
  2182. If (Index>0) and (Index<=Length(S)) then
  2183. Result:=Pos(S[Index],Delimiters)<>0; // Note we don't do MBCS yet
  2184. end;
  2185. Function ByteToCharLen(const S: string; MaxLen: Integer): Integer;
  2186. begin
  2187. Result:=Length(S);
  2188. If Result>MaxLen then
  2189. Result:=MaxLen;
  2190. end;
  2191. Function ByteToCharIndex(const S: string; Index: Integer): Integer;
  2192. begin
  2193. Result:=Index;
  2194. end;
  2195. Function CharToByteLen(const S: string; MaxLen: Integer): Integer;
  2196. begin
  2197. Result:=Length(S);
  2198. If Result>MaxLen then
  2199. Result:=MaxLen;
  2200. end;
  2201. Function CharToByteIndex(const S: string; Index: Integer): Integer;
  2202. begin
  2203. Result:=Index;
  2204. end;
  2205. Function ByteType(const S: string; Index: Integer): TMbcsByteType;
  2206. begin
  2207. Result:=mbSingleByte;
  2208. end;
  2209. Function StrByteType(Str: PChar; Index: Cardinal): TMbcsByteType;
  2210. begin
  2211. Result:=mbSingleByte;
  2212. end;
  2213. Function StrCharLength(const Str: PChar): Integer;
  2214. begin
  2215. result:=widestringmanager.CharLengthPCharProc(Str);
  2216. end;
  2217. function StrNextChar(const Str: PChar): PChar;
  2218. begin
  2219. result:=Str+StrCharLength(Str);
  2220. end;
  2221. Function FindCmdLineSwitch(const Switch: string; const Chars: TSysCharSet;IgnoreCase: Boolean): Boolean;
  2222. Var
  2223. I,L : Integer;
  2224. S,T : String;
  2225. begin
  2226. Result:=False;
  2227. S:=Switch;
  2228. If IgnoreCase then
  2229. S:=UpperCase(S);
  2230. I:=ParamCount;
  2231. While (Not Result) and (I>0) do
  2232. begin
  2233. L:=Length(Paramstr(I));
  2234. If (L>0) and (ParamStr(I)[1] in Chars) then
  2235. begin
  2236. T:=Copy(ParamStr(I),2,L-1);
  2237. If IgnoreCase then
  2238. T:=UpperCase(T);
  2239. Result:=S=T;
  2240. end;
  2241. Dec(i);
  2242. end;
  2243. end;
  2244. Function FindCmdLineSwitch(const Switch: string; IgnoreCase: Boolean): Boolean;
  2245. begin
  2246. Result:=FindCmdLineSwitch(Switch,SwitchChars,IgnoreCase);
  2247. end;
  2248. Function FindCmdLineSwitch(const Switch: string): Boolean;
  2249. begin
  2250. Result:=FindCmdLineSwitch(Switch,SwitchChars,False);
  2251. end;
  2252. function WrapText(const Line, BreakStr: string; const BreakChars: TSysCharSet; MaxCol: Integer): string;
  2253. const
  2254. Quotes = ['''', '"'];
  2255. Var
  2256. L : String;
  2257. C,LQ,BC : Char;
  2258. P,BLen,Len : Integer;
  2259. HB,IBC : Boolean;
  2260. begin
  2261. Result:='';
  2262. L:=Line;
  2263. Blen:=Length(BreakStr);
  2264. If (BLen>0) then
  2265. BC:=BreakStr[1]
  2266. else
  2267. BC:=#0;
  2268. Len:=Length(L);
  2269. While (Len>0) do
  2270. begin
  2271. P:=1;
  2272. LQ:=#0;
  2273. HB:=False;
  2274. IBC:=False;
  2275. While ((P<=Len) and ((P<=MaxCol) or not IBC)) and ((LQ<>#0) or Not HB) do
  2276. begin
  2277. C:=L[P];
  2278. If (C=LQ) then
  2279. LQ:=#0
  2280. else If (C in Quotes) then
  2281. LQ:=C;
  2282. If (LQ<>#0) then
  2283. Inc(P)
  2284. else
  2285. begin
  2286. HB:=((C=BC) and (BreakStr=Copy(L,P,BLen)));
  2287. If HB then
  2288. Inc(P,Blen)
  2289. else
  2290. begin
  2291. If (P>MaxCol) then
  2292. IBC:=C in BreakChars;
  2293. Inc(P);
  2294. end;
  2295. end;
  2296. // Writeln('"',C,'" : IBC : ',IBC,' HB : ',HB,' LQ : ',LQ,' P>MaxCol : ',P>MaxCol);
  2297. end;
  2298. Result:=Result+Copy(L,1,P-1);
  2299. If Not HB then
  2300. Result:=Result+BreakStr;
  2301. Delete(L,1,P-1);
  2302. Len:=Length(L);
  2303. end;
  2304. end;
  2305. function WrapText(const Line: string; MaxCol: Integer): string;
  2306. begin
  2307. Result:=WrapText(Line,sLineBreak, [' ', '-', #9], MaxCol);
  2308. end;
  2309. {$ifndef FPC_NOGENERICANSIROUTINES}
  2310. {
  2311. Case Translation Tables
  2312. Can be used in internationalization support.
  2313. Although these tables can be obtained through system calls
  2314. cd it is better to not use those, since most implementation are not 100%
  2315. WARNING:
  2316. before modifying a translation table make sure that the current codepage
  2317. of the OS corresponds to the one you make changes to
  2318. }
  2319. const
  2320. { upper case translation table for character set 850 }
  2321. CP850UCT: array[128..255] of char =
  2322. (#128,#154,#144,#182,#142,#182,#143,#128,#210,#211,#212,#216,#215,#222,#142,#143,
  2323. #144,#146,#146,#226,#153,#227,#234,#235,'Y',#153,#154,#157,#156,#157,#158,#159,
  2324. #181,#214,#224,#233,#165,#165,#166,#167,#168,#169,#170,#171,#172,#173,#174,#175,
  2325. #176,#177,#178,#179,#180,#181,#182,#183,#184,#185,#186,#187,#188,#189,#190,#191,
  2326. #192,#193,#194,#195,#196,#197,#199,#199,#200,#201,#202,#203,#204,#205,#206,#207,
  2327. #208,#209,#210,#211,#212,#213,#214,#215,#216,#217,#218,#219,#220,#221,#222,#223,
  2328. #224,#225,#226,#227,#229,#229,#230,#237,#232,#233,#234,#235,#237,#237,#238,#239,
  2329. #240,#241,#242,#243,#244,#245,#246,#247,#248,#249,#250,#251,#252,#253,#254,#255);
  2330. { lower case translation table for character set 850 }
  2331. CP850LCT: array[128..255] of char =
  2332. (#135,#129,#130,#131,#132,#133,#134,#135,#136,#137,#138,#139,#140,#141,#132,#134,
  2333. #130,#145,#145,#147,#148,#149,#150,#151,#152,#148,#129,#155,#156,#155,#158,#159,
  2334. #160,#161,#162,#163,#164,#164,#166,#167,#168,#169,#170,#171,#172,#173,#174,#175,
  2335. #176,#177,#178,#179,#180,#160,#131,#133,#184,#185,#186,#187,#188,#189,#190,#191,
  2336. #192,#193,#194,#195,#196,#197,#198,#198,#200,#201,#202,#203,#204,#205,#206,#207,
  2337. #208,#209,#136,#137,#138,#213,#161,#140,#139,#217,#218,#219,#220,#221,#141,#223,
  2338. #162,#225,#147,#149,#228,#228,#230,#237,#232,#163,#150,#151,#236,#236,#238,#239,
  2339. #240,#241,#242,#243,#244,#245,#246,#247,#248,#249,#250,#251,#252,#253,#254,#255);
  2340. { upper case translation table for character set ISO 8859/1 Latin 1 }
  2341. CPISO88591UCT: array[192..255] of char =
  2342. ( #192, #193, #194, #195, #196, #197, #198, #199,
  2343. #200, #201, #202, #203, #204, #205, #206, #207,
  2344. #208, #209, #210, #211, #212, #213, #214, #215,
  2345. #216, #217, #218, #219, #220, #221, #222, #223,
  2346. #192, #193, #194, #195, #196, #197, #198, #199,
  2347. #200, #201, #202, #203, #204, #205, #206, #207,
  2348. #208, #209, #210, #211, #212, #213, #214, #247,
  2349. #216, #217, #218, #219, #220, #221, #222, #89 );
  2350. { lower case translation table for character set ISO 8859/1 Latin 1 }
  2351. CPISO88591LCT: array[192..255] of char =
  2352. ( #224, #225, #226, #227, #228, #229, #230, #231,
  2353. #232, #233, #234, #235, #236, #237, #238, #239,
  2354. #240, #241, #242, #243, #244, #245, #246, #215,
  2355. #248, #249, #250, #251, #252, #253, #254, #223,
  2356. #224, #225, #226, #227, #228, #229, #230, #231,
  2357. #232, #233, #234, #235, #236, #237, #238, #239,
  2358. #240, #241, #242, #243, #244, #245, #246, #247,
  2359. #248, #249, #250, #251, #252, #253, #254, #255 );
  2360. {$endif FPC_NOGENERICANSIROUTINES}
  2361. function sscanf(const s: string; const fmt : string;const Pointers : array of Pointer) : Integer;
  2362. var
  2363. i,j,n,m : SizeInt;
  2364. s1 : string;
  2365. function GetInt(unsigned : boolean=false) : Integer;
  2366. begin
  2367. s1 := '';
  2368. while (Length(s) > n) and (s[n] = ' ') do
  2369. inc(n);
  2370. { read sign }
  2371. if (Length(s)>= n) and (s[n] in ['+', '-']) then
  2372. begin
  2373. { don't accept - when reading unsigned }
  2374. if unsigned and (s[n]='-') then
  2375. begin
  2376. result:=length(s1);
  2377. exit;
  2378. end
  2379. else
  2380. begin
  2381. s1:=s1+s[n];
  2382. inc(n);
  2383. end;
  2384. end;
  2385. { read numbers }
  2386. while (Length(s) >= n) and
  2387. (s[n] in ['0'..'9']) do
  2388. begin
  2389. s1 := s1+s[n];
  2390. inc(n);
  2391. end;
  2392. Result := Length(s1);
  2393. end;
  2394. function GetFloat : Integer;
  2395. begin
  2396. s1 := '';
  2397. while (Length(s) > n) and (s[n] = ' ') do
  2398. inc(n);
  2399. while (Length(s) >= n) and
  2400. (s[n] in ['0'..'9', '+', '-', '.', 'e', 'E']) do
  2401. begin
  2402. s1 := s1+s[n];
  2403. inc(n);
  2404. end;
  2405. Result := Length(s1);
  2406. end;
  2407. function GetString : Integer;
  2408. begin
  2409. s1 := '';
  2410. while (Length(s) > n) and (s[n] = ' ') do
  2411. inc(n);
  2412. while (Length(s) >= n) and (s[n] <> ' ')do
  2413. begin
  2414. s1 := s1+s[n];
  2415. inc(n);
  2416. end;
  2417. Result := Length(s1);
  2418. end;
  2419. function ScanStr(c : Char) : Boolean;
  2420. begin
  2421. while (Length(s) > n) and (s[n] <> c) do
  2422. inc(n);
  2423. inc(n);
  2424. If (n <= Length(s)) then
  2425. Result := True
  2426. else
  2427. Result := False;
  2428. end;
  2429. function GetFmt : Integer;
  2430. begin
  2431. Result := -1;
  2432. while true do
  2433. begin
  2434. while (Length(fmt) > m) and (fmt[m] = ' ') do
  2435. inc(m);
  2436. if (m >= Length(fmt)) then
  2437. break;
  2438. if (fmt[m] = '%') then
  2439. begin
  2440. inc(m);
  2441. case fmt[m] of
  2442. 'd':
  2443. Result:=vtInteger;
  2444. {$ifndef FPUNONE}
  2445. 'f':
  2446. Result:=vtExtended;
  2447. {$endif}
  2448. 's':
  2449. Result:=vtString;
  2450. 'c':
  2451. Result:=vtChar;
  2452. else
  2453. raise EFormatError.CreateFmt(SInvalidFormat,[fmt]);
  2454. end;
  2455. inc(m);
  2456. break;
  2457. end;
  2458. if not(ScanStr(fmt[m])) then
  2459. break;
  2460. inc(m);
  2461. end;
  2462. end;
  2463. begin
  2464. n := 1;
  2465. m := 1;
  2466. Result := 0;
  2467. for i:=0 to High(Pointers) do
  2468. begin
  2469. j := GetFmt;
  2470. case j of
  2471. vtInteger :
  2472. begin
  2473. if GetInt>0 then
  2474. begin
  2475. pLongint(Pointers[i])^:=StrToInt(s1);
  2476. inc(Result);
  2477. end
  2478. else
  2479. break;
  2480. end;
  2481. vtchar :
  2482. begin
  2483. if Length(s)>n then
  2484. begin
  2485. pchar(Pointers[i])^:=s[n];
  2486. inc(n);
  2487. inc(Result);
  2488. end
  2489. else
  2490. break;
  2491. end;
  2492. {$ifndef FPUNONE}
  2493. vtExtended :
  2494. begin
  2495. if GetFloat>0 then
  2496. begin
  2497. pextended(Pointers[i])^:=StrToFloat(s1);
  2498. inc(Result);
  2499. end
  2500. else
  2501. break;
  2502. end;
  2503. {$endif}
  2504. vtString :
  2505. begin
  2506. if GetString > 0 then
  2507. begin
  2508. pansistring(Pointers[i])^:=s1;
  2509. inc(Result);
  2510. end
  2511. else
  2512. break;
  2513. end;
  2514. else
  2515. break;
  2516. end;
  2517. end;
  2518. end;