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,a30e2d840674d0ef X-Google-Attributes: gid103376,public From: dennison@telepath.com Subject: Re: Stream IO - Correct usage of Set_Index Date: 1999/03/02 Message-ID: <7bh1me$v4t$1@nnrp1.dejanews.com>#1/1 X-Deja-AN: 450378623 References: <7bemc8$snn$1@whatsit.aston.ac.uk> X-Http-Proxy: 1.0 x14.dejanews.com:80 (Squid/1.1.22) for client 204.48.27.130 Organization: Deja News - The Leader in Internet Discussion X-Article-Creation-Date: Tue Mar 02 15:56:04 1999 GMT Newsgroups: comp.lang.ada X-Http-User-Agent: Mozilla/4.5 [en] (WinNT; I) Date: 1999-03-02T00:00:00+00:00 List-Id: 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