comp.lang.ada
 help / color / mirror / Atom feed
From: Marin David Condic <mcondic.nospam@acm.org>
Subject: Re: Ada Streams usage (was Escape Sequences in Strings)
Date: 2000/11/19
Date: 2000-11-19T18:33:00+00:00	[thread overview]
Message-ID: <3A181D0F.B1FC2485@acm.org> (raw)
In-Reply-To: 3A17ED81.3FD596@acm.org

Marin David Condic wrote:

> I'll see what I can do to trim down a small example and post it here.

Here's a relatively short example that will compile and run. It doesn't do anything
terribly useful, but you could use it as the basis for playing some games & learning
how it works. For example, you can throw some Text_IO into the code to see when the
Read and Write get called and with what values. You could add a big ring buffer to
the My_Stream_Type and have it store and retrieve values from there - sort of
simulating the existence of a Stream file. Or you could use Text_IO to simulate an
I/O device in some way. You might also try defining your own data type - some sort
of class (record, tagged record, etc.) and fool with overriding the Read and Write.
See what you get with the defaults and see how you might implement your own methods
of getting things into a byte stream.

This will begin to give you a feel for how it works and you'll understand some of
the issues relating to streams better. (Efficiency, representations, etc.) More
questions? Post them here and we'll see if answers come up.

with Ada.Streams ;


package My_Stream is

    type My_Stream_Type is new Ada.Streams.Root_Stream_Type with private ;

    type My_Stream_Ptr is access all Ada.Streams.Root_Stream_Type'Class ;

    --
    --  You need this to get a pointer to the My_Stream object. The
    --  pointer is used in calls such as:
    --  Some_Type'Read (My_Stream_Ptr, My_Object) ;
    --
    procedure Stream (
        My_Stream      : in out My_Stream_Type ;
        Ptr            :    out My_Stream_Ptr) ;

private

    procedure Read (
        Stream  : in out My_Stream_Type ;
        Item    :    out Ada.Streams.Stream_Element_Array ;
        Last    :    out Ada.Streams.Stream_Element_Offset) ;

    procedure Write (
        Stream  : in out My_Stream_Type ;
        Item    : in     Ada.Streams.Stream_Element_Array) ;

    type My_Stream_Type is new Ada.Streams.Root_Stream_Type with record
        --
        --  This is where you would retain information about I/O buffers,
        --  I/O ports, etc. so that Read and Write can know where to
        --  retreive or send their data. A simple case might be a big ring
        --  buffer with Write attaching to the end and Read pulling stuff
        --  off from the beginning. Not really useful, but it makes the
        --  point.
        --
        null ;
    end record ;
    --
end My_Stream ;

package body My_Stream is




    procedure Stream (
        My_Stream      : in out My_Stream_Type ;
        Ptr            :    out My_Stream_Ptr) is
        --
    begin
        Ptr    := My_Stream'Unchecked_Access ;
    end Stream ;



    procedure Read (
        Stream  : in out My_Stream_Type ;
        Item    :    out Ada.Streams.Stream_Element_Array ;
        Last    :    out Ada.Streams.Stream_Element_Offset) is
        --
        --  This is called automagically from the background as things
        --  are being converted by Something'Read.
        --
    begin
        --
        --  Normally, you'd do something to access a low-level I/O
        --  device or use some other means to get hold of raw bytes
        --  from some source at this point in the code. Here we
        --  just fill it with zeros as a not too useful example.
        --
        for X in Item'Range loop
            Item (X) := 0 ;
        end loop ;
        Last    := Item'Last ;
    end Read ;



    procedure Write (
        Stream  : in out My_Stream_Type ;
        Item    : in     Ada.Streams.Stream_Element_Array) is
        --
        --  This is called automagically from the background as things
        --  are being converted by Something'Write.
        --
    begin
       --
       --  This is where you would do something to make the bytes in Item
       --  go out to some low level I/O port or other destination. Here
       --  we basically do nothing for this example.
       --
       for X in Item'Range loop
           null ;    -- Insert your "Write a byte to the I/O port" code here.
       end loop ;
    end Write ;



end My_Stream ;

with My_Stream ;
use My_Stream ;

procedure My_Stream_Driver is
    A_Stream    : My_Stream_Type ;
    A_Ptr       : My_Stream_Ptr ;
    --
    Some_Int    : Integer           := 15 ;
    Some_Float  : Float             := 10.5 ;
    Some_String : String (1..10)    := "XXXXXXXXXX" ;
begin
    Stream (
        My_Stream    => A_Stream,
        Ptr          => A_Ptr) ;
    --
    --  Finds its way to My_Stream.Write with (probably) a 4 byte
    --  Stream_Element_Array.
    --
    Integer'Write (A_Ptr, Some_Int) ;
    --
    --  Goes to My_Stream.Write with how many bytes?
    --
    Float'Write (A_Ptr, Some_Float) ;
    --
    --  This *should* go to My_Stream.Write with 10 bytes.
    --
    String'Write (A_Ptr, Some_String) ;
    --
    --  Read the values in - We get useless values unless
    --  we fill in the My_Stream.Read with something to
    --  get the values from somewhere.
    --
    String'Read (A_Ptr, Some_String) ;
    Float'Read (A_Ptr, Some_Float) ;
    Integer'Read (A_Ptr, Some_Int) ;
    --
end My_Stream_Driver ;
--
======================================================================
Marin David Condic - Quadrus Corporation - http://www.quadruscorp.com/
Send Replies To: m c o n d i c @ q u a d r u s c o r p . c o m
Visit my web site at:  http://www.mcondic.com/

    "Giving money and power to Government is like giving whiskey
    and car keys to teenage boys."

        --   P. J. O'Rourke
======================================================================






  reply	other threads:[~2000-11-19  0:00 UTC|newest]

