From: gautier_niouzes on
....is it possible ?
The RM tells about the reverse, getting a stream access from a text
file (Text_IO.Text_Streams).
TIA
______________________________________________________________
Gautier -- http://www.mysunrise.ch/users/gdm/index.htm
Ada programming -- http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: For a direct answer, e-mail address on the Web site!
From: Dmitry A. Kazakov on
On Mon, 31 Mar 2008 08:27:21 -0700 (PDT), gautier_niouzes(a)hotmail.com
wrote:

> ...is it possible ?
> The RM tells about the reverse, getting a stream access from a text
> file (Text_IO.Text_Streams).

You can always wrap a File_Type in your own stream object.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
From: Dmitry A. Kazakov on
On Mon, 31 Mar 2008 09:05:35 -0700 (PDT), gautier_niouzes(a)hotmail.com
wrote:

> Dmitry A. Kazakov:
>
>> You can always wrap a File_Type in your own stream object.
>
> Do you mean something like:
>
> type Text_Stream_Type is new Ada.Streams.Root_Stream_Type with record
> virtual_file : Ada.Text_IO.File_Type;
> end record;

Yes

> At the end of the day, what I'd really like is
> function File(stream: access Ada.Streams.Root_Stream_Type 'Class)
> return Ada.Text_IO.File_Type;
> so that I can do read ASCII data from any stream:
> f:= File(my_stream);
> -- f is as if open in In_File mode
> Get_Line(f, a_string, length);
> Get(f, an_integer); -- not binary, but 123_456
> Get(f, a_float); -- not binary, but -123.456e78
>
> I guess it would be difficult... But maybe I'm wrong ?
> Gautier

But a stream has no defined formatting and is unrelated to ASCII. Then the
idea of parsing stream looks problematic. If you have in the stream a
sequence " + -", what Get should do after discovering '-'?

Anyway, it is quite simple to do. You can do

type Formatted_Stream (Source : access Root_Stream_Type'Class) is
tagged limited private;
function Get (Stream : access Formatted_Stream) return Integer;
...

The implementation of Formatted_Stream will use Source for all its I/O. So
you could hang it on any existing stream.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
From: gautier_niouzes on
Dmitry A. Kazakov:

> You can always wrap a File_Type in your own stream object.

Do you mean something like:

type Text_Stream_Type is new Ada.Streams.Root_Stream_Type with record
virtual_file : Ada.Text_IO.File_Type;
end record;

?
At the end of the day, what I'd really like is
function File(stream: access Ada.Streams.Root_Stream_Type 'Class)
return Ada.Text_IO.File_Type;
so that I can do read ASCII data from any stream:
f:= File(my_stream);
-- f is as if open in In_File mode
Get_Line(f, a_string, length);
Get(f, an_integer); -- not binary, but 123_456
Get(f, a_float); -- not binary, but -123.456e78

I guess it would be difficult... But maybe I'm wrong ?
Gautier
From: Dmitry A. Kazakov on
On Mon, 31 Mar 2008 22:54:35 +0200, Gautier wrote:

> Dmitry A. Kazakov:
>
> Meanwhile (#ada irc...) there seems to be a nice solution:
>
> http://jesselang.com/software/streams_text_io/

Yes.

>>> Well, I think I'll switch to a binary format...
>>
>> I would do that too. Also I would override 'Read and 'Write in order to
>> make it platform-independent. (Stream_Element should be replaced by
>> octets.)
>
> No problem with that - there is already a framework in that package (for other
> files) that sends things byte-per-byte in a deterministic endianess; floats are
> split/merged with certain a Float_portable_binary_transfer package.

No, I meant your idea of a text-oriented stream. So Float'Write would put
Float in a human-readable format (instead of Put). But of course, a binary
transfer is always preferable.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de