File : s-stratt.adb


     ------------------------------------------------------------------------------
     --                                                                          --
     --                         GNAT RUNTIME COMPONENTS                          --
     --                                                                          --
   5 --             S Y S T E M . S T R E A M _ A T T R I B U T E S              --
     --                                                                          --
     --                                 B o d y                                  --
     --                                                                          --
     --                            $Revision: 1.7 $                              --
  10 --                                                                          --
     --          Copyright (C) 1992-1998, Free Software Foundation, Inc.         --
     --                                                                          --
     -- GNAT is free software;  you can  redistribute it  and/or modify it under --
     -- terms of the  GNU General Public License as published  by the Free Soft- --
  15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
     -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
     -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
     -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
     -- for  more details.  You should have  received  a copy of the GNU General --
  20 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
     -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
     -- MA 02111-1307, USA.                                                      --
     --                                                                          --
     -- As a special exception,  if other files  instantiate  generics from this --
  25 -- unit, or you link  this unit with other files  to produce an executable, --
     -- this  unit  does not  by itself cause  the resulting  executable  to  be --
     -- covered  by the  GNU  General  Public  License.  This exception does not --
     -- however invalidate  any other reasons why  the executable file  might be --
     -- covered by the  GNU Public License.                                      --
  30 --                                                                          --
     -- GNAT was originally developed  by the GNAT team at  New York University. --
     -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
     --                                                                          --
     ------------------------------------------------------------------------------
  35 
     with Ada.IO_Exceptions;
     with Ada.Streams; use Ada.Streams;
     with Unchecked_Conversion;
     
  40 package body System.Stream_Attributes is
     
        Err : exception renames Ada.IO_Exceptions.End_Error;
        --  Exception raised if insufficient data read (note that the RM implies
        --  that Data_Error might be the appropriate choice, but AI195-00132
  45    --  decides with a binding interpretation that End_Error is preferred).
     
        SU : constant := System.Storage_Unit;
     
        subtype SEA is Ada.Streams.Stream_Element_Array;
  50    subtype SEO is Ada.Streams.Stream_Element_Offset;
     
        generic function UC renames Unchecked_Conversion;
     
        --  Subtypes used to define Stream_Element_Array values that map
  55    --  into the elementary types, using unchecked conversion.
     
        Thin_Pointer_Size : constant := System.Address'Size;
        Fat_Pointer_Size  : constant := System.Address'Size * 2;
     
  60    subtype S_AD  is SEA (1 .. (Fat_Pointer_Size              + SU - 1) / SU);
        subtype S_AS  is SEA (1 .. (Thin_Pointer_Size             + SU - 1) / SU);
        subtype S_B   is SEA (1 .. (Boolean'Size                  + SU - 1) / SU);
        subtype S_C   is SEA (1 .. (Character'Size                + SU - 1) / SU);
        subtype S_F   is SEA (1 .. (Float'Size                    + SU - 1) / SU);
  65    subtype S_I   is SEA (1 .. (Integer'Size                  + SU - 1) / SU);
        subtype S_LF  is SEA (1 .. (Long_Float'Size               + SU - 1) / SU);
        subtype S_LI  is SEA (1 .. (Long_Integer'Size             + SU - 1) / SU);
        subtype S_LLF is SEA (1 .. (Long_Long_Float'Size          + SU - 1) / SU);
        subtype S_LLI is SEA (1 .. (Long_Long_Integer'Size        + SU - 1) / SU);
  70    subtype S_LLU is SEA (1 .. (UST.Long_Long_Unsigned'Size   + SU - 1) / SU);
        subtype S_LU  is SEA (1 .. (UST.Long_Unsigned'Size        + SU - 1) / SU);
        subtype S_SF  is SEA (1 .. (Short_Float'Size              + SU - 1) / SU);
        subtype S_SI  is SEA (1 .. (Short_Integer'Size            + SU - 1) / SU);
        subtype S_SSI is SEA (1 .. (Short_Short_Integer'Size      + SU - 1) / SU);
  75    subtype S_SSU is SEA (1 .. (UST.Short_Short_Unsigned'Size + SU - 1) / SU);
        subtype S_SU  is SEA (1 .. (UST.Short_Unsigned'Size       + SU - 1) / SU);
        subtype S_U   is SEA (1 .. (UST.Unsigned'Size             + SU - 1) / SU);
        subtype S_WC  is SEA (1 .. (Wide_Character'Size           + SU - 1) / SU);
     
  80    --  Unchecked conversions from the elementary type to the stream type
     
        function From_AD  is new UC (Fat_Pointer,              S_AD);
        function From_AS  is new UC (Thin_Pointer,             S_AS);
        function From_C   is new UC (Character,                S_C);
  85    function From_F   is new UC (Float,                    S_F);
        function From_I   is new UC (Integer,                  S_I);
        function From_LF  is new UC (Long_Float,               S_LF);
        function From_LI  is new UC (Long_Integer,             S_LI);
        function From_LLF is new UC (Long_Long_Float,          S_LLF);
  90    function From_LLI is new UC (Long_Long_Integer,        S_LLI);
        function From_LLU is new UC (UST.Long_Long_Unsigned,   S_LLU);
        function From_LU  is new UC (UST.Long_Unsigned,        S_LU);
        function From_SF  is new UC (Short_Float,              S_SF);
        function From_SI  is new UC (Short_Integer,            S_SI);
  95    function From_SSI is new UC (Short_Short_Integer,      S_SSI);
        function From_SSU is new UC (UST.Short_Short_Unsigned, S_SSU);
        function From_SU  is new UC (UST.Short_Unsigned,       S_SU);
        function From_U   is new UC (UST.Unsigned,             S_U);
        function From_WC  is new UC (Wide_Character,           S_WC);
 100 
        --  Unchecked conversions from the stream type to elementary type
     
        function To_AD  is new UC (S_AD,  Fat_Pointer);
        function To_AS  is new UC (S_AS,  Thin_Pointer);
 105    function To_C   is new UC (S_C,   Character);
        function To_F   is new UC (S_F,   Float);
        function To_I   is new UC (S_I,   Integer);
        function To_LF  is new UC (S_LF,  Long_Float);
        function To_LI  is new UC (S_LI,  Long_Integer);
 110    function To_LLF is new UC (S_LLF, Long_Long_Float);
        function To_LLI is new UC (S_LLI, Long_Long_Integer);
        function To_LLU is new UC (S_LLU, UST.Long_Long_Unsigned);
        function To_LU  is new UC (S_LU,  UST.Long_Unsigned);
        function To_SF  is new UC (S_SF,  Short_Float);
 115    function To_SI  is new UC (S_SI,  Short_Integer);
        function To_SSI is new UC (S_SSI, Short_Short_Integer);
        function To_SSU is new UC (S_SSU, UST.Short_Short_Unsigned);
        function To_SU  is new UC (S_SU,  UST.Short_Unsigned);
        function To_U   is new UC (S_U,   UST.Unsigned);
 120    function To_WC  is new UC (S_WC,  Wide_Character);
     
        ----------
        -- I_AD --
        ----------
 125 
        function I_AD (Stream : access RST) return Fat_Pointer is
           T : S_AD;
           L : SEO;
     
 130    begin
           Ada.Streams.Read (Stream.all, T, L);
     
           if L < T'Last then
              raise Err;
 135       else
              return To_AD (T);
           end if;
        end I_AD;
     
 140    ----------
        -- I_AS --
        ----------
     
        function I_AS (Stream : access RST) return Thin_Pointer is
 145       T : S_AS;
           L : SEO;
     
        begin
           Ada.Streams.Read (Stream.all, T, L);
 150 
           if L < T'Last then
              raise Err;
           else
              return To_AS (T);
 155       end if;
        end I_AS;
     
        ---------
        -- I_B --
 160    ---------
     
        function I_B (Stream : access RST) return Boolean is
           T : S_B;
           L : SEO;
 165 
        begin
           Ada.Streams.Read (Stream.all, T, L);
     
           if L < T'Last then
 170          raise Err;
           else
              return Boolean'Val (T (1));
           end if;
        end I_B;
 175 
        ---------
        -- I_C --
        ---------
     
 180    function I_C (Stream : access RST) return Character is
           T : S_C;
           L : SEO;
     
        begin
 185       Ada.Streams.Read (Stream.all, T, L);
     
           if L < T'Last then
              raise Err;
           else
 190          return To_C (T);
           end if;
        end I_C;
     
        ---------
 195    -- I_F --
        ---------
     
        function I_F (Stream : access RST) return Float is
           T : S_F;
 200       L : SEO;
     
        begin
           Ada.Streams.Read (Stream.all, T, L);
     
 205       if L < T'Last then
              raise Err;
           else
              return To_F (T);
           end if;
 210    end I_F;
     
        ---------
        -- I_I --
        ---------
 215 
        function I_I (Stream : access RST) return Integer is
           T : S_I;
           L : SEO;
     
 220    begin
           Ada.Streams.Read (Stream.all, T, L);
     
           if L < T'Last then
              raise Err;
 225       else
              return To_I (T);
           end if;
        end I_I;
     
 230    ----------
        -- I_LF --
        ----------
     
        function I_LF (Stream : access RST) return Long_Float is
 235       T : S_LF;
           L : SEO;
     
        begin
           Ada.Streams.Read (Stream.all, T, L);
 240 
           if L < T'Last then
              raise Err;
           else
              return To_LF (T);
 245       end if;
        end I_LF;
     
        ----------
        -- I_LI --
 250    ----------
     
        function I_LI (Stream : access RST) return Long_Integer is
           T : S_LI;
           L : SEO;
 255 
        begin
           Ada.Streams.Read (Stream.all, T, L);
     
           if L < T'Last then
 260          raise Err;
           else
              return To_LI (T);
           end if;
        end I_LI;
 265 
        -----------
        -- I_LLF --
        -----------
     
 270    function I_LLF (Stream : access RST) return Long_Long_Float is
           T : S_LLF;
           L : SEO;
     
        begin
 275       Ada.Streams.Read (Stream.all, T, L);
     
           if L < T'Last then
              raise Err;
           else
 280          return To_LLF (T);
           end if;
        end I_LLF;
     
        -----------
 285    -- I_LLI --
        -----------
     
        function I_LLI (Stream : access RST) return Long_Long_Integer is
           T : S_LLI;
 290       L : SEO;
     
        begin
           Ada.Streams.Read (Stream.all, T, L);
     
 295       if L < T'Last then
              raise Err;
           else
              return To_LLI (T);
           end if;
 300    end I_LLI;
     
        -----------
        -- I_LLU --
        -----------
 305 
        function I_LLU (Stream : access RST) return UST.Long_Long_Unsigned is
           T : S_LLU;
           L : SEO;
     
 310    begin
           Ada.Streams.Read (Stream.all, T, L);
     
           if L < T'Last then
              raise Err;
 315       else
              return To_LLU (T);
           end if;
        end I_LLU;
     
 320    ----------
        -- I_LU --
        ----------
     
        function I_LU (Stream : access RST) return UST.Long_Unsigned is
 325       T : S_LU;
           L : SEO;
     
        begin
           Ada.Streams.Read (Stream.all, T, L);
 330 
           if L < T'Last then
              raise Err;
           else
              return To_LU (T);
 335       end if;
        end I_LU;
     
        ----------
        -- I_SF --
 340    ----------
     
        function I_SF (Stream : access RST) return Short_Float is
           T : S_SF;
           L : SEO;
 345 
        begin
           Ada.Streams.Read (Stream.all, T, L);
     
           if L < T'Last then
 350          raise Err;
           else
              return To_SF (T);
           end if;
        end I_SF;
 355 
        ----------
        -- I_SI --
        ----------
     
 360    function I_SI (Stream : access RST) return Short_Integer is
           T : S_SI;
           L : SEO;
     
        begin
 365       Ada.Streams.Read (Stream.all, T, L);
     
           if L < T'Last then
              raise Err;
           else
 370          return To_SI (T);
           end if;
        end I_SI;
     
        -----------
 375    -- I_SSI --
        -----------
     
        function I_SSI (Stream : access RST) return Short_Short_Integer is
           T : S_SSI;
 380       L : SEO;
     
        begin
           Ada.Streams.Read (Stream.all, T, L);
     
 385       if L < T'Last then
              raise Err;
           else
              return To_SSI (T);
           end if;
 390    end I_SSI;
     
        -----------
        -- I_SSU --
        -----------
 395 
        function I_SSU (Stream : access RST) return UST.Short_Short_Unsigned is
           T : S_SSU;
           L : SEO;
     
 400    begin
           Ada.Streams.Read (Stream.all, T, L);
     
           if L < T'Last then
              raise Err;
 405       else
              return To_SSU (T);
           end if;
        end I_SSU;
     
 410    ----------
        -- I_SU --
        ----------
     
        function I_SU (Stream : access RST) return UST.Short_Unsigned is
 415       T : S_SU;
           L : SEO;
     
        begin
           Ada.Streams.Read (Stream.all, T, L);
 420 
           if L < T'Last then
              raise Err;
           else
              return To_SU (T);
 425       end if;
        end I_SU;
     
        ---------
        -- I_U --
 430    ---------
     
        function I_U (Stream : access RST) return UST.Unsigned is
           T : S_U;
           L : SEO;
 435 
        begin
           Ada.Streams.Read (Stream.all, T, L);
     
           if L < T'Last then
 440          raise Err;
           else
              return To_U (T);
           end if;
        end I_U;
 445 
        ----------
        -- I_WC --
        ----------
     
 450    function I_WC (Stream : access RST) return Wide_Character is
           T : S_WC;
           L : SEO;
     
        begin
 455       Ada.Streams.Read (Stream.all, T, L);
     
           if L < T'Last then
              raise Err;
           else
 460          return To_WC (T);
           end if;
        end I_WC;
     
        ----------
 465    -- W_AD --
        ----------
     
        procedure W_AD (Stream : access RST; Item : in Fat_Pointer) is
           T : constant S_AD := From_AD (Item);
 470 
        begin
           Ada.Streams.Write (Stream.all, T);
        end W_AD;
     
 475    ----------
        -- W_AS --
        ----------
     
        procedure W_AS (Stream : access RST; Item : in Thin_Pointer) is
 480       T : constant S_AS := From_AS (Item);
     
        begin
           Ada.Streams.Write (Stream.all, T);
        end W_AS;
 485 
        ---------
        -- W_B --
        ---------
     
 490    procedure W_B (Stream : access RST; Item : in Boolean) is
           T : S_B;
     
        begin
           T (1) := Boolean'Pos (Item);
 495       Ada.Streams.Write (Stream.all, T);
        end W_B;
     
        ---------
        -- W_C --
 500    ---------
     
        procedure W_C (Stream : access RST; Item : in Character) is
           T : constant S_C := From_C (Item);
     
 505    begin
           Ada.Streams.Write (Stream.all, T);
        end W_C;
     
        ---------
 510    -- W_F --
        ---------
     
        procedure W_F (Stream : access RST; Item : in Float) is
           T : constant S_F := From_F (Item);
 515 
        begin
           Ada.Streams.Write (Stream.all, T);
        end W_F;
     
 520    ---------
        -- W_I --
        ---------
     
        procedure W_I (Stream : access RST; Item : in Integer) is
 525       T : constant S_I := From_I (Item);
     
        begin
           Ada.Streams.Write (Stream.all, T);
        end W_I;
 530 
        ----------
        -- W_LF --
        ----------
     
 535    procedure W_LF (Stream : access RST; Item : in Long_Float) is
           T : constant S_LF := From_LF (Item);
     
        begin
           Ada.Streams.Write (Stream.all, T);
 540    end W_LF;
     
        ----------
        -- W_LI --
        ----------
 545 
        procedure W_LI (Stream : access RST; Item : in Long_Integer) is
           T : constant S_LI := From_LI (Item);
     
        begin
 550       Ada.Streams.Write (Stream.all, T);
        end W_LI;
     
        -----------
        -- W_LLF --
 555    -----------
     
        procedure W_LLF (Stream : access RST; Item : in Long_Long_Float) is
           T : constant S_LLF := From_LLF (Item);
     
 560    begin
           Ada.Streams.Write (Stream.all, T);
        end W_LLF;
     
        -----------
 565    -- W_LLI --
        -----------
     
        procedure W_LLI (Stream : access RST; Item : in Long_Long_Integer) is
           T : constant S_LLI := From_LLI (Item);
 570 
        begin
           Ada.Streams.Write (Stream.all, T);
        end W_LLI;
     
 575    -----------
        -- W_LLU --
        -----------
     
        procedure W_LLU (Stream : access RST; Item : in UST.Long_Long_Unsigned) is
 580       T : constant S_LLU := From_LLU (Item);
     
        begin
           Ada.Streams.Write (Stream.all, T);
        end W_LLU;
 585 
        ----------
        -- W_LU --
        ----------
     
 590    procedure W_LU (Stream : access RST; Item : in UST.Long_Unsigned) is
           T : constant S_LU := From_LU (Item);
     
        begin
           Ada.Streams.Write (Stream.all, T);
 595    end W_LU;
     
        ----------
        -- W_SF --
        ----------
 600 
        procedure W_SF (Stream : access RST; Item : in Short_Float) is
           T : constant S_SF := From_SF (Item);
     
        begin
 605       Ada.Streams.Write (Stream.all, T);
        end W_SF;
     
        ----------
        -- W_SI --
 610    ----------
     
        procedure W_SI (Stream : access RST; Item : in Short_Integer) is
           T : constant S_SI := From_SI (Item);
     
 615    begin
           Ada.Streams.Write (Stream.all, T);
        end W_SI;
     
        -----------
 620    -- W_SSI --
        -----------
     
        procedure W_SSI (Stream : access RST; Item : in Short_Short_Integer) is
           T : constant S_SSI := From_SSI (Item);
 625 
        begin
           Ada.Streams.Write (Stream.all, T);
        end W_SSI;
     
 630    -----------
        -- W_SSU --
        -----------
     
        procedure W_SSU (Stream : access RST; Item : in UST.Short_Short_Unsigned) is
 635       T : constant S_SSU := From_SSU (Item);
     
        begin
           Ada.Streams.Write (Stream.all, T);
        end W_SSU;
 640 
        ----------
        -- W_SU --
        ----------
     
 645    procedure W_SU (Stream : access RST; Item : in UST.Short_Unsigned) is
           T : constant S_SU := From_SU (Item);
     
        begin
           Ada.Streams.Write (Stream.all, T);
 650    end W_SU;
     
        ---------
        -- W_U --
        ---------
 655 
        procedure W_U (Stream : access RST; Item : in UST.Unsigned) is
           T : constant S_U := From_U (Item);
     
        begin
 660       Ada.Streams.Write (Stream.all, T);
        end W_U;
     
        ----------
        -- W_WC --
 665    ----------
     
        procedure W_WC (Stream : access RST; Item : in Wide_Character) is
           T : constant S_WC := From_WC (Item);
     
 670    begin
           Ada.Streams.Write (Stream.all, T);
        end W_WC;
     
     end System.Stream_Attributes;