user1009073
user1009073

Reputation: 3238

Delphi - Saving Records to File using Streams

Delphi Tokyo - I have a parameter file that I am needing to save (and later load) from disk. The parameters are a series of record objects. There is one HEADER record and then multiple COMMAND records. These are true records (i.e type = records). The HEADER record has String, Boolean, Integer, and TStringList types within it. I save, which appears to work fine, but when I load, whatever is AFTER a TStringList causes a Stream read error. For example...

type tEDP_PROJ = record
   Version : Integer;
   Name: String;
    ...
   ColList1: TStringList;
   ColList2: TStringList;
   ReadyToRun : Boolean;
   ...
 end;

When I read ReadyToRun I get a Stream read error. If I move it BEFORE TStringList (on both SAVE and LOAD routines) then ReadyToRun will load properly, but whatever is after the TStringList will cause an error. It is interesting to note that ColList2 loads fine (even though it is NOT the first TStringList).

I am specifying the Encoding method when I save the TStringList.

...
ColList1.SaveToStream(SavingStream, TEncoding.Unicode);
ColList2.SaveToStream(SavingStream, TEncoding.Unicode);

I am using the same encoding when I load from the (file) Stream.

...
   ColList1.LoadFromStream(SavingStream, TEncoding.Unicode);
   ColList2.LoadFromStream(SavingStream, TEncoding.Unicode);

Note that when I create the StringList, I am just doing the standard create...

ColList1 := TStringList.Create;

When I save and load, I am following the examples Remy gave here...

The TStringList appears to be changing the way that the stream reads non-TStringList types... What do I need to do to fix this?

Upvotes: 0

Views: 5039

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595349

Why are you using TEncoding.Unicode? TEncoding.UTF8 would have made more sense.

In any case, this is not an encoding issue. What you are attempting to do will simply not work the way you are trying to do it, because TStrings data is variable-length and needs to be handled accordingly. However, TStrings does not save any kind of terminating delimiter or size information to an output stream. When loading in a stream, TStrings.LoadFromStream() simply reads the ENTIRE stream (well, everything between the current Position and the End-Of-Stream, anyway). That is why you are getting streaming errors when trying to read/write any non-TStrings data after any TStrings data.

Just like the earlier code needed to serialize String data and other variable-length data into a flat format to know where one field ends and the next begins, you need to serialize TStrings data as well.

One option is to save a TStrings object to an intermediate TMemoryStream first, then write that stream's Size to your output stream followed by the TMemoryStream's data. When loading back later, first read the Size, then read the specified number of bytes into an intermediate TMemoryStream, and then load that stream into your receiving TStrings object:

procedure WriteInt64ToStream(Stream: TStream; Value: Int64);
begin
  Stream.WriteBuffer(Value, Sizeof(Value));
end;

function ReadInt64FromStream(Stream: TStream): Int64;
begin
  Stream.ReadBuffer(Result, Sizeof(Result));
end;

procedure WriteStringsToStream(Stream: TStream; Values: TStrings);
var
  MS: TMemoryStream;
  Size: Int64;
begin
  MS := TMemoryStream.Create;
  try
    Values.SaveToStream(MS, TEncoding.UTF8);
    Size := MS.Size;
    WriteInt64ToStream(Stream, Size);
    if Size > 0 then
    begin
      MS.Position := 0;
      Stream.CopyFrom(MS, Size);
    end;
  finally
    MS.Free;
  end;
end;

procedure ReadStringsFromStream(Stream: TStream; Values: TStrings);
var
  MS: TMemoryStream;
  Size: Int64;
begin
  Size := ReadInt64FromStream(Stream);
  MS := TMemoryStream.Create;
  try
    if Size > 0 then
    begin
      MS.CopyFrom(Stream, Size);
      MS.Position := 0;
    end;
    Values.LoadFromStream(MS, TEncoding.UTF8);
  finally
    MS.Free;
  end;
end;

Another option is to write the number of string elements in the TStrings object to your output stream, and then write the individual strings:

procedure WriteStringsToStream(Stream: TStream; Values: TStrings);
var
  Count, I: Integer;
begin
  Count := Values.Count;
  WriteIntegerToStream(Stream, Count);
  for I := 0 to Count-1 do
    WriteStringToStream(Stream, Values[I]);
end;

procedure ReadStringsFromStream(Stream: TStream; Values: TStrings);
var
  Count, I: Integer;
begin
  Count := ReadIntegerFromStream(Stream);
  if Count > 0 then
  begin
    Values.BeginUpdate;
    try
      for I := 0 to Count-1 do
        Values.Add(ReadStringFromStream(Stream));
    finally
      Values.EndUpdate;
    end;
  end;
end;

Either way, you can then do this when streaming your individual records:

WriteIntegerToStream(SavingStream, Version);
WriteStringToStream(SavingStream, Name);
...
WriteStringsToStream(SavingStream, ColList1);
WriteStringsToStream(SavingStream, ColList2);
WriteBooleanToStream(SavingStream, ReadyToRun);

Version := ReadIntegerFromStream(SavingStream);
Name := ReadStringFromStream(SavingStream);
...
ReadStringsFromStream(SavingStream, ColList1);
ReadStringsFromStream(SavingStream, ColList2);
ReadyToRun := ReadBooleanFromStream(SavingStream);

Upvotes: 4

Related Questions