Recent

Author Topic: BufDataset and LoadFromStream problem  (Read 12492 times)

iru

  • Sr. Member
  • ****
  • Posts: 321
BufDataset and LoadFromStream problem
« on: May 25, 2010, 11:04:05 pm »
Gentlefolk,

Lazarus 0.9.29 snapshot (using to address other issues with dbgrid), windows xp, SP-3.

I have a peice of delphi code which I have migrated to lazarus. The Lazarus code is to load the 'fields' in a .CSV file to a BufDataset, the code crashes on 'LoadfromStream' with the following error message:

"the data stream format is not recognised".

The executed code is

   F := TFileStream.Create(FileName, fmOpenRead);
   S := TMemoryStream.Create;

   try
      S.SetSize(F.Size + 2);
      P := S.Memory;
      Move(CrLf[1], P^, 2);
      Inc(P, 2);
      F.ReadBuffer(P^, F.Size);
      MT.LoadFromStream(S, dfAny); 

The file contains one line of text '123,"test description","F","T"'.
After the ReadBuffer P contains  '123,"test description","F","T"r'.

Looking at the code in Bufdataset.  and Bufdataset.TFpcBinaryDatapacketReader.RecognizeStream shows the code doing some strange things where it compares

#13#10'123,"test d' against FpcBinaryIdent = 'BinBufDataset';

this fails and it is all down hill from there.......

Any ideas, solutions, explanation, examples of using a LoadFromStream with BufDataset appreciated.

thanks, Ian. 
                 

Loesje

  • Full Member
  • ***
  • Posts: 168
    • Lazarus Support website
Re: BufDataset and LoadFromStream problem
« Reply #1 on: May 27, 2010, 11:13:02 am »
LoadFromStream uses by default a binary format. You can add more formats by adding more formatter-units.

There is currently no formatter for csv files, but you can use the xml-format that Delphi's TClientDataset also uses by default. Add the 'xmldatapacketreader' to your program and it will recognize Delphi's xml format.

stonefull

  • Jr. Member
  • **
  • Posts: 54
Re: BufDataset and LoadFromStream problem
« Reply #2 on: May 27, 2010, 02:32:59 pm »
I'd use TSdfDataSet.

iru

  • Sr. Member
  • ****
  • Posts: 321
Re: BufDataset and LoadFromStream problem
« Reply #3 on: May 27, 2010, 09:09:48 pm »
Gentlefolk,

Thanks for the responses.

May I ask why use 'TSdfDataSet' ?????

Ian

stonefull

  • Jr. Member
  • **
  • Posts: 54
Re: BufDataset and LoadFromStream problem
« Reply #4 on: May 28, 2010, 06:50:36 am »
TSdfDataset can define commas as separator, like csv format. It's very simple.

iru

  • Sr. Member
  • ****
  • Posts: 321
Re: BufDataset and LoadFromStream problem
« Reply #5 on: May 29, 2010, 08:25:57 am »
Thanks for the response.

All goes well up to a point. I can load the contents of a file (can see it in an associated DBGrid).

However when I try  SdfDataSet.FieldByName('Name').AsString a 'field not known' exception occurs. The field is there, it is defined at design time.

Looking at the code in dtaset.inc it would appear that no fields are defined.

I will attempt to dynamically define the fields and see what happens.

Ian

stonefull

  • Jr. Member
  • **
  • Posts: 54
Re: BufDataset and LoadFromStream problem
« Reply #6 on: May 29, 2010, 09:05:57 am »
TSdfDataset define field names as Field1, Field2, Field3, etc... You can't modify them. Try

SdfDataSet.FieldByName('Field1').AsString

I wrote a post (in Portuguese) about this:

http://professorcarlos.blogspot.com/2010/03/lazarus-tratamento-de-arquivos-texto.html


iru

  • Sr. Member
  • ****
  • Posts: 321
Re: BufDataset and LoadFromStream problem
« Reply #8 on: May 29, 2010, 08:00:35 pm »
Gentlefolk,

Thank you, abbrigado (my one word of Portugese learned on a breif vizit to Mozambique (spelling ???)).

Things are now working OK.

Ian


typo

  • Hero Member
  • *****
  • Posts: 3051
Re: BufDataset and LoadFromStream problem
« Reply #9 on: May 29, 2010, 08:04:40 pm »
It's "obrigado".

 

TinyPortal © 2005-2018