comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <Stephen.Leake@gsfc.nasa.gov>
Subject: 16 bit integers in streams
Date: 1997/09/25
Date: 1997-09-25T00:00:00+00:00	[thread overview]
Message-ID: <342A57CC.2553@gsfc.nasa.gov> (raw)


I'm trying to use streams to read Microsoft Windows resources into Ada
types, so I can manipulate them. I've run into a problem reading 16 bit
integers. ObjectAda 7.1 under Windows 95 reads and writes four bytes (32
bits) to a stream for a 16 bit integer. GNAT 3.10 under Windows 95
writes two bytes to a stream for a 16 bit integer.

Here's code that demonstrates this:

with Ada.Streams; use Ada.Streams;
package Test_Streams is
   type Stream_Type is new Root_Stream_Type with null record;

   procedure Write
     (Stream : in out Stream_Type;
      Item   : in Stream_Element_Array);

   procedure Read
     (Stream : in out Stream_Type;
      Item   : out Stream_Element_Array;
      Last   : out Stream_Element_Offset);

end Test_Streams;

-----------------
with Ada.Text_IO; use Ada.Text_IO;
package body Test_Streams is

   procedure Put (Item : in Stream_Element_Array)
   is begin
      Put ("(");
      for I in Item'First .. Item'Last - 1 loop
         Put (Stream_Element'Image (Item (I)) & ", ");
      end loop;
      Put (Stream_Element'Image (Item (Item'last)) & ")");
   end Put;

   procedure Write
     (Stream : in out Stream_Type;
      Item   : in Stream_Element_Array)
   is begin
      Put (Item);
   end Write;

   -- dummy definition to override abstract Ada.Streams.Read
   procedure Read
     (Stream : in out Stream_Type;
      Item   : out Stream_Element_Array;
      Last   : out Stream_Element_Offset)
   is begin
      Item := (others => 1);
      Last := 0;
   end Read;

end Test_Streams;

-----------------
with Interfaces;
with Ada.Text_IO; use Ada.Text_IO;
with Test_Streams;
procedure Test
is
   Stream : aliased Test_Streams.Stream_Type;
begin
   Put_Line ("write 1 as 16 bit integer");
   Interfaces.Integer_16'Write (Stream'access,
Interfaces.Integer_16'(1));
end Test;

Compiled with GNAT 3.10 on Windows 95, then run, we get:

write 1 as 16 bit integer
( 1,  0)

This writes two bytes, as expected.

Compiled with ObjectAda 7.1 on Windows 95, then run, we get:

write 1 as 16 bit integer
( 1,  0,  0,  0)

This writes 4 bytes, or 32 bits.

This difference in behavior is allowed by RM 13.13.2 (9): "For
elementary types, the representation in terms of stream elements is
implementation defined." But it prevents using ObjectAda to read a
stream file written by GNAT. It also prevents reading Windows resources
directly. Here's an attempt to force ObjectAda to read a 16 bit integer:

with Ada.Streams;
package Interface_Streams is

   type Integer_16 is range -32768 .. 32767;
   for Integer_16'Size use 16;

   procedure Read_16
      (Stream : access Ada.Streams.Root_Stream_Type'Class;
       Item : out Integer_16);

   subtype Integer_16_Base is Integer_16'base;
   for Integer_16_Base'Read use Read_16;

   procedure Write_16
      (Stream : access Ada.Streams.Root_Stream_Type'Class;
       Item : in Integer_16);

   for Integer_16'Write use Write_16;

end Interface_Streams;

For both attribute definition clauses, ObjectAda 7.1 gives the error:

interface_streams.ads: Error: line 18 col 33 LRM:13.13.2(36), For an
attribute definition clause specifying a stream attribute for a scalar
type, the subtype  for the Item parameter must be the base subtype

Apparently ObjectAda uses a 32 bit base integer type, even when only 16
bits are requested. What is the rationale for this? Is it legal for
ObjectAda to reject these attribute definition clauses? What is the base
type of Integer_16?

I hope this issue can be addressed by the Uniformity Rapporteur Group;
it would be very nice if streams generated by one compiler/run-time
could be read by another. It would also be nice if a 16 bit integer
would read and write just 16 bits.

I just looked (on AdaHome) for the commentaries produced by the URG; I
found a bunch of AI's, but no UI's. Can someone point me to where the
URG output is?

-- 
- Stephe




             reply	other threads:[~1997-09-25  0:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-09-25  0:00 Stephen Leake [this message]
1997-09-25  0:00 ` 16 bit integers in streams Tucker Taft
1997-09-25  0:00   ` Stephen Leake
1997-09-25  0:00     ` Tucker Taft
1997-09-25  0:00       ` Stephen Leake
1997-09-26  0:00         ` Tucker Taft
1997-09-26  0:00           ` Stephen Leake
replies disabled

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