Thread overview: 94+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-11-15  0:00 Escape Sequences in Strings Jean Cohen
2000-11-15  0:00 ` Marin David Condic
2000-11-16  0:00   ` Ada Streams usage (was Escape Sequences in Strings) Marc A. Criley
2000-11-16  0:00     ` Marin David Condic
2000-11-16  0:00       ` Ted Dennison
2000-11-16  0:00         ` Marin David Condic
2000-11-16  0:00           ` Ted Dennison
2000-11-16  0:00             ` Marin David Condic
     [not found]     ` <igh81t8b3hdrsc167do6qr0h1joa73c1jr@borpin.co.uk>
2000-11-18  0:00       ` Marin David Condic
2000-11-18  0:00         ` David Kristola
2000-11-19  0:00           ` Marin David Condic
2000-11-19  0:00             ` Marin David Condic [this message]
2000-11-20  0:00               ` Brian Orpin
2000-11-20  0:00                 ` Marin David Condic
2000-11-19  0:00         ` Ted Dennison
2000-11-19  0:00           ` Marin David Condic
2000-11-19  0:00             ` Robert Dewar
2000-11-20  0:00               ` Marin David Condic
2000-11-21  0:00                 ` Robert Dewar
2000-11-20  0:00               ` Randy Brukardt
2000-11-21  0:00                 ` Ted Dennison
2000-11-21  0:00                   ` Randy Brukardt
2000-11-21  1:31                 ` Robert Dewar
2000-11-21  1:33                 ` Robert Dewar
2000-11-21  0:00                   ` Randy Brukardt
2000-11-22  5:00                     ` Robert Dewar
2000-11-19  0:00             ` Ted Dennison
2000-11-19  0:00               ` Robert Dewar
2000-11-15  0:00 ` Escape Sequences in Strings Preben Randhol
2000-11-15  0:00 ` John English
2000-11-15  0:00   ` Robert Dewar
2000-11-15  0:00     ` Ehud Lamm
2000-11-16  0:00       ` John English
2000-11-16  0:00         ` Tarjei T. Jensen
2000-11-16  0:00           ` Ken Garlington
2000-11-16  0:00             ` Marin David Condic
2000-11-16  0:00             ` Keith Thompson
2000-11-16  0:00         ` Marin David Condic
2001-01-12 13:18 ` Andrew Hately
2001-01-12 20:03   ` Keith Thompson
2001-01-18  8:29     ` Lao Xiao Hai
2001-01-18 15:22       ` Robert Dewar
2001-01-18 22:43         ` Randy Brukardt
2001-01-21 11:54         ` Dale Stanbrough
2001-01-21 22:35           ` directly accessing DOS screen memory (was: Re: Escape Sequences in Strings) Jeffrey Carter
2001-01-12 19:37 ` Escape Sequences in Strings tmoran
2001-01-13  1:38   ` Robert Dewar
2001-01-13  6:48     ` tmoran
2001-01-13 18:36       ` Robert Dewar
2001-01-16  3:30         ` Examples in Docs, was " peter_richtmyer
2001-01-16  5:42           ` Robert Dewar
2001-01-16 20:44             ` mark_lundquist
2001-01-16 22:43               ` Larry Kilgallen
2001-01-17 15:06                 ` mark_lundquist
2001-01-17  2:25               ` Robert Dewar
2001-01-17 15:28                 ` mark_lundquist
2001-01-17 16:20                 ` Brian Rogoff
2001-01-17 18:04                   ` Wayne Lydecker
2001-01-17 19:23                     ` BSCrawford
2001-01-18  0:15                       ` Jerry Petrey
2001-01-19  0:01                     ` tmoran
2001-01-18  3:44                   ` Robert Dewar
2001-01-18 16:45                     ` Brian Rogoff
2001-01-18 19:53                       ` Robert Dewar
2001-01-18 22:58                         ` Georg Bauhaus
2001-01-19 16:40                           ` Robert Dewar
2001-01-19  7:49                         ` Learning methods Anders Wirzenius
2001-01-19 18:57                         ` Examples in Docs, was Re: Escape Sequences in Strings mark_lundquist
2001-01-21 12:05                         ` Dale Stanbrough
2001-01-21 15:35                           ` Robert Dewar
2001-01-17 22:10                 ` Matthew Woodcraft
2001-01-18  3:52                   ` Robert Dewar
2001-01-16 16:06           ` Examples in Docs Robert C. Leif, Ph.D.
2001-01-16 21:29             ` mark_lundquist
2001-01-18  0:50               ` Randy Brukardt
2001-01-18 16:46                 ` mark_lundquist
2001-01-18 17:24               ` J. David Bryan
2001-01-17  2:43             ` Robert Dewar
2001-01-17 21:17               ` Robert C. Leif, Ph.D.
2001-01-16 20:01           ` Examples in Docs, was Re: Escape Sequences in Strings mark_lundquist
2001-01-17 10:59             ` Peter Richtmyer
2001-01-19 18:55               ` mark_lundquist
2001-01-20 14:24                 ` Robert Dewar
2001-01-20 14:36                   ` Preben Randhol
2001-01-20 15:00                     ` Robert Dewar
2001-01-21 11:24                       ` Preben Randhol
2001-01-22 14:47                         ` Ted Dennison
2001-01-22 20:08                           ` Preben Randhol
2001-01-22 20:14                             ` Preben Randhol
2001-01-20 19:02                   ` Stephen Leake
2001-01-20 19:50                     ` Georg Bauhaus
2001-01-21 11:35                     ` Preben Randhol
2001-01-22 23:58                   ` Mark Lundquist
2001-01-27  1:43                   ` Increasing the readability of Ada was RE: Examples in Docs Robert C. Leif, Ph.D.
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox