1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT RUN-TIME COMPONENTS                         --
4--                                                                          --
5--      A D A . W I D E _ W I D E _ T E X T _ I O . M O D U L A R _ I O     --
6--                                                                          --
7--                                 B o d y                                  --
8--                                                                          --
9--          Copyright (C) 1992-2021, 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
32with Ada.Wide_Wide_Text_IO.Integer_Aux;
33with System.Img_BIU; use System.Img_BIU;
34with System.Img_Uns; use System.Img_Uns;
35with System.Img_LLB; use System.Img_LLB;
36with System.Img_LLU; use System.Img_LLU;
37with System.Img_LLW; use System.Img_LLW;
38with System.Img_WIU; use System.Img_WIU;
39with System.Val_Uns; use System.Val_Uns;
40with System.Val_LLU; use System.Val_LLU;
41with System.WCh_Con; use System.WCh_Con;
42with System.WCh_WtS; use System.WCh_WtS;
43
44package body Ada.Wide_Wide_Text_IO.Modular_IO is
45
46   package Aux_Uns is new
47     Ada.Wide_Wide_Text_IO.Integer_Aux
48       (Unsigned,
49        Scan_Unsigned,
50        Set_Image_Unsigned,
51        Set_Image_Width_Unsigned,
52        Set_Image_Based_Unsigned);
53
54   package Aux_LLU is new
55     Ada.Wide_Wide_Text_IO.Integer_Aux
56       (Long_Long_Unsigned,
57        Scan_Long_Long_Unsigned,
58        Set_Image_Long_Long_Unsigned,
59        Set_Image_Width_Long_Long_Unsigned,
60        Set_Image_Based_Long_Long_Unsigned);
61
62   Need_LLU : constant Boolean := Num'Base'Size > Unsigned'Size;
63   --  Throughout this generic body, we distinguish between the case where type
64   --  Unsigned is acceptable, and where a Long_Long_Unsigned is needed. This
65   --  Boolean is used to test for these cases and since it is a constant, only
66   --  code for the relevant case will be included in the instance.
67
68   ---------
69   -- Get --
70   ---------
71
72   procedure Get
73     (File  : File_Type;
74      Item  : out Num;
75      Width : Field := 0)
76   is
77      --  We depend on a range check to get Data_Error
78
79      pragma Unsuppress (Range_Check);
80
81   begin
82      if Need_LLU then
83         Aux_LLU.Get (File, Long_Long_Unsigned (Item), Width);
84      else
85         Aux_Uns.Get (File, Unsigned (Item), Width);
86      end if;
87
88   exception
89      when Constraint_Error => raise Data_Error;
90   end Get;
91
92   procedure Get
93     (Item  : out Num;
94      Width : Field := 0)
95   is
96   begin
97      Get (Current_In, Item, Width);
98   end Get;
99
100   procedure Get
101     (From : Wide_Wide_String;
102      Item : out Num;
103      Last : out Positive)
104   is
105      --  We depend on a range check to get Data_Error
106
107      pragma Unsuppress (Range_Check);
108
109      S : constant String := Wide_Wide_String_To_String (From, WCEM_Upper);
110      --  String on which we do the actual conversion. Note that the method
111      --  used for wide character encoding is irrelevant, since if there is
112      --  a character outside the Standard.Character range then the call to
113      --  Aux.Gets will raise Data_Error in any case.
114
115   begin
116      if Need_LLU then
117         Aux_LLU.Gets (S, Long_Long_Unsigned (Item), Last);
118      else
119         Aux_Uns.Gets (S, Unsigned (Item), Last);
120      end if;
121
122   exception
123      when Constraint_Error => raise Data_Error;
124   end Get;
125
126   ---------
127   -- Put --
128   ---------
129
130   procedure Put
131     (File  : File_Type;
132      Item  : Num;
133      Width : Field := Default_Width;
134      Base  : Number_Base := Default_Base)
135   is
136   begin
137      if Need_LLU then
138         Aux_LLU.Put (File, Long_Long_Unsigned (Item), Width, Base);
139      else
140         Aux_Uns.Put (File, Unsigned (Item), Width, Base);
141      end if;
142   end Put;
143
144   procedure Put
145     (Item  : Num;
146      Width : Field := Default_Width;
147      Base  : Number_Base := Default_Base)
148   is
149   begin
150      Put (Current_Out, Item, Width, Base);
151   end Put;
152
153   procedure Put
154     (To   : out Wide_Wide_String;
155      Item : Num;
156      Base : Number_Base := Default_Base)
157   is
158      S : String (To'First .. To'Last);
159
160   begin
161      if Need_LLU then
162         Aux_LLU.Puts (S, Long_Long_Unsigned (Item), Base);
163      else
164         Aux_Uns.Puts (S, Unsigned (Item), Base);
165      end if;
166
167      for J in S'Range loop
168         To (J) := Wide_Wide_Character'Val (Character'Pos (S (J)));
169      end loop;
170   end Put;
171
172end Ada.Wide_Wide_Text_IO.Modular_IO;
173