stringl.inc 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  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; Quote : String) : String;
  35. Var
  36. I,J : Integer;
  37. begin
  38. J:=0;
  39. Result:=S;
  40. for i:=1to 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. FSpecialCharsInited:=true;
  64. FLBS:=DefaultTextLineBreakStyle;
  65. end;
  66. end;
  67. Function TStrings.GetLBS : TTextLineBreakStyle;
  68. begin
  69. CheckSpecialChars;
  70. Result:=FLBS;
  71. end;
  72. Procedure TStrings.SetLBS (AValue : TTextLineBreakStyle);
  73. begin
  74. CheckSpecialChars;
  75. FLBS:=AValue;
  76. end;
  77. procedure TStrings.SetDelimiter(c:Char);
  78. begin
  79. CheckSpecialChars;
  80. FDelimiter:=c;
  81. end;
  82. procedure TStrings.SetQuoteChar(c:Char);
  83. begin
  84. CheckSpecialChars;
  85. FQuoteChar:=c;
  86. end;
  87. procedure TStrings.SetNameValueSeparator(c:Char);
  88. begin
  89. CheckSpecialChars;
  90. FNameValueSeparator:=c;
  91. end;
  92. function TStrings.GetCommaText: string;
  93. Var
  94. C1,C2 : Char;
  95. FSD : Boolean;
  96. begin
  97. CheckSpecialChars;
  98. FSD:=StrictDelimiter;
  99. C1:=Delimiter;
  100. C2:=QuoteChar;
  101. Delimiter:=',';
  102. QuoteChar:='"';
  103. StrictDelimiter:=False;
  104. Try
  105. Result:=GetDelimitedText;
  106. Finally
  107. Delimiter:=C1;
  108. QuoteChar:=C2;
  109. StrictDelimiter:=FSD;
  110. end;
  111. end;
  112. Function TStrings.GetDelimitedText: string;
  113. Var
  114. I : integer;
  115. p : pchar;
  116. c : set of char;
  117. S : String;
  118. begin
  119. CheckSpecialChars;
  120. result:='';
  121. if StrictDelimiter then
  122. c:=[#0,Delimiter]
  123. else
  124. c:=[#0..' ',QuoteChar,Delimiter];
  125. For i:=0 to count-1 do
  126. begin
  127. S:=Strings[i];
  128. p:=pchar(S);
  129. while not(p^ in c) do
  130. inc(p);
  131. // strings in list may contain #0
  132. if (p<>pchar(S)+length(S)) and not StrictDelimiter then
  133. Result:=Result+QuoteString(S,QuoteChar)
  134. else
  135. Result:=Result+S;
  136. if I<Count-1 then
  137. Result:=Result+Delimiter;
  138. end;
  139. If (Length(Result)=0) and (Count=1) then
  140. Result:=QuoteChar+QuoteChar;
  141. end;
  142. procedure TStrings.GetNameValue(Index : Integer; Out AName,AValue : String);
  143. Var L : longint;
  144. begin
  145. CheckSpecialChars;
  146. AValue:=Strings[Index];
  147. L:=Pos(FNameValueSeparator,AValue);
  148. If L<>0 then
  149. begin
  150. AName:=Copy(AValue,1,L-1);
  151. System.Delete(AValue,1,L);
  152. end
  153. else
  154. AName:='';
  155. end;
  156. function TStrings.ExtractName(const s:String):String;
  157. var
  158. L: Longint;
  159. begin
  160. CheckSpecialChars;
  161. L:=Pos(FNameValueSeparator,S);
  162. If L<>0 then
  163. Result:=Copy(S,1,L-1)
  164. else
  165. Result:='';
  166. end;
  167. function TStrings.GetName(Index: Integer): string;
  168. Var
  169. V : String;
  170. begin
  171. GetNameValue(Index,Result,V);
  172. end;
  173. Function TStrings.GetValue(const Name: string): string;
  174. Var
  175. L : longint;
  176. N : String;
  177. begin
  178. Result:='';
  179. L:=IndexOfName(Name);
  180. If L<>-1 then
  181. GetNameValue(L,N,Result);
  182. end;
  183. Function TStrings.GetValueFromIndex(Index: Integer): string;
  184. Var
  185. N : String;
  186. begin
  187. GetNameValue(Index,N,Result);
  188. end;
  189. Procedure TStrings.SetValueFromIndex(Index: Integer; const Value: string);
  190. begin
  191. If (Value='') then
  192. Delete(Index)
  193. else
  194. begin
  195. If (Index<0) then
  196. Index:=Add('');
  197. CheckSpecialChars;
  198. Strings[Index]:=GetName(Index)+FNameValueSeparator+Value;
  199. end;
  200. end;
  201. procedure TStrings.ReadData(Reader: TReader);
  202. begin
  203. Reader.ReadListBegin;
  204. BeginUpdate;
  205. try
  206. Clear;
  207. while not Reader.EndOfList do
  208. Add(Reader.ReadString);
  209. finally
  210. EndUpdate;
  211. end;
  212. Reader.ReadListEnd;
  213. end;
  214. Procedure TStrings.SetDelimitedText(const AValue: string);
  215. var i,j:integer;
  216. aNotFirst:boolean;
  217. begin
  218. CheckSpecialChars;
  219. BeginUpdate;
  220. i:=1;
  221. j:=1;
  222. aNotFirst:=false;
  223. try
  224. Clear;
  225. If StrictDelimiter then
  226. begin
  227. // Easier, faster loop.
  228. While I<=Length(AValue) do
  229. begin
  230. If (AValue[I] in [FDelimiter,#0]) then
  231. begin
  232. Add(Copy(AValue,J,I-J));
  233. J:=I+1;
  234. end;
  235. Inc(i);
  236. end;
  237. If (Length(AValue)>0) then
  238. Add(Copy(AValue,J,I-J));
  239. end
  240. else
  241. begin
  242. while i<=length(AValue) do begin
  243. // skip delimiter
  244. if aNotFirst and (i<=length(AValue)) and (AValue[i]=FDelimiter) then inc(i);
  245. // skip spaces
  246. while (i<=length(AValue)) and (Ord(AValue[i])<=Ord(' ')) do inc(i);
  247. // read next string
  248. if i<=length(AValue) then begin
  249. if AValue[i]=FQuoteChar then begin
  250. // next string is quoted
  251. j:=i+1;
  252. while (j<=length(AValue)) and
  253. ( (AValue[j]<>FQuoteChar) or
  254. ( (j+1<=length(AValue)) and (AValue[j+1]=FQuoteChar) ) ) do begin
  255. if (j<=length(AValue)) and (AValue[j]=FQuoteChar) then inc(j,2)
  256. else inc(j);
  257. end;
  258. // j is position of closing quote
  259. Add( StringReplace (Copy(AValue,i+1,j-i-1),
  260. FQuoteChar+FQuoteChar,FQuoteChar, [rfReplaceAll]));
  261. i:=j+1;
  262. end else begin
  263. // next string is not quoted
  264. j:=i;
  265. while (j<=length(AValue)) and
  266. (Ord(AValue[j])>Ord(' ')) and
  267. (AValue[j]<>FDelimiter) do inc(j);
  268. Add( Copy(AValue,i,j-i));
  269. i:=j;
  270. end;
  271. end else begin
  272. if aNotFirst then Add('');
  273. end;
  274. // skip spaces
  275. while (i<=length(AValue)) and (Ord(AValue[i])<=Ord(' ')) do inc(i);
  276. aNotFirst:=true;
  277. end;
  278. end;
  279. finally
  280. EndUpdate;
  281. end;
  282. end;
  283. Procedure TStrings.SetCommaText(const Value: string);
  284. Var
  285. C1,C2 : Char;
  286. begin
  287. CheckSpecialChars;
  288. C1:=Delimiter;
  289. C2:=QuoteChar;
  290. Delimiter:=',';
  291. QuoteChar:='"';
  292. Try
  293. SetDelimitedText(Value);
  294. Finally
  295. Delimiter:=C1;
  296. QuoteChar:=C2;
  297. end;
  298. end;
  299. Procedure TStrings.SetStringsAdapter(const Value: IStringsAdapter);
  300. begin
  301. end;
  302. Procedure TStrings.SetValue(const Name, Value: string);
  303. Var L : longint;
  304. begin
  305. CheckSpecialChars;
  306. L:=IndexOfName(Name);
  307. if L=-1 then
  308. Add (Name+FNameValueSeparator+Value)
  309. else
  310. Strings[L]:=Name+FNameValueSeparator+value;
  311. end;
  312. procedure TStrings.WriteData(Writer: TWriter);
  313. var
  314. i: Integer;
  315. begin
  316. Writer.WriteListBegin;
  317. for i := 0 to Count - 1 do
  318. Writer.WriteString(Strings[i]);
  319. Writer.WriteListEnd;
  320. end;
  321. procedure TStrings.DefineProperties(Filer: TFiler);
  322. var
  323. HasData: Boolean;
  324. begin
  325. if Assigned(Filer.Ancestor) then
  326. // Only serialize if string list is different from ancestor
  327. if Filer.Ancestor.InheritsFrom(TStrings) then
  328. HasData := not Equals(TStrings(Filer.Ancestor))
  329. else
  330. HasData := True
  331. else
  332. HasData := Count > 0;
  333. Filer.DefineProperty('Strings', @ReadData, @WriteData, HasData);
  334. end;
  335. Procedure TStrings.Error(const Msg: string; Data: Integer);
  336. begin
  337. Raise EStringListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame);
  338. end;
  339. Procedure TStrings.Error(const Msg: pstring; Data: Integer);
  340. begin
  341. Raise EStringListError.CreateFmt(Msg^,[Data]) at get_caller_addr(get_frame);
  342. end;
  343. Function TStrings.GetCapacity: Integer;
  344. begin
  345. Result:=Count;
  346. end;
  347. Function TStrings.GetObject(Index: Integer): TObject;
  348. begin
  349. Result:=Nil;
  350. end;
  351. Function TStrings.GetTextStr: string;
  352. Var P : Pchar;
  353. I,L,NLS : Longint;
  354. S,NL : String;
  355. begin
  356. CheckSpecialChars;
  357. // Determine needed place
  358. Case FLBS of
  359. tlbsLF : NL:=#10;
  360. tlbsCRLF : NL:=#13#10;
  361. tlbsCR : NL:=#13;
  362. end;
  363. L:=0;
  364. NLS:=Length(NL);
  365. For I:=0 to count-1 do
  366. L:=L+Length(Strings[I])+NLS;
  367. Setlength(Result,L);
  368. P:=Pointer(Result);
  369. For i:=0 To count-1 do
  370. begin
  371. S:=Strings[I];
  372. L:=Length(S);
  373. if L<>0 then
  374. System.Move(Pointer(S)^,P^,L);
  375. P:=P+L;
  376. For L:=1 to NLS do
  377. begin
  378. P^:=NL[L];
  379. inc(P);
  380. end;
  381. end;
  382. end;
  383. Procedure TStrings.Put(Index: Integer; const S: string);
  384. Var Obj : TObject;
  385. begin
  386. Obj:=Objects[Index];
  387. Delete(Index);
  388. InsertObject(Index,S,Obj);
  389. end;
  390. Procedure TStrings.PutObject(Index: Integer; AObject: TObject);
  391. begin
  392. // Empty.
  393. end;
  394. Procedure TStrings.SetCapacity(NewCapacity: Integer);
  395. begin
  396. // Empty.
  397. end;
  398. Function GetNextLine (Const Value : String; Var S : String; Var P : Integer) : Boolean;
  399. Var
  400. PS : PChar;
  401. IP,L : Integer;
  402. begin
  403. L:=Length(Value);
  404. S:='';
  405. Result:=False;
  406. If ((L-P)<0) then
  407. exit;
  408. if ((L-P)=0) and (not (value[P] in [#10,#13])) Then
  409. Begin
  410. s:=value[P];
  411. inc(P);
  412. Exit(True);
  413. End;
  414. PS:=PChar(Value)+P-1;
  415. IP:=P;
  416. While ((L-P)>=0) and (not (PS^ in [#10,#13])) do
  417. begin
  418. P:=P+1;
  419. Inc(PS);
  420. end;
  421. SetLength (S,P-IP);
  422. System.Move (Value[IP],Pointer(S)^,P-IP);
  423. If (P<=L) and (Value[P]=#13) then
  424. Inc(P);
  425. If (P<=L) and (Value[P]=#10) then
  426. Inc(P); // Point to character after #10(#13)
  427. Result:=True;
  428. end;
  429. Procedure TStrings.SetTextStr(const Value: string);
  430. Var
  431. S : String;
  432. P : Integer;
  433. begin
  434. Try
  435. beginUpdate;
  436. Clear;
  437. P:=1;
  438. While GetNextLine (Value,S,P) do
  439. Add(S);
  440. finally
  441. EndUpdate;
  442. end;
  443. end;
  444. Procedure TStrings.SetUpdateState(Updating: Boolean);
  445. begin
  446. end;
  447. destructor TSTrings.Destroy;
  448. begin
  449. inherited destroy;
  450. end;
  451. Function TStrings.Add(const S: string): Integer;
  452. begin
  453. Result:=Count;
  454. Insert (Count,S);
  455. end;
  456. Function TStrings.AddObject(const S: string; AObject: TObject): Integer;
  457. begin
  458. Result:=Add(S);
  459. Objects[result]:=AObject;
  460. end;
  461. Procedure TStrings.Append(const S: string);
  462. begin
  463. Add (S);
  464. end;
  465. Procedure TStrings.AddStrings(TheStrings: TStrings);
  466. Var Runner : longint;
  467. begin
  468. try
  469. beginupdate;
  470. For Runner:=0 to TheStrings.Count-1 do
  471. self.AddObject (Thestrings[Runner],TheStrings.Objects[Runner]);
  472. finally
  473. EndUpdate;
  474. end;
  475. end;
  476. Procedure TStrings.Assign(Source: TPersistent);
  477. Var
  478. S : TStrings;
  479. begin
  480. If Source is TStrings then
  481. begin
  482. S:=TStrings(Source);
  483. BeginUpdate;
  484. Try
  485. clear;
  486. FSpecialCharsInited:=S.FSpecialCharsInited;
  487. FQuoteChar:=S.FQuoteChar;
  488. FDelimiter:=S.FDelimiter;
  489. FNameValueSeparator:=S.FNameValueSeparator;
  490. FLBS:=S.FLBS;
  491. AddStrings(S);
  492. finally
  493. EndUpdate;
  494. end;
  495. end
  496. else
  497. Inherited Assign(Source);
  498. end;
  499. Procedure TStrings.BeginUpdate;
  500. begin
  501. if FUpdateCount = 0 then SetUpdateState(true);
  502. inc(FUpdateCount);
  503. end;
  504. Procedure TStrings.EndUpdate;
  505. begin
  506. If FUpdateCount>0 then
  507. Dec(FUpdateCount);
  508. if FUpdateCount=0 then
  509. SetUpdateState(False);
  510. end;
  511. Function TStrings.Equals(Obj: TObject): Boolean;
  512. begin
  513. if Obj is TStrings then
  514. Result := Equals(TStrings(Obj))
  515. else
  516. Result := inherited Equals(Obj);
  517. end;
  518. Function TStrings.Equals(TheStrings: TStrings): Boolean;
  519. Var Runner,Nr : Longint;
  520. begin
  521. Result:=False;
  522. Nr:=Self.Count;
  523. if Nr<>TheStrings.Count then exit;
  524. For Runner:=0 to Nr-1 do
  525. If Strings[Runner]<>TheStrings[Runner] then exit;
  526. Result:=True;
  527. end;
  528. Procedure TStrings.Exchange(Index1, Index2: Integer);
  529. Var
  530. Obj : TObject;
  531. Str : String;
  532. begin
  533. Try
  534. beginUpdate;
  535. Obj:=Objects[Index1];
  536. Str:=Strings[Index1];
  537. Objects[Index1]:=Objects[Index2];
  538. Strings[Index1]:=Strings[Index2];
  539. Objects[Index2]:=Obj;
  540. Strings[Index2]:=Str;
  541. finally
  542. EndUpdate;
  543. end;
  544. end;
  545. function TStrings.GetEnumerator: TStringsEnumerator;
  546. begin
  547. Result:=TStringsEnumerator.Create(Self);
  548. end;
  549. Function TStrings.GetText: PChar;
  550. begin
  551. Result:=StrNew(Pchar(Self.Text));
  552. end;
  553. Function TStrings.DoCompareText(const s1,s2 : string) : PtrInt;
  554. begin
  555. result:=CompareText(s1,s2);
  556. end;
  557. Function TStrings.IndexOf(const S: string): Integer;
  558. begin
  559. Result:=0;
  560. While (Result<Count) and (DoCompareText(Strings[Result],S)<>0) do Result:=Result+1;
  561. if Result=Count then Result:=-1;
  562. end;
  563. Function TStrings.IndexOfName(const Name: string): Integer;
  564. Var
  565. len : longint;
  566. S : String;
  567. begin
  568. CheckSpecialChars;
  569. Result:=0;
  570. while (Result<Count) do
  571. begin
  572. S:=Strings[Result];
  573. len:=pos(FNameValueSeparator,S)-1;
  574. if (len>0) and (DoCompareText(Name,Copy(S,1,Len))=0) then
  575. exit;
  576. inc(result);
  577. end;
  578. result:=-1;
  579. end;
  580. Function TStrings.IndexOfObject(AObject: TObject): Integer;
  581. begin
  582. Result:=0;
  583. While (Result<count) and (Objects[Result]<>AObject) do Result:=Result+1;
  584. If Result=Count then Result:=-1;
  585. end;
  586. Procedure TStrings.InsertObject(Index: Integer; const S: string;
  587. AObject: TObject);
  588. begin
  589. Insert (Index,S);
  590. Objects[Index]:=AObject;
  591. end;
  592. Procedure TStrings.LoadFromFile(const FileName: string);
  593. Var
  594. TheStream : TFileStream;
  595. begin
  596. TheStream:=TFileStream.Create(FileName,fmOpenRead or fmShareDenyWrite);
  597. try
  598. LoadFromStream(TheStream);
  599. finally
  600. TheStream.Free;
  601. end;
  602. end;
  603. Procedure TStrings.LoadFromStream(Stream: TStream);
  604. {
  605. Borlands method is no good, since a pipe for
  606. instance doesn't have a size.
  607. So we must do it the hard way.
  608. }
  609. Const
  610. BufSize = 1024;
  611. MaxGrow = 1 shl 29;
  612. Var
  613. Buffer : AnsiString;
  614. BytesRead,
  615. BufLen,
  616. I,BufDelta : Longint;
  617. begin
  618. // reread into a buffer
  619. try
  620. beginupdate;
  621. Buffer:='';
  622. BufLen:=0;
  623. I:=1;
  624. Repeat
  625. BufDelta:=BufSize*I;
  626. SetLength(Buffer,BufLen+BufDelta);
  627. BytesRead:=Stream.Read(Buffer[BufLen+1],BufDelta);
  628. inc(BufLen,BufDelta);
  629. If I<MaxGrow then
  630. I:=I shl 1;
  631. Until BytesRead<>BufDelta;
  632. SetLength(Buffer, BufLen-BufDelta+BytesRead);
  633. SetTextStr(Buffer);
  634. SetLength(Buffer,0);
  635. finally
  636. EndUpdate;
  637. end;
  638. end;
  639. Procedure TStrings.Move(CurIndex, NewIndex: Integer);
  640. Var
  641. Obj : TObject;
  642. Str : String;
  643. begin
  644. BeginUpdate;
  645. Obj:=Objects[CurIndex];
  646. Str:=Strings[CurIndex];
  647. Delete(Curindex);
  648. InsertObject(NewIndex,Str,Obj);
  649. EndUpdate;
  650. end;
  651. Procedure TStrings.SaveToFile(const FileName: string);
  652. Var TheStream : TFileStream;
  653. begin
  654. TheStream:=TFileStream.Create(FileName,fmCreate);
  655. try
  656. SaveToStream(TheStream);
  657. finally
  658. TheStream.Free;
  659. end;
  660. end;
  661. Procedure TStrings.SaveToStream(Stream: TStream);
  662. Var
  663. S : String;
  664. begin
  665. S:=Text;
  666. Stream.WriteBuffer(Pointer(S)^,Length(S));
  667. end;
  668. Procedure TStrings.SetText(TheText: PChar);
  669. Var S : String;
  670. begin
  671. If TheText<>Nil then
  672. S:=StrPas(TheText)
  673. else
  674. S:='';
  675. SetTextStr(S);
  676. end;
  677. {****************************************************************************}
  678. {* TStringList *}
  679. {****************************************************************************}
  680. {$if defined(VER2_0) or not defined(FPC_TESTGENERICS)}
  681. Procedure TStringList.ExchangeItems(Index1, Index2: Integer);
  682. Var P1,P2 : Pointer;
  683. begin
  684. P1:=Pointer(Flist^[Index1].FString);
  685. P2:=Pointer(Flist^[Index1].FObject);
  686. Pointer(Flist^[Index1].Fstring):=Pointer(Flist^[Index2].Fstring);
  687. Pointer(Flist^[Index1].FObject):=Pointer(Flist^[Index2].FObject);
  688. Pointer(Flist^[Index2].Fstring):=P1;
  689. Pointer(Flist^[Index2].FObject):=P2;
  690. end;
  691. Procedure TStringList.Grow;
  692. Var
  693. NC : Integer;
  694. begin
  695. NC:=FCapacity;
  696. If NC>=256 then
  697. NC:=NC+(NC Div 4)
  698. else if NC=0 then
  699. NC:=4
  700. else
  701. NC:=NC*4;
  702. SetCapacity(NC);
  703. end;
  704. Procedure TStringList.QuickSort(L, R: Integer; CompareFn: TStringListSortCompare);
  705. var
  706. Pivot, vL, vR: Integer;
  707. begin
  708. if R - L <= 1 then begin // a little bit of time saver
  709. if L < R then
  710. if CompareFn(Self, L, R) > 0 then
  711. ExchangeItems(L, R);
  712. Exit;
  713. end;
  714. vL := L;
  715. vR := R;
  716. Pivot := L + Random(R - L); // they say random is best
  717. while vL < vR do begin
  718. while (vL < Pivot) and (CompareFn(Self, vL, Pivot) <= 0) do
  719. Inc(vL);
  720. while (vR > Pivot) and (CompareFn(Self, vR, Pivot) > 0) do
  721. Dec(vR);
  722. ExchangeItems(vL, vR);
  723. if Pivot = vL then // swap pivot if we just hit it from one side
  724. Pivot := vR
  725. else if Pivot = vR then
  726. Pivot := vL;
  727. end;
  728. if Pivot - 1 >= L then
  729. QuickSort(L, Pivot - 1, CompareFn);
  730. if Pivot + 1 <= R then
  731. QuickSort(Pivot + 1, R, CompareFn);
  732. end;
  733. Procedure TStringList.InsertItem(Index: Integer; const S: string);
  734. begin
  735. Changing;
  736. If FCount=Fcapacity then Grow;
  737. If Index<FCount then
  738. System.Move (FList^[Index],FList^[Index+1],
  739. (FCount-Index)*SizeOf(TStringItem));
  740. Pointer(Flist^[Index].Fstring):=Nil; // Needed to initialize...
  741. Flist^[Index].FString:=S;
  742. Flist^[Index].Fobject:=Nil;
  743. Inc(FCount);
  744. Changed;
  745. end;
  746. Procedure TStringList.InsertItem(Index: Integer; const S: string; O: TObject);
  747. begin
  748. Changing;
  749. If FCount=Fcapacity then Grow;
  750. If Index<FCount then
  751. System.Move (FList^[Index],FList^[Index+1],
  752. (FCount-Index)*SizeOf(TStringItem));
  753. Pointer(Flist^[Index].Fstring):=Nil; // Needed to initialize...
  754. Flist^[Index].FString:=S;
  755. Flist^[Index].FObject:=O;
  756. Inc(FCount);
  757. Changed;
  758. end;
  759. Procedure TStringList.SetSorted(Value: Boolean);
  760. begin
  761. If FSorted<>Value then
  762. begin
  763. If Value then sort;
  764. FSorted:=VAlue
  765. end;
  766. end;
  767. Procedure TStringList.Changed;
  768. begin
  769. If (FUpdateCount=0) Then
  770. If Assigned(FOnChange) then
  771. FOnchange(Self);
  772. end;
  773. Procedure TStringList.Changing;
  774. begin
  775. If FUpdateCount=0 then
  776. if Assigned(FOnChanging) then
  777. FOnchanging(Self);
  778. end;
  779. Function TStringList.Get(Index: Integer): string;
  780. begin
  781. If (Index<0) or (INdex>=Fcount) then
  782. Error (SListIndexError,Index);
  783. Result:=Flist^[Index].FString;
  784. end;
  785. Function TStringList.GetCapacity: Integer;
  786. begin
  787. Result:=FCapacity;
  788. end;
  789. Function TStringList.GetCount: Integer;
  790. begin
  791. Result:=FCount;
  792. end;
  793. Function TStringList.GetObject(Index: Integer): TObject;
  794. begin
  795. If (Index<0) or (INdex>=Fcount) then
  796. Error (SListIndexError,Index);
  797. Result:=Flist^[Index].FObject;
  798. end;
  799. Procedure TStringList.Put(Index: Integer; const S: string);
  800. begin
  801. If Sorted then
  802. Error(SSortedListError,0);
  803. If (Index<0) or (INdex>=Fcount) then
  804. Error (SListIndexError,Index);
  805. Changing;
  806. Flist^[Index].FString:=S;
  807. Changed;
  808. end;
  809. Procedure TStringList.PutObject(Index: Integer; AObject: TObject);
  810. begin
  811. If (Index<0) or (INdex>=Fcount) then
  812. Error (SListIndexError,Index);
  813. Changing;
  814. Flist^[Index].FObject:=AObject;
  815. Changed;
  816. end;
  817. Procedure TStringList.SetCapacity(NewCapacity: Integer);
  818. Var NewList : Pointer;
  819. MSize : Longint;
  820. begin
  821. If (NewCapacity<0) then
  822. Error (SListCapacityError,NewCapacity);
  823. If NewCapacity>FCapacity then
  824. begin
  825. GetMem (NewList,NewCapacity*SizeOf(TStringItem));
  826. If NewList=Nil then
  827. Error (SListCapacityError,NewCapacity);
  828. If Assigned(FList) then
  829. begin
  830. MSize:=FCapacity*Sizeof(TStringItem);
  831. System.Move (FList^,NewList^,MSize);
  832. FillWord (Pchar(NewList)[MSize],(NewCapacity-FCapacity)*WordRatio, 0);
  833. FreeMem (Flist,MSize);
  834. end;
  835. Flist:=NewList;
  836. FCapacity:=NewCapacity;
  837. end
  838. else if NewCapacity<FCapacity then
  839. begin
  840. if NewCapacity = 0 then
  841. begin
  842. FreeMem(FList);
  843. FList := nil;
  844. end else
  845. begin
  846. GetMem(NewList, NewCapacity * SizeOf(TStringItem));
  847. System.Move(FList^, NewList^, NewCapacity * SizeOf(TStringItem));
  848. FreeMem(FList);
  849. FList := NewList;
  850. end;
  851. FCapacity:=NewCapacity;
  852. end;
  853. end;
  854. Procedure TStringList.SetUpdateState(Updating: Boolean);
  855. begin
  856. If Updating then
  857. Changing
  858. else
  859. Changed
  860. end;
  861. destructor TStringList.Destroy;
  862. Var I : Longint;
  863. begin
  864. FOnChange:=Nil;
  865. FOnChanging:=Nil;
  866. // This will force a dereference. Can be done better...
  867. For I:=0 to FCount-1 do
  868. FList^[I].FString:='';
  869. FCount:=0;
  870. SetCapacity(0);
  871. Inherited destroy;
  872. end;
  873. Function TStringList.Add(const S: string): Integer;
  874. begin
  875. If Not Sorted then
  876. Result:=FCount
  877. else
  878. If Find (S,Result) then
  879. Case DUplicates of
  880. DupIgnore : Exit;
  881. DupError : Error(SDuplicateString,0)
  882. end;
  883. InsertItem (Result,S);
  884. end;
  885. Procedure TStringList.Clear;
  886. Var I : longint;
  887. begin
  888. if FCount = 0 then Exit;
  889. Changing;
  890. For I:=0 to FCount-1 do
  891. Flist^[I].FString:='';
  892. FCount:=0;
  893. SetCapacity(0);
  894. Changed;
  895. end;
  896. Procedure TStringList.Delete(Index: Integer);
  897. begin
  898. If (Index<0) or (Index>=FCount) then
  899. Error(SlistINdexError,Index);
  900. Changing;
  901. Flist^[Index].FString:='';
  902. Dec(FCount);
  903. If Index<FCount then
  904. System.Move(Flist^[Index+1],
  905. Flist^[Index],
  906. (Fcount-Index)*SizeOf(TStringItem));
  907. Changed;
  908. end;
  909. Procedure TStringList.Exchange(Index1, Index2: Integer);
  910. begin
  911. If (Index1<0) or (Index1>=FCount) then
  912. Error(SListIndexError,Index1);
  913. If (Index2<0) or (Index2>=FCount) then
  914. Error(SListIndexError,Index2);
  915. Changing;
  916. ExchangeItems(Index1,Index2);
  917. changed;
  918. end;
  919. procedure TStringList.SetCaseSensitive(b : boolean);
  920. begin
  921. if b<>FCaseSensitive then
  922. begin
  923. FCaseSensitive:=b;
  924. if FSorted then
  925. sort;
  926. end;
  927. end;
  928. Function TStringList.DoCompareText(const s1,s2 : string) : PtrInt;
  929. begin
  930. if FCaseSensitive then
  931. result:=AnsiCompareStr(s1,s2)
  932. else
  933. result:=AnsiCompareText(s1,s2);
  934. end;
  935. Function TStringList.Find(const S: string; var Index: Integer): Boolean;
  936. var
  937. L, R, I: Integer;
  938. CompareRes: PtrInt;
  939. begin
  940. Result := false;
  941. // Use binary search.
  942. L := 0;
  943. R := Count - 1;
  944. while (L<=R) do
  945. begin
  946. I := L + (R - L) div 2;
  947. CompareRes := DoCompareText(S, Flist^[I].FString);
  948. if (CompareRes>0) then
  949. L := I+1
  950. else begin
  951. R := I-1;
  952. if (CompareRes=0) then begin
  953. Result := true;
  954. if (Duplicates<>dupAccept) then
  955. L := I; // forces end of while loop
  956. end;
  957. end;
  958. end;
  959. Index := L;
  960. end;
  961. Function TStringList.IndexOf(const S: string): Integer;
  962. begin
  963. If Not Sorted then
  964. Result:=Inherited indexOf(S)
  965. else
  966. // faster using binary search...
  967. If Not Find (S,Result) then
  968. Result:=-1;
  969. end;
  970. Procedure TStringList.Insert(Index: Integer; const S: string);
  971. begin
  972. If Sorted then
  973. Error (SSortedListError,0)
  974. else
  975. If (Index<0) or (Index>FCount) then
  976. Error (SListIndexError,Index)
  977. else
  978. InsertItem (Index,S);
  979. end;
  980. Procedure TStringList.CustomSort(CompareFn: TStringListSortCompare);
  981. begin
  982. If Not Sorted and (FCount>1) then
  983. begin
  984. Changing;
  985. QuickSort(0,FCount-1, CompareFn);
  986. Changed;
  987. end;
  988. end;
  989. function StringListAnsiCompare(List: TStringList; Index1, Index: Integer): Integer;
  990. begin
  991. Result := List.DoCompareText(List.FList^[Index1].FString,
  992. List.FList^[Index].FString);
  993. end;
  994. Procedure TStringList.Sort;
  995. begin
  996. CustomSort(@StringListAnsiCompare);
  997. end;
  998. {$else}
  999. { generics based implementation of TStringList follows }
  1000. function StringListAnsiCompare(List: TStringList; Index1, Index2: Integer): Integer;
  1001. begin
  1002. Result := List.DoCompareText(List.Strings[Index1], List.Strings[Index2]);
  1003. end;
  1004. constructor TStringList.Create;
  1005. begin
  1006. inherited;
  1007. FMap := TFPStrObjMap.Create;
  1008. FMap.OnPtrCompare := @MapPtrCompare;
  1009. FOnCompareText := @DefaultCompareText;
  1010. CheckSpecialChars;
  1011. end;
  1012. destructor TStringList.Destroy;
  1013. begin
  1014. FMap.Free;
  1015. inherited;
  1016. end;
  1017. function TStringList.GetDuplicates: TDuplicates;
  1018. begin
  1019. Result := FMap.Duplicates;
  1020. end;
  1021. function TStringList.GetSorted: boolean;
  1022. begin
  1023. Result := FMap.Sorted;
  1024. end;
  1025. procedure TStringList.SetDuplicates(NewDuplicates: TDuplicates);
  1026. begin
  1027. FMap.Duplicates := NewDuplicates;
  1028. end;
  1029. procedure TStringList.SetSorted(NewSorted: Boolean);
  1030. begin
  1031. FMap.Sorted := NewSorted;
  1032. end;
  1033. procedure TStringList.Changed;
  1034. begin
  1035. if FUpdateCount = 0 then
  1036. if Assigned(FOnChange) then
  1037. FOnChange(Self);
  1038. end;
  1039. procedure TStringList.Changing;
  1040. begin
  1041. if FUpdateCount = 0 then
  1042. if Assigned(FOnChanging) then
  1043. FOnChanging(Self);
  1044. end;
  1045. function TStringList.Get(Index: Integer): string;
  1046. begin
  1047. Result := FMap.Keys[Index];
  1048. end;
  1049. function TStringList.GetCapacity: Integer;
  1050. begin
  1051. Result := FMap.Capacity;
  1052. end;
  1053. function TStringList.GetCount: Integer;
  1054. begin
  1055. Result := FMap.Count;
  1056. end;
  1057. function TStringList.GetObject(Index: Integer): TObject;
  1058. begin
  1059. Result := FMap.Data[Index];
  1060. end;
  1061. procedure TStringList.Put(Index: Integer; const S: string);
  1062. begin
  1063. Changing;
  1064. FMap.Keys[Index] := S;
  1065. Changed;
  1066. end;
  1067. procedure TStringList.PutObject(Index: Integer; AObject: TObject);
  1068. begin
  1069. Changing;
  1070. FMap.Data[Index] := AObject;
  1071. Changed;
  1072. end;
  1073. procedure TStringList.SetCapacity(NewCapacity: Integer);
  1074. begin
  1075. FMap.Capacity := NewCapacity;
  1076. end;
  1077. procedure TStringList.SetUpdateState(Updating: Boolean);
  1078. begin
  1079. if Updating then
  1080. Changing
  1081. else
  1082. Changed
  1083. end;
  1084. function TStringList.Add(const S: string): Integer;
  1085. begin
  1086. Result := FMap.Add(S);
  1087. end;
  1088. procedure TStringList.Clear;
  1089. begin
  1090. if FMap.Count = 0 then exit;
  1091. Changing;
  1092. FMap.Clear;
  1093. Changed;
  1094. end;
  1095. procedure TStringList.Delete(Index: Integer);
  1096. begin
  1097. if (Index < 0) or (Index >= FMap.Count) then
  1098. Error(SListIndexError, Index);
  1099. Changing;
  1100. FMap.Delete(Index);
  1101. Changed;
  1102. end;
  1103. procedure TStringList.Exchange(Index1, Index2: Integer);
  1104. begin
  1105. if (Index1 < 0) or (Index1 >= FMap.Count) then
  1106. Error(SListIndexError, Index1);
  1107. if (Index2 < 0) or (Index2 >= FMap.Count) then
  1108. Error(SListIndexError, Index2);
  1109. Changing;
  1110. FMap.InternalExchange(Index1, Index2);
  1111. Changed;
  1112. end;
  1113. procedure TStringList.SetCaseSensitive(NewSensitive: Boolean);
  1114. begin
  1115. if NewSensitive <> FCaseSensitive then
  1116. begin
  1117. FCaseSensitive := NewSensitive;
  1118. if Sorted then
  1119. Sort;
  1120. end;
  1121. end;
  1122. function TStringList.MapPtrCompare(Key1, Key2: Pointer): Integer;
  1123. begin
  1124. Result := FOnCompareText(string(Key1^), string(Key2^));
  1125. end;
  1126. function TStringList.DefaultCompareText(const s1, s2: string): PtrInt;
  1127. begin
  1128. if FCaseSensitive then
  1129. Result := AnsiCompareStr(s1, s2)
  1130. else
  1131. Result := AnsiCompareText(s1, s2);
  1132. end;
  1133. function TStringList.DoCompareText(const s1, s2: string): PtrInt;
  1134. begin
  1135. Result := FOnCompareText(s1, s2);
  1136. end;
  1137. function TStringList.Find(const S: string; var Index: Integer): Boolean;
  1138. begin
  1139. Result := FMap.Find(S, Index);
  1140. end;
  1141. function TStringList.IndexOf(const S: string): Integer;
  1142. begin
  1143. Result := FMap.IndexOf(S);
  1144. end;
  1145. procedure TStringList.Insert(Index: Integer; const S: string);
  1146. begin
  1147. if not Sorted and (0 <= Index) and (Index < FMap.Count) then
  1148. Changing;
  1149. FMap.InsertKey(Index, S);
  1150. Changed;
  1151. end;
  1152. procedure TStringList.QuickSort(L, R: Integer; CompareFn: TStringListSortCompare);
  1153. var
  1154. I, J, Pivot: Integer;
  1155. begin
  1156. repeat
  1157. I := L;
  1158. J := R;
  1159. Pivot := (L + R) div 2;
  1160. repeat
  1161. while CompareFn(Self, I, Pivot) < 0 do Inc(I);
  1162. while CompareFn(Self, J, Pivot) > 0 do Dec(J);
  1163. if I <= J then
  1164. begin
  1165. FMap.InternalExchange(I, J); // No check, indices are correct.
  1166. if Pivot = I then
  1167. Pivot := J
  1168. else if Pivot = J then
  1169. Pivot := I;
  1170. Inc(I);
  1171. Dec(j);
  1172. end;
  1173. until I > J;
  1174. if L < J then
  1175. QuickSort(L,J, CompareFn);
  1176. L := I;
  1177. until I >= R;
  1178. end;
  1179. procedure TStringList.CustomSort(CompareFn: TStringListSortCompare);
  1180. begin
  1181. if not Sorted and (FMap.Count > 1) then
  1182. begin
  1183. Changing;
  1184. QuickSort(0, FMap.Count-1, CompareFn);
  1185. Changed;
  1186. end;
  1187. end;
  1188. procedure TStringList.Sort;
  1189. begin
  1190. if not Sorted and (FMap.Count > 1) then
  1191. begin
  1192. Changing;
  1193. FMap.Sort;
  1194. Changed;
  1195. end;
  1196. end;
  1197. {$endif}