[next] [prev] [prev-tail] [tail] [up]

### 17.13 TStrCollection

The TStrCollection object manages a sorted collection of null-terminated strings (pchar strings). To this end, it overrides the Compare (614) method of TSortedCollection, and it introduces methods to read/write strings from a stream.

Here is the full declaration of the TStrCollection object:

     TYPE
        TStrCollection = OBJECT (TSortedCollection)
           Function Compare (Key1, Key2: Pointer): Sw_Integer; Virtual;
           Function GetItem (Var S: TStream): Pointer; Virtual;
           Procedure FreeItem (Item: Pointer); Virtual;
           Procedure PutItem (Var S: TStream; Item: Pointer); Virtual;
        END;
        PStrCollection = ^TStrCollection;

#### TStrCollection.GetItem

Declaration:
  Function TStrCollection.GetItem (Var S: TStream): Pointer; Virtual;
Description:
  GetItem reads a null-terminated string from the stream S and returns a pointer to it. It doesn’t insert the string in the collection.

  This method is primarily introduced to be able to load and store the collection from and to a stream.

Errors:
  The errors returned are those of TStream.StrRead (567).
See also:
  PutItem (625)

#### TStrCollection.Compare

Declaration:
  Function TStrCollection.Compare (Key1, Key2: Pointer): Sw_Integer; Virtual;
Description:
  TStrCollection overrides the Compare function so it compares the two keys as if they were pointers to strings. The compare is done case sensitive. It returns
  -1
    if the first string is alphabetically earlier than the second string.
  0
    if the two strings are equal.
  1
    if the first string is alphabetically later than the second string.
Errors:
  None.
See also:
  TSortedCollection.Compare (614)

Listing: objectex/ex38.pp

Program ex38;
{ Program to demonstrate the TStrCollection.Compare method }
Uses Objects,Strings;
Var C : PStrCollection;
S : String;
I : longint;
P : Pchar;
begin
Randomize;
C:=New(PStrCollection,Init(120,10));
C^.Duplicates:=True; { Duplicates allowed }
Writeln ('Inserting 100 records at random places.');
For I:=1 to 100 do
begin
Str(Random(100),S);
S:='String with value '+S;
P:=StrAlloc(Length(S)+1);
C^.Insert(StrPCopy(P,S));
end;
For I:=0 to 98 do
With C^ do
If Compare (At(I),At(I+1))=0 then
Writeln ('Duplicate string found at position ',I);
Dispose(C,Done);
end.

#### TStrCollection.FreeItem

Declaration:
  Procedure TStrCollection.FreeItem (Item: Pointer); Virtual;
Description:
  TStrCollection overrides FreeItem so that the string pointed to by Item is disposed from memory.
Errors:
  None.
See also:
  TCollection.FreeItem (605)

#### TStrCollection.PutItem

Declaration:
  Procedure TStrCollection.PutItem (Var S: TStream; Item: Pointer); Virtual;
Description:
  PutItem writes the string pointed to by Item to the stream S.

  This method is primarily used in the Load and Store methods, and should not be used directly.

Errors:
  Errors are those of TStream.StrWrite (572).
See also:
  GetItem (624)

[next] [prev] [prev-tail] [front] [up]