stringl.inc 28 KB

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