stringl.inc 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  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. {****************************************************************************}
  11. {* TStringsEnumerator *}
  12. {****************************************************************************}
  13. constructor TStringsEnumerator.Create(AStrings: TStrings);
  14. begin
  15. inherited Create;
  16. FStrings := AStrings;
  17. FPosition := -1;
  18. end;
  19. function TStringsEnumerator.GetCurrent: String;
  20. begin
  21. Result := FStrings[FPosition];
  22. end;
  23. function TStringsEnumerator.MoveNext: Boolean;
  24. begin
  25. Inc(FPosition);
  26. Result := FPosition < FStrings.Count;
  27. end;
  28. {****************************************************************************}
  29. {* TStrings *}
  30. {****************************************************************************}
  31. // Function to quote text. Should move maybe to sysutils !!
  32. // Also, it is not clear at this point what exactly should be done.
  33. { //!! is used to mark unsupported things. }
  34. Function QuoteString (Const S : String; Const Quote : String) : String;
  35. Var
  36. I,J : Integer;
  37. begin
  38. J:=0;
  39. Result:=S;
  40. for i:=1 to length(s) do
  41. begin
  42. inc(j);
  43. if S[i]=Quote then
  44. begin
  45. System.Insert(Quote,Result,J);
  46. inc(j);
  47. end;
  48. end;
  49. Result:=Quote+Result+Quote;
  50. end;
  51. {
  52. For compatibility we can't add a Constructor to TSTrings to initialize
  53. the special characters. Therefore we add a routine which is called whenever
  54. the special chars are needed.
  55. }
  56. Procedure Tstrings.CheckSpecialChars;
  57. begin
  58. If Not FSpecialCharsInited then
  59. begin
  60. FQuoteChar:='"';
  61. FDelimiter:=',';
  62. FNameValueSeparator:='=';
  63. FLBS:=DefaultTextLineBreakStyle;
  64. FSpecialCharsInited:=true;
  65. FLineBreak:=sLineBreak;
  66. end;
  67. end;
  68. Function TStrings.GetSkipLastLineBreak : Boolean;
  69. begin
  70. Result:=not TrailingLineBreak;
  71. end;
  72. procedure TStrings.SetSkipLastLineBreak(const AValue : Boolean);
  73. begin
  74. TrailingLineBreak:=not AValue;
  75. end;
  76. Function TStrings.GetLBS : TTextLineBreakStyle;
  77. begin
  78. CheckSpecialChars;
  79. Result:=FLBS;
  80. end;
  81. Procedure TStrings.SetLBS (AValue : TTextLineBreakStyle);
  82. begin
  83. CheckSpecialChars;
  84. FLBS:=AValue;
  85. end;
  86. procedure TStrings.SetDelimiter(c:Char);
  87. begin
  88. CheckSpecialChars;
  89. FDelimiter:=c;
  90. end;
  91. Procedure TStrings.SetEncoding(const AEncoding: TEncoding);
  92. begin
  93. if (FEncoding<>nil) and not TEncoding.IsStandardEncoding(FEncoding) then
  94. FEncoding.Free;
  95. if TEncoding.IsStandardEncoding(AEncoding) then
  96. FEncoding:=AEncoding
  97. else if AEncoding<>nil then
  98. FEncoding:=AEncoding.Clone
  99. else
  100. FEncoding:=nil;
  101. end;
  102. Function TStrings.GetDelimiter : Char;
  103. begin
  104. CheckSpecialChars;
  105. Result:=FDelimiter;
  106. end;
  107. procedure TStrings.SetLineBreak(Const S : String);
  108. begin
  109. CheckSpecialChars;
  110. FLineBreak:=S;
  111. end;
  112. Function TStrings.GetLineBreak : String;
  113. begin
  114. CheckSpecialChars;
  115. Result:=FLineBreak;
  116. end;
  117. procedure TStrings.SetQuoteChar(c:Char);
  118. begin
  119. CheckSpecialChars;
  120. FQuoteChar:=c;
  121. end;
  122. Function TStrings.GetQuoteChar :Char;
  123. begin
  124. CheckSpecialChars;
  125. Result:=FQuoteChar;
  126. end;
  127. procedure TStrings.SetNameValueSeparator(c:Char);
  128. begin
  129. CheckSpecialChars;
  130. FNameValueSeparator:=c;
  131. end;
  132. Function TStrings.GetNameValueSeparator :Char;
  133. begin
  134. CheckSpecialChars;
  135. Result:=FNameValueSeparator;
  136. end;
  137. function TStrings.GetCommaText: string;
  138. Var
  139. C1,C2 : Char;
  140. FSD : Boolean;
  141. begin
  142. CheckSpecialChars;
  143. FSD:=StrictDelimiter;
  144. C1:=Delimiter;
  145. C2:=QuoteChar;
  146. Delimiter:=',';
  147. QuoteChar:='"';
  148. StrictDelimiter:=False;
  149. Try
  150. Result:=GetDelimitedText;
  151. Finally
  152. Delimiter:=C1;
  153. QuoteChar:=C2;
  154. StrictDelimiter:=FSD;
  155. end;
  156. end;
  157. function TStrings.GetLineBreakCharLBS: string;
  158. begin
  159. CheckSpecialChars;
  160. if FLineBreak<>sLineBreak then
  161. Result:=FLineBreak
  162. else
  163. Case FLBS of
  164. tlbsLF : Result:=#10;
  165. tlbsCRLF : Result:=#13#10;
  166. tlbsCR : Result:=#13;
  167. end;
  168. end;
  169. function TStrings.GetMissingNameValueSeparatorAction: TMissingNameValueSeparatorAction;
  170. begin
  171. CheckSpecialChars;
  172. Result:=FMissingNameValueSeparatorAction;
  173. end;
  174. Function TStrings.GetDelimitedText: string;
  175. Var
  176. I : integer;
  177. p : pchar;
  178. BreakChars : set of char;
  179. S : String;
  180. doQuote : Boolean;
  181. begin
  182. CheckSpecialChars;
  183. result:='';
  184. if StrictDelimiter then
  185. BreakChars:=[#0,QuoteChar,Delimiter]
  186. else
  187. BreakChars:=[#0..' ',QuoteChar,Delimiter];
  188. // Check for break characters and quote if required.
  189. For i:=0 to count-1 do
  190. begin
  191. S:=Strings[i];
  192. doQuote:=FAlwaysQuote;
  193. If not DoQuote then
  194. begin
  195. p:=pchar(S);
  196. //Quote strings that include BreakChars:
  197. while not(p^ in BreakChars) do
  198. inc(p);
  199. DoQuote:=(p<>pchar(S)+length(S));
  200. end;
  201. if DoQuote then
  202. Result:=Result+QuoteString(S,QuoteChar)
  203. else
  204. Result:=Result+S;
  205. if I<Count-1 then
  206. Result:=Result+Delimiter;
  207. end;
  208. // Quote empty string:
  209. If (Length(Result)=0) and (Count=1) then
  210. Result:=QuoteChar+QuoteChar;
  211. end;
  212. procedure TStrings.GetNameValue(Index : Integer; Out AName,AValue : String);
  213. Var L : longint;
  214. begin
  215. aName:='';
  216. CheckSpecialChars;
  217. AValue:=Strings[Index];
  218. L:=Pos(FNameValueSeparator,AValue);
  219. If L<>0 then
  220. begin
  221. AName:=Copy(AValue,1,L-1);
  222. System.Delete(AValue,1,L);
  223. end
  224. else
  225. case FMissingNameValueSeparatorAction of
  226. mnvaValue : ;
  227. mnvaName :
  228. begin
  229. aName:=aValue;
  230. aValue:='';
  231. end;
  232. mnvaEmpty :
  233. aValue:='';
  234. mnvaError :
  235. Raise EStringListError.CreateFmt(SErrNoNameValuePairAt,[Index]);
  236. end;
  237. end;
  238. function TStrings.ExtractName(const s:String):String;
  239. var
  240. L: Longint;
  241. begin
  242. CheckSpecialChars;
  243. L:=Pos(FNameValueSeparator,S);
  244. If L<>0 then
  245. Result:=Copy(S,1,L-1)
  246. else
  247. Result:='';
  248. end;
  249. procedure TStrings.Filter(aFilter: TStringsFilterMethod; aList: TStrings);
  250. var
  251. S : string;
  252. begin
  253. for S in self do
  254. if aFilter(S) then
  255. aList.Add(S);
  256. end;
  257. procedure TStrings.ForEach(aCallback: TStringsForeachMethod);
  258. var
  259. S : String;
  260. begin
  261. for S in self do
  262. aCallBack(S);
  263. end;
  264. procedure TStrings.ForEach(aCallback: TStringsForeachMethodEx);
  265. var
  266. i: integer;
  267. begin
  268. for i:=0 to Count-1 do
  269. aCallBack(Strings[i],i);
  270. end;
  271. procedure TStrings.ForEach(aCallback: TStringsForeachMethodExObj);
  272. var
  273. i: integer;
  274. begin
  275. for i:=0 to Count-1 do
  276. aCallback(Strings[i],i,Objects[i]);
  277. end;
  278. function TStrings.Filter(aFilter: TStringsFilterMethod): TStrings;
  279. begin
  280. Result:=TStringsClass(Self.ClassType).Create;
  281. try
  282. Filter(aFilter,Result);
  283. except
  284. FreeAndNil(Result);
  285. Raise;
  286. end;
  287. end;
  288. procedure TStrings.Fill(const aValue: String; aStart, aEnd: Integer);
  289. var
  290. i: integer;
  291. begin
  292. if aEnd<0 then
  293. aEnd:=Self.Count+aEnd;
  294. if aEnd>=Count then
  295. aEnd:=Count-1;
  296. for i:=aStart to aEnd do
  297. Strings[i]:=aValue;
  298. end;
  299. Procedure TStrings.Map(aMap: TStringsMapMethod; aList : TStrings);
  300. Var
  301. S : String;
  302. begin
  303. For S in self do
  304. aList.Add(aMap(S));
  305. end;
  306. Function TStrings.Map(aMap: TStringsMapMethod) : TStrings;
  307. begin
  308. Result:=TStringsClass(Self.ClassType).Create;
  309. try
  310. Map(aMap,Result);
  311. except
  312. FreeAndNil(Result);
  313. Raise;
  314. end;
  315. end;
  316. function TStrings.Reduce(aReduceMethod: TStringsReduceMethod; const startingValue: string): string;
  317. var
  318. S : String;
  319. begin
  320. Result:=startingValue;
  321. for S in self do
  322. Result:=aReduceMethod(Result, S);
  323. end;
  324. Function TStrings.Reverse : TStrings;
  325. begin
  326. Result:=TStringsClass(Self.ClassType).Create;
  327. try
  328. Reverse(Result);
  329. except
  330. FreeAndNil(Result);
  331. Raise;
  332. end;
  333. end;
  334. Procedure TStrings.Reverse(aList : TStrings);
  335. Var
  336. I : Integer;
  337. begin
  338. for I:=Count-1 downto 0 do
  339. aList.Add(Strings[i]);
  340. end;
  341. Procedure TStrings.Slice(fromIndex: integer; aList : TStrings);
  342. var
  343. i: integer;
  344. begin
  345. for i:=fromIndex to Count-1 do
  346. aList.Add(Self[i]);
  347. end;
  348. Function TStrings.Slice(fromIndex: integer) : TStrings;
  349. begin
  350. Result:=TStringsClass(Self.ClassType).Create;
  351. try
  352. Slice(FromIndex,Result);
  353. except
  354. FreeAndNil(Result);
  355. Raise;
  356. end;
  357. end;
  358. function TStrings.GetName(Index: Integer): string;
  359. Var
  360. V : String;
  361. begin
  362. GetNameValue(Index,Result,V);
  363. end;
  364. function TStrings.GetStrictDelimiter: Boolean;
  365. begin
  366. Result:=soStrictDelimiter in FOptions;
  367. end;
  368. function TStrings.GetTrailingLineBreak: Boolean;
  369. begin
  370. Result:=soTrailingLineBreak in FOptions;
  371. end;
  372. function TStrings.GetUseLocale: Boolean;
  373. begin
  374. Result:=soUseLocale in FOptions;
  375. end;
  376. function TStrings.GetWriteBOM: Boolean;
  377. begin
  378. Result:=soWriteBOM in FOptions;
  379. end;
  380. Function TStrings.GetValue(const Name: string): string;
  381. Var
  382. L : longint;
  383. N : String;
  384. begin
  385. Result:='';
  386. L:=IndexOfName(Name);
  387. If L<>-1 then
  388. GetNameValue(L,N,Result);
  389. end;
  390. Function TStrings.GetValueFromIndex(Index: Integer): string;
  391. Var
  392. N : String;
  393. begin
  394. GetNameValue(Index,N,Result);
  395. end;
  396. Procedure TStrings.SetValueFromIndex(Index: Integer; const Value: string);
  397. begin
  398. If (Value='') then
  399. Delete(Index)
  400. else
  401. begin
  402. If (Index<0) then
  403. Index:=Add('');
  404. CheckSpecialChars;
  405. Strings[Index]:=GetName(Index)+FNameValueSeparator+Value;
  406. end;
  407. end;
  408. procedure TStrings.ReadData(Reader: TReader);
  409. begin
  410. Reader.ReadListBegin;
  411. BeginUpdate;
  412. try
  413. Clear;
  414. while not Reader.EndOfList do
  415. Add(Reader.ReadString);
  416. finally
  417. EndUpdate;
  418. end;
  419. Reader.ReadListEnd;
  420. end;
  421. Procedure TStrings.SetDelimitedText(const AValue: string);
  422. begin
  423. CheckSpecialChars;
  424. DoSetDelimitedText(aValue,True,StrictDelimiter,FQuoteChar,FDelimiter);
  425. end;
  426. Procedure TStrings.DoSetDelimitedText(const AValue: string; DoClear,aStrictDelimiter : Boolean; aQuoteChar,aDelimiter : Char);
  427. var
  428. len,i,j: SizeInt;
  429. aNotFirst:boolean;
  430. Procedure AddQuoted;
  431. begin
  432. Add(StringReplace(Copy(AValue,i+1,j-i-1),aQuoteChar+aQuoteChar,aQuoteChar, [rfReplaceAll]));
  433. end;
  434. begin
  435. BeginUpdate;
  436. i:=1;
  437. j:=1;
  438. aNotFirst:=false;
  439. { Paraphrased from Delphi XE2 help:
  440. Strings must be separated by Delimiter characters or spaces.
  441. They may be enclosed in QuoteChars.
  442. QuoteChars in the string must be repeated to distinguish them from the QuoteChars enclosing the string.
  443. }
  444. try
  445. if DoClear then
  446. Clear;
  447. len:=length(AValue);
  448. If aStrictDelimiter then
  449. begin
  450. while i<=Len do begin
  451. // skip delimiter
  452. if aNotFirst and (i<=len) and (AValue[i]=aDelimiter) then
  453. inc(i);
  454. // read next string
  455. if i<=len then begin
  456. if AValue[i]=aQuoteChar then begin
  457. // next string is quoted
  458. j:=i+1;
  459. while (j<=len) and
  460. ((AValue[j]<>aQuoteChar) or
  461. ((j+1<=len) and (AValue[j+1]=aQuoteChar))) do
  462. begin
  463. if (j<=len) and (AValue[j]=aQuoteChar) then
  464. inc(j,2)
  465. else
  466. inc(j);
  467. end;
  468. AddQuoted;
  469. i:=j+1;
  470. end else begin
  471. // next string is not quoted; read until delimiter
  472. j:=i;
  473. while (j<=len) and
  474. (AValue[j]<>aDelimiter) do inc(j);
  475. Add( Copy(AValue,i,j-i));
  476. i:=j;
  477. end;
  478. end else begin
  479. if aNotFirst then Add('');
  480. end;
  481. aNotFirst:=true;
  482. end;
  483. end
  484. else
  485. begin
  486. while i<=len do begin
  487. // skip delimiter
  488. if aNotFirst and (i<=len) and (AValue[i]=aDelimiter) then inc(i);
  489. // skip spaces
  490. while (i<=len) and (Ord(AValue[i])<=Ord(' ')) do inc(i);
  491. // read next string
  492. if i<=len then begin
  493. if AValue[i]=aQuoteChar then begin
  494. // next string is quoted
  495. j:=i+1;
  496. while (j<=len) and
  497. ( (AValue[j]<>aQuoteChar) or
  498. ( (j+1<=len) and (AValue[j+1]=aQuoteChar) ) ) do begin
  499. if (j<=len) and (AValue[j]=aQuoteChar) then inc(j,2)
  500. else inc(j);
  501. end;
  502. AddQuoted;
  503. i:=j+1;
  504. end else begin
  505. // next string is not quoted; read until control character/space/delimiter
  506. j:=i;
  507. while (j<=len) and
  508. (Ord(AValue[j])>Ord(' ')) and
  509. (AValue[j]<>aDelimiter) do inc(j);
  510. Add( Copy(AValue,i,j-i));
  511. i:=j;
  512. end;
  513. end else begin
  514. if aNotFirst then Add('');
  515. end;
  516. // skip spaces
  517. while (i<=len) and (Ord(AValue[i])<=Ord(' ')) do inc(i);
  518. aNotFirst:=true;
  519. end;
  520. end;
  521. finally
  522. EndUpdate;
  523. end;
  524. end;
  525. Procedure TStrings.SetCommaText(const Value: string);
  526. begin
  527. CheckSpecialChars;
  528. DoSetDelimitedText(Value,True,StrictDelimiter,'"',',');
  529. end;
  530. procedure TStrings.SetMissingNameValueSeparatorAction(AValue: TMissingNameValueSeparatorAction);
  531. begin
  532. CheckSpecialChars;
  533. FMissingNameValueSeparatorAction:=aValue;
  534. end;
  535. Procedure TStrings.SetStringsAdapter(const Value: IStringsAdapter);
  536. begin
  537. end;
  538. procedure TStrings.SetStrictDelimiter(AValue: Boolean);
  539. begin
  540. if AValue then
  541. Include(FOptions,soStrictDelimiter)
  542. else
  543. Exclude(FOptions,soStrictDelimiter);
  544. end;
  545. procedure TStrings.SetTrailingLineBreak(AValue: Boolean);
  546. begin
  547. if AValue then
  548. Include(FOptions,soTrailingLineBreak)
  549. else
  550. Exclude(FOptions,soTrailingLineBreak);
  551. end;
  552. procedure TStrings.SetUseLocale(AValue: Boolean);
  553. begin
  554. if AValue then
  555. Include(FOptions,soUseLocale)
  556. else
  557. Exclude(FOptions,soUseLocale);
  558. end;
  559. procedure TStrings.SetWriteBOM(AValue: Boolean);
  560. begin
  561. if AValue then
  562. Include(FOptions,soWriteBOM)
  563. else
  564. Exclude(FOptions,soWriteBOM);
  565. end;
  566. Procedure TStrings.SetDefaultEncoding(const ADefaultEncoding: TEncoding);
  567. begin
  568. if (FDefaultEncoding<>nil) and not TEncoding.IsStandardEncoding(FDefaultEncoding) then
  569. FDefaultEncoding.Free;
  570. if TEncoding.IsStandardEncoding(ADefaultEncoding) then
  571. FDefaultEncoding:=ADefaultEncoding
  572. else if ADefaultEncoding<>nil then
  573. FDefaultEncoding:=ADefaultEncoding.Clone
  574. else
  575. FDefaultEncoding:=TEncoding.Default;
  576. end;
  577. Procedure TStrings.SetValue(const Name, Value: string);
  578. Var L : longint;
  579. begin
  580. CheckSpecialChars;
  581. L:=IndexOfName(Name);
  582. if L=-1 then
  583. Add (Name+FNameValueSeparator+Value)
  584. else
  585. Strings[L]:=Name+FNameValueSeparator+value;
  586. end;
  587. procedure TStrings.WriteData(Writer: TWriter);
  588. var
  589. i: Integer;
  590. begin
  591. Writer.WriteListBegin;
  592. for i := 0 to Count - 1 do
  593. Writer.WriteString(Strings[i]);
  594. Writer.WriteListEnd;
  595. end;
  596. function TStrings.CompareStrings(const s1,s2 : string) : Integer;
  597. begin
  598. Result := DoCompareText(s1, s2);
  599. end;
  600. procedure TStrings.DefineProperties(Filer: TFiler);
  601. var
  602. HasData: Boolean;
  603. begin
  604. if Assigned(Filer.Ancestor) then
  605. // Only serialize if string list is different from ancestor
  606. if Filer.Ancestor.InheritsFrom(TStrings) then
  607. HasData := not Equals(TStrings(Filer.Ancestor))
  608. else
  609. HasData := True
  610. else
  611. HasData := Count > 0;
  612. Filer.DefineProperty('Strings', @ReadData, @WriteData, HasData);
  613. end;
  614. Procedure TStrings.Error(const Msg: string; Data: Integer);
  615. begin
  616. Raise EStringListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame), get_caller_frame(get_frame);
  617. end;
  618. Procedure TStrings.Error(const Msg: pstring; Data: Integer);
  619. begin
  620. Raise EStringListError.CreateFmt(Msg^,[Data]) at get_caller_addr(get_frame), get_caller_frame(get_frame);
  621. end;
  622. Function TStrings.GetCapacity: Integer;
  623. begin
  624. Result:=Count;
  625. end;
  626. Function TStrings.GetObject(Index: Integer): TObject;
  627. begin
  628. Result:=Nil;
  629. end;
  630. Function TStrings.GetTextStr: string;
  631. Var P : Pchar;
  632. I,L,NLS : SizeInt;
  633. S,NL : String;
  634. begin
  635. NL:=GetLineBreakCharLBS;
  636. // Determine needed place
  637. L:=0;
  638. NLS:=Length(NL);
  639. For I:=0 to count-1 do
  640. L:=L+Length(Strings[I])+NLS;
  641. if SkipLastLineBreak then
  642. Dec(L,NLS);
  643. Setlength(Result,L);
  644. P:=Pointer(Result);
  645. For i:=0 To count-1 do
  646. begin
  647. S:=Strings[I];
  648. L:=Length(S);
  649. if L<>0 then
  650. System.Move(Pointer(S)^,P^,L);
  651. P:=P+L;
  652. if (I<Count-1) or Not SkipLastLineBreak then
  653. For L:=1 to NLS do
  654. begin
  655. P^:=NL[L];
  656. inc(P);
  657. end;
  658. end;
  659. end;
  660. Procedure TStrings.Put(Index: Integer; const S: string);
  661. Var Obj : TObject;
  662. begin
  663. Obj:=Objects[Index];
  664. Delete(Index);
  665. InsertObject(Index,S,Obj);
  666. end;
  667. Procedure TStrings.PutObject(Index: Integer; AObject: TObject);
  668. begin
  669. // Empty.
  670. end;
  671. Procedure TStrings.SetCapacity(NewCapacity: Integer);
  672. begin
  673. // Empty.
  674. end;
  675. Class Function TStrings.GetNextLine (Const Value : String; Var S : String; Var P : SizeInt) : Boolean;
  676. var
  677. LengthOfValue: SizeInt;
  678. StartPos, FuturePos: SizeInt;
  679. begin
  680. LengthOfValue := Length(Value);
  681. StartPos := P;
  682. if (StartPos <= 0) or (StartPos > LengthOfValue) then // True for LengthOfValue <= 0
  683. begin
  684. S := '';
  685. Exit(False);
  686. end;
  687. FuturePos := StartPos;
  688. while (FuturePos <= LengthOfValue) and not (Value[FuturePos] in [#10, #13]) do
  689. Inc(FuturePos);
  690. // If we use S := Copy(Value, StartPos, FuturePos - StartPos); then compiler
  691. // generate TempS := Copy(...); S := TempS to eliminate side effects and
  692. // implicit "try finally" for TempS finalization
  693. // When we use SetString then no TempS, no try finally generated,
  694. // but we must check case when Value and S is same (side effects)
  695. if Pointer(S) = Pointer(Value) then
  696. System.Delete(S, FuturePos, High(FuturePos))
  697. else
  698. begin
  699. SetString(S, @Value[StartPos], FuturePos - StartPos);
  700. if (FuturePos <= LengthOfValue) and (Value[FuturePos] = #13) then
  701. Inc(FuturePos);
  702. if (FuturePos <= LengthOfValue) and (Value[FuturePos] = #10) then
  703. Inc(FuturePos);
  704. end;
  705. P := FuturePos;
  706. Result := True;
  707. end;
  708. Function TStrings.GetNextLineBreak (Const Value : String; Var S : String; Var P : SizeInt) : Boolean;
  709. var
  710. StartPos, FuturePos: SizeInt;
  711. begin
  712. StartPos := P;
  713. if (StartPos <= 0) or (StartPos > Length(Value)) then // True for Length <= 0
  714. begin
  715. S := '';
  716. Exit(False);
  717. end;
  718. FuturePos := Pos(FLineBreak, Value, StartPos); // Use PosEx in old RTL
  719. // Why we don't use Copy but use SetString read in GetNextLine
  720. if FuturePos = 0 then // No line breaks
  721. begin
  722. FuturePos := Length(Value) + 1;
  723. if Pointer(S) = Pointer(Value) then
  724. // Nothing to do
  725. else
  726. SetString(S, @Value[StartPos], FuturePos - StartPos)
  727. end
  728. else
  729. if Pointer(S) = Pointer(Value) then
  730. System.Delete(S, FuturePos, High(FuturePos))
  731. else
  732. begin
  733. SetString(S, @Value[StartPos], FuturePos - StartPos);
  734. Inc(FuturePos, Length(FLineBreak));
  735. end;
  736. P := FuturePos;
  737. Result := True;
  738. end;
  739. {$IF (SizeOf(Integer) < SizeOf(SizeInt)) }
  740. class function TStrings.GetNextLine(const Value: string; var S: string; var P: Integer) : Boolean;
  741. var
  742. LP: SizeInt;
  743. begin
  744. LP := P;
  745. Result := GetNextLine(Value, S, LP);
  746. P := LP;
  747. end;
  748. function TStrings.GetNextLineBreak(const Value: string; var S: string; var P: Integer) : Boolean;
  749. var
  750. LP: SizeInt;
  751. begin
  752. LP := P;
  753. Result := GetNextLineBreak(Value, S, LP);
  754. P := LP;
  755. end;
  756. {$IFEND}
  757. Procedure TStrings.DoSetTextStr(const Value: string; DoClear : Boolean);
  758. Var
  759. S : String;
  760. P : SizeInt;
  761. begin
  762. Try
  763. beginUpdate;
  764. if DoClear then
  765. Clear;
  766. P:=1;
  767. if FLineBreak=sLineBreak then
  768. begin
  769. While GetNextLine (Value,S,P) do
  770. Add(S)
  771. end
  772. else
  773. While GetNextLineBreak (Value,S,P) do
  774. Add(S);
  775. finally
  776. EndUpdate;
  777. end;
  778. end;
  779. Procedure TStrings.SetTextStr(const Value: string);
  780. begin
  781. CheckSpecialChars;
  782. DoSetTextStr(Value,True);
  783. end;
  784. Procedure TStrings.AddText(const S: string);
  785. begin
  786. CheckSpecialChars;
  787. DoSetTextStr(S,False);
  788. end;
  789. procedure TStrings.AddCommaText(const S: String);
  790. begin
  791. DoSetDelimitedText(S,False,StrictDelimiter,'"',',');
  792. end;
  793. procedure TStrings.AddDelimitedText(const S: String; ADelimiter: Char; AStrictDelimiter: Boolean);
  794. begin
  795. CheckSpecialChars;
  796. DoSetDelimitedText(S,False,AStrictDelimiter,FQuoteChar,ADelimiter);
  797. end;
  798. procedure TStrings.AddDelimitedText(const S: String);
  799. begin
  800. CheckSpecialChars;
  801. DoSetDelimitedText(S,False,StrictDelimiter,FQuoteChar,FDelimiter);
  802. end;
  803. Procedure TStrings.SetUpdateState(Updating: Boolean);
  804. begin
  805. FPONotifyObservers(Self,ooChange,Nil);
  806. end;
  807. destructor TSTrings.Destroy;
  808. begin
  809. if (FEncoding<>nil) and not TEncoding.IsStandardEncoding(FEncoding) then
  810. FreeAndNil(FEncoding);
  811. if (FDefaultEncoding<>nil) and not TEncoding.IsStandardEncoding(FDefaultEncoding) then
  812. FreeAndNil(FDefaultEncoding);
  813. inherited destroy;
  814. end;
  815. constructor TStrings.Create;
  816. begin
  817. inherited Create;
  818. FDefaultEncoding:=TEncoding.Default;
  819. FEncoding:=nil;
  820. FOptions := [soTrailingLineBreak,soUseLocale,soPreserveBOM];
  821. FAlwaysQuote:=False;
  822. end;
  823. Function TStrings.Add(const S: string): Integer;
  824. begin
  825. Result:=Count;
  826. Insert (Count,S);
  827. end;
  828. function TStrings.Add(const Fmt : string; const Args : Array of const): Integer;
  829. begin
  830. Result:=Add(Format(Fmt,Args));
  831. end;
  832. Function TStrings.AddObject(const S: string; AObject: TObject): Integer;
  833. begin
  834. Result:=Add(S);
  835. Objects[result]:=AObject;
  836. end;
  837. function TStrings.AddObject(const Fmt: string; Args : Array of const; AObject: TObject): Integer;
  838. begin
  839. Result:=AddObject(Format(Fmt,Args),AObject);
  840. end;
  841. function TStrings.AddPair(const AName, AValue: string): TStrings;
  842. begin
  843. Result:=AddPair(AName,AValue,Nil);
  844. end;
  845. function TStrings.AddPair(const AName, AValue: string; AObject: TObject): TStrings;
  846. begin
  847. Result := Self;
  848. AddObject(Concat(AName, NameValueSeparator, AValue), AObject);
  849. end;
  850. Procedure TStrings.Append(const S: string);
  851. begin
  852. Add (S);
  853. end;
  854. Procedure TStrings.AddStrings(TheStrings: TStrings; ClearFirst : Boolean);
  855. Var Runner : longint;
  856. begin
  857. beginupdate;
  858. try
  859. if ClearFirst then
  860. Clear;
  861. if Count + TheStrings.Count > Capacity then
  862. Capacity := Count + TheStrings.Count;
  863. For Runner:=0 to TheStrings.Count-1 do
  864. self.AddObject (Thestrings[Runner],TheStrings.Objects[Runner]);
  865. finally
  866. EndUpdate;
  867. end;
  868. end;
  869. Procedure TStrings.AddStrings(TheStrings: TStrings);
  870. begin
  871. AddStrings(TheStrings, False);
  872. end;
  873. Procedure TStrings.AddStrings(const TheStrings: array of string);
  874. begin
  875. AddStrings(TheStrings, False);
  876. end;
  877. Procedure TStrings.AddStrings(const TheStrings: array of string; ClearFirst : Boolean);
  878. Var Runner : longint;
  879. begin
  880. beginupdate;
  881. try
  882. if ClearFirst then
  883. Clear;
  884. if Count + High(TheStrings)+1 > Capacity then
  885. Capacity := Count + High(TheStrings)+1;
  886. For Runner:=Low(TheStrings) to High(TheStrings) do
  887. self.Add(Thestrings[Runner]);
  888. finally
  889. EndUpdate;
  890. end;
  891. end;
  892. Procedure TStrings.Assign(Source: TPersistent);
  893. Var
  894. S : TStrings;
  895. begin
  896. If Source is TStrings then
  897. begin
  898. S:=TStrings(Source);
  899. BeginUpdate;
  900. Try
  901. clear;
  902. FSpecialCharsInited:=S.FSpecialCharsInited;
  903. FQuoteChar:=S.FQuoteChar;
  904. FDelimiter:=S.FDelimiter;
  905. FNameValueSeparator:=S.FNameValueSeparator;
  906. FLBS:=S.FLBS;
  907. FLineBreak:=S.FLineBreak;
  908. FOptions:=S.FOptions;
  909. DefaultEncoding:=S.DefaultEncoding;
  910. SetEncoding(S.Encoding);
  911. AddStrings(S);
  912. finally
  913. EndUpdate;
  914. end;
  915. end
  916. else
  917. Inherited Assign(Source);
  918. end;
  919. Procedure TStrings.BeginUpdate;
  920. begin
  921. if FUpdateCount = 0 then SetUpdateState(true);
  922. inc(FUpdateCount);
  923. end;
  924. Procedure TStrings.EndUpdate;
  925. begin
  926. If FUpdateCount>0 then
  927. Dec(FUpdateCount);
  928. if FUpdateCount=0 then
  929. SetUpdateState(False);
  930. end;
  931. Function TStrings.Equals(Obj: TObject): Boolean;
  932. begin
  933. if Obj is TStrings then
  934. Result := Equals(TStrings(Obj))
  935. else
  936. Result := inherited Equals(Obj);
  937. end;
  938. Function TStrings.Equals(TheStrings: TStrings): Boolean;
  939. Var Runner,Nr : Longint;
  940. begin
  941. Result:=False;
  942. Nr:=Self.Count;
  943. if Nr<>TheStrings.Count then exit;
  944. For Runner:=0 to Nr-1 do
  945. If Strings[Runner]<>TheStrings[Runner] then exit;
  946. Result:=True;
  947. end;
  948. Procedure TStrings.Exchange(Index1, Index2: Integer);
  949. Var
  950. Obj : TObject;
  951. Str : String;
  952. begin
  953. beginUpdate;
  954. Try
  955. Obj:=Objects[Index1];
  956. Str:=Strings[Index1];
  957. Objects[Index1]:=Objects[Index2];
  958. Strings[Index1]:=Strings[Index2];
  959. Objects[Index2]:=Obj;
  960. Strings[Index2]:=Str;
  961. finally
  962. EndUpdate;
  963. end;
  964. end;
  965. function TStrings.GetEnumerator: TStringsEnumerator;
  966. begin
  967. Result:=TStringsEnumerator.Create(Self);
  968. end;
  969. Function TStrings.GetText: PChar;
  970. begin
  971. Result:=StrNew(Pchar(Self.Text));
  972. end;
  973. Function TStrings.DoCompareText(const s1,s2 : string) : PtrInt;
  974. begin
  975. if UseLocale then
  976. result:=AnsiCompareText(s1,s2)
  977. else
  978. result:=CompareText(s1,s2);
  979. end;
  980. Function TStrings.IndexOf(const S: string): Integer;
  981. begin
  982. Result:=0;
  983. While (Result<Count) and (DoCompareText(Strings[Result],S)<>0) do Result:=Result+1;
  984. if Result=Count then Result:=-1;
  985. end;
  986. function TStrings.IndexOf(const S: string; aStart: Integer): Integer;
  987. begin
  988. if aStart<0 then
  989. begin
  990. aStart:=Count+aStart;
  991. if aStart<0 then
  992. aStart:=0;
  993. end;
  994. Result:=aStart;
  995. While (Result<Count) and (DoCompareText(Strings[Result],S)<>0) do Result:=Result+1;
  996. if Result=Count then Result:=-1;
  997. end;
  998. Function TStrings.IndexOfName(const Name: string): Integer;
  999. Var
  1000. len : longint;
  1001. S : String;
  1002. begin
  1003. CheckSpecialChars;
  1004. Result:=0;
  1005. while (Result<Count) do
  1006. begin
  1007. S:=Strings[Result];
  1008. len:=pos(FNameValueSeparator,S)-1;
  1009. if (len>=0) and (DoCompareText(Name,Copy(S,1,Len))=0) then
  1010. exit;
  1011. inc(result);
  1012. end;
  1013. result:=-1;
  1014. end;
  1015. Function TStrings.IndexOfObject(AObject: TObject): Integer;
  1016. begin
  1017. Result:=0;
  1018. While (Result<count) and (Objects[Result]<>AObject) do Result:=Result+1;
  1019. If Result=Count then Result:=-1;
  1020. end;
  1021. Procedure TStrings.InsertObject(Index: Integer; const S: string;
  1022. AObject: TObject);
  1023. begin
  1024. Insert (Index,S);
  1025. Objects[Index]:=AObject;
  1026. end;
  1027. function TStrings.LastIndexOf(const S: string): Integer;
  1028. begin
  1029. Result:=LastIndexOf(S,Count-1);
  1030. end;
  1031. function TStrings.LastIndexOf(const S: string; aStart : Integer): Integer;
  1032. begin
  1033. if aStart<0 then
  1034. begin
  1035. aStart:=Count+aStart;
  1036. if aStart<0 then
  1037. aStart:=0;
  1038. end;
  1039. Result:=aStart;
  1040. if Result>=Count-1 then
  1041. Result:=Count-1;
  1042. While (Result>=0) and (DoCompareText(Strings[Result],S)<>0) do
  1043. Result:=Result-1;
  1044. end;
  1045. Procedure TStrings.LoadFromFile(const FileName: string);
  1046. begin
  1047. LoadFromFile(FileName,False)
  1048. end;
  1049. Procedure TStrings.LoadFromFile(const FileName: string; IgnoreEncoding : Boolean);
  1050. Var
  1051. TheStream : TFileStream;
  1052. begin
  1053. TheStream:=TFileStream.Create(FileName,fmOpenRead or fmShareDenyWrite);
  1054. try
  1055. LoadFromStream(TheStream, IgnoreEncoding);
  1056. finally
  1057. TheStream.Free;
  1058. end;
  1059. end;
  1060. Procedure TStrings.LoadFromFile(const FileName: string; AEncoding: TEncoding);
  1061. Var
  1062. TheStream : TFileStream;
  1063. begin
  1064. TheStream:=TFileStream.Create(FileName,fmOpenRead or fmShareDenyWrite);
  1065. try
  1066. LoadFromStream(TheStream,AEncoding);
  1067. finally
  1068. TheStream.Free;
  1069. end;
  1070. end;
  1071. Procedure TStrings.LoadFromStream(Stream: TStream);
  1072. begin
  1073. LoadFromStream(Stream,False);
  1074. end;
  1075. Const
  1076. LoadBufSize = 1024;
  1077. LoadMaxGrow = MaxInt Div 2;
  1078. Procedure TStrings.LoadFromStream(Stream: TStream; IgnoreEncoding : Boolean);
  1079. {
  1080. Borlands method is no good, since a pipe for
  1081. instance doesn't have a size.
  1082. So we must do it the hard way.
  1083. }
  1084. Var
  1085. Buffer : AnsiString;
  1086. BufLen : SizeInt;
  1087. BytesRead, I, BufDelta : Longint;
  1088. begin
  1089. if not IgnoreEncoding then
  1090. begin
  1091. LoadFromStream(Stream,Nil);
  1092. Exit;
  1093. end;
  1094. // reread into a buffer
  1095. beginupdate;
  1096. try
  1097. Buffer:='';
  1098. BufLen:=0;
  1099. I:=1;
  1100. Repeat
  1101. BufDelta:=LoadBufSize*I;
  1102. SetLength(Buffer,BufLen+BufDelta);
  1103. BytesRead:=Stream.Read(Buffer[BufLen+1],BufDelta);
  1104. inc(BufLen,BufDelta);
  1105. If I<LoadMaxGrow then
  1106. I:=I shl 1;
  1107. Until BytesRead<>BufDelta;
  1108. SetLength(Buffer, BufLen-BufDelta+BytesRead);
  1109. SetTextStr(Buffer);
  1110. SetLength(Buffer,0);
  1111. finally
  1112. EndUpdate;
  1113. end;
  1114. if soPreserveBOM in FOptions then
  1115. WriteBOM:=False;
  1116. end;
  1117. Procedure TStrings.LoadFromStream(Stream: TStream; AEncoding: TEncoding);
  1118. {
  1119. Borlands method is no good, since a pipe for
  1120. instance doesn't have a size.
  1121. So we must do it the hard way.
  1122. }
  1123. Var
  1124. Buffer : TBytes;
  1125. T : string;
  1126. BufLen : SizeInt;
  1127. BytesRead, I, BufDelta, PreambleLength : Longint;
  1128. begin
  1129. // reread into a buffer
  1130. beginupdate;
  1131. try
  1132. SetLength(Buffer,0);
  1133. BufLen:=0;
  1134. I:=1;
  1135. Repeat
  1136. BufDelta:=LoadBufSize*I;
  1137. SetLength(Buffer,BufLen+BufDelta);
  1138. BytesRead:=Stream.Read(Buffer[BufLen],BufDelta);
  1139. inc(BufLen,BufDelta);
  1140. If I<LoadMaxGrow then
  1141. I:=I shl 1;
  1142. Until BytesRead<>BufDelta;
  1143. SetLength(Buffer,BufLen-BufDelta+BytesRead);
  1144. PreambleLength:=TEncoding.GetBufferEncoding(Buffer,AEncoding,FDefaultEncoding);
  1145. T:=AEncoding.GetAnsiString(Buffer,PreambleLength,Length(Buffer)-PreambleLength);
  1146. if soPreserveBOM in FOptions then
  1147. WriteBOM:=PreambleLength>0;
  1148. SetEncoding(AEncoding);
  1149. SetLength(Buffer,0);
  1150. SetTextStr(T);
  1151. finally
  1152. EndUpdate;
  1153. end;
  1154. end;
  1155. Procedure TStrings.Move(CurIndex, NewIndex: Integer);
  1156. Var
  1157. Obj : TObject;
  1158. Str : String;
  1159. begin
  1160. BeginUpdate;
  1161. Try
  1162. Obj:=Objects[CurIndex];
  1163. Str:=Strings[CurIndex];
  1164. Objects[CurIndex]:=Nil; // Prevent Delete from freeing.
  1165. Delete(Curindex);
  1166. InsertObject(NewIndex,Str,Obj);
  1167. finally
  1168. EndUpdate;
  1169. end;
  1170. end;
  1171. function TStrings.Pop: string;
  1172. var
  1173. C : Integer;
  1174. begin
  1175. Result:='';
  1176. C:=Count-1;
  1177. if (C>=0) then
  1178. begin
  1179. Result:=Strings[C];
  1180. Delete(C);
  1181. end;
  1182. end;
  1183. function TStrings.Shift: String;
  1184. begin
  1185. Result:='';
  1186. if (Count > 0) then
  1187. begin
  1188. Result:=Strings[0];
  1189. Delete(0);
  1190. end;
  1191. end;
  1192. Procedure TStrings.SaveToFile(const FileName: string);
  1193. Var TheStream : TFileStream;
  1194. begin
  1195. TheStream:=TFileStream.Create(FileName,fmCreate);
  1196. try
  1197. SaveToStream(TheStream);
  1198. finally
  1199. TheStream.Free;
  1200. end;
  1201. end;
  1202. Procedure TStrings.SaveToFile(const FileName: string; IgnoreEncoding : Boolean);
  1203. Var TheStream : TFileStream;
  1204. begin
  1205. TheStream:=TFileStream.Create(FileName,fmCreate);
  1206. try
  1207. SaveToStream(TheStream, IgnoreEncoding);
  1208. finally
  1209. TheStream.Free;
  1210. end;
  1211. end;
  1212. Procedure TStrings.SaveToFile(const FileName: string; AEncoding: TEncoding);
  1213. Var TheStream : TFileStream;
  1214. begin
  1215. TheStream:=TFileStream.Create(FileName,fmCreate);
  1216. try
  1217. SaveToStream(TheStream,AEncoding);
  1218. finally
  1219. TheStream.Free;
  1220. end;
  1221. end;
  1222. Procedure TStrings.SaveToStream(Stream: TStream);
  1223. begin
  1224. SaveToStream(Stream,False)
  1225. end;
  1226. Procedure TStrings.SaveToStream(Stream: TStream; IgnoreEncoding: Boolean);
  1227. Var
  1228. I,L,NLS : SizeInt;
  1229. S,NL : String;
  1230. begin
  1231. if not IgnoreEncoding then
  1232. begin
  1233. SaveToStream(Stream,FEncoding);
  1234. Exit;
  1235. end;
  1236. NL:=GetLineBreakCharLBS;
  1237. NLS:=Length(NL)*SizeOf(Char);
  1238. For i:=0 To count-1 do
  1239. begin
  1240. S:=Strings[I];
  1241. L:=Length(S);
  1242. if L<>0 then
  1243. Stream.WriteBuffer(S[1], L*SizeOf(Char));
  1244. if (I<Count-1) or Not SkipLastLineBreak then
  1245. Stream.WriteBuffer(NL[1], NLS);
  1246. end;
  1247. end;
  1248. Procedure TStrings.SaveToStream(Stream: TStream; AEncoding: TEncoding);
  1249. Var B,BNL : TBytes;
  1250. NL,S: string;
  1251. i,BNLS: SizeInt;
  1252. begin
  1253. if AEncoding=nil then
  1254. AEncoding:=FDefaultEncoding;
  1255. if WriteBOM then
  1256. begin
  1257. B:=AEncoding.GetPreamble;
  1258. if Length(B)>0 then
  1259. Stream.WriteBuffer(B[0],Length(B));
  1260. end;
  1261. NL := GetLineBreakCharLBS;
  1262. BNL:=AEncoding.GetAnsiBytes(NL);
  1263. BNLS:=Length(BNL);
  1264. For i:=0 To count-1 do
  1265. begin
  1266. S:=Strings[I];
  1267. if S<>'' then
  1268. begin
  1269. B:=AEncoding.GetAnsiBytes(S);
  1270. Stream.WriteBuffer(B[0],Length(B));
  1271. end;
  1272. if (I<Count-1) or Not SkipLastLineBreak then
  1273. Stream.WriteBuffer(BNL[0],BNLS);
  1274. end;
  1275. end;
  1276. Procedure TStrings.SetText(TheText: PChar);
  1277. Var S : String;
  1278. begin
  1279. If TheText<>Nil then
  1280. S:=StrPas(TheText)
  1281. else
  1282. S:='';
  1283. SetTextStr(S);
  1284. end;
  1285. {****************************************************************************}
  1286. {* TStringList *}
  1287. {****************************************************************************}
  1288. {$if not defined(FPC_TESTGENERICS)}
  1289. procedure TStringList.ExchangeItemsInt(Index1, Index2: Integer);
  1290. Var P1,P2 : Pointer;
  1291. begin
  1292. P1:=Pointer(Flist^[Index1].FString);
  1293. P2:=Pointer(Flist^[Index1].FObject);
  1294. Pointer(Flist^[Index1].Fstring):=Pointer(Flist^[Index2].Fstring);
  1295. Pointer(Flist^[Index1].FObject):=Pointer(Flist^[Index2].FObject);
  1296. Pointer(Flist^[Index2].Fstring):=P1;
  1297. Pointer(Flist^[Index2].FObject):=P2;
  1298. end;
  1299. function TStringList.GetSorted: Boolean;
  1300. begin
  1301. Result:=FSortStyle in [sslUser,sslAuto];
  1302. end;
  1303. procedure TStringList.ExchangeItems(Index1, Index2: Integer);
  1304. begin
  1305. ExchangeItemsInt(Index1, Index2);
  1306. end;
  1307. procedure TStringList.Grow;
  1308. Var
  1309. NC : Integer;
  1310. begin
  1311. NC:=FCapacity;
  1312. If NC>=256 then
  1313. NC:=NC+(NC Div 4)
  1314. else if NC=0 then
  1315. NC:=4
  1316. else
  1317. NC:=NC*4;
  1318. SetCapacity(NC);
  1319. end;
  1320. procedure TStringList.InternalClear(FromIndex: Integer; ClearOnly: Boolean);
  1321. Var
  1322. I: Integer;
  1323. begin
  1324. if FromIndex < FCount then
  1325. begin
  1326. if FOwnsObjects then
  1327. begin
  1328. For I:=FromIndex to FCount-1 do
  1329. begin
  1330. Flist^[I].FString:='';
  1331. freeandnil(Flist^[i].FObject);
  1332. end;
  1333. end
  1334. else
  1335. begin
  1336. For I:=FromIndex to FCount-1 do
  1337. Flist^[I].FString:='';
  1338. end;
  1339. FCount:=FromIndex;
  1340. end;
  1341. if Not ClearOnly then
  1342. SetCapacity(0);
  1343. end;
  1344. procedure TStringList.QuickSort(L, R: Integer; CompareFn: TStringListSortCompare
  1345. );
  1346. var
  1347. Pivot, vL, vR: Integer;
  1348. ExchangeProc: procedure(Left, Right: Integer) of object;
  1349. begin
  1350. //if ExchangeItems is override call that, else call (faster) ExchangeItemsInt
  1351. if TMethod(@Self.ExchangeItems).Code = CodePointer(@TStringList.ExchangeItems) then
  1352. ExchangeProc := @ExchangeItemsInt
  1353. else
  1354. ExchangeProc := @ExchangeItems;
  1355. if R - L <= 1 then begin // a little bit of time saver
  1356. if L < R then
  1357. if CompareFn(Self, L, R) > 0 then
  1358. ExchangeProc(L, R);
  1359. Exit;
  1360. end;
  1361. vL := L;
  1362. vR := R;
  1363. Pivot := L + Random(R - L); // they say random is best
  1364. while vL < vR do begin
  1365. while (vL < Pivot) and (CompareFn(Self, vL, Pivot) <= 0) do
  1366. Inc(vL);
  1367. while (vR > Pivot) and (CompareFn(Self, vR, Pivot) > 0) do
  1368. Dec(vR);
  1369. ExchangeProc(vL, vR);
  1370. if Pivot = vL then // swap pivot if we just hit it from one side
  1371. Pivot := vR
  1372. else if Pivot = vR then
  1373. Pivot := vL;
  1374. end;
  1375. if Pivot - 1 >= L then
  1376. QuickSort(L, Pivot - 1, CompareFn);
  1377. if Pivot + 1 <= R then
  1378. QuickSort(Pivot + 1, R, CompareFn);
  1379. end;
  1380. procedure TStringList.InsertItem(Index: Integer; const S: string);
  1381. begin
  1382. InsertItem(Index, S, nil);
  1383. end;
  1384. procedure TStringList.InsertItem(Index: Integer; const S: string; O: TObject);
  1385. begin
  1386. Changing;
  1387. If FCount=Fcapacity then Grow;
  1388. If Index<FCount then
  1389. System.Move (FList^[Index],FList^[Index+1],
  1390. (FCount-Index)*SizeOf(TStringItem));
  1391. Pointer(Flist^[Index].Fstring):=Nil; // Needed to initialize...
  1392. Flist^[Index].FString:=S;
  1393. Flist^[Index].FObject:=O;
  1394. Inc(FCount);
  1395. Changed;
  1396. end;
  1397. procedure TStringList.SetSorted(Value: Boolean);
  1398. begin
  1399. If Value then
  1400. SortStyle:=sslAuto
  1401. else
  1402. SortStyle:=sslNone
  1403. end;
  1404. procedure TStringList.Changed;
  1405. begin
  1406. If (FUpdateCount=0) Then
  1407. begin
  1408. If Assigned(FOnChange) then
  1409. FOnchange(Self);
  1410. FPONotifyObservers(Self,ooChange,Nil);
  1411. end;
  1412. end;
  1413. procedure TStringList.Changing;
  1414. begin
  1415. If FUpdateCount=0 then
  1416. if Assigned(FOnChanging) then
  1417. FOnchanging(Self);
  1418. end;
  1419. function TStringList.Get(Index: Integer): string;
  1420. begin
  1421. CheckIndex(Index);
  1422. Result:=Flist^[Index].FString;
  1423. end;
  1424. function TStringList.GetCapacity: Integer;
  1425. begin
  1426. Result:=FCapacity;
  1427. end;
  1428. function TStringList.GetCount: Integer;
  1429. begin
  1430. Result:=FCount;
  1431. end;
  1432. function TStringList.GetObject(Index: Integer): TObject;
  1433. begin
  1434. CheckIndex(Index);
  1435. Result:=Flist^[Index].FObject;
  1436. end;
  1437. procedure TStringList.Put(Index: Integer; const S: string);
  1438. begin
  1439. If Sorted then
  1440. Error(SSortedListError,0);
  1441. CheckIndex(Index);
  1442. Changing;
  1443. Flist^[Index].FString:=S;
  1444. Changed;
  1445. end;
  1446. procedure TStringList.PutObject(Index: Integer; AObject: TObject);
  1447. begin
  1448. CheckIndex(Index);
  1449. Changing;
  1450. Flist^[Index].FObject:=AObject;
  1451. Changed;
  1452. end;
  1453. procedure TStringList.SetCapacity(NewCapacity: Integer);
  1454. Var NewList : Pointer;
  1455. MSize : Longint;
  1456. begin
  1457. If (NewCapacity<0) then
  1458. Error (SListCapacityError,NewCapacity);
  1459. If NewCapacity>FCapacity then
  1460. begin
  1461. GetMem (NewList,NewCapacity*SizeOf(TStringItem));
  1462. If NewList=Nil then
  1463. Error (SListCapacityError,NewCapacity);
  1464. If Assigned(FList) then
  1465. begin
  1466. MSize:=FCapacity*Sizeof(TStringItem);
  1467. System.Move (FList^,NewList^,MSize);
  1468. FillWord (Pchar(NewList)[MSize],(NewCapacity-FCapacity)*(SizeOf(TStringItem) div SizeOf(Word)), 0);
  1469. FreeMem (Flist,MSize);
  1470. end;
  1471. Flist:=NewList;
  1472. FCapacity:=NewCapacity;
  1473. end
  1474. else if NewCapacity<FCapacity then
  1475. begin
  1476. if NewCapacity = 0 then
  1477. begin
  1478. if FCount > 0 then
  1479. InternalClear(0,True);
  1480. FreeMem(FList);
  1481. FList := nil;
  1482. end else
  1483. begin
  1484. InternalClear(NewCapacity,True);
  1485. GetMem(NewList, NewCapacity * SizeOf(TStringItem));
  1486. System.Move(FList^, NewList^, NewCapacity * SizeOf(TStringItem));
  1487. FreeMem(FList);
  1488. FList := NewList;
  1489. end;
  1490. FCapacity:=NewCapacity;
  1491. end;
  1492. end;
  1493. procedure TStringList.SetUpdateState(Updating: Boolean);
  1494. begin
  1495. If Updating then
  1496. Changing
  1497. else
  1498. Changed
  1499. end;
  1500. destructor TStringList.Destroy;
  1501. begin
  1502. InternalClear;
  1503. Inherited destroy;
  1504. end;
  1505. function TStringList.Add(const S: string): Integer;
  1506. begin
  1507. If (SortStyle<>sslAuto) then
  1508. Result:=FCount
  1509. else
  1510. If Find (S,Result) then
  1511. Case DUplicates of
  1512. DupIgnore : Exit;
  1513. DupError : Error(SDuplicateString,0)
  1514. end;
  1515. InsertItem (Result,S);
  1516. end;
  1517. procedure TStringList.Clear;
  1518. begin
  1519. if FCount = 0 then Exit;
  1520. Changing;
  1521. InternalClear;
  1522. Changed;
  1523. end;
  1524. procedure TStringList.Delete(Index: Integer);
  1525. begin
  1526. CheckIndex(Index);
  1527. Changing;
  1528. Flist^[Index].FString:='';
  1529. if FOwnsObjects then
  1530. FreeAndNil(Flist^[Index].FObject);
  1531. Dec(FCount);
  1532. If Index<FCount then
  1533. System.Move(Flist^[Index+1],
  1534. Flist^[Index],
  1535. (Fcount-Index)*SizeOf(TStringItem));
  1536. Changed;
  1537. end;
  1538. procedure TStringList.Exchange(Index1, Index2: Integer);
  1539. begin
  1540. CheckIndex(Index1);
  1541. CheckIndex(Index2);
  1542. Changing;
  1543. ExchangeItemsInt(Index1,Index2);
  1544. changed;
  1545. end;
  1546. procedure TStringList.SetCaseSensitive(b : boolean);
  1547. begin
  1548. if b=FCaseSensitive then
  1549. Exit;
  1550. FCaseSensitive:=b;
  1551. if FSortStyle=sslAuto then
  1552. begin
  1553. FForceSort:=True;
  1554. try
  1555. Sort;
  1556. finally
  1557. FForceSort:=False;
  1558. end;
  1559. end;
  1560. end;
  1561. procedure TStringList.SetSortStyle(AValue: TStringsSortStyle);
  1562. begin
  1563. if FSortStyle=AValue then Exit;
  1564. if (AValue=sslAuto) then
  1565. Sort;
  1566. FSortStyle:=AValue;
  1567. end;
  1568. procedure TStringList.CheckIndex(AIndex: Integer);
  1569. begin
  1570. If (AIndex<0) or (AIndex>=FCount) then
  1571. Error(SListIndexError,AIndex);
  1572. end;
  1573. function TStringList.DoCompareText(const s1, s2: string): PtrInt;
  1574. begin
  1575. if FCaseSensitive then
  1576. begin
  1577. if UseLocale then
  1578. result:=AnsiCompareStr(s1,s2)
  1579. else
  1580. result:=CompareStr(s1,s2);
  1581. end else
  1582. begin
  1583. if UseLocale then
  1584. result:=AnsiCompareText(s1,s2)
  1585. else
  1586. result:=CompareText(s1,s2);
  1587. end;
  1588. end;
  1589. function TStringList.Find(const S: string; out Index: Integer): Boolean;
  1590. var
  1591. L, R, I: Integer;
  1592. CompareRes: PtrInt;
  1593. begin
  1594. Result := false;
  1595. Index:=-1;
  1596. if Not Sorted then
  1597. Raise EListError.Create(SErrFindNeedsSortedList);
  1598. // Use binary search.
  1599. L := 0;
  1600. R := Count - 1;
  1601. while (L<=R) do
  1602. begin
  1603. I := L + (R - L) div 2;
  1604. CompareRes := DoCompareText(S, Flist^[I].FString);
  1605. if (CompareRes>0) then
  1606. L := I+1
  1607. else begin
  1608. R := I-1;
  1609. if (CompareRes=0) then begin
  1610. Result := true;
  1611. if (Duplicates<>dupAccept) then
  1612. L := I; // forces end of while loop
  1613. end;
  1614. end;
  1615. end;
  1616. Index := L;
  1617. end;
  1618. function TStringList.IndexOf(const S: string): Integer;
  1619. begin
  1620. If Not Sorted then
  1621. Result:=Inherited indexOf(S)
  1622. else
  1623. // faster using binary search...
  1624. If Not Find (S,Result) then
  1625. Result:=-1;
  1626. end;
  1627. procedure TStringList.Insert(Index: Integer; const S: string);
  1628. begin
  1629. If SortStyle=sslAuto then
  1630. Error (SSortedListError,0)
  1631. else
  1632. begin
  1633. If (Index<0) or (Index>FCount) then
  1634. Error(SListIndexError,Index); // Cannot use CheckIndex, because there >= FCount...
  1635. InsertItem (Index,S);
  1636. end;
  1637. end;
  1638. procedure TStringList.CustomSort(CompareFn: TStringListSortCompare);
  1639. begin
  1640. If (FCount>1) and (FForceSort or (FSortStyle<>sslAuto)) then
  1641. begin
  1642. Changing;
  1643. QuickSort(0,FCount-1, CompareFn);
  1644. Changed;
  1645. end;
  1646. end;
  1647. function StringListAnsiCompare(List: TStringList; Index1, Index: Integer): Integer;
  1648. begin
  1649. Result := List.DoCompareText(List.FList^[Index1].FString,
  1650. List.FList^[Index].FString);
  1651. end;
  1652. procedure TStringList.Sort;
  1653. begin
  1654. CustomSort(@StringListAnsiCompare);
  1655. end;
  1656. {$else}
  1657. { generics based implementation of TStringList follows }
  1658. function StringListAnsiCompare(List: TStringList; Index1, Index2: Integer): Integer;
  1659. begin
  1660. Result := List.DoCompareText(List.Strings[Index1], List.Strings[Index2]);
  1661. end;
  1662. constructor TStringList.Create;
  1663. begin
  1664. inherited;
  1665. FOwnsObjects:=false;
  1666. FMap := TFPStrObjMap.Create;
  1667. FMap.OnPtrCompare := @MapPtrCompare;
  1668. FOnCompareText := @DefaultCompareText;
  1669. NameValueSeparator:='=';
  1670. CheckSpecialChars;
  1671. end;
  1672. destructor TStringList.Destroy;
  1673. begin
  1674. FMap.Free;
  1675. inherited;
  1676. end;
  1677. function TStringList.GetDuplicates: TDuplicates;
  1678. begin
  1679. Result := FMap.Duplicates;
  1680. end;
  1681. function TStringList.GetSorted: boolean;
  1682. begin
  1683. Result := FMap.Sorted;
  1684. end;
  1685. procedure TStringList.SetDuplicates(NewDuplicates: TDuplicates);
  1686. begin
  1687. FMap.Duplicates := NewDuplicates;
  1688. end;
  1689. procedure TStringList.SetSorted(NewSorted: Boolean);
  1690. begin
  1691. FMap.Sorted := NewSorted;
  1692. end;
  1693. procedure TStringList.Changed;
  1694. begin
  1695. if FUpdateCount = 0 then
  1696. if Assigned(FOnChange) then
  1697. FOnChange(Self);
  1698. end;
  1699. procedure TStringList.Changing;
  1700. begin
  1701. if FUpdateCount = 0 then
  1702. if Assigned(FOnChanging) then
  1703. FOnChanging(Self);
  1704. end;
  1705. function TStringList.Get(Index: Integer): string;
  1706. begin
  1707. Result := FMap.Keys[Index];
  1708. end;
  1709. function TStringList.GetCapacity: Integer;
  1710. begin
  1711. Result := FMap.Capacity;
  1712. end;
  1713. function TStringList.GetCount: Integer;
  1714. begin
  1715. Result := FMap.Count;
  1716. end;
  1717. function TStringList.GetObject(Index: Integer): TObject;
  1718. begin
  1719. Result := FMap.Data[Index];
  1720. end;
  1721. procedure TStringList.Put(Index: Integer; const S: string);
  1722. begin
  1723. Changing;
  1724. FMap.Keys[Index] := S;
  1725. Changed;
  1726. end;
  1727. procedure TStringList.PutObject(Index: Integer; AObject: TObject);
  1728. begin
  1729. Changing;
  1730. FMap.Data[Index] := AObject;
  1731. Changed;
  1732. end;
  1733. procedure TStringList.SetCapacity(NewCapacity: Integer);
  1734. begin
  1735. FMap.Capacity := NewCapacity;
  1736. end;
  1737. procedure TStringList.SetUpdateState(Updating: Boolean);
  1738. begin
  1739. if Updating then
  1740. Changing
  1741. else
  1742. Changed
  1743. end;
  1744. function TStringList.Add(const S: string): Integer;
  1745. begin
  1746. Result := FMap.Add(S);
  1747. end;
  1748. procedure TStringList.Clear;
  1749. begin
  1750. if FMap.Count = 0 then exit;
  1751. Changing;
  1752. FMap.Clear;
  1753. Changed;
  1754. end;
  1755. procedure TStringList.Delete(Index: Integer);
  1756. begin
  1757. if (Index < 0) or (Index >= FMap.Count) then
  1758. Error(SListIndexError, Index);
  1759. Changing;
  1760. FMap.Delete(Index);
  1761. Changed;
  1762. end;
  1763. procedure TStringList.Exchange(Index1, Index2: Integer);
  1764. begin
  1765. if (Index1 < 0) or (Index1 >= FMap.Count) then
  1766. Error(SListIndexError, Index1);
  1767. if (Index2 < 0) or (Index2 >= FMap.Count) then
  1768. Error(SListIndexError, Index2);
  1769. Changing;
  1770. FMap.InternalExchange(Index1, Index2);
  1771. Changed;
  1772. end;
  1773. procedure TStringList.SetCaseSensitive(NewSensitive: Boolean);
  1774. begin
  1775. if NewSensitive <> FCaseSensitive then
  1776. begin
  1777. FCaseSensitive := NewSensitive;
  1778. if Sorted then
  1779. Sort;
  1780. end;
  1781. end;
  1782. function TStringList.MapPtrCompare(Key1, Key2: Pointer): Integer;
  1783. begin
  1784. Result := FOnCompareText(string(Key1^), string(Key2^));
  1785. end;
  1786. function TStringList.DefaultCompareText(const s1, s2: string): PtrInt;
  1787. begin
  1788. if FCaseSensitive then
  1789. Result := AnsiCompareStr(s1, s2)
  1790. else
  1791. Result := AnsiCompareText(s1, s2);
  1792. end;
  1793. function TStringList.DoCompareText(const s1, s2: string): PtrInt;
  1794. begin
  1795. Result := FOnCompareText(s1, s2);
  1796. end;
  1797. function TStringList.Find(const S: string; var Index: Integer): Boolean;
  1798. begin
  1799. Result := FMap.Find(S, Index);
  1800. end;
  1801. function TStringList.IndexOf(const S: string): Integer;
  1802. begin
  1803. Result := FMap.IndexOf(S);
  1804. end;
  1805. procedure TStringList.Insert(Index: Integer; const S: string);
  1806. begin
  1807. if not Sorted and (0 <= Index) and (Index < FMap.Count) then
  1808. Changing;
  1809. FMap.InsertKey(Index, S);
  1810. Changed;
  1811. end;
  1812. procedure TStringList.QuickSort(L, R: Integer; CompareFn: TStringListSortCompare);
  1813. var
  1814. I, J, Pivot: Integer;
  1815. begin
  1816. repeat
  1817. I := L;
  1818. J := R;
  1819. Pivot := (L + R) div 2;
  1820. repeat
  1821. while CompareFn(Self, I, Pivot) < 0 do Inc(I);
  1822. while CompareFn(Self, J, Pivot) > 0 do Dec(J);
  1823. if I <= J then
  1824. begin
  1825. FMap.InternalExchange(I, J); // No check, indices are correct.
  1826. if Pivot = I then
  1827. Pivot := J
  1828. else if Pivot = J then
  1829. Pivot := I;
  1830. Inc(I);
  1831. Dec(j);
  1832. end;
  1833. until I > J;
  1834. if L < J then
  1835. QuickSort(L,J, CompareFn);
  1836. L := I;
  1837. until I >= R;
  1838. end;
  1839. procedure TStringList.CustomSort(CompareFn: TStringListSortCompare);
  1840. begin
  1841. if not Sorted and (FMap.Count > 1) then
  1842. begin
  1843. Changing;
  1844. QuickSort(0, FMap.Count-1, CompareFn);
  1845. Changed;
  1846. end;
  1847. end;
  1848. procedure TStringList.Sort;
  1849. begin
  1850. if not Sorted and (FMap.Count > 1) then
  1851. begin
  1852. Changing;
  1853. FMap.Sort;
  1854. Changed;
  1855. end;
  1856. end;
  1857. {$endif}