comp.lang.ada
 help / color / mirror / Atom feed
From: "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com>
Subject: Re: S'Write and How To Count Bytes
Date: 2000/10/01
Date: 2000-10-01T00:00:00+00:00	[thread overview]
Message-ID: <hXTB5.64979$Ao1.5807473@newsrump.sjc.telocity.net> (raw)
In-Reply-To: 39D6891A.7DC448B6@acm.org

If your record contains an unbounded string, I would in the My_Record'Write
procedure convert the unbounded string to a Standard.String, and write its
length to the stream with Natural'Write, followed by a call to String'Write.
This will put the length of the string on the stream, followed by the
string itself.

Then, the My_Record'Read procedure would read the length from the stream,
using Natural'Read, use that length to declare a string of the proper size
(in a declare block), then read the string with String'Read.
Finally, you would convert the string to the unbounded string component in
your My_Record'Read's out parameter of type My_Record.


Marin David Condic <mcondic.nospam@acm.org> wrote in message
news:39D6891A.7DC448B6@acm.org...
> Suppose I have a type something like this:
>
>     type My_Record is record
>         A_String    : Unbounded_String := Null_Unbounded_String ;
>     end record ;
>
> In theory, I should be able to create an object that is derived from
> Ada.Streams.Root_Stream_Type which has a Read and Write procedure that
> will allow me to create a call such as:
>
>     My_Record'Write (Ptr, A_Record) ;
>
> where Ptr is a pointer to my derived Root_Stream_Type object. Now here's
> where it gets fun:
>
> The object derived from Root_Stream_Type is going to contain some
> version of a Stream_Element_Array. Possibly dynamically allocated. The
> Write procedure that I create for the Root_Stream_Type is going to get
> automagically called with a Stream_Element_Array parameter, from which
> you could determine the size, allocate what you need for a buffer and
> attach it to the Root_Stream_Type. That buffer can then be sent
> somewhere or used in some other manner.
>
> The problem is this: What if you want the record to contain the number
> of bytes that it converts to so that the number ends up in the stream?
> You can't easily determine how many bytes there will be in the record
> nor can you determine what the 'Write is really going to produce. You
> could possibly retain the size information in your Root_Stream_Type
> after the conversion, but by then its too late. You already converted it
> and if you had a "Count" field in the record, its value is probably dead
> wrong. You could convert it to the stream once, interrogate the
> Root_Stream_Type you built to determine the number of bytes in it, then
> include that byte count in your record and convert it all over again.
> That seems a little inefficient.
>
> Is there any good way of determining in advance how many bytes you are
> going to get when converting an object to a stream?
>
> MDC
> --
> ======================================================================
> 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-10-01  0:00 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-09-30  0:00 S'Write and How To Count Bytes Marin David Condic
2000-10-01  0:00 ` David C. Hoos, Sr. [this message]
2000-10-02  0:00   ` Marin David Condic
2000-10-02  0:00     ` Ted Dennison
2000-10-02  0:00       ` tmoran
2000-10-02  0:00         ` tmoran
2000-10-02  0:00           ` Marin David Condic
2000-10-11  0:00             ` Nick Roberts
2000-10-11  0:00               ` Marin David Condic
2000-10-02  0:00           ` Ted Dennison
2000-10-02  0:00         ` Marin David Condic
2000-10-03  0:00       ` Robert A Duff
2000-10-06  0:00         ` Randy Brukardt
2000-10-07  0:11           ` Ted Dennison
2000-10-06  0:00             ` Randy Brukardt
2000-10-07  0:00             ` Marin David Condic
2000-10-08  0:00               ` Jean-Pierre Rosen
2000-10-09  0:00                 ` Randy Brukardt
2000-10-11  0:00                 ` Marin David Condic
2000-10-02  0:00   ` Dr. Joachim Schr�er
2000-10-02  0:00     ` Marin David Condic
2000-10-06  0:00       ` Charles Hixson
2000-10-02  0:00 ` David C. Hoos, Sr.
2000-10-02  6:58 ` tmoran
2000-10-02  0:00   ` Marin David Condic
  -- strict thread matches above, loose matches on Subject: below --
2000-10-02  0:00 tmoran
2000-10-03  5:21 ` Marin David Condic
2000-10-03  0:00 Mario Amado Alves
2000-10-03  0:00 ` Marin David Condic
replies disabled

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