sysstr.inc 77 KB

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