sysstr.inc 73 KB

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