comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: Address and bit mask
Date: Wed, 31 Aug 2011 01:35:53 -0700 (PDT)
Date: 2011-08-31T01:35:53-07:00	[thread overview]
Message-ID: <5485676d-59bb-40ca-ac9a-5b216511294b@d7g2000vbv.googlegroups.com> (raw)
In-Reply-To: 24db86cf-940c-4d95-b21d-ad819cd77142@t5g2000yqj.googlegroups.com

milouz wrote:
> On Aug 30, 5:14 pm, Adam Beneschan <a...@irvine.com> 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:
>
>   [ size |    data     ]
>
> The 'size' field is a 32-bit unsigned integer and it's the size of the
> whole chunk.
> The 'data' field has various lenght (>= 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 => File,
                               Mode => Ada.Streams.Stream_IO.In_Mode,
                               Name => 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 := 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 /= 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.



      reply	other threads:[~2011-08-31  8:35 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-29 15:46 Address and bit mask milouz
2011-08-29 16:06 ` Martin
2011-08-29 16:33   ` milouz
2011-08-29 17:47     ` Dmitry A. Kazakov
2011-08-29 17:54     ` Martin
2011-08-29 18:46     ` tmoran
2011-08-29 19:41 ` Ludovic Brenta
2011-08-29 19:54   ` Adam Beneschan
2011-08-30  9:14     ` milouz
2011-08-30 10:34       ` Ludovic Brenta
2011-08-30 10:58         ` Ludovic Brenta
2011-08-30 12:44           ` Georg Bauhaus
2011-08-30 14:04             ` Dmitry A. Kazakov
2011-08-30 16:12               ` Georg Bauhaus
2011-08-30 16:59                 ` Dmitry A. Kazakov
2011-08-30 14:52         ` Adam Beneschan
2011-08-30 10:40       ` Simon Wright
2011-08-30 10:44         ` Simon Wright
2011-08-30 15:20         ` tmoran
2011-08-30 16:08           ` milouz
2011-08-30 16:45             ` Georg Bauhaus
2011-08-30 19:31               ` Adam Beneschan
2011-08-30 19:56                 ` Dmitry A. Kazakov
2011-08-31  6:16                   ` The simple Image issue (was: Address and bit mask) Georg Bauhaus
2011-08-31 14:44                     ` The simple Image issue Dmitry A. Kazakov
2011-08-31 15:36                       ` Georg Bauhaus
2011-08-31 15:53                         ` Dmitry A. Kazakov
2011-08-31 16:23                           ` Georg Bauhaus
2011-08-31 16:27                             ` Dmitry A. Kazakov
2011-08-31 16:30                               ` Georg Bauhaus
2011-08-31 16:50                                 ` Dmitry A. Kazakov
2011-08-31 20:41                                   ` Georg Bauhaus
2011-08-31 21:17                                     ` Robert A Duff
2011-09-01  7:36                                       ` Dmitry A. Kazakov
2011-09-01  7:46                                     ` Dmitry A. Kazakov
2011-09-01  9:50                                       ` Overloading parentheses and type expectations (was: The simple Image issue) Georg Bauhaus
2011-09-02  7:54                                         ` Overloading parentheses and type expectations Dmitry A. Kazakov
2011-09-02 10:37                                           ` Georg Bauhaus
2011-09-02 12:40                                             ` Dmitry A. Kazakov
2011-09-02 16:08                                               ` Georg Bauhaus
2011-09-02 17:29                                                 ` Dmitry A. Kazakov
2011-08-31 15:53                     ` The simple Image issue Hyman Rosen
2011-08-31 16:07                       ` Dmitry A. Kazakov
2011-08-31 16:08                       ` Simon Wright
2011-08-31 16:26                         ` Dmitry A. Kazakov
2011-08-31 16:25                       ` Georg Bauhaus
2011-08-31 16:30                         ` Hyman Rosen
2011-08-31 16:34                           ` Georg Bauhaus
2011-08-31 16:43                             ` Adam Beneschan
2011-08-31 21:58                               ` Georg Bauhaus
2011-09-01  7:59                                 ` Dmitry A. Kazakov
2011-08-31 16:08                     ` The simple Image issue (was: Address and bit mask) Adam Beneschan
2011-08-31 16:53                       ` The simple Image issue Simon Wright
2011-08-31 17:02                         ` Hyman Rosen
2011-08-31 20:33                       ` Georg Bauhaus
2011-08-30 19:37             ` Address and bit mask Martin
2011-08-30 16:32           ` Simon Wright
2011-08-31  7:55             ` Ludovic Brenta
2011-08-30 12:35       ` Georg Bauhaus
2011-08-30 13:03       ` Georg Bauhaus
2011-08-30 15:14       ` Adam Beneschan
2011-08-30 15:59         ` Adam Beneschan
2011-08-31  7:45         ` milouz
2011-08-31  8:35           ` Ludovic Brenta [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox