stringl.inc 28 KB

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