| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- A D A . W I D E _ T E X T _ I O . F I X E D _ 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 Interfaces; |
| 33 |
with Ada.Wide_Text_IO.Fixed_Aux; |
| 34 |
with Ada.Wide_Text_IO.Float_Aux; |
| 35 |
with System.Img_Fixed_32; use System.Img_Fixed_32; |
| 36 |
with System.Img_Fixed_64; use System.Img_Fixed_64; |
| 37 |
with System.Img_LFlt; use System.Img_LFlt; |
| 38 |
with System.Val_Fixed_32; use System.Val_Fixed_32; |
| 39 |
with System.Val_Fixed_64; use System.Val_Fixed_64; |
| 40 |
with System.Val_LFlt; use System.Val_LFlt; |
| 41 |
with System.WCh_Con; use System.WCh_Con; |
| 42 |
with System.WCh_WtS; use System.WCh_WtS; |
| 43 |
|
| 44 |
package body Ada.Wide_Text_IO.Fixed_IO is |
| 45 |
|
| 46 |
-- Note: we still use the floating-point I/O routines for types whose small |
| 47 |
-- is not the ratio of two sufficiently small integers. This will result in |
| 48 |
-- inaccuracies for fixed point types that require more precision than is |
| 49 |
-- available in Long_Float. |
| 50 |
|
| 51 |
subtype Int32 is Interfaces.Integer_32; use type Int32; |
| 52 |
subtype Int64 is Interfaces.Integer_64; use type Int64; |
| 53 |
|
| 54 |
package Aux32 is new |
| 55 |
Ada.Wide_Text_IO.Fixed_Aux (Int32, Scan_Fixed32, Set_Image_Fixed32); |
| 56 |
|
| 57 |
package Aux64 is new |
| 58 |
Ada.Wide_Text_IO.Fixed_Aux (Int64, Scan_Fixed64, Set_Image_Fixed64); |
| 59 |
|
| 60 |
package Aux_Long_Float is new |
| 61 |
Ada.Wide_Text_IO.Float_Aux |
| 62 |
(Long_Float, Scan_Long_Float, Set_Image_Long_Float); |
| 63 |
|
| 64 |
-- Throughout this generic body, we distinguish between the case where type |
| 65 |
-- Int32 is OK and where type Int64 is OK. These boolean constants are used |
| 66 |
-- to test for this, such that only code for the relevant case is included |
| 67 |
-- in the instance; that's why the computation of their value must be fully |
| 68 |
-- static (although it is not a static expressions in the RM sense). |
| 69 |
|
| 70 |
OK_Get_32 : constant Boolean := |
| 71 |
Num'Base'Object_Size <= 32 |
| 72 |
and then |
| 73 |
((Num'Base'Small_Numerator = 1 |
| 74 |
and then Num'Base'Small_Denominator <= 2**31) |
| 75 |
or else |
| 76 |
(Num'Base'Small_Denominator = 1 |
| 77 |
and then Num'Base'Small_Numerator <= 2**31) |
| 78 |
or else |
| 79 |
(Num'Base'Small_Numerator <= 2**27 |
| 80 |
and then Num'Base'Small_Denominator <= 2**27)); |
| 81 |
-- These conditions are derived from the prerequisites of System.Value_F |
| 82 |
|
| 83 |
OK_Put_32 : constant Boolean := |
| 84 |
Num'Base'Object_Size <= 32 |
| 85 |
and then |
| 86 |
((Num'Base'Small_Numerator = 1 |
| 87 |
and then Num'Base'Small_Denominator <= 2**31) |
| 88 |
or else |
| 89 |
(Num'Base'Small_Denominator = 1 |
| 90 |
and then Num'Base'Small_Numerator <= 2**31) |
| 91 |
or else |
| 92 |
(Num'Base'Small_Numerator < Num'Base'Small_Denominator |
| 93 |
and then Num'Base'Small_Denominator <= 2**27) |
| 94 |
or else |
| 95 |
(Num'Base'Small_Denominator < Num'Base'Small_Numerator |
| 96 |
and then Num'Base'Small_Numerator <= 2**25)); |
| 97 |
-- These conditions are derived from the prerequisites of System.Image_F |
| 98 |
|
| 99 |
OK_Get_64 : constant Boolean := |
| 100 |
Num'Base'Object_Size <= 64 |
| 101 |
and then |
| 102 |
((Num'Base'Small_Numerator = 1 |
| 103 |
and then Num'Base'Small_Denominator <= 2**63) |
| 104 |
or else |
| 105 |
(Num'Base'Small_Denominator = 1 |
| 106 |
and then Num'Base'Small_Numerator <= 2**63) |
| 107 |
or else |
| 108 |
(Num'Base'Small_Numerator <= 2**59 |
| 109 |
and then Num'Base'Small_Denominator <= 2**59)); |
| 110 |
-- These conditions are derived from the prerequisites of System.Value_F |
| 111 |
|
| 112 |
OK_Put_64 : constant Boolean := |
| 113 |
Num'Base'Object_Size <= 64 |
| 114 |
and then |
| 115 |
((Num'Base'Small_Numerator = 1 |
| 116 |
and then Num'Base'Small_Denominator <= 2**63) |
| 117 |
or else |
| 118 |
(Num'Base'Small_Denominator = 1 |
| 119 |
and then Num'Base'Small_Numerator <= 2**63) |
| 120 |
or else |
| 121 |
(Num'Base'Small_Numerator < Num'Base'Small_Denominator |
| 122 |
and then Num'Base'Small_Denominator <= 2**59) |
| 123 |
or else |
| 124 |
(Num'Base'Small_Denominator < Num'Base'Small_Numerator |
| 125 |
and then Num'Base'Small_Numerator <= 2**53)); |
| 126 |
-- These conditions are derived from the prerequisites of System.Image_F |
| 127 |
|
| 128 |
E : constant Natural := 63 - 32 * Boolean'Pos (OK_Put_32); |
| 129 |
-- T'Size - 1 for the selected Int{32,64} |
| 130 |
|
| 131 |
F0 : constant Natural := 0; |
| 132 |
F1 : constant Natural := |
| 133 |
F0 + 18 * Boolean'Pos (2.0**E * Num'Small * 10.0**(-F0) >= 1.0E+18); |
| 134 |
F2 : constant Natural := |
| 135 |
F1 + 9 * Boolean'Pos (2.0**E * Num'Small * 10.0**(-F1) >= 1.0E+9); |
| 136 |
F3 : constant Natural := |
| 137 |
F2 + 5 * Boolean'Pos (2.0**E * Num'Small * 10.0**(-F2) >= 1.0E+5); |
| 138 |
F4 : constant Natural := |
| 139 |
F3 + 3 * Boolean'Pos (2.0**E * Num'Small * 10.0**(-F3) >= 1.0E+3); |
| 140 |
F5 : constant Natural := |
| 141 |
F4 + 2 * Boolean'Pos (2.0**E * Num'Small * 10.0**(-F4) >= 1.0E+2); |
| 142 |
F6 : constant Natural := |
| 143 |
F5 + 1 * Boolean'Pos (2.0**E * Num'Small * 10.0**(-F5) >= 1.0E+1); |
| 144 |
-- Binary search for the number of digits - 1 before the decimal point of |
| 145 |
-- the product 2.0**E * Num'Small. |
| 146 |
|
| 147 |
For0 : constant Natural := 2 + F6; |
| 148 |
-- Fore value for the fixed point type whose mantissa is Int{32,64} and |
| 149 |
-- whose small is Num'Small. |
| 150 |
|
| 151 |
--------- |
| 152 |
-- Get -- |
| 153 |
--------- |
| 154 |
|
| 155 |
procedure Get |
| 156 |
(File : File_Type; |
| 157 |
Item : out Num; |
| 158 |
Width : Field := 0) |
| 159 |
is |
| 160 |
pragma Unsuppress (Range_Check); |
| 161 |
|
| 162 |
begin |
| 163 |
if OK_Get_32 then |
| 164 |
Item := Num'Fixed_Value |
| 165 |
(Aux32.Get (File, Width, |
| 166 |
-Num'Small_Numerator, |
| 167 |
-Num'Small_Denominator)); |
| 168 |
elsif OK_Get_64 then |
| 169 |
Item := Num'Fixed_Value |
| 170 |
(Aux64.Get (File, Width, |
| 171 |
-Num'Small_Numerator, |
| 172 |
-Num'Small_Denominator)); |
| 173 |
else |
| 174 |
Aux_Long_Float.Get (File, Long_Float (Item), Width); |
| 175 |
end if; |
| 176 |
|
| 177 |
exception |
| 178 |
when Constraint_Error => raise Data_Error; |
| 179 |
end Get; |
| 180 |
|
| 181 |
procedure Get |
| 182 |
(Item : out Num; |
| 183 |
Width : Field := 0) |
| 184 |
is |
| 185 |
begin |
| 186 |
Get (Current_In, Item, Width); |
| 187 |
end Get; |
| 188 |
|
| 189 |
procedure Get |
| 190 |
(From : Wide_String; |
| 191 |
Item : out Num; |
| 192 |
Last : out Positive) |
| 193 |
is |
| 194 |
pragma Unsuppress (Range_Check); |
| 195 |
|
| 196 |
S : constant String := Wide_String_To_String (From, WCEM_Upper); |
| 197 |
-- String on which we do the actual conversion. Note that the method |
| 198 |
-- used for wide character encoding is irrelevant, since if there is |
| 199 |
-- a character outside the Standard.Character range then the call to |
| 200 |
-- Aux.Gets will raise Data_Error in any case. |
| 201 |
|
| 202 |
begin |
| 203 |
if OK_Get_32 then |
| 204 |
Item := Num'Fixed_Value |
| 205 |
(Aux32.Gets (S, Last, |
| 206 |
-Num'Small_Numerator, |
| 207 |
-Num'Small_Denominator)); |
| 208 |
elsif OK_Get_64 then |
| 209 |
Item := Num'Fixed_Value |
| 210 |
(Aux64.Gets (S, Last, |
| 211 |
-Num'Small_Numerator, |
| 212 |
-Num'Small_Denominator)); |
| 213 |
else |
| 214 |
Aux_Long_Float.Gets (S, Long_Float (Item), Last); |
| 215 |
end if; |
| 216 |
|
| 217 |
exception |
| 218 |
when Constraint_Error => raise Data_Error; |
| 219 |
end Get; |
| 220 |
|
| 221 |
--------- |
| 222 |
-- Put -- |
| 223 |
--------- |
| 224 |
|
| 225 |
procedure Put |
| 226 |
(File : File_Type; |
| 227 |
Item : Num; |
| 228 |
Fore : Field := Default_Fore; |
| 229 |
Aft : Field := Default_Aft; |
| 230 |
Exp : Field := Default_Exp) |
| 231 |
is |
| 232 |
begin |
| 233 |
if OK_Put_32 then |
| 234 |
Aux32.Put (File, Int32'Integer_Value (Item), Fore, Aft, Exp, |
| 235 |
-Num'Small_Numerator, -Num'Small_Denominator, |
| 236 |
For0, Num'Aft); |
| 237 |
elsif OK_Put_64 then |
| 238 |
Aux64.Put (File, Int64'Integer_Value (Item), Fore, Aft, Exp, |
| 239 |
-Num'Small_Numerator, -Num'Small_Denominator, |
| 240 |
For0, Num'Aft); |
| 241 |
else |
| 242 |
Aux_Long_Float.Put (File, Long_Float (Item), Fore, Aft, Exp); |
| 243 |
end if; |
| 244 |
end Put; |
| 245 |
|
| 246 |
procedure Put |
| 247 |
(Item : Num; |
| 248 |
Fore : Field := Default_Fore; |
| 249 |
Aft : Field := Default_Aft; |
| 250 |
Exp : Field := Default_Exp) |
| 251 |
is |
| 252 |
begin |
| 253 |
Put (Current_Out, Item, Fore, Aft, Exp); |
| 254 |
end Put; |
| 255 |
|
| 256 |
procedure Put |
| 257 |
(To : out Wide_String; |
| 258 |
Item : Num; |
| 259 |
Aft : Field := Default_Aft; |
| 260 |
Exp : Field := Default_Exp) |
| 261 |
is |
| 262 |
S : String (To'First .. To'Last); |
| 263 |
|
| 264 |
begin |
| 265 |
if OK_Put_32 then |
| 266 |
Aux32.Puts (S, Int32'Integer_Value (Item), Aft, Exp, |
| 267 |
-Num'Small_Numerator, -Num'Small_Denominator, |
| 268 |
For0, Num'Aft); |
| 269 |
elsif OK_Put_64 then |
| 270 |
Aux64.Puts (S, Int64'Integer_Value (Item), Aft, Exp, |
| 271 |
-Num'Small_Numerator, -Num'Small_Denominator, |
| 272 |
For0, Num'Aft); |
| 273 |
else |
| 274 |
Aux_Long_Float.Puts (S, Long_Float (Item), Aft, Exp); |
| 275 |
end if; |
| 276 |
|
| 277 |
for J in S'Range loop |
| 278 |
To (J) := Wide_Character'Val (Character'Pos (S (J))); |
| 279 |
end loop; |
| 280 |
end Put; |
| 281 |
|
| 282 |
end Ada.Wide_Text_IO.Fixed_IO; |