Debian Codegraph @

gcc/ada/libgnat/a-tiflio.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- A D A . T E X T _ I O . F L O A T _ I O --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 with Ada.Text_IO.Float_Aux;
33 with System.Img_Flt; use System.Img_Flt;
34 with System.Img_LFlt; use System.Img_LFlt;
35 with System.Img_LLF; use System.Img_LLF;
36 with System.Val_Flt; use System.Val_Flt;
37 with System.Val_LFlt; use System.Val_LFlt;
38 with System.Val_LLF; use System.Val_LLF;
39
40 package body Ada.Text_IO.Float_IO with SPARK_Mode => Off is
41
42 package Aux_Float is new
43 Ada.Text_IO.Float_Aux (Float, Scan_Float, Set_Image_Float);
44
45 package Aux_Long_Float is new
46 Ada.Text_IO.Float_Aux
47 (Long_Float, Scan_Long_Float, Set_Image_Long_Float);
48
49 package Aux_Long_Long_Float is new
50 Ada.Text_IO.Float_Aux
51 (Long_Long_Float, Scan_Long_Long_Float, Set_Image_Long_Long_Float);
52
53 -- Throughout this generic body, we distinguish between the case where type
54 -- Float is OK, where type Long_Float is OK and where type Long_Long_Float
55 -- is needed. These boolean constants are used to test for this, such that
56 -- only code for the relevant case is included in the instance.
57
58 OK_Float : constant Boolean := Num'Base'Digits <= Float'Digits;
59
60 OK_Long_Float : constant Boolean := Num'Base'Digits <= Long_Float'Digits;
61
62 ---------
63 -- Get --
64 ---------
65
66 procedure Get
67 (File : File_Type;
68 Item : out Num;
69 Width : Field := 0)
70 is
71 pragma Unsuppress (Range_Check);
72
73 begin
74 if OK_Float then
75 Aux_Float.Get (File, Float (Item), Width);
76 elsif OK_Long_Float then
77 Aux_Long_Float.Get (File, Long_Float (Item), Width);
78 else
79 Aux_Long_Long_Float.Get (File, Long_Long_Float (Item), Width);
80 end if;
81
82 -- In the case where the type is unconstrained (e.g. Standard'Float),
83 -- the above conversion may result in an infinite value, which is
84 -- normally fine for a conversion, but in this case, we want to treat
85 -- that as a data error.
86
87 if not Item'Valid then
88 raise Data_Error;
89 end if;
90
91 exception
92 when Constraint_Error => raise Data_Error;
93 end Get;
94
95 procedure Get
96 (Item : out Num;
97 Width : Field := 0)
98 is
99 begin
100 Get (Current_In, Item, Width);
101 end Get;
102
103 procedure Get
104 (From : String;
105 Item : out Num;
106 Last : out Positive)
107 is
108 pragma Unsuppress (Range_Check);
109
110 begin
111 if OK_Float then
112 Aux_Float.Gets (From, Float (Item), Last);
113 elsif OK_Long_Float then
114 Aux_Long_Float.Gets (From, Long_Float (Item), Last);
115 else
116 Aux_Long_Long_Float.Gets (From, Long_Long_Float (Item), Last);
117 end if;
118
119 -- In the case where the type is unconstrained (e.g. Standard'Float),
120 -- the above conversion may result in an infinite value, which is
121 -- normally fine for a conversion, but in this case, we want to treat
122 -- that as a data error.
123
124 if not Item'Valid then
125 raise Data_Error;
126 end if;
127
128 exception
129 when Constraint_Error => raise Data_Error;
130 end Get;
131
132 ---------
133 -- Put --
134 ---------
135
136 procedure Put
137 (File : File_Type;
138 Item : Num;
139 Fore : Field := Default_Fore;
140 Aft : Field := Default_Aft;
141 Exp : Field := Default_Exp)
142 is
143 begin
144 if OK_Float then
145 Aux_Float.Put (File, Float (Item), Fore, Aft, Exp);
146 elsif OK_Long_Float then
147 Aux_Long_Float.Put (File, Long_Float (Item), Fore, Aft, Exp);
148 else
149 Aux_Long_Long_Float.Put
150 (File, Long_Long_Float (Item), Fore, Aft, Exp);
151 end if;
152 end Put;
153
154 procedure Put
155 (Item : Num;
156 Fore : Field := Default_Fore;
157 Aft : Field := Default_Aft;
158 Exp : Field := Default_Exp)
159 is
160 begin
161 Put (Current_Out, Item, Fore, Aft, Exp);
162 end Put;
163
164 procedure Put
165 (To : out String;
166 Item : Num;
167 Aft : Field := Default_Aft;
168 Exp : Field := Default_Exp)
169 is
170 begin
171 if OK_Float then
172 Aux_Float.Puts (To, Float (Item), Aft, Exp);
173 elsif OK_Long_Float then
174 Aux_Long_Float.Puts (To, Long_Float (Item), Aft, Exp);
175 else
176 Aux_Long_Long_Float.Puts (To, Long_Long_Float (Item), Aft, Exp);
177 end if;
178 end Put;
179
180 end Ada.Text_IO.Float_IO;