From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,2e66fe8b30e3ee2c X-Google-Attributes: gid103376,public From: "David C. Hoos, Sr." Subject: Re: S'Write and How To Count Bytes Date: 2000/10/01 Message-ID: #1/1 X-Deja-AN: 676413365 Content-Transfer-Encoding: 7bit References: <39D6891A.7DC448B6@acm.org> Content-Type: text/plain; charset="iso-8859-1" X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 X-Abuse-Info: Please forward ALL headers when reporting abuse. X-Complaints-To: abuse@telocity.net X-Trace: NjggTm9BdXRoVXNlciBURUyFQ0lUWS1SRUFERVJGIDIxNi4yMjcuNDcuNDkgIFN1biwgMMYgT2N0!IDIwMDAgMjE6MTM6MDEgUERU MIME-Version: 1.0 NNTP-Posting-Date: Sun, 01 Oct 2000 21:13:01 PDT Newsgroups: comp.lang.ada Date: 2000-10-01T00:00:00+00:00 List-Id: 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 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 > ====================================================================== > >