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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,8a34575d5eb275cb X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!newsfeed.icl.net!newsfeed.fjserv.net!colt.net!feeder.news-service.com!feeder8.cambrium.nl!feed.tweaknews.nl!195.238.0.231.MISMATCH!news.skynet.be!195.238.0.222.MISMATCH!newsspl501.isp.belgacom.be!tjb!not-for-mail Date: Sun, 19 Apr 2009 15:08:33 +0200 From: Olivier Scalbert User-Agent: Thunderbird 2.0.0.21 (X11/20090318) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Load an object from a file References: <49d5fa88$0$2862$ba620e4c@news.skynet.be> <443d72ca-5bbd-46a3-84c6-e8bd984e5b80@k41g2000yqh.googlegroups.com> In-Reply-To: <443d72ca-5bbd-46a3-84c6-e8bd984e5b80@k41g2000yqh.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <49eb223e$0$2849$ba620e4c@news.skynet.be> Organization: -= Belgacom Usenet Service =- NNTP-Posting-Host: 5bbca56e.news.skynet.be X-Trace: 1240146494 news.skynet.be 2849 87.65.213.139:53005 X-Complaints-To: usenet-abuse@skynet.be Xref: g2news1.google.com comp.lang.ada:4539 Date: 2009-04-19T15:08:33+02:00 List-Id: Hello, I have an endianness problem. In the file, The u2 data are stored in big-endian order. As I am working on a Pentium, the reading is done in little-endian order. Is it possible to fix it with the representation stuff ? Thanks, Olivier Ludovic Brenta wrote: > > I would create a record type with one discriminant for each array, > like so: > > type Constant_Pool_Array is array (Positive range <>) of cp_info; > type Interfaces_Array is array (Positive range <>) of u2; > -- etc. > > type Class_File > (constant_pool_count, > interfaces_count, > fields_count, > methods_count, > attributes_count : u2) > is record > ... > constant_pool : Constant_Pool_Array (2 .. constant_pool_count); > ... > interfaces : Interfaces_Array (1 .. interfaces_count); > ... etc. > end record; > >> Also how can I fill this array ? > > You would normally simply call the predefined Class_File'Read but this > wouldn't work since the order of the components in type Class_File > does not match the order in the file. So, you'd specify your own Read: > > function Input( > Stream : not null access Ada.Streams.Root_Stream_Type'Class) > return Class_File); -- see RM 13.13(22) and following > for Class_File'Input use Input; -- as per RM 13.13(38/2) > > function Input( > Stream : not null access Ada.Streams.Root_Stream_Type'Class) > return Class_File) > is > Constant_Pool_Count : u2; > type Constant_Pool_Array_Access is access Constant_Pool_Array; > procedure Free is new Ada.Unchecked_Deallocation > (Constant_Pool_Array, Constant_Pool_Array_Access); > Constant_Pool : Constant_Pool_Array_Access; > begin > ... > u2'Read (Stream, Constant_Pool_Count); > Constant_Pool := new Constant_Pool_Array (1 .. Constant_Pool - 1); > Constant_Pool_Array'Read (Stream, Constant_Pool.all); > ... > > After reading all members, construct the result: > > declare > Result : Class_File > (constant_pool_count => Constant_Pool_Count, > interfaces_count => Interfaces_Count, > fields_count => Fields_Count, > methods_count => Methods_Count, > attributes_count => Attricutes_Count); > begin > Result.Constant_Pool := Constant_Pool.all; > ... > Free (Constant_Pool); > ... > return Result; > end; > end Input; > > You can also eliminate the use of access types and dynamic allocation > and deallocation by nesting declare blocks, e.g. > > u2'Read (Stream, Constant_Pool_Count); > declare > Constant_Pool : Constant_Pool_Array (1 .. Constant_Pool_Count); > begin > Constant_Pool_Array'Read (Stream, Constant_Pool); > ... > u2'Read (Stream, Interfaces_Count); > declare > Interfaces : Interfaces_Count_Array (1 .. Interfaces_Count); > begin > ... > end; > end; > > HTH > > -- > Ludovic Brenta.