comp.lang.ada
 help / color / mirror / Atom feed
* Stream_io / records with default discriminants
@ 1999-02-14  0:00 Bernd Ragutt
  1999-02-14  0:00 ` David C. Hoos, Sr.
  1999-02-14  0:00 ` Matthew Heaney
  0 siblings, 2 replies; 9+ messages in thread
From: Bernd Ragutt @ 1999-02-14  0:00 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1426 bytes --]

Hello!

I am trying to understand writing to and reading from streams.

The Ada95 LRM 13.13.2 (26) states "If T has discriminants without (!)
defaults, S�Output ...".

I have no problems writing and reading records without (!) default
discriminants (using S'Output and S'Input).
But when trying to read a record with (!) a default discriminant (using
S'Output and S'Input), there is an exception.

What's wrong?

I do understand the LRM the following way:
For every subtype S of a specific type T, that is, for all (!) types,  with
and without default discriminants, writing and reading with S'Output and
S'Input should function.

Any comments?

Bernd Ragutt
from Munich
----------------------
BerndRagutt@csi.com


*** ***
Ada95 LRM 13.13.2 Stream-Oriented Attributes
...
25 Unless overridden by an attribute_definition_clause, ... [S'Input,
S'Output] ... execute as follows:
26 � If T is an array type, S�Output first writes the bounds, and S�Input
first reads the bounds.
If T has discriminants without defaults, S�Output first writes the
discriminants (using S�Write for each),
and S�Input first reads the discriminants (using S�Read for each).
27 � S�Output then calls S�Write to write the value of Item to the stream.
S�Input then creates an object (with the bounds or discriminants, if any,
taken from the stream),
initializes it with S�Read, and returns the value of the object.
*** ***






^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Stream_io / records with default discriminants
  1999-02-14  0:00 Stream_io / records with default discriminants Bernd Ragutt
  1999-02-14  0:00 ` David C. Hoos, Sr.
@ 1999-02-14  0:00 ` Matthew Heaney
  1999-02-14  0:00   ` Bernd Ragutt
  1 sibling, 1 reply; 9+ messages in thread
From: Matthew Heaney @ 1999-02-14  0:00 UTC (permalink / raw)


"Bernd Ragutt" <BerndRagutt@csi.com> writes:

> I have no problems writing and reading records without (!) default
> discriminants (using S'Output and S'Input).
> But when trying to read a record with (!) a default discriminant (using
> S'Output and S'Input), there is an exception.


You must post the smallest fragment of compilable code that illustrates
the behavior you observe.  Without code, we can only guess what is
going on.

Make sure that the object into which you are reading isn't constrained
in any way.  If the discriminant of the data in the stream doesn't match
the discriminant of your (constrained) object, then of course
Constraint_Error would occur.

But this is only a guess, because you haven't even told us which
exception you're getting.





^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Stream_io / records with default discriminants
  1999-02-14  0:00 Stream_io / records with default discriminants Bernd Ragutt
@ 1999-02-14  0:00 ` David C. Hoos, Sr.
  1999-02-14  0:00 ` Matthew Heaney
  1 sibling, 0 replies; 9+ messages in thread
From: David C. Hoos, Sr. @ 1999-02-14  0:00 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1162 bytes --]


Bernd Ragutt wrote in message ...
>Hello!
>
>I am trying to understand writing to and reading from streams.
>
>The Ada95 LRM 13.13.2 (26) states "If T has discriminants without (!)
>defaults, S�Output ...".
>
>I have no problems writing and reading records without (!) default
>discriminants (using S'Output and S'Input).
>But when trying to read a record with (!) a default discriminant (using
>S'Output and S'Input), there is an exception.
>
>What's wrong?
>
>I do understand the LRM the following way:
>For every subtype S of a specific type T, that is, for all (!) types,  with
>and without default discriminants, writing and reading with S'Output and
>S'Input should function.
>
If the type of the object into which you're reading has default
discriminant(s), then you should use a declare block within your
read loop, declaring an object of that type, but initialized by the
call to the S'input attribute.  In that way, the object is constrained
by its initialization, and each time through the loop a new object
is declared with the appropriate constrains -- thus permitting
heterogeneous files to be read.

Hope this helps,

David C. Hoos, Sr.








^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Stream_io / records with default discriminants
  1999-02-14  0:00 ` Matthew Heaney
@ 1999-02-14  0:00   ` Bernd Ragutt
  1999-02-14  0:00     ` robert_dewar
  1999-02-14  0:00     ` Nick Roberts
  0 siblings, 2 replies; 9+ messages in thread
From: Bernd Ragutt @ 1999-02-14  0:00 UTC (permalink / raw)



Matthew Heaney wrote...

>You must post the smallest fragment of compilable code that illustrates
>the behavior you observe.  Without code, we can only guess what is
>going on.

Here is the fragment of code.
The code runs with GNAT/NT; with Apex/AIX  I get an exception (constraint
error).

Thanks.

Bernd Ragutt
from Munich
----------------------
BerndRagutt@csi.com



###################

--  ************ Types
    ...
    subtype Lstring_Length is Natural range 0 .. 25;

    type Lstring (Len : Lstring_Length := 0) is
        record
            Str : String (1 .. Len) := (others => ' ');
        end record;

    type Test_Enumeration is (Eins, Zwei, Drei);

    type Test_Records
            (Indicator : Test_Enumeration := Test_Enumeration'First) is
        record
            case Indicator is
                when Eins =>
                    Eins_Int : Integer := 1;
                when Zwei =>
                    Zwei_String : Lstring;
                when Drei =>
                    Drei_Int : Integer := 3;
                    Drei_String : Lstring;
            end case;
        end record;
    ...
--  ***********
    ...
    function Input_From_Stream return Test_Records is
    begin
        return Test_Records'Input (Date_File_Stream);
    exception
        when Ada.Streams.Stream_Io.End_Error =>
            raise Eof;
    end Input_From_Stream;
    ...
--  ************

with Text_Io;
with Ada.Io_Exceptions;
with Ada.Tags;

procedure Record_Io.Test2 is

    use Ada;

    Test_Record1 : Test_Records (Indicator => Eins);
    Test_Record2 : Test_Records := (Eins, 321);

    Test_Record3 : Test_Records :=
       (Indicator => Drei, Drei_Int => 999, Drei_String => (4, "AbcD"));

    Test_Record4 : Test_Records :=
       (Indicator => Zwei, Zwei_String => (5, "vWXYz"));

begin
    Open_Stream;

    Output_To_Stream (Test_Record1);
    Output_To_Stream (Test_Record1);
    Output_To_Stream (Test_Record2);
    Output_To_Stream (Test_Record3);
    Output_To_Stream (Test_Record4);

    Reset_To_Instream;

    loop
        declare
        begin
            -- ***
     declare
                Readed_Record : Test_Records
                      := Input_From_Stream;
                     --################################
                      -- Constraint Error when reading Test_Record2
            begin
                Text_Io.Put_Line
                   (Test_Enumeration'Image (Readed_Record.Indicator) & "
readed");
            exception
                when Eof =>
                    Text_Io.Put_Line ("EoF!");
                    exit;
            end;
     -- ***
        exception
         ...
        end;
    end loop;
end Record_Io.Test2;










^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Stream_io / records with default discriminants
  1999-02-14  0:00   ` Bernd Ragutt
@ 1999-02-14  0:00     ` robert_dewar
  1999-02-14  0:00     ` Nick Roberts
  1 sibling, 0 replies; 9+ messages in thread
From: robert_dewar @ 1999-02-14  0:00 UTC (permalink / raw)


In article <#8nPq9DW#GA.131@nih2naac.prod2.compuserve.com>,
  "Bernd Ragutt" <BerndRagutt@csi.com> wrote:
> Here is the fragment of code.
> The code runs with GNAT/NT; with Apex/AIX  I get an
> exception (constraint error).

I see no reason why this code should generate a
constraint_error. It is certainly tricky to get the
implementation right in this area, and I could be
mistaken, but it looks perfectly straightforward to
me that this should work correctly.

Robert Dewar

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Stream_io / records with default discriminants
  1999-02-14  0:00   ` Bernd Ragutt
  1999-02-14  0:00     ` robert_dewar
@ 1999-02-14  0:00     ` Nick Roberts
  1999-02-15  0:00       ` robert_dewar
  1 sibling, 1 reply; 9+ messages in thread
From: Nick Roberts @ 1999-02-14  0:00 UTC (permalink / raw)


Bernd,

I think your problem is this: when you declare Test_Record1, it is
constrained but not initialised. The Eins_Int component within this record
can, therefore, be any value. You then (presumably) write out this record
into a file, and then read it in again. On the AIX, I guess, it just so
happens that an invalid value is written out, and then detected upon being
read back in (raising Constraint_Error).

Let us know if this doesn't solve your problem.

-------------------------------------------
Nick Roberts

The meek shall inherit the Earth. And then
pay the capital transfer tax on it in full.
-------------------------------------------







^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Stream_io / records with default discriminants
  1999-02-14  0:00     ` Nick Roberts
@ 1999-02-15  0:00       ` robert_dewar
  1999-02-17  0:00         ` Nick Roberts
  0 siblings, 1 reply; 9+ messages in thread
From: robert_dewar @ 1999-02-15  0:00 UTC (permalink / raw)


In article <7a7fbd$t50$1@plug.news.pipex.net>,
  "Nick Roberts" <Nick.Roberts@dial.pipex.com> wrote:
> Bernd,
>
> I think your problem is this: when you declare
> Test_Record1, it is constrained but not initialised.

This is completely wrong, I am not sure why Nick is getting
so confused over this. Of course it is initialized, why
would you ever think it wasn't?

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Stream_io / records with default discriminants
  1999-02-15  0:00       ` robert_dewar
@ 1999-02-17  0:00         ` Nick Roberts
  1999-02-18  0:00           ` dennison
  0 siblings, 1 reply; 9+ messages in thread
From: Nick Roberts @ 1999-02-17  0:00 UTC (permalink / raw)


robert_dewar@my-dejanews.com wrote in message
<7a873a$kon$1@nnrp1.dejanews.com>...
|In article <7a7fbd$t50$1@plug.news.pipex.net>,
|  "Nick Roberts" <Nick.Roberts@dial.pipex.com> wrote:
|> Bernd,
|>
|> I think your problem is this: when you declare
|> Test_Record1, it is constrained but not initialised.
|
|This is completely wrong,

Robert (and Bernd in an e-mail) is absolutely right. Test_Record_1 is
initialised. My apologies.

|I am not sure why Nick is getting
|so confused over this. Of course it is initialized, why
|would you ever think it wasn't?

Eyesight's going ;-) I simply didn't see the ":= 1" initialisation.

Maybe it is an implementation problem. Bernd, you might like to try using

   with Ada.Exceptions; use Ada.Exceptions;

and then

            exception
                when Eof =>
                    Text_Io.Put_Line ("EoF!");
                    exit;
                when Error: Constraint_Error =>
                    Text_IO.Put_Line(Exception_Information(Error));
                    exit; -- maybe
            end;

in order to get some more information on what the compiler thinks is wrong.

Also, I have an RM95 question: where, in the RM, does it say that a
component_definition must be definite? (Or does it not have to be
definite?!)

Again, my apologies for any confusion.


-------------------------------------
Nick Roberts

'The time has come,' the Walrus said,
  'To talk of many things:
Of shoes--of ships--and sealing wax--
  Of cabbages--and kings--
And why the sea is boiling hot--
  And whether pigs have wings.'
                        Lewis Carroll
          "Through the Looking Glass"
-------------------------------------







^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Stream_io / records with default discriminants
  1999-02-17  0:00         ` Nick Roberts
@ 1999-02-18  0:00           ` dennison
  0 siblings, 0 replies; 9+ messages in thread
From: dennison @ 1999-02-18  0:00 UTC (permalink / raw)


In article <7afc1n$3mi$1@plug.news.pipex.net>,
  "Nick Roberts" <Nick.Roberts@dial.pipex.com> wrote:
> robert_dewar@my-dejanews.com wrote in message

> Maybe it is an implementation problem. Bernd, you might like to try using
>
>    with Ada.Exceptions; use Ada.Exceptions;
>
> and then
>
>             exception
>                 when Eof =>
>                     Text_Io.Put_Line ("EoF!");
>                     exit;
>                 when Error: Constraint_Error =>
>                     Text_IO.Put_Line(Exception_Information(Error));
>                     exit; -- maybe
>             end;
>
> in order to get some more information on what the compiler thinks is wrong.

wouldn't:

   exception
      when Error: others =>
         Text_IO.Put_Line (Exception_Information(Error));
         raise;
   end;

...do the same thing? Exception_Information "should" include the exception
name.

T.E.D.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~1999-02-18  0:00 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-02-14  0:00 Stream_io / records with default discriminants Bernd Ragutt
1999-02-14  0:00 ` David C. Hoos, Sr.
1999-02-14  0:00 ` Matthew Heaney
1999-02-14  0:00   ` Bernd Ragutt
1999-02-14  0:00     ` robert_dewar
1999-02-14  0:00     ` Nick Roberts
1999-02-15  0:00       ` robert_dewar
1999-02-17  0:00         ` Nick Roberts
1999-02-18  0:00           ` dennison

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