comp.lang.ada
 help / color / mirror / Atom feed
From: dennison@telepath.com
Subject: Re: Stream IO - Correct usage of Set_Index
Date: 1999/03/02
Date: 1999-03-02T00:00:00+00:00	[thread overview]
Message-ID: <7bh1me$v4t$1@nnrp1.dejanews.com> (raw)
In-Reply-To: 7bemc8$snn$1@whatsit.aston.ac.uk

In article <7bemc8$snn$1@whatsit.aston.ac.uk>,
  gaukrogi@aston.ac.uk (Rofi) wrote:

> 	I am trying to use Stream_IO to create (and use) a file that contains
> different data types.  Creating, writing to and reading from the file in a
> sequential manner is not a problem.
> 	My problem is using Set_Index to move to a particular data structure
> that is situated part way through the file.  How do I find out the size of
> a data structures so that I can use Set_Index correctly.

> 	I have tried using the attribute Size which appears to return the
> size of the datastructure in bits.  On this compiler I can divide this by 8
> and I get the correct values but this method seem to me to be very Adalike.
> Am I missing something monumentally obvious or ...... ?

First off, the safe way to find the byte size of an object from 'size is to
add 7 and then divide by 8. Otherwise you are likely to get a byte size of 0
for Boolean.

Now for the larger question, I don't think there's a way to know beforehand
how many storage elements an object will take up in a stream, unless you
wrote the 'Write routines for the type and the Write routine for the stream
yourself. For all you know, it may be variable. This is quite likely if
'Output is used on a String. But either 'Write or Write could have used some
kind of compression algorithm as well. You just don't know.

Additionally, Ada.Streams.Stream_IO.Index returns a count of stream elements,
not bytes. While there's a good chance they are the same, you don't even know
that for sure. You have to do a bit of math first. Here's how I do it inside
one of my stream pacakges:

   Bytes_Per_Element : constant := (Ada.Streams.Stream_Element'Size + 7) / 8;

  Elements_Per_Header : constant Ada.Streams.Stream_Element_Offset := 
Ada.Streams.Stream_Element_Offset(  ((Instance'Size + 7) / 8) +
(Bytes_Per_Element - 1) / Bytes_Per_Element);

"Instance" is a type of mine I want to convert to stream elements.

So you may now ask, what are Ada.Streams.Index and Ada.Streams.Set_Index good
for, if you can't use them to skip unnessecary entries in a stream? Well,
what I'm currently using them for is to keep track of previous locations in a
stream that I may want to go back to.

To search for a certian entry (eg: an entry with a timestamp past a certian
time), I will read in all entries one by one, noting their index before I do
so. When an entry with the proper value is found, I call
Ada.Streams.Set_Index with the index I saved before I read the item, and
continue from there.

So what if you *do* need to know how big every entry in a stream is? Simple;
create your own stream that keeps track of that information. (more info on
doing that is available upon request).

T.E.D.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




  reply	other threads:[~1999-03-02  0:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-03-01  0:00 Stream IO - Correct usage of Set_Index Rofi
1999-03-02  0:00 ` dennison [this message]
1999-03-04  0:00   ` Rofi
1999-03-04  0:00     ` dennison
1999-03-02  0:00 ` bourguet
1999-03-04  0:00   ` Rofi
replies disabled

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