Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-08

TStream.WriteAnsiString

5 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Brian Grainger

Greetings.

I'm having a problem with TStream.WriteAnsiString. My apologies if this has been answered previously, I haven't seen it in the archive.

I'm using the procedure to write an ansistring of valid XML to a file. The problem is that the procedure also writes the length of the ansistring as a variable length binary field at the start of the string. The XML parser chokes on what it thinks is some kind of binary gibberish at the very start of the file.

   Procedure TStream.WriteAnsiString (S : String);
   Var L : Longint;
   begin
     L:=Length(S);
     WriteBuffer (L,SizeOf(L));  <---- This is the problem.
     WriteBuffer (Pointer(S)^,L);
   end;

What was the reason for this design decision? I can work around it by writing my own similar procedure which doesn't write the string length bytes, but it would be nicer to use a standard routine from the library.

Regards,
Brian

Michael Van Canneyt
to Brian Grainger
Brian Grainger wrote 22 lines
Greetings. I'm having a problem with TStream.WriteAnsiString. My apologies if this has been answered previously, I haven't seen it in the archive. I'm using the procedure to write an ansistring of valid XML to a file. The problem is that the procedure also writes the length of the ansistring as a variable length binary field at the start of the string. The XML parser chokes on what it thinks is some kind of binary gibberish at the very start of the file. Procedure TStream.WriteAnsiString (S : String); Var L : Longint; begin L:=Length(S); WriteBuffer (L,SizeOf(L)); <---- This is the problem. WriteBuffer (Pointer(S)^,L); end; What was the reason for this design decision? I can work around it by writing my own similar procedure which doesn't write the string length bytes, but it would be nicer to use a standard routine from the library.

The behaviour is as intended. The WriteByte/WriteWord/WriteString etc. all write binary data to the stream. If you want to write text data, then you should write your own routines.

Michael.

Brian Grainger
to Michael Van Canneyt
you wrote
The behaviour is as intended. The WriteByte/WriteWord/WriteString etc. all write binary data to the stream. If you want to write text data, then you should write your own routines.

I can understand that where the native data type is binary, but it seems odd that WriteString and WriteAnsiString also write binary data. Basically it means that for writing text data, which one would assume was happening with these routines, they are unusable.

Perhaps some sort of comment in the source code would be helpful, just to note that WriteString and WriteAnsiString do not write text data.

Regards,
Brian

Michael Van Canneyt
to Brian Grainger
Brian Grainger wrote 8 lines
At 09:36 AM 8/21/02 +0200, you wrote: >The behaviour is as intended. The WriteByte/WriteWord/WriteString etc. all >write binary data to the stream. If you want to write text data, then >you should write your own routines. I can understand that where the native data type is binary, but it seems odd that WriteString and WriteAnsiString also write binary data.

Not more than for WriteWord or WriteByte - If you write 1 as a byte you will get something totally unusable for XML or whatever text format.

Basically it means that for writing text data, which one would assume was happening with these routines, they are unusable.

Yes. They are meant for reading and writing binary data.

Perhaps some sort of comment in the source code would be helpful, just to note that WriteString and WriteAnsiString do not write text data.

These methods are documented, but the classes unit is not yet completely documented, which is why the documentation is not yet published. A preliminary version is at

http://www.freepascal.org/docs-html/classes/

I can understand that people also want real text support. Therefore, to support text writing, I propose to implement

TIntegerFormat = (ifDecimal,ifHex,ifOctal,ifBinary);
Function TStream.WriteAsText(L : Longint; Format : TTextFormat) : Integer;
Function TStream.WriteAsText(L : Longint) : Integer; // Use default ifDecimal
Function TStream.WriteAsText(L : int64) : Integer;
// repeat for smallint, shortint, byte, word, cardinal.
Function TStream.WriteAsText(S : Ansistring) : Integer;
Function TStream.WriteAsText(S : ShortString) : Integer;

These would do what you want, i.e. write a text representation of the argument, much like Writeln() does. Return value is the number of bytes written.

The name can be changed to WriteText,textwrite, textout or so, this is not really a principal issue.

Suggestions, comments welcome.

Michael.

Brian Grainger
to Michael Van Canneyt
you wrote
These would do what you want, i.e. write a text representation of the argument, much like Writeln() does. Return value is the number of bytes written.

Yes, that sounds very good. My own particular use for output streams is mainly to write XML, which is really just a text string, so these routines would be extremely helpful.

Thanks again for the quick reply.

Regards,
Brian