stringl.inc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  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. LoadFromStream(TheStream);
  598. TheStream.Free;
  599. end;
  600. Procedure TStrings.LoadFromStream(Stream: TStream);
  601. {
  602. Borlands method is no good, since a pipe for
  603. instance doesn't have a size.
  604. So we must do it the hard way.
  605. }
  606. Const
  607. BufSize = 1024;
  608. MaxGrow = 1 shl 29;
  609. Var
  610. Buffer : AnsiString;
  611. BytesRead,
  612. BufLen,
  613. I,BufDelta : Longint;
  614. begin
  615. // reread into a buffer
  616. try
  617. beginupdate;
  618. Buffer:='';
  619. BufLen:=0;
  620. I:=1;
  621. Repeat
  622. BufDelta:=BufSize*I;
  623. SetLength(Buffer,BufLen+BufDelta);
  624. BytesRead:=Stream.Read(Buffer[BufLen+1],BufDelta);
  625. inc(BufLen,BufDelta);
  626. If I<MaxGrow then
  627. I:=I shl 1;
  628. Until BytesRead<>BufDelta;
  629. SetLength(Buffer, BufLen-BufDelta+BytesRead);
  630. SetTextStr(Buffer);
  631. SetLength(Buffer,0);
  632. finally
  633. EndUpdate;
  634. end;
  635. end;
  636. Procedure TStrings.Move(CurIndex, NewIndex: Integer);
  637. Var
  638. Obj : TObject;
  639. Str : String;
  640. begin
  641. BeginUpdate;
  642. Obj:=Objects[CurIndex];
  643. Str:=Strings[CurIndex];
  644. Delete(Curindex);
  645. InsertObject(NewIndex,Str,Obj);
  646. EndUpdate;
  647. end;
  648. Procedure TStrings.SaveToFile(const FileName: string);
  649. Var TheStream : TFileStream;
  650. begin
  651. TheStream:=TFileStream.Create(FileName,fmCreate);
  652. SaveToStream(TheStream);
  653. TheStream.Free;
  654. end;
  655. Procedure TStrings.SaveToStream(Stream: TStream);
  656. Var
  657. S : String;
  658. begin
  659. S:=Text;
  660. Stream.WriteBuffer(Pointer(S)^,Length(S));
  661. end;
  662. Procedure TStrings.SetText(TheText: PChar);
  663. Var S : String;
  664. begin
  665. If TheText<>Nil then
  666. S:=StrPas(TheText)
  667. else
  668. S:='';
  669. SetTextStr(S);
  670. end;
  671. {****************************************************************************}
  672. {* TStringList *}
  673. {****************************************************************************}
  674. {$if defined(VER2_0) or not defined(FPC_TESTGENERICS)}
  675. Procedure TStringList.ExchangeItems(Index1, Index2: Integer);
  676. Var P1,P2 : Pointer;
  677. begin
  678. P1:=Pointer(Flist^[Index1].FString);
  679. P2:=Pointer(Flist^[Index1].FObject);
  680. Pointer(Flist^[Index1].Fstring):=Pointer(Flist^[Index2].Fstring);
  681. Pointer(Flist^[Index1].FObject):=Pointer(Flist^[Index2].FObject);
  682. Pointer(Flist^[Index2].Fstring):=P1;
  683. Pointer(Flist^[Index2].FObject):=P2;
  684. end;
  685. Procedure TStringList.Grow;
  686. Var
  687. NC : Integer;
  688. begin
  689. NC:=FCapacity;
  690. If NC>=256 then
  691. NC:=NC+(NC Div 4)
  692. else if NC=0 then
  693. NC:=4
  694. else
  695. NC:=NC*4;
  696. SetCapacity(NC);
  697. end;
  698. Procedure TStringList.QuickSort(L, R: Integer; CompareFn: TStringListSortCompare);
  699. var
  700. Pivot, vL, vR: Integer;
  701. begin
  702. if R - L <= 1 then begin // a little bit of time saver
  703. if L < R then
  704. if CompareFn(Self, L, R) > 0 then
  705. ExchangeItems(L, R);
  706. Exit;
  707. end;
  708. vL := L;
  709. vR := R;
  710. Pivot := L + Random(R - L); // they say random is best
  711. while vL < vR do begin
  712. while (vL < Pivot) and (CompareFn(Self, vL, Pivot) <= 0) do
  713. Inc(vL);
  714. while (vR > Pivot) and (CompareFn(Self, vR, Pivot) > 0) do
  715. Dec(vR);
  716. ExchangeItems(vL, vR);
  717. if Pivot = vL then // swap pivot if we just hit it from one side
  718. Pivot := vR
  719. else if Pivot = vR then
  720. Pivot := vL;
  721. end;
  722. if Pivot - 1 >= L then
  723. QuickSort(L, Pivot - 1, CompareFn);
  724. if Pivot + 1 <= R then
  725. QuickSort(Pivot + 1, R, CompareFn);
  726. end;
  727. Procedure TStringList.InsertItem(Index: Integer; const S: string);
  728. begin
  729. Changing;
  730. If FCount=Fcapacity then Grow;
  731. If Index<FCount then
  732. System.Move (FList^[Index],FList^[Index+1],
  733. (FCount-Index)*SizeOf(TStringItem));
  734. Pointer(Flist^[Index].Fstring):=Nil; // Needed to initialize...
  735. Flist^[Index].FString:=S;
  736. Flist^[Index].Fobject:=Nil;
  737. Inc(FCount);
  738. Changed;
  739. end;
  740. Procedure TStringList.InsertItem(Index: Integer; const S: string; O: TObject);
  741. begin
  742. Changing;
  743. If FCount=Fcapacity then Grow;
  744. If Index<FCount then
  745. System.Move (FList^[Index],FList^[Index+1],
  746. (FCount-Index)*SizeOf(TStringItem));
  747. Pointer(Flist^[Index].Fstring):=Nil; // Needed to initialize...
  748. Flist^[Index].FString:=S;
  749. Flist^[Index].FObject:=O;
  750. Inc(FCount);
  751. Changed;
  752. end;
  753. Procedure TStringList.SetSorted(Value: Boolean);
  754. begin
  755. If FSorted<>Value then
  756. begin
  757. If Value then sort;
  758. FSorted:=VAlue
  759. end;
  760. end;
  761. Procedure TStringList.Changed;
  762. begin
  763. If (FUpdateCount=0) Then
  764. If Assigned(FOnChange) then
  765. FOnchange(Self);
  766. end;
  767. Procedure TStringList.Changing;
  768. begin
  769. If FUpdateCount=0 then
  770. if Assigned(FOnChanging) then
  771. FOnchanging(Self);
  772. end;
  773. Function TStringList.Get(Index: Integer): string;
  774. begin
  775. If (Index<0) or (INdex>=Fcount) then
  776. Error (SListIndexError,Index);
  777. Result:=Flist^[Index].FString;
  778. end;
  779. Function TStringList.GetCapacity: Integer;
  780. begin
  781. Result:=FCapacity;
  782. end;
  783. Function TStringList.GetCount: Integer;
  784. begin
  785. Result:=FCount;
  786. end;
  787. Function TStringList.GetObject(Index: Integer): TObject;
  788. begin
  789. If (Index<0) or (INdex>=Fcount) then
  790. Error (SListIndexError,Index);
  791. Result:=Flist^[Index].FObject;
  792. end;
  793. Procedure TStringList.Put(Index: Integer; const S: string);
  794. begin
  795. If Sorted then
  796. Error(SSortedListError,0);
  797. If (Index<0) or (INdex>=Fcount) then
  798. Error (SListIndexError,Index);
  799. Changing;
  800. Flist^[Index].FString:=S;
  801. Changed;
  802. end;
  803. Procedure TStringList.PutObject(Index: Integer; AObject: TObject);
  804. begin
  805. If (Index<0) or (INdex>=Fcount) then
  806. Error (SListIndexError,Index);
  807. Changing;
  808. Flist^[Index].FObject:=AObject;
  809. Changed;
  810. end;
  811. Procedure TStringList.SetCapacity(NewCapacity: Integer);
  812. Var NewList : Pointer;
  813. MSize : Longint;
  814. begin
  815. If (NewCapacity<0) then
  816. Error (SListCapacityError,NewCapacity);
  817. If NewCapacity>FCapacity then
  818. begin
  819. GetMem (NewList,NewCapacity*SizeOf(TStringItem));
  820. If NewList=Nil then
  821. Error (SListCapacityError,NewCapacity);
  822. If Assigned(FList) then
  823. begin
  824. MSize:=FCapacity*Sizeof(TStringItem);
  825. System.Move (FList^,NewList^,MSize);
  826. FillWord (Pchar(NewList)[MSize],(NewCapacity-FCapacity)*WordRatio, 0);
  827. FreeMem (Flist,MSize);
  828. end;
  829. Flist:=NewList;
  830. FCapacity:=NewCapacity;
  831. end
  832. else if NewCapacity<FCapacity then
  833. begin
  834. if NewCapacity = 0 then
  835. begin
  836. FreeMem(FList);
  837. FList := nil;
  838. end else
  839. begin
  840. GetMem(NewList, NewCapacity * SizeOf(TStringItem));
  841. System.Move(FList^, NewList^, NewCapacity * SizeOf(TStringItem));
  842. FreeMem(FList);
  843. FList := NewList;
  844. end;
  845. FCapacity:=NewCapacity;
  846. end;
  847. end;
  848. Procedure TStringList.SetUpdateState(Updating: Boolean);
  849. begin
  850. If Updating then
  851. Changing
  852. else
  853. Changed
  854. end;
  855. destructor TStringList.Destroy;
  856. Var I : Longint;
  857. begin
  858. FOnChange:=Nil;
  859. FOnChanging:=Nil;
  860. // This will force a dereference. Can be done better...
  861. For I:=0 to FCount-1 do
  862. FList^[I].FString:='';
  863. FCount:=0;
  864. SetCapacity(0);
  865. Inherited destroy;
  866. end;
  867. Function TStringList.Add(const S: string): Integer;
  868. begin
  869. If Not Sorted then
  870. Result:=FCount
  871. else
  872. If Find (S,Result) then
  873. Case DUplicates of
  874. DupIgnore : Exit;
  875. DupError : Error(SDuplicateString,0)
  876. end;
  877. InsertItem (Result,S);
  878. end;
  879. Procedure TStringList.Clear;
  880. Var I : longint;
  881. begin
  882. if FCount = 0 then Exit;
  883. Changing;
  884. For I:=0 to FCount-1 do
  885. Flist^[I].FString:='';
  886. FCount:=0;
  887. SetCapacity(0);
  888. Changed;
  889. end;
  890. Procedure TStringList.Delete(Index: Integer);
  891. begin
  892. If (Index<0) or (Index>=FCount) then
  893. Error(SlistINdexError,Index);
  894. Changing;
  895. Flist^[Index].FString:='';
  896. Dec(FCount);
  897. If Index<FCount then
  898. System.Move(Flist^[Index+1],
  899. Flist^[Index],
  900. (Fcount-Index)*SizeOf(TStringItem));
  901. Changed;
  902. end;
  903. Procedure TStringList.Exchange(Index1, Index2: Integer);
  904. begin
  905. If (Index1<0) or (Index1>=FCount) then
  906. Error(SListIndexError,Index1);
  907. If (Index2<0) or (Index2>=FCount) then
  908. Error(SListIndexError,Index2);
  909. Changing;
  910. ExchangeItems(Index1,Index2);
  911. changed;
  912. end;
  913. procedure TStringList.SetCaseSensitive(b : boolean);
  914. begin
  915. if b<>FCaseSensitive then
  916. begin
  917. FCaseSensitive:=b;
  918. if FSorted then
  919. sort;
  920. end;
  921. end;
  922. Function TStringList.DoCompareText(const s1,s2 : string) : PtrInt;
  923. begin
  924. if FCaseSensitive then
  925. result:=AnsiCompareStr(s1,s2)
  926. else
  927. result:=AnsiCompareText(s1,s2);
  928. end;
  929. Function TStringList.Find(const S: string; var Index: Integer): Boolean;
  930. var
  931. L, R, I: Integer;
  932. CompareRes: PtrInt;
  933. begin
  934. Result := false;
  935. // Use binary search.
  936. L := 0;
  937. R := Count - 1;
  938. while (L<=R) do
  939. begin
  940. I := L + (R - L) div 2;
  941. CompareRes := DoCompareText(S, Flist^[I].FString);
  942. if (CompareRes>0) then
  943. L := I+1
  944. else begin
  945. R := I-1;
  946. if (CompareRes=0) then begin
  947. Result := true;
  948. if (Duplicates<>dupAccept) then
  949. L := I; // forces end of while loop
  950. end;
  951. end;
  952. end;
  953. Index := L;
  954. end;
  955. Function TStringList.IndexOf(const S: string): Integer;
  956. begin
  957. If Not Sorted then
  958. Result:=Inherited indexOf(S)
  959. else
  960. // faster using binary search...
  961. If Not Find (S,Result) then
  962. Result:=-1;
  963. end;
  964. Procedure TStringList.Insert(Index: Integer; const S: string);
  965. begin
  966. If Sorted then
  967. Error (SSortedListError,0)
  968. else
  969. If (Index<0) or (Index>FCount) then
  970. Error (SListIndexError,Index)
  971. else
  972. InsertItem (Index,S);
  973. end;
  974. Procedure TStringList.CustomSort(CompareFn: TStringListSortCompare);
  975. begin
  976. If Not Sorted and (FCount>1) then
  977. begin
  978. Changing;
  979. QuickSort(0,FCount-1, CompareFn);
  980. Changed;
  981. end;
  982. end;
  983. function StringListAnsiCompare(List: TStringList; Index1, Index: Integer): Integer;
  984. begin
  985. Result := List.DoCompareText(List.FList^[Index1].FString,
  986. List.FList^[Index].FString);
  987. end;
  988. Procedure TStringList.Sort;
  989. begin
  990. CustomSort(@StringListAnsiCompare);
  991. end;
  992. {$else}
  993. { generics based implementation of TStringList follows }
  994. function StringListAnsiCompare(List: TStringList; Index1, Index2: Integer): Integer;
  995. begin
  996. Result := List.DoCompareText(List.Strings[Index1], List.Strings[Index2]);
  997. end;
  998. constructor TStringList.Create;
  999. begin
  1000. inherited;
  1001. FMap := TFPStrObjMap.Create;
  1002. FMap.OnPtrCompare := @MapPtrCompare;
  1003. FOnCompareText := @DefaultCompareText;
  1004. end;
  1005. destructor TStringList.Destroy;
  1006. begin
  1007. FMap.Free;
  1008. inherited;
  1009. end;
  1010. function TStringList.GetDuplicates: TDuplicates;
  1011. begin
  1012. Result := FMap.Duplicates;
  1013. end;
  1014. function TStringList.GetSorted: boolean;
  1015. begin
  1016. Result := FMap.Sorted;
  1017. end;
  1018. procedure TStringList.SetDuplicates(NewDuplicates: TDuplicates);
  1019. begin
  1020. FMap.Duplicates := NewDuplicates;
  1021. end;
  1022. procedure TStringList.SetSorted(NewSorted: Boolean);
  1023. begin
  1024. FMap.Sorted := NewSorted;
  1025. end;
  1026. procedure TStringList.Changed;
  1027. begin
  1028. if FUpdateCount = 0 then
  1029. if Assigned(FOnChange) then
  1030. FOnChange(Self);
  1031. end;
  1032. procedure TStringList.Changing;
  1033. begin
  1034. if FUpdateCount = 0 then
  1035. if Assigned(FOnChanging) then
  1036. FOnChanging(Self);
  1037. end;
  1038. function TStringList.Get(Index: Integer): string;
  1039. begin
  1040. Result := FMap.Keys[Index];
  1041. end;
  1042. function TStringList.GetCapacity: Integer;
  1043. begin
  1044. Result := FMap.Capacity;
  1045. end;
  1046. function TStringList.GetCount: Integer;
  1047. begin
  1048. Result := FMap.Count;
  1049. end;
  1050. function TStringList.GetObject(Index: Integer): TObject;
  1051. begin
  1052. Result := FMap.Data[Index];
  1053. end;
  1054. procedure TStringList.Put(Index: Integer; const S: string);
  1055. begin
  1056. Changing;
  1057. FMap.Keys[Index] := S;
  1058. Changed;
  1059. end;
  1060. procedure TStringList.PutObject(Index: Integer; AObject: TObject);
  1061. begin
  1062. Changing;
  1063. FMap.Data[Index] := AObject;
  1064. Changed;
  1065. end;
  1066. procedure TStringList.SetCapacity(NewCapacity: Integer);
  1067. begin
  1068. FMap.Capacity := NewCapacity;
  1069. end;
  1070. procedure TStringList.SetUpdateState(Updating: Boolean);
  1071. begin
  1072. if Updating then
  1073. Changing
  1074. else
  1075. Changed
  1076. end;
  1077. function TStringList.Add(const S: string): Integer;
  1078. begin
  1079. Result := FMap.Add(S);
  1080. end;
  1081. procedure TStringList.Clear;
  1082. begin
  1083. if FMap.Count = 0 then exit;
  1084. Changing;
  1085. FMap.Clear;
  1086. Changed;
  1087. end;
  1088. procedure TStringList.Delete(Index: Integer);
  1089. begin
  1090. if (Index < 0) or (Index >= FMap.Count) then
  1091. Error(SListIndexError, Index);
  1092. Changing;
  1093. FMap.Delete(Index);
  1094. Changed;
  1095. end;
  1096. procedure TStringList.Exchange(Index1, Index2: Integer);
  1097. begin
  1098. if (Index1 < 0) or (Index1 >= FMap.Count) then
  1099. Error(SListIndexError, Index1);
  1100. if (Index2 < 0) or (Index2 >= FMap.Count) then
  1101. Error(SListIndexError, Index2);
  1102. Changing;
  1103. FMap.InternalExchange(Index1, Index2);
  1104. Changed;
  1105. end;
  1106. procedure TStringList.SetCaseSensitive(NewSensitive: Boolean);
  1107. begin
  1108. if NewSensitive <> FCaseSensitive then
  1109. begin
  1110. FCaseSensitive := NewSensitive;
  1111. if Sorted then
  1112. Sort;
  1113. end;
  1114. end;
  1115. function TStringList.MapPtrCompare(Key1, Key2: Pointer): Integer;
  1116. begin
  1117. Result := FOnCompareText(string(Key1^), string(Key2^));
  1118. end;
  1119. function TStringList.DefaultCompareText(const s1, s2: string): PtrInt;
  1120. begin
  1121. if FCaseSensitive then
  1122. Result := AnsiCompareStr(s1, s2)
  1123. else
  1124. Result := AnsiCompareText(s1, s2);
  1125. end;
  1126. function TStringList.DoCompareText(const s1, s2: string): PtrInt;
  1127. begin
  1128. Result := FOnCompareText(s1, s2);
  1129. end;
  1130. function TStringList.Find(const S: string; var Index: Integer): Boolean;
  1131. begin
  1132. Result := FMap.Find(S, Index);
  1133. end;
  1134. function TStringList.IndexOf(const S: string): Integer;
  1135. begin
  1136. Result := FMap.IndexOf(S);
  1137. end;
  1138. procedure TStringList.Insert(Index: Integer; const S: string);
  1139. begin
  1140. if not Sorted and (0 <= Index) and (Index < FMap.Count) then
  1141. Changing;
  1142. FMap.InsertKey(Index, S);
  1143. Changed;
  1144. end;
  1145. procedure TStringList.QuickSort(L, R: Integer; CompareFn: TStringListSortCompare);
  1146. var
  1147. I, J, Pivot: Integer;
  1148. begin
  1149. repeat
  1150. I := L;
  1151. J := R;
  1152. Pivot := (L + R) div 2;
  1153. repeat
  1154. while CompareFn(Self, I, Pivot) < 0 do Inc(I);
  1155. while CompareFn(Self, J, Pivot) > 0 do Dec(J);
  1156. if I <= J then
  1157. begin
  1158. FMap.InternalExchange(I, J); // No check, indices are correct.
  1159. if Pivot = I then
  1160. Pivot := J
  1161. else if Pivot = J then
  1162. Pivot := I;
  1163. Inc(I);
  1164. Dec(j);
  1165. end;
  1166. until I > J;
  1167. if L < J then
  1168. QuickSort(L,J, CompareFn);
  1169. L := I;
  1170. until I >= R;
  1171. end;
  1172. procedure TStringList.CustomSort(CompareFn: TStringListSortCompare);
  1173. begin
  1174. if not Sorted and (FMap.Count > 1) then
  1175. begin
  1176. Changing;
  1177. QuickSort(0, FMap.Count-1, CompareFn);
  1178. Changed;
  1179. end;
  1180. end;
  1181. procedure TStringList.Sort;
  1182. begin
  1183. if not Sorted and (FMap.Count > 1) then
  1184. begin
  1185. Changing;
  1186. FMap.Sort;
  1187. Changed;
  1188. end;
  1189. end;
  1190. {$endif}