[fpc-pascal]TStream.WriteAnsiString

Michael Van Canneyt michael.vancanneyt at wisa.be
Wed Aug 21 14:34:04 CEST 2002


On Wed, 21 Aug 2002, Brian Grainger wrote:

> 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.





More information about the fpc-pascal mailing list