OSDN Git Service

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