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!news3.google.com!feeder.news-service.com!de-l.enfer-du-nord.net!news.weisnix.org!citadel.nobulus.com!fi.sn.net!newsfeed1.fi.sn.net!news.song.fi!not-for-mail Date: Fri, 03 Apr 2009 16:07:57 +0300 From: Niklas Holsti User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) Gecko/20060628 Debian/1.7.8-1sarge7.1 X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Load an object from a file References: <49d5fa88$0$2862$ba620e4c@news.skynet.be> In-Reply-To: <49d5fa88$0$2862$ba620e4c@news.skynet.be> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <49d60a26$0$22051$4f793bc4@news.tdc.fi> Organization: TDC Internet Services NNTP-Posting-Host: 81.17.205.61 X-Trace: 1238764070 news.tdc.fi 22051 81.17.205.61:32793 X-Complaints-To: abuse@tdcnet.fi Xref: g2news1.google.com comp.lang.ada:4418 Date: 2009-04-03T16:07:57+03:00 List-Id: Olivier Scalbert wrote: > 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: > > ClassFile { > u4 magic; > u2 minor_version; > u2 major_version; > u2 constant_pool_count; > cp_info constant_pool[constant_pool_count-1]; > u2 access_flags; > u2 this_class; > u2 super_class; > u2 interfaces_count; > u2 interfaces[interfaces_count]; > u2 fields_count; > field_info fields[fields_count]; > u2 methods_count; > method_info methods[methods_count]; > u2 attributes_count; > attribute_info attributes[attributes_count]; > } > > > JVM Specs can be found there: > http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html > > I have no problem to represent and to fill from file, the first four > fields. > 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.(= > constant_pool_cout). You could use suitable container structures, for example Ada.Containers.Vectors. That would hide the storage management from your problem space. There are two more basic methods: 1. Make the size components discriminants of the record, not ordinary components: type Constant_Pool_T is array (Positive range <>) of cp_info; ... etc types for the other arrays. type Class_File_T ( Constant_Pool_Count : u2; Interfaces_Count : u2; Fields_Count : u2; Attributes_Count : u2) is record ... Constant_Pool : Constant_Pool_T (1 .. Constant_Pool_Count); ... Attributes : Attributes_T (1 .. Attributes_Count); end record; In this method, you must know the sizes of the arrays before you can create the Class_File_T object. This could be hard in your case -- I assume you must read the components from a file in the order given in the record type, so you would have to read all constant-pool items before reading interfaces_count, for example. In this method it is more convenient to index the arrays starting from 1, not 0, because you cannot do arithmetic on the discriminants to define the array bounds; the "-1" is forbidden here: type Class_File_T ( ... ) is record ... Constant_Pool : Constant_Pool_T (0 .. Constant_Pool_Count - 1); ... end record; 2. Use heap allocation and access types: type Constant_Pool_T is array Natural range <>) of cp_info; type Constant_Pool_Ref is access Constant_Pool_T; type Class_File_T is record ... Constant_Pool : Constant_Pool_Ref; ... Attributes : Attributes_Ref; end record; In this method, you can create the Class_File_T object before you know the array sizes. The components Constant_Pool etc. will initially be null. When you know the size, you can create arrays of the right size on the heap: CF.Constant_Pool := new Constant_Pool_T (0 .. constant_pool_count - 1); HTH, -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .