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: a07f3367d7,826cd690cb6a7585 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!postnews.google.com!d7g2000vbv.googlegroups.com!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Address and bit mask Date: Wed, 31 Aug 2011 01:35:53 -0700 (PDT) Organization: http://groups.google.com Message-ID: <5485676d-59bb-40ca-ac9a-5b216511294b@d7g2000vbv.googlegroups.com> References: <71159ccc-bf20-4fcf-a7f1-3b90629c1ecb@l4g2000vbv.googlegroups.com> <87fwkk0zv7.fsf@ludovic-brenta.org> <7d70b603-a03d-456a-b775-1325eac83aa0@t30g2000prm.googlegroups.com> <24db86cf-940c-4d95-b21d-ad819cd77142@t5g2000yqj.googlegroups.com> 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 1314780129 24489 127.0.0.1 (31 Aug 2011 08:42:09 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 31 Aug 2011 08:42:09 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: d7g2000vbv.googlegroups.com; posting-host=153.98.68.197; posting-account=pcLQNgkAAAD9TrXkhkIgiY6-MDtJjIlC User-Agent: G2/1.0 X-HTTP-Via: ICAP/1.0 192.168.152.2 X-Google-Web-Client: true X-Google-Header-Order: HUALESRCVNK X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Red Hat/3.6-2.el5 Firefox/3.6.13,gzip(gfe) Xref: g2news2.google.com comp.lang.ada:21741 Date: 2011-08-31T01:35:53-07:00 List-Id: milouz wrote: > On Aug 30, 5:14=A0pm, Adam Beneschan wrote: > >> If the reason you want to do address arithmetic is something else, >> though, you're welcome to ask about it, but rather than giving us C >> code with no context and asking us how to translate it, please give us >> a bigger-picture description of what you're trying to accomplish > > Ok, so I'll try to be more precise. > I have to parse a list of chunks which are 4 bytes aligned. Each chunk > is like this: > > =A0 [ size | =A0 =A0data =A0 =A0 ] > > The 'size' field is a 32-bit unsigned integer and it's the size of the > whole chunk. > The 'data' field has various lenght (>=3D 1 byte). > > I can solve that problem in a C-ish style, but I wonder what is the > proper way to solve that problem in Ada. If the chunks are contiguous in memory (or, better yet, in a file), my first reaction would be to construct a stream and then use Interfaces.Unsigned_32'Read to read the sizes and Interfaces.Unsigned_X'Read to read the data. Something like: with Ada.Streams.Stream_IO; with Interfaces; procedure Parse (File_Name : in String) is File : Ada.Streams.Stream_IO.File_Type; begin Ada.Streams.Stream_IO.Open (File =3D> File, Mode =3D> Ada.Streams.Stream_IO.In_Mode, Name =3D> File_Name); Parse (Ada.Streams.Stream_IO.Stream (File)); end Parse; procedure Parse (Stream : in Ada.Streams.Stream_IO.Stream_Access) is Length : Interfaces.Unsigned_32; Alignment : constant :=3D 4; Padding_Byte : Interfaces.Unsigned_8; begin Interfaces.Unsigned_32'Read (Stream, Length); declare Data : Ada.Streams.Stream_Element_Array (1 .. Ada.Streams.Stream_Element_Count (Length)); Last : Ada.Streams.Stream_Element_Offset; begin Ada.Streams.Read (Stream, Data, Last); if Last < Length then raise Program_Error with "not enough data in the stream"; end if; -- process Data here end; -- Now read as many padding bytes as necessary to reach the next alignment -- boundary if Length mod Alignment /=3D 0 then for J in 1 .. Alignment - Length mod Alignment loop Interfaces.Unsigned_8'Read (Stream, Padding_Byte); end loop; end if; end Parse; -- Ludovic Brenta. The Chief Legal Officer globally achieves the industry-standard baseline starting point.