OSDN Git Service

Nathanael Nerode <neroden@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-wtdeau.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUNTIME COMPONENTS                          --
4 --                                                                          --
5 --         A D A . W I D E _ T E X T _ I O . D E C I M A L _ 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.Wide_Text_IO.Generic_Aux; use Ada.Wide_Text_IO.Generic_Aux;
36 with Ada.Wide_Text_IO.Float_Aux;   use Ada.Wide_Text_IO.Float_Aux;
37
38 with System.Img_Dec; use System.Img_Dec;
39 with System.Img_LLD; use System.Img_LLD;
40 with System.Val_Dec; use System.Val_Dec;
41 with System.Val_LLD; use System.Val_LLD;
42
43 package body Ada.Wide_Text_IO.Decimal_Aux is
44
45    -------------
46    -- Get_Dec --
47    -------------
48
49    function Get_Dec
50      (File   : File_Type;
51       Width  : Field;
52       Scale  : Integer)
53       return   Integer
54    is
55       Buf  : String (1 .. Field'Last);
56       Ptr  : aliased Integer;
57       Stop : Integer := 0;
58       Item : Integer;
59
60    begin
61       if Width /= 0 then
62          Load_Width (File, Width, Buf, Stop);
63          String_Skip (Buf, Ptr);
64       else
65          Load_Real (File, Buf, Stop);
66          Ptr := 1;
67       end if;
68
69       Item := Scan_Decimal (Buf, Ptr'Access, Stop, Scale);
70       Check_End_Of_Field (Buf, Stop, Ptr, Width);
71       return Item;
72    end Get_Dec;
73
74    -------------
75    -- Get_LLD --
76    -------------
77
78    function Get_LLD
79      (File   : File_Type;
80       Width  : Field;
81       Scale  : Integer)
82       return   Long_Long_Integer
83    is
84       Buf  : String (1 .. Field'Last);
85       Ptr  : aliased Integer;
86       Stop : Integer := 0;
87       Item : Long_Long_Integer;
88
89    begin
90       if Width /= 0 then
91          Load_Width (File, Width, Buf, Stop);
92          String_Skip (Buf, Ptr);
93       else
94          Load_Real (File, Buf, Stop);
95          Ptr := 1;
96       end if;
97
98       Item := Scan_Long_Long_Decimal (Buf, Ptr'Access, Stop, Scale);
99       Check_End_Of_Field (Buf, Stop, Ptr, Width);
100       return Item;
101    end Get_LLD;
102
103    --------------
104    -- Gets_Dec --
105    --------------
106
107    function Gets_Dec
108      (From  : String;
109       Last  : access Positive;
110       Scale : Integer)
111       return  Integer
112    is
113       Pos  : aliased Integer;
114       Item : Integer;
115
116    begin
117       String_Skip (From, Pos);
118       Item := Scan_Decimal (From, Pos'Access, From'Last, Scale);
119       Last.all := Pos - 1;
120       return Item;
121
122    exception
123       when Constraint_Error =>
124          Last.all := Pos - 1;
125          raise Data_Error;
126
127    end Gets_Dec;
128
129    --------------
130    -- Gets_LLD --
131    --------------
132
133    function Gets_LLD
134      (From  : String;
135       Last  : access Positive;
136       Scale : Integer)
137       return  Long_Long_Integer
138    is
139       Pos  : aliased Integer;
140       Item : Long_Long_Integer;
141
142    begin
143       String_Skip (From, Pos);
144       Item := Scan_Long_Long_Decimal (From, Pos'Access, From'Last, Scale);
145       Last.all := Pos - 1;
146       return Item;
147
148    exception
149       when Constraint_Error =>
150          Last.all := Pos - 1;
151          raise Data_Error;
152
153    end Gets_LLD;
154
155    -------------
156    -- Put_Dec --
157    -------------
158
159    procedure Put_Dec
160      (File  : File_Type;
161       Item  : Integer;
162       Fore  : Field;
163       Aft   : Field;
164       Exp   : Field;
165       Scale : Integer)
166    is
167       Buf : String (1 .. Field'Last);
168       Ptr : Natural := 0;
169
170    begin
171       Set_Image_Decimal (Item, Buf, Ptr, Scale, Fore, Aft, Exp);
172       Put_Item (File, Buf (1 .. Ptr));
173    end Put_Dec;
174
175    -------------
176    -- Put_LLD --
177    -------------
178
179    procedure Put_LLD
180      (File  : File_Type;
181       Item  : Long_Long_Integer;
182       Fore  : Field;
183       Aft   : Field;
184       Exp   : Field;
185       Scale : Integer)
186    is
187       Buf : String (1 .. Field'Last);
188       Ptr : Natural := 0;
189
190    begin
191       Set_Image_Long_Long_Decimal (Item, Buf, Ptr, Scale, Fore, Aft, Exp);
192       Put_Item (File, Buf (1 .. Ptr));
193    end Put_LLD;
194
195    --------------
196    -- Puts_Dec --
197    --------------
198
199    procedure Puts_Dec
200      (To    : out String;
201       Item  : Integer;
202       Aft   : Field;
203       Exp   : Field;
204       Scale : Integer)
205    is
206       Buf  : String (1 .. Field'Last);
207       Fore : Integer;
208       Ptr  : Natural := 0;
209
210    begin
211       if Exp = 0 then
212          Fore := To'Length - 1 - Aft;
213       else
214          Fore := To'Length - 2 - Aft - Exp;
215       end if;
216
217       if Fore < 1 then
218          raise Layout_Error;
219       end if;
220
221       Set_Image_Decimal (Item, Buf, Ptr, Scale, Fore, Aft, Exp);
222
223       if Ptr > To'Length then
224          raise Layout_Error;
225       else
226          To := Buf (1 .. Ptr);
227       end if;
228    end Puts_Dec;
229
230    --------------
231    -- Puts_Dec --
232    --------------
233
234    procedure Puts_LLD
235      (To    : out String;
236       Item  : Long_Long_Integer;
237       Aft   : Field;
238       Exp   : Field;
239       Scale : Integer)
240    is
241       Buf  : String (1 .. Field'Last);
242       Fore : Integer;
243       Ptr  : Natural := 0;
244
245    begin
246       if Exp = 0 then
247          Fore := To'Length - 1 - Aft;
248       else
249          Fore := To'Length - 2 - Aft - Exp;
250       end if;
251
252       if Fore < 1 then
253          raise Layout_Error;
254       end if;
255
256       Set_Image_Long_Long_Decimal (Item, Buf, Ptr, Scale, Fore, Aft, Exp);
257
258       if Ptr > To'Length then
259          raise Layout_Error;
260       else
261          To := Buf (1 .. Ptr);
262       end if;
263    end Puts_LLD;
264
265 end Ada.Wide_Text_IO.Decimal_Aux;