#1 2015-03-13 22:52:53

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Load and save stream

Is there somethings link StreamFromString and StringFromSteam functions?

I need save and load a graph generate by a VCL component, this component have LoadFromStream and SaveToStream methods.

Thanks

Offline

#2 2015-03-14 08:44:14

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,662
Website

Re: Load and save stream

Take a look at the TRawByteStringStream class.

Offline

#3 2015-03-14 13:55:33

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Load and save stream

I just use it but I get some problem. I post some piece of code.

This is my TSQLRecord:

  TSQLNotes = class(TSQLRecord)
  private
    fRecordCreated: TCreateTime;
    fRecordModified: TModTime;
    fParent: Integer;
    fKind: Integer;
    fTitle: RawUTF8;
    fNote: RawUTF8;
    fText: RawUTF8;
  published
    property RecordCreated: TCreateTime read fRecordCreated write fRecordCreated;
    property RecordModified: TModTime read fRecordModified write fRecordModified;
    property Parent: Integer read fParent write fParent;
    property Kind: Integer read fKind write fKind;
    property Title: RawUTF8 read fTitle write fTitle;
    property Note: RawUTF8 read fNote write fNote;
    property Text: RawUTF8 read fText write fText;
  end;

I need save and load a graph generate from TSimpleGraph, so I use this code to save the graph:

Stream := TRawByteStringStream.Create;
Stream.Position := 0;
SimpleGraph.SaveToStream(Stream);
NotaRecord.Note := S2U(Stream.DataString);
Stream.Free;

and then I try to load it with this code:

Stream := TRawByteStringStream.Create;

try
  NotaRecord := TSQLNotes.Create(Database,'ID=?',[idRow]);

  WriteStringToStream(Stream, NotaRecord.Note);
  if (Stream.Size > 0) and (NotaRecord.Note <> '') then
    begin
       Stream.Seek(0,soBeginning);
       SimpleGraph.LoadFromStream(Stream);
    end;

  NotaRecord.Free;
finally
  Stream.Free;
end;

However TSimpleGraph return exception when LoadFromStream is call because the stream format is not correct.

What am I doing wrong?

Thanks

Offline

#4 2015-03-14 20:37:25

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,662
Website

Re: Load and save stream

WriteStringToStream() is not to be used like this!
As stated by the doc, it is a wrapper to be used with our binary stream format.

Just use Stream := TRawByteStringStream.Create(NotaRecord.Note).

And IMHO you should better define the Note field as TSQLRawBlob and not RawUTF8.
Your content is NOT UTF-8 text, but raw binary, right?

Offline

Board footer

Powered by FluxBB