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                 --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 1992-2021, Free Software Foundation, Inc.         --
10--                                                                          --
11-- This specification is derived from the Ada Reference Manual for use with --
12-- GNAT. The copyright notice above, and the license provisions that follow --
13-- apply solely to the  contents of the part following the private keyword. --
14--                                                                          --
15-- GNAT is free software;  you can  redistribute it  and/or modify it under --
16-- terms of the  GNU General Public License as published  by the Free Soft- --
17-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
18-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
19-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
20-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
21--                                                                          --
22-- As a special exception under Section 7 of GPL version 3, you are granted --
23-- additional permissions described in the GCC Runtime Library Exception,   --
24-- version 3.1, as published by the Free Software Foundation.               --
25--                                                                          --
26-- You should have received a copy of the GNU General Public License and    --
27-- a copy of the GCC Runtime Library Exception along with this program;     --
28-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
29-- <http://www.gnu.org/licenses/>.                                          --
30--                                                                          --
31-- GNAT was originally developed  by the GNAT team at  New York University. --
32-- Extensive contributions were provided by Ada Core Technologies Inc.      --
33--                                                                          --
34------------------------------------------------------------------------------
35
36--  Note: the generic subpackages of Wide_Wide_Text_IO (Integer_IO, Float_IO,
37--  Fixed_IO, Modular_IO, Decimal_IO and Enumeration_IO) appear as private
38--  children in GNAT. These children are with'ed automatically if they are
39--  referenced, so this rearrangement is invisible to user programs, but has
40--  the advantage that only the needed parts of Wide_Wide_Text_IO are processed
41--  and loaded.
42
43with Ada.IO_Exceptions;
44with Ada.Streams;
45
46with Interfaces.C_Streams;
47
48with System;
49with System.File_Control_Block;
50with System.WCh_Con;
51
52package Ada.Wide_Wide_Text_IO is
53
54   type File_Type is limited private with Default_Initial_Condition;
55   type File_Mode is (In_File, Out_File, Append_File);
56
57   --  The following representation clause allows the use of unchecked
58   --  conversion for rapid translation between the File_Mode type
59   --  used in this package and System.File_IO.
60
61   for File_Mode use
62     (In_File     => 0,  -- System.FIle_IO.File_Mode'Pos (In_File)
63      Out_File    => 2,  -- System.File_IO.File_Mode'Pos (Out_File)
64      Append_File => 3); -- System.File_IO.File_Mode'Pos (Append_File)
65
66   type Count is range 0 .. Natural'Last;
67   --  The value of Count'Last must be large enough so that the assumption that
68   --  the Line, Column and Page counts can never exceed this value is valid.
69
70   subtype Positive_Count is Count range 1 .. Count'Last;
71
72   Unbounded : constant Count := 0;
73   --  Line and page length
74
75   subtype Field is Integer range 0 .. 255;
76
77   subtype Number_Base is Integer range 2 .. 16;
78
79   type Type_Set is (Lower_Case, Upper_Case);
80
81   ---------------------
82   -- File Management --
83   ---------------------
84
85   procedure Create
86     (File : in out File_Type;
87      Mode : File_Mode := Out_File;
88      Name : String := "";
89      Form : String := "");
90
91   procedure Open
92     (File : in out File_Type;
93      Mode : File_Mode;
94      Name : String;
95      Form : String := "");
96
97   procedure Close  (File : in out File_Type);
98   procedure Delete (File : in out File_Type);
99   procedure Reset  (File : in out File_Type; Mode : File_Mode);
100   procedure Reset  (File : in out File_Type);
101
102   function Mode (File : File_Type) return File_Mode;
103   function Name (File : File_Type) return String;
104   function Form (File : File_Type) return String;
105
106   function Is_Open (File : File_Type) return Boolean;
107
108   ------------------------------------------------------
109   -- Control of default input, output and error files --
110   ------------------------------------------------------
111
112   procedure Set_Input  (File : File_Type);
113   procedure Set_Output (File : File_Type);
114   procedure Set_Error  (File : File_Type);
115
116   function Standard_Input  return File_Type;
117   function Standard_Output return File_Type;
118   function Standard_Error  return File_Type;
119
120   function Current_Input  return File_Type;
121   function Current_Output return File_Type;
122   function Current_Error  return File_Type;
123
124   type File_Access is access constant File_Type;
125
126   function Standard_Input  return File_Access;
127   function Standard_Output return File_Access;
128   function Standard_Error  return File_Access;
129
130   function Current_Input  return File_Access;
131   function Current_Output return File_Access;
132   function Current_Error  return File_Access;
133
134   --------------------
135   -- Buffer control --
136   --------------------
137
138   --  Note: The parameter file is in out in the RM, but as pointed out
139   --  in <<95-5166.a Tucker Taft 95-6-23>> this is clearly an oversight.
140
141   procedure Flush (File : File_Type);
142   procedure Flush;
143
144   --------------------------------------------
145   -- Specification of line and page lengths --
146   --------------------------------------------
147
148   procedure Set_Line_Length (File : File_Type; To : Count);
149   procedure Set_Line_Length (To : Count);
150
151   procedure Set_Page_Length (File : File_Type; To : Count);
152   procedure Set_Page_Length (To : Count);
153
154   function Line_Length (File : File_Type) return Count;
155   function Line_Length return Count;
156
157   function Page_Length (File : File_Type) return Count;
158   function Page_Length return Count;
159
160   ------------------------------------
161   -- Column, Line, and Page Control --
162   ------------------------------------
163
164   procedure New_Line (File : File_Type; Spacing : Positive_Count := 1);
165   procedure New_Line (Spacing : Positive_Count := 1);
166
167   procedure Skip_Line (File : File_Type; Spacing : Positive_Count := 1);
168   procedure Skip_Line (Spacing : Positive_Count := 1);
169
170   function End_Of_Line (File : File_Type) return Boolean;
171   function End_Of_Line return Boolean;
172
173   procedure New_Page (File : File_Type);
174   procedure New_Page;
175
176   procedure Skip_Page (File : File_Type);
177   procedure Skip_Page;
178
179   function End_Of_Page (File : File_Type) return Boolean;
180   function End_Of_Page return Boolean;
181
182   function End_Of_File (File : File_Type) return Boolean;
183   function End_Of_File return Boolean;
184
185   procedure Set_Col (File : File_Type;  To : Positive_Count);
186   procedure Set_Col (To : Positive_Count);
187
188   procedure Set_Line (File : File_Type; To : Positive_Count);
189   procedure Set_Line (To : Positive_Count);
190
191   function Col (File : File_Type) return Positive_Count;
192   function Col return Positive_Count;
193
194   function Line (File : File_Type) return Positive_Count;
195   function Line return Positive_Count;
196
197   function Page (File : File_Type) return Positive_Count;
198   function Page return Positive_Count;
199
200   ----------------------------
201   -- Character Input-Output --
202   ----------------------------
203
204   procedure Get (File : File_Type; Item : out Wide_Wide_Character);
205   procedure Get (Item : out Wide_Wide_Character);
206   procedure Put (File : File_Type; Item : Wide_Wide_Character);
207   procedure Put (Item : Wide_Wide_Character);
208
209   procedure Look_Ahead
210     (File        : File_Type;
211      Item        : out Wide_Wide_Character;
212      End_Of_Line : out Boolean);
213
214   procedure Look_Ahead
215     (Item        : out Wide_Wide_Character;
216      End_Of_Line : out Boolean);
217
218   procedure Get_Immediate
219     (File : File_Type;
220      Item : out Wide_Wide_Character);
221
222   procedure Get_Immediate
223     (Item : out Wide_Wide_Character);
224
225   procedure Get_Immediate
226     (File      : File_Type;
227      Item      : out Wide_Wide_Character;
228      Available : out Boolean);
229
230   procedure Get_Immediate
231     (Item      : out Wide_Wide_Character;
232      Available : out Boolean);
233
234   -------------------------
235   -- String Input-Output --
236   -------------------------
237
238   procedure Get (File : File_Type; Item : out Wide_Wide_String);
239   procedure Get (Item : out Wide_Wide_String);
240   procedure Put (File : File_Type; Item : Wide_Wide_String);
241   procedure Put (Item : Wide_Wide_String);
242
243   procedure Get_Line
244     (File : File_Type;
245      Item : out Wide_Wide_String;
246      Last : out Natural);
247
248   function Get_Line (File : File_Type) return Wide_Wide_String;
249   pragma Ada_05 (Get_Line);
250
251   function Get_Line return Wide_Wide_String;
252   pragma Ada_05 (Get_Line);
253
254   procedure Get_Line
255     (Item : out Wide_Wide_String;
256      Last : out Natural);
257
258   procedure Put_Line
259     (File : File_Type;
260      Item : Wide_Wide_String);
261
262   procedure Put_Line
263     (Item : Wide_Wide_String);
264
265   ---------------------------------------
266   -- Generic packages for Input-Output --
267   ---------------------------------------
268
269   --  The generic packages:
270
271   --    Ada.Wide_Wide_Text_IO.Integer_IO
272   --    Ada.Wide_Wide_Text_IO.Modular_IO
273   --    Ada.Wide_Wide_Text_IO.Float_IO
274   --    Ada.Wide_Wide_Text_IO.Fixed_IO
275   --    Ada.Wide_Wide_Text_IO.Decimal_IO
276   --    Ada.Wide_Wide_Text_IO.Enumeration_IO
277
278   --  are implemented as separate child packages in GNAT, so the
279   --  spec and body of these packages are to be found in separate
280   --  child units. This implementation detail is hidden from the
281   --  Ada programmer by special circuitry in the compiler that
282   --  treats these child packages as though they were nested in
283   --  Text_IO. The advantage of this special processing is that
284   --  the subsidiary routines needed if these generics are used
285   --  are not loaded when they are not used.
286
287   ----------------
288   -- Exceptions --
289   ----------------
290
291   Status_Error : exception renames IO_Exceptions.Status_Error;
292   Mode_Error   : exception renames IO_Exceptions.Mode_Error;
293   Name_Error   : exception renames IO_Exceptions.Name_Error;
294   Use_Error    : exception renames IO_Exceptions.Use_Error;
295   Device_Error : exception renames IO_Exceptions.Device_Error;
296   End_Error    : exception renames IO_Exceptions.End_Error;
297   Data_Error   : exception renames IO_Exceptions.Data_Error;
298   Layout_Error : exception renames IO_Exceptions.Layout_Error;
299
300private
301
302   --  The following procedures have a File_Type formal of mode IN OUT because
303   --  they may close the original file. The Close operation may raise an
304   --  exception, but in that case we want any assignment to the formal to
305   --  be effective anyway, so it must be passed by reference (or the caller
306   --  will be left with a dangling pointer).
307
308   pragma Export_Procedure
309     (Internal  => Close,
310      External  => "",
311      Mechanism => Reference);
312   pragma Export_Procedure
313     (Internal  => Delete,
314      External  => "",
315      Mechanism => Reference);
316   pragma Export_Procedure
317     (Internal        => Reset,
318      External        => "",
319      Parameter_Types => (File_Type),
320      Mechanism       => Reference);
321   pragma Export_Procedure
322     (Internal        => Reset,
323      External        => "",
324      Parameter_Types => (File_Type, File_Mode),
325      Mechanism       => (File => Reference));
326
327   package WCh_Con renames System.WCh_Con;
328
329   -----------------------------------
330   -- Handling of Format Characters --
331   -----------------------------------
332
333   --  Line marks are represented by the single character ASCII.LF (16#0A#).
334   --  In DOS and similar systems, underlying file translation takes care
335   --  of translating this to and from the standard CR/LF sequences used in
336   --  these operating systems to mark the end of a line. On output there is
337   --  always a line mark at the end of the last line, but on input, this
338   --  line mark can be omitted, and is implied by the end of file.
339
340   --  Page marks are represented by the single character ASCII.FF (16#0C#),
341   --  The page mark at the end of the file may be omitted, and is normally
342   --  omitted on output unless an explicit New_Page call is made before
343   --  closing the file. No page mark is added when a file is appended to,
344   --  so, in accordance with the permission in (RM A.10.2(4)), there may
345   --  or may not be a page mark separating preexisting text in the file
346   --  from the new text to be written.
347
348   --  A file mark is marked by the physical end of file. In DOS translation
349   --  mode on input, an EOF character (SUB = 16#1A#) gets translated to the
350   --  physical end of file, so in effect this character is recognized as
351   --  marking the end of file in DOS and similar systems.
352
353   LM : constant := Character'Pos (ASCII.LF);
354   --  Used as line mark
355
356   PM : constant := Character'Pos (ASCII.FF);
357   --  Used as page mark, except at end of file where it is implied
358
359   ------------------------------------------
360   -- Wide_Wide_Text_IO File Control Block --
361   ------------------------------------------
362
363   Default_WCEM : WCh_Con.WC_Encoding_Method := WCh_Con.WCEM_UTF8;
364   --  This gets modified during initialization (see body) using the default
365   --  value established in the call to Set_Globals.
366
367   package FCB renames System.File_Control_Block;
368
369   type Wide_Wide_Text_AFCB is new FCB.AFCB with record
370      Page        : Count := 1;
371      Line        : Count := 1;
372      Col         : Count := 1;
373      Line_Length : Count := 0;
374      Page_Length : Count := 0;
375
376      Self : aliased File_Type;
377      --  Set to point to the containing Text_AFCB block. This is used to
378      --  implement the Current_{Error,Input,Output} functions which return
379      --  a File_Access, the file access value returned is a pointer to
380      --  the Self field of the corresponding file.
381
382      Before_LM : Boolean := False;
383      --  This flag is used to deal with the anomalies introduced by the
384      --  peculiar definition of End_Of_File and End_Of_Page in Ada. These
385      --  functions require looking ahead more than one character. Since
386      --  there is no convenient way of backing up more than one character,
387      --  what we do is to leave ourselves positioned past the LM, but set
388      --  this flag, so that we know that from an Ada point of view we are
389      --  in front of the LM, not after it. A bit odd, but it works.
390
391      Before_LM_PM : Boolean := False;
392      --  This flag similarly handles the case of being physically positioned
393      --  after a LM-PM sequence when logically we are before the LM-PM. This
394      --  flag can only be set if Before_LM is also set.
395
396      WC_Method : WCh_Con.WC_Encoding_Method := Default_WCEM;
397      --  Encoding method to be used for this file
398
399      Before_Wide_Wide_Character : Boolean := False;
400      --  This flag is set to indicate that a wide character in the input has
401      --  been read by Wide_Wide_Text_IO.Look_Ahead. If it is set to True,
402      --  then it means that the stream is logically positioned before the
403      --  character but is physically positioned after it. The character
404      --  involved must not be in the range 16#00#-16#7F#, i.e. if the flag is
405      --  set, then we know the next character has a code greater than 16#7F#,
406      --  and the value of this character is saved in
407      --  Saved_Wide_Wide_Character.
408
409      Saved_Wide_Wide_Character : Wide_Wide_Character;
410      --  This field is valid only if Before_Wide_Wide_Character is set. It
411      --  contains a wide character read by Look_Ahead. If Look_Ahead
412      --  reads a character in the range 16#0000# to 16#007F#, then it
413      --  can use ungetc to put it back, but ungetc cannot be called
414      --  more than once, so for characters above this range, we don't
415      --  try to back up the file. Instead we save the character in this
416      --  field and set the flag Before_Wide_Wide_Character to indicate that
417      --  we are logically positioned before this character even though
418      --  the stream is physically positioned after it.
419
420   end record;
421
422   type File_Type is access all Wide_Wide_Text_AFCB;
423
424   function AFCB_Allocate
425     (Control_Block : Wide_Wide_Text_AFCB) return FCB.AFCB_Ptr;
426
427   procedure AFCB_Close (File : not null access Wide_Wide_Text_AFCB);
428   procedure AFCB_Free  (File : not null access Wide_Wide_Text_AFCB);
429
430   procedure Read
431     (File : in out Wide_Wide_Text_AFCB;
432      Item : out Ada.Streams.Stream_Element_Array;
433      Last : out Ada.Streams.Stream_Element_Offset);
434   --  Read operation used when Wide_Wide_Text_IO file is treated as a Stream
435
436   procedure Write
437     (File : in out Wide_Wide_Text_AFCB;
438      Item : Ada.Streams.Stream_Element_Array);
439   --  Write operation used when Wide_Wide_Text_IO file is treated as a Stream
440
441   ------------------------
442   -- The Standard Files --
443   ------------------------
444
445   Standard_Err_AFCB : aliased Wide_Wide_Text_AFCB;
446   Standard_In_AFCB  : aliased Wide_Wide_Text_AFCB;
447   Standard_Out_AFCB : aliased Wide_Wide_Text_AFCB;
448
449   Standard_Err : aliased File_Type := Standard_Err_AFCB'Access;
450   Standard_In  : aliased File_Type := Standard_In_AFCB'Access;
451   Standard_Out : aliased File_Type := Standard_Out_AFCB'Access;
452   --  Standard files
453
454   Current_In   : aliased File_Type := Standard_In;
455   Current_Out  : aliased File_Type := Standard_Out;
456   Current_Err  : aliased File_Type := Standard_Err;
457   --  Current files
458
459   procedure Initialize_Standard_Files;
460   --  Initializes the file control blocks for the standard files. Called from
461   --  the elaboration routine for this package, and from Reset_Standard_Files
462   --  in package Ada.Wide_Wide_Text_IO.Reset_Standard_Files.
463
464   -----------------------
465   -- Local Subprograms --
466   -----------------------
467
468   --  These subprograms are in the private part of the spec so that they can
469   --  be shared by the children of Ada.Text_IO.Wide_Wide_Text_IO.
470
471   function Getc (File : File_Type) return Interfaces.C_Streams.int;
472   --  Gets next character from file, which has already been checked for being
473   --  in read status, and returns the character read if no error occurs. The
474   --  result is EOF if the end of file was read.
475
476   procedure Get_Character (File : File_Type; Item : out Character);
477   --  This is essentially copy of Wide_Wide_Text_IO.Get. It obtains a single
478   --  obtains a single character from the input file File, and places it in
479   --  Item. This result may be the leading character of a Wide_Wide_Character
480   --  sequence, but that is up to the caller to deal with.
481
482   function Get_Wide_Wide_Char
483     (C    : Character;
484      File : File_Type) return Wide_Wide_Character;
485   --  This function is shared by Get and Get_Immediate to extract a wide
486   --  character value from the given File. The first byte has already been
487   --  read and is passed in C. The wide character value is returned as the
488   --  result, and the file pointer is bumped past the character.
489
490   function Nextc (File : File_Type) return Interfaces.C_Streams.int;
491   --  Returns next character from file without skipping past it (i.e. it is a
492   --  combination of Getc followed by an Ungetc).
493
494end Ada.Wide_Wide_Text_IO;
495