OSDN Git Service

Nathanael Nerode <neroden@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-tiflau.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                        GNAT RUN-TIME COMPONENTS                          --
4 --                                                                          --
5 --                A D A . T E X T _ I O . F L O A T _ A U X                 --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --                                                                          --
10 --          Copyright (C) 1992-2001 Free Software Foundation, Inc.          --
11 --                                                                          --
12 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
13 -- terms of the  GNU General Public License as published  by the Free Soft- --
14 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
15 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
18 -- for  more details.  You should have  received  a copy of the GNU General --
19 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
20 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
21 -- MA 02111-1307, USA.                                                      --
22 --                                                                          --
23 -- As a special exception,  if other files  instantiate  generics from this --
24 -- unit, or you link  this unit with other files  to produce an executable, --
25 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
26 -- covered  by the  GNU  General  Public  License.  This exception does not --
27 -- however invalidate  any other reasons why  the executable file  might be --
28 -- covered by the  GNU Public License.                                      --
29 --                                                                          --
30 -- GNAT was originally developed  by the GNAT team at  New York University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
32 --                                                                          --
33 ------------------------------------------------------------------------------
34
35 with Ada.Text_IO.Generic_Aux; use Ada.Text_IO.Generic_Aux;
36
37 with System.Img_Real;  use System.Img_Real;
38 with System.Val_Real;  use System.Val_Real;
39
40 package body Ada.Text_IO.Float_Aux is
41
42    ---------
43    -- Get --
44    ---------
45
46    procedure Get
47      (File  : in File_Type;
48       Item  : out Long_Long_Float;
49       Width : in Field)
50    is
51       Buf  : String (1 .. Field'Last);
52       Stop : Integer := 0;
53       Ptr  : aliased Integer := 1;
54
55    begin
56       if Width /= 0 then
57          Load_Width (File, Width, Buf, Stop);
58          String_Skip (Buf, Ptr);
59       else
60          Load_Real (File, Buf, Stop);
61       end if;
62
63       Item := Scan_Real (Buf, Ptr'Access, Stop);
64
65       Check_End_Of_Field (Buf, Stop, Ptr, Width);
66    end Get;
67
68    ----------
69    -- Gets --
70    ----------
71
72    procedure Gets
73      (From : in String;
74       Item : out Long_Long_Float;
75       Last : out Positive)
76    is
77       Pos : aliased Integer;
78
79    begin
80       String_Skip (From, Pos);
81       Item := Scan_Real (From, Pos'Access, From'Last);
82       Last := Pos - 1;
83
84    exception
85       when Constraint_Error =>
86          Last := Pos - 1;
87          raise Data_Error;
88    end Gets;
89
90    ---------------
91    -- Load_Real --
92    ---------------
93
94    procedure Load_Real
95      (File : in File_Type;
96       Buf  : out String;
97       Ptr  : in out Natural)
98    is
99       Loaded   : Boolean;
100
101    begin
102       --  Skip initial blanks, and load possible sign
103
104       Load_Skip (File);
105       Load (File, Buf, Ptr, '+', '-');
106
107       --  Case of .nnnn
108
109       Load (File, Buf, Ptr, '.', Loaded);
110
111       if Loaded then
112          Load_Digits (File, Buf, Ptr, Loaded);
113
114          --  Hopeless junk if no digits loaded
115
116          if not Loaded then
117             return;
118          end if;
119
120       --  Otherwise must have digits to start
121
122       else
123          Load_Digits (File, Buf, Ptr, Loaded);
124
125          --  Hopeless junk if no digits loaded
126
127          if not Loaded then
128             return;
129          end if;
130
131          --  Based cases
132
133          Load (File, Buf, Ptr, '#', ':', Loaded);
134
135          if Loaded then
136
137             --  Case of nnn#.xxx#
138
139             Load (File, Buf, Ptr, '.', Loaded);
140
141             if Loaded then
142                Load_Extended_Digits (File, Buf, Ptr);
143
144             --  Case of nnn#xxx.[xxx]# or nnn#xxx#
145
146             else
147                Load_Extended_Digits (File, Buf, Ptr);
148                Load (File, Buf, Ptr, '.', Loaded);
149
150                if Loaded then
151                   Load_Extended_Digits (File, Buf, Ptr);
152                end if;
153
154                --  As usual, it seems strange to allow mixed base characters,
155                --  but that is what ACVC tests expect, see CE3804M, case (3).
156
157                Load (File, Buf, Ptr, '#', ':');
158             end if;
159
160          --  Case of nnn.[nnn] or nnn
161
162          else
163             Load (File, Buf, Ptr, '.', Loaded);
164
165             if Loaded then
166                Load_Digits (File, Buf, Ptr);
167             end if;
168          end if;
169       end if;
170
171       --  Deal with exponent
172
173       Load (File, Buf, Ptr, 'E', 'e', Loaded);
174
175       if Loaded then
176          Load (File, Buf, Ptr, '+', '-');
177          Load_Digits (File, Buf, Ptr);
178       end if;
179    end Load_Real;
180
181    ---------
182    -- Put --
183    ---------
184
185    procedure Put
186      (File : in File_Type;
187       Item : in Long_Long_Float;
188       Fore : in Field;
189       Aft  : in Field;
190       Exp  : in Field)
191    is
192       Buf : String (1 .. 3 * Field'Last + 2);
193       Ptr : Natural := 0;
194
195    begin
196       Set_Image_Real (Item, Buf, Ptr, Fore, Aft, Exp);
197       Put_Item (File, Buf (1 .. Ptr));
198    end Put;
199
200    ----------
201    -- Puts --
202    ----------
203
204    procedure Puts
205      (To   : out String;
206       Item : in Long_Long_Float;
207       Aft  : in Field;
208       Exp  : in Field)
209    is
210       Buf : String (1 .. 3 * Field'Last + 2);
211       Ptr : Natural := 0;
212
213    begin
214       Set_Image_Real (Item, Buf, Ptr, Fore => 1, Aft => Aft, Exp => Exp);
215
216       if Ptr > To'Length then
217          raise Layout_Error;
218
219       else
220          for J in 1 .. Ptr loop
221             To (To'Last - Ptr + J) := Buf (J);
222          end loop;
223
224          for J in To'First .. To'Last - Ptr loop
225             To (J) := ' ';
226          end loop;
227       end if;
228    end Puts;
229
230 end Ada.Text_IO.Float_Aux;