* Read booleans from streams
@ 2002-03-24 23:18 Erik Sigra
2002-03-25 3:57 ` Eric G. Miller
0 siblings, 1 reply; 6+ messages in thread
From: Erik Sigra @ 2002-03-24 23:18 UTC (permalink / raw)
I tried to read bytes from a stream and interpret each bit as a Boolean. But
it did not work as expected. It reads 1 byte for each bit. How should I do
instead?
streamtest.adb
==============
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Streams; use Ada.Streams;
with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
procedure Streamtest is
type Boolean_Aggregate is array (0 .. 7) of Boolean;
for Boolean_Aggregate'Component_Size use 1;
Current_Byte : Boolean_Aggregate;
The_File : Stream_IO.File_Type;
The_Stream : Stream_Access;
begin
Open (The_File, In_File, "data");
The_Stream := Stream (The_File);
loop
Boolean_Aggregate'Read (The_Stream, Current_Byte);
for I in Boolean_Aggregate'Range loop
Put(Current_Byte (I)'Img & ' ');
end loop;
New_Line;
end loop;
end Streamtest;
data
====
02027203
user > ./streamtest
FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
raised ADA.IO_EXCEPTIONS.END_ERROR : s-stratt.adb:170
I have checked that the size of Boolean_Aggregate is 8 and not 64.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Read booleans from streams
[not found] <200203242318.g2ONIkc12876@a77.ib.student.liu.se>
@ 2002-03-25 2:05 ` sk
[not found] ` <3C9E85F6.8EDF3AAD@myob.com>
1 sibling, 0 replies; 6+ messages in thread
From: sk @ 2002-03-25 2:05 UTC (permalink / raw)
Hi,
>raised ADA.IO_EXCEPTIONS.END_ERROR : s-stratt.adb:170
Your loop doesn't do an EOF test.
Your other issue ?
>data
>====
>02027203
Which looks like
0000_0000,0000_0010,0000_0000,0000_0010,
^ ^ ^ ^
0 0 0 0
0000_0111,0000_0010,0000_0000,0000_0011
^ ^ ^ ^
1 0 0 1
And your results look like
0 0 0 0 1 0 0 1
Interesting, I havn't checked it but how about
changing
"for Boolean_Aggregate'Component_Size use 1;"
to
"pragma Pack (Boolean_Aggregate);" and if
that doesn't work, try
"for Boolean_Aggregate'Component_Size use 1;"
"for Boolean_Aggregate'Size use 8";
if
"for Boolean_Aggregate'Size use 8";
doesn't work by itself.
--
-------------------------------------
-- Merge vertically for real address
-------------------------------------
s n p @ t . o
k i e k c c m
-------------------------------------
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Read booleans from streams
2002-03-24 23:18 Read booleans from streams Erik Sigra
@ 2002-03-25 3:57 ` Eric G. Miller
2002-03-25 11:21 ` Erik Sigra
[not found] ` <200203251121.g2PBL9401018@a77.ib.student.liu.se>
0 siblings, 2 replies; 6+ messages in thread
From: Eric G. Miller @ 2002-03-25 3:57 UTC (permalink / raw)
In <mailman.1017011702.18089.comp.lang.ada@ada.eu.org>, Erik Sigra wrote:
> I tried to read bytes from a stream and interpret each bit as a Boolean. But
> it did not work as expected. It reads 1 byte for each bit. How should I do
> instead?
>
>
> streamtest.adb
> ==============
> with Ada.Text_IO; use Ada.Text_IO;
> with Ada.Streams; use Ada.Streams;
> with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
>
> procedure Streamtest is
> type Boolean_Aggregate is array (0 .. 7) of Boolean;
> for Boolean_Aggregate'Component_Size use 1;
>
> Current_Byte : Boolean_Aggregate;
>
> The_File : Stream_IO.File_Type;
>
> The_Stream : Stream_Access;
>
> begin
>
> Open (The_File, In_File, "data");
>
> The_Stream := Stream (The_File);
>
> loop
> Boolean_Aggregate'Read (The_Stream, Current_Byte);
> for I in Boolean_Aggregate'Range loop
> Put(Current_Byte (I)'Img & ' ');
> end loop;
> New_Line;
> end loop;
> end Streamtest;
>
>
> data
> ====
> 02027203
>
>
> user > ./streamtest
> FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
>
> raised ADA.IO_EXCEPTIONS.END_ERROR : s-stratt.adb:170
>
>
> I have checked that the size of Boolean_Aggregate is 8 and not 64.
I think the problem is in the read procedure. It sees an array of 8 elements,
so wants to read 8 bytes. I think you need a 'Read wrapper to read a single
byte (type Byte is mod 2**8), and then do an Unchecked_Conversion:
function To_Boolean_Aggregate is new
Ada.Unchecked_Conversion(Byte,Boolean_Aggregate);
procedure Read_Boolean_Aggregate
(Stream : Stream_Access; Data : in out Boolean_Aggregate) is
Item : Byte;
begin
Byte'Read (Stream, Item);
Data := To_Boolean_Aggregate (Item);
end Read_Boolean_Aggregate;
...
while not End_Of_File (File_Type) loop
Read_Boolean_Aggregate (Stream, Data);
...
end loop;
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Read booleans from streams
[not found] ` <3C9E85F6.8EDF3AAD@myob.com>
@ 2002-03-25 10:46 ` Erik Sigra
0 siblings, 0 replies; 6+ messages in thread
From: Erik Sigra @ 2002-03-25 10:46 UTC (permalink / raw)
Thanks for your reply!
m�ndagen den 25 mars 2002 03.05 skrev du:
> >raised ADA.IO_EXCEPTIONS.END_ERROR : s-stratt.adb:170
>
> Your loop doesn't do an EOF test.
That was expected. It was the reading I wanted to investigate, so I didn't
bother with an EOF test.
> >data
> >====
> >02027203
>
> Which looks like
>
> 0000_0000,0000_0010,0000_0000,0000_0010,
> ^ ^ ^ ^
> 0 0 0 0
>
> 0000_0111,0000_0010,0000_0000,0000_0011
> ^ ^ ^ ^
> 1 0 0 1
>
>
> And your results look like
>
> 0 0 0 0 1 0 0 1
>
> Interesting, I havn't checked it but how about
> changing
Actually it were the characters '0', '2' and '7' that were stored in the
file, so it was rather
0001_1000,0001_1010,0001_1000,0001_1010,
^ ^ ^ ^
0 0 0 0
0001_1111,0001_1010,0001_1000,0001_1011
^ ^ ^ ^
1 0 0 1
but that's essentially the same in this matter.
>
> "for Boolean_Aggregate'Component_Size use 1;"
>
> to
>
> "pragma Pack (Boolean_Aggregate);" and if
> that doesn't work, try
I already tried that.
>
> "for Boolean_Aggregate'Component_Size use 1;"
> "for Boolean_Aggregate'Size use 8";
And I tried that too. I even used the test
Put_Line ("Boolean_Aggregate'Size = " & Boolean_Aggregate'Size'Img);
in the program and it showed "Boolean_Aggregate'Size = 8". I use gnat-3.13p.
Any other suggestions?
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Read booleans from streams
2002-03-25 3:57 ` Eric G. Miller
@ 2002-03-25 11:21 ` Erik Sigra
[not found] ` <200203251121.g2PBL9401018@a77.ib.student.liu.se>
1 sibling, 0 replies; 6+ messages in thread
From: Erik Sigra @ 2002-03-25 11:21 UTC (permalink / raw)
m�ndagen den 25 mars 2002 04.57 skrev du:
> I think the problem is in the read procedure. It sees an array of 8
> elements, so wants to read 8 bytes. I think you need a 'Read wrapper to
> read a single byte (type Byte is mod 2**8), and then do an
> Unchecked_Conversion:
>
> function To_Boolean_Aggregate is new
> Ada.Unchecked_Conversion(Byte,Boolean_Aggregate);
>
> procedure Read_Boolean_Aggregate
> (Stream : Stream_Access; Data : in out Boolean_Aggregate) is
>
> Item : Byte;
> begin
> Byte'Read (Stream, Item);
> Data := To_Boolean_Aggregate (Item);
> end Read_Boolean_Aggregate;
>
> ...
>
> while not End_Of_File (File_Type) loop
> Read_Boolean_Aggregate (Stream, Data);
>
> ...
> end loop;
Thanks, it works!
The only problem is that it seems impossible to state
"for Boolean_Aggregate'Read use Boolean_Aggregate_Read;".
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Read booleans from streams
[not found] ` <200203251121.g2PBL9401018@a77.ib.student.liu.se>
@ 2002-03-25 16:00 ` sk
0 siblings, 0 replies; 6+ messages in thread
From: sk @ 2002-03-25 16:00 UTC (permalink / raw)
Hi,
>Actually it were the characters '0', '2' and '7'
>that were stored in the file, so it was rather
>
>0001_1000,0001_1010,0001_1000,0001_1010,
> ^ ^ ^ ^
> 0 0 0 0
>0001_1111,0001_1010,0001_1000,0001_1011
> ^ ^ ^ ^
> 1 0 0 1
Yes, stupid me, CHARACTER not byte, doh ! (Kind
of lucky that there was any correspondence at all :-).
>The only problem is that it seems impossible to state
>"for Boolean_Aggregate'Read use Boolean_Aggregate_Read;".
Have you tried :-
procedure Read_Boolean_Aggregate ( ...);
-- SPEC ONLY
for Boolean_Aggregate'Read use Boolean_Aggregate_Read;".
-- FULL BODY
procedure Read_Boolean_Aggregate (...) is
begin
...
--
-------------------------------------
-- Merge vertically for real address
-------------------------------------
s n p @ t . o
k i e k c c m
-------------------------------------
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2002-03-25 16:00 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-03-24 23:18 Read booleans from streams Erik Sigra
2002-03-25 3:57 ` Eric G. Miller
2002-03-25 11:21 ` Erik Sigra
[not found] ` <200203251121.g2PBL9401018@a77.ib.student.liu.se>
2002-03-25 16:00 ` sk
[not found] <200203242318.g2ONIkc12876@a77.ib.student.liu.se>
2002-03-25 2:05 ` sk
[not found] ` <3C9E85F6.8EDF3AAD@myob.com>
2002-03-25 10:46 ` Erik Sigra
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox