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 Path: g2news1.google.com!postnews.google.com!k41g2000yqh.googlegroups.com!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Load an object from a file Date: Fri, 3 Apr 2009 06:37:46 -0700 (PDT) Organization: http://groups.google.com Message-ID: <443d72ca-5bbd-46a3-84c6-e8bd984e5b80@k41g2000yqh.googlegroups.com> References: <49d5fa88$0$2862$ba620e4c@news.skynet.be> NNTP-Posting-Host: 153.98.68.197 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1238765866 16654 127.0.0.1 (3 Apr 2009 13:37:46 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 3 Apr 2009 13:37:46 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: k41g2000yqh.googlegroups.com; posting-host=153.98.68.197; posting-account=pcLQNgkAAAD9TrXkhkIgiY6-MDtJjIlC User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:4419 Date: 2009-04-03T06:37:46-07:00 List-Id: Olivier Scalbert wrote on comp.lang.ada: > Hello everybody ! > > In my Ada self-study context, I was asking myself how can I create and > fill objects or records from a file. > As an example, I have tried to represent a java class file format > structure and fill it with a .class java file. > > The ClassFile structure is something like: > > =A0 =A0 =A0ClassFile { > =A0 =A0 =A0 =A0 u4 magic; > =A0 =A0 =A0 =A0 u2 minor_version; > =A0 =A0 =A0 =A0 u2 major_version; > =A0 =A0 =A0 =A0 u2 constant_pool_count; > =A0 =A0 =A0 =A0 cp_info constant_pool[constant_pool_count-1]; > =A0 =A0 =A0 =A0 u2 access_flags; > =A0 =A0 =A0 =A0 u2 this_class; > =A0 =A0 =A0 =A0 u2 super_class; > =A0 =A0 =A0 =A0 u2 interfaces_count; > =A0 =A0 =A0 =A0 u2 interfaces[interfaces_count]; > =A0 =A0 =A0 =A0 u2 fields_count; > =A0 =A0 =A0 =A0 field_info fields[fields_count]; > =A0 =A0 =A0 =A0 u2 methods_count; > =A0 =A0 =A0 =A0 method_info methods[methods_count]; > =A0 =A0 =A0 =A0 u2 attributes_count; > =A0 =A0 =A0 =A0 attribute_info attributes[attributes_count]; > =A0 =A0 =A0} > > JVM Specs can be found there:http://java.sun.com/docs/books/jvms/second_e= dition/html/ClassFile.doc... > > I have no problem to represent and to fill from file, the first four fiel= ds. > But I do not know what is the best (Ada) way of representing the array > of info constant_pool as the size is only known at run time.(=3D > constant_pool_cout). 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 :=3D 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 =3D> Constant_Pool_Count, interfaces_count =3D> Interfaces_Count, fields_count =3D> Fields_Count, methods_count =3D> Methods_Count, attributes_count =3D> Attricutes_Count); begin Result.Constant_Pool :=3D 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.