OSDN Git Service

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