OSDN Git Service

2005-03-08 Ed Schonberg <schonberg@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-textio.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --                          A D A . T E X T _ I O                           --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2005 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 2,  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.  See the GNU General Public License --
21 -- for  more details.  You should have  received  a copy of the GNU General --
22 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
23 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
24 -- MA 02111-1307, USA.                                                      --
25 --                                                                          --
26 -- As a special exception,  if other files  instantiate  generics from this --
27 -- unit, or you link  this unit with other files  to produce an executable, --
28 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
29 -- covered  by the  GNU  General  Public  License.  This exception does not --
30 -- however invalidate  any other reasons why  the executable file  might be --
31 -- covered by the  GNU Public License.                                      --
32 --                                                                          --
33 -- GNAT was originally developed  by the GNAT team at  New York University. --
34 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
35 --                                                                          --
36 ------------------------------------------------------------------------------
37
38 --  Note: the generic subpackages of Text_IO (Integer_IO, Float_IO, Fixed_IO,
39 --  Modular_IO, Decimal_IO and Enumeration_IO) appear as private children in
40 --  GNAT. These children are with'ed automatically if they are referenced, so
41 --  this rearrangement is invisible to user programs, but has the advantage
42 --  that only the needed parts of Text_IO are processed and loaded.
43
44 with Ada.IO_Exceptions;
45 with Ada.Streams;
46 with System;
47 with System.File_Control_Block;
48
49 package Ada.Text_IO is
50 pragma Elaborate_Body (Text_IO);
51
52    type File_Type is limited private;
53    type File_Mode is (In_File, Out_File, Append_File);
54
55    --  The following representation clause allows the use of unchecked
56    --  conversion for rapid translation between the File_Mode type
57    --  used in this package and System.File_IO.
58
59    for File_Mode use
60      (In_File     => 0,  -- System.FIle_IO.File_Mode'Pos (In_File)
61       Out_File    => 2,  -- System.File_IO.File_Mode'Pos (Out_File)
62       Append_File => 3); -- System.File_IO.File_Mode'Pos (Append_File)
63
64    type Count is range 0 .. Natural'Last;
65    --  The value of Count'Last must be large enough so that the assumption
66    --  enough so that the assumption that the Line, Column and Page
67    --  counts can never exceed this value is a valid assumption.
68
69    subtype Positive_Count is Count range 1 .. Count'Last;
70
71    Unbounded : constant Count := 0;
72    --  Line and page length
73
74    subtype Field is Integer range 0 .. 255;
75    --  Note: if for any reason, there is a need to increase this value,
76    --  then it will be necessary to change the corresponding value in
77    --  System.Img_Real in file s-imgrea.adb.
78
79    subtype Number_Base is Integer range 2 .. 16;
80
81    type Type_Set is (Lower_Case, Upper_Case);
82
83    ---------------------
84    -- File Management --
85    ---------------------
86
87    procedure Create
88      (File : in out File_Type;
89       Mode : File_Mode := Out_File;
90       Name : String := "";
91       Form : String := "");
92
93    procedure Open
94      (File : in out File_Type;
95       Mode : File_Mode;
96       Name : String;
97       Form : String := "");
98
99    procedure Close  (File : in out File_Type);
100    procedure Delete (File : in out File_Type);
101    procedure Reset  (File : in out File_Type; Mode : File_Mode);
102    procedure Reset  (File : in out File_Type);
103
104    function Mode (File : File_Type) return File_Mode;
105    function Name (File : File_Type) return String;
106    function Form (File : File_Type) return String;
107
108    function Is_Open (File : File_Type) return Boolean;
109
110    ------------------------------------------------------
111    -- Control of default input, output and error files --
112    ------------------------------------------------------
113
114    procedure Set_Input  (File : File_Type);
115    procedure Set_Output (File : File_Type);
116    procedure Set_Error  (File : File_Type);
117
118    function Standard_Input  return File_Type;
119    function Standard_Output return File_Type;
120    function Standard_Error  return File_Type;
121
122    function Current_Input  return File_Type;
123    function Current_Output return File_Type;
124    function Current_Error  return File_Type;
125
126    type File_Access is access constant File_Type;
127
128    function Standard_Input  return File_Access;
129    function Standard_Output return File_Access;
130    function Standard_Error  return File_Access;
131
132    function Current_Input  return File_Access;
133    function Current_Output return File_Access;
134    function Current_Error  return File_Access;
135
136    --------------------
137    -- Buffer control --
138    --------------------
139
140    --  Note: The parameter file is IN OUT in the RM, but this is clearly
141    --  an oversight, and was intended to be IN, see AI95-00057.
142
143    procedure Flush (File : File_Type);
144    procedure Flush;
145
146    --------------------------------------------
147    -- Specification of line and page lengths --
148    --------------------------------------------
149
150    procedure Set_Line_Length (File : File_Type; To : Count);
151    procedure Set_Line_Length (To : Count);
152
153    procedure Set_Page_Length (File : File_Type; To : Count);
154    procedure Set_Page_Length (To : Count);
155
156    function Line_Length (File : File_Type) return Count;
157    function Line_Length return Count;
158
159    function Page_Length (File : File_Type) return Count;
160    function Page_Length return Count;
161
162    ------------------------------------
163    -- Column, Line, and Page Control --
164    ------------------------------------
165
166    procedure New_Line (File : File_Type; Spacing : Positive_Count := 1);
167    procedure New_Line (Spacing : Positive_Count := 1);
168
169    procedure Skip_Line (File : File_Type; Spacing : Positive_Count := 1);
170    procedure Skip_Line (Spacing : Positive_Count := 1);
171
172    function End_Of_Line (File : File_Type) return Boolean;
173    function End_Of_Line return Boolean;
174
175    procedure New_Page (File : File_Type);
176    procedure New_Page;
177
178    procedure Skip_Page (File : File_Type);
179    procedure Skip_Page;
180
181    function End_Of_Page (File : File_Type) return Boolean;
182    function End_Of_Page return Boolean;
183
184    function End_Of_File (File : File_Type) return Boolean;
185    function End_Of_File return Boolean;
186
187    procedure Set_Col (File : File_Type;  To : Positive_Count);
188    procedure Set_Col (To : Positive_Count);
189
190    procedure Set_Line (File : File_Type; To : Positive_Count);
191    procedure Set_Line (To : Positive_Count);
192
193    function Col (File : File_Type) return Positive_Count;
194    function Col return Positive_Count;
195
196    function Line (File : File_Type) return Positive_Count;
197    function Line return Positive_Count;
198
199    function Page (File : File_Type) return Positive_Count;
200    function Page return Positive_Count;
201
202    ----------------------------
203    -- Character Input-Output --
204    ----------------------------
205
206    procedure Get (File : File_Type; Item : out Character);
207    procedure Get (Item : out Character);
208    procedure Put (File : File_Type; Item : Character);
209    procedure Put (Item : Character);
210
211    procedure Look_Ahead
212      (File        : File_Type;
213       Item        : out Character;
214       End_Of_Line : out Boolean);
215
216    procedure Look_Ahead
217      (Item        : out Character;
218       End_Of_Line : out Boolean);
219
220    procedure Get_Immediate
221      (File : File_Type;
222       Item : out Character);
223
224    procedure Get_Immediate
225      (Item : out Character);
226
227    procedure Get_Immediate
228      (File      : File_Type;
229       Item      : out Character;
230       Available : out Boolean);
231
232    procedure Get_Immediate
233      (Item      : out Character;
234       Available : out Boolean);
235
236    -------------------------
237    -- String Input-Output --
238    -------------------------
239
240    procedure Get (File : File_Type; Item : out String);
241    procedure Get (Item : out String);
242    procedure Put (File : File_Type; Item : String);
243    procedure Put (Item : String);
244
245    procedure Get_Line
246      (File : File_Type;
247       Item : out String;
248       Last : out Natural);
249
250    procedure Get_Line
251      (Item : out String;
252       Last : out Natural);
253
254    function Get_Line (File : File_Type) return String;
255    pragma Ada_05 (Get_Line);
256
257    function Get_Line return String;
258    pragma Ada_05 (Get_Line);
259
260    procedure Put_Line
261      (File : File_Type;
262       Item : String);
263
264    procedure Put_Line
265      (Item : String);
266
267    ---------------------------------------
268    -- Generic packages for Input-Output --
269    ---------------------------------------
270
271    --  The generic packages:
272
273    --    Ada.Text_IO.Integer_IO
274    --    Ada.Text_IO.Modular_IO
275    --    Ada.Text_IO.Float_IO
276    --    Ada.Text_IO.Fixed_IO
277    --    Ada.Text_IO.Decimal_IO
278    --    Ada.Text_IO.Enumeration_IO
279
280    --  are implemented as separate child packages in GNAT, so the
281    --  spec and body of these packages are to be found in separate
282    --  child units. This implementation detail is hidden from the
283    --  Ada programmer by special circuitry in the compiler that
284    --  treats these child packages as though they were nested in
285    --  Text_IO. The advantage of this special processing is that
286    --  the subsidiary routines needed if these generics are used
287    --  are not loaded when they are not used.
288
289    ----------------
290    -- Exceptions --
291    ----------------
292
293    Status_Error : exception renames IO_Exceptions.Status_Error;
294    Mode_Error   : exception renames IO_Exceptions.Mode_Error;
295    Name_Error   : exception renames IO_Exceptions.Name_Error;
296    Use_Error    : exception renames IO_Exceptions.Use_Error;
297    Device_Error : exception renames IO_Exceptions.Device_Error;
298    End_Error    : exception renames IO_Exceptions.End_Error;
299    Data_Error   : exception renames IO_Exceptions.Data_Error;
300    Layout_Error : exception renames IO_Exceptions.Layout_Error;
301
302 private
303    -----------------------------------
304    -- Handling of Format Characters --
305    -----------------------------------
306
307    --  Line marks are represented by the single character ASCII.LF (16#0A#).
308    --  In DOS and similar systems, underlying file translation takes care
309    --  of translating this to and from the standard CR/LF sequences used in
310    --  these operating systems to mark the end of a line. On output there is
311    --  always a line mark at the end of the last line, but on input, this
312    --  line mark can be omitted, and is implied by the end of file.
313
314    --  Page marks are represented by the single character ASCII.FF (16#0C#),
315    --  The page mark at the end of the file may be omitted, and is normally
316    --  omitted on output unless an explicit New_Page call is made before
317    --  closing the file. No page mark is added when a file is appended to,
318    --  so, in accordance with the permission in (RM A.10.2(4)), there may
319    --  or may not be a page mark separating preexising text in the file
320    --  from the new text to be written.
321
322    --  A file mark is marked by the physical end of file. In DOS translation
323    --  mode on input, an EOF character (SUB = 16#1A#) gets translated to the
324    --  physical end of file, so in effect this character is recognized as
325    --  marking the end of file in DOS and similar systems.
326
327    LM : constant := Character'Pos (ASCII.LF);
328    --  Used as line mark
329
330    PM : constant := Character'Pos (ASCII.FF);
331    --  Used as page mark, except at end of file where it is implied
332
333    --------------------------------
334    -- Text_IO File Control Block --
335    --------------------------------
336
337    package FCB renames System.File_Control_Block;
338
339    type Text_AFCB;
340    type File_Type is access all Text_AFCB;
341
342    type 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       Self : aliased File_Type;
350       --  Set to point to the containing Text_AFCB block. This is used to
351       --  implement the Current_{Error,Input,Ouput} functions which return
352       --  a File_Access, the file access value returned is a pointer to
353       --  the Self field of the corresponding file.
354
355       Before_LM : Boolean := False;
356       --  This flag is used to deal with the anomolies introduced by the
357       --  peculiar definition of End_Of_File and End_Of_Page in Ada. These
358       --  functions require looking ahead more than one character. Since
359       --  there is no convenient way of backing up more than one character,
360       --  what we do is to leave ourselves positioned past the LM, but set
361       --  this flag, so that we know that from an Ada point of view we are
362       --  in front of the LM, not after it. A bit of a kludge, but it works!
363
364       Before_LM_PM : Boolean := False;
365       --  This flag similarly handles the case of being physically positioned
366       --  after a LM-PM sequence when logically we are before the LM-PM. This
367       --  flag can only be set if Before_LM is also set.
368
369    end record;
370
371    function AFCB_Allocate (Control_Block : Text_AFCB) return FCB.AFCB_Ptr;
372
373    procedure AFCB_Close (File : access Text_AFCB);
374    procedure AFCB_Free  (File : access Text_AFCB);
375
376    procedure Read
377      (File : in out Text_AFCB;
378       Item : out Ada.Streams.Stream_Element_Array;
379       Last : out Ada.Streams.Stream_Element_Offset);
380    --  Read operation used when Text_IO file is treated directly as Stream
381
382    procedure Write
383      (File : in out Text_AFCB;
384       Item : Ada.Streams.Stream_Element_Array);
385    --  Write operation used when Text_IO file is treated directly as Stream
386
387    ------------------------
388    -- The Standard Files --
389    ------------------------
390
391    Null_Str : aliased constant String := "";
392    --  Used as name and form of standard files
393
394    Standard_Err_AFCB : aliased Text_AFCB;
395    Standard_In_AFCB  : aliased Text_AFCB;
396    Standard_Out_AFCB : aliased Text_AFCB;
397
398    Standard_Err : aliased File_Type := Standard_Err_AFCB'Access;
399    Standard_In  : aliased File_Type := Standard_In_AFCB'Access;
400    Standard_Out : aliased File_Type := Standard_Out_AFCB'Access;
401    --  Standard files
402
403    Current_In   : aliased File_Type := Standard_In;
404    Current_Out  : aliased File_Type := Standard_Out;
405    Current_Err  : aliased File_Type := Standard_Err;
406    --  Current files
407
408    -----------------------
409    -- Local Subprograms --
410    -----------------------
411
412    --  These subprograms are in the private part of the spec so that they can
413    --  be shared by the routines in the body of Ada.Text_IO.Wide_Text_IO.
414
415    --  Note: we use Integer in these declarations instead of the more accurate
416    --  Interfaces.C_Streams.int, because we do not want to drag in the spec of
417    --  this interfaces package with the spec of Ada.Text_IO, and we know that
418    --  in fact these types are identical
419
420    function EOF_Char return Integer;
421    --  Returns the system-specific character indicating the end of a text file.
422    --  This is exported for use by child packages such as Enumeration_Aux to
423    --  eliminate their needing to depend directly on Interfaces.C_Streams.
424
425    function Getc (File : File_Type) return Integer;
426    --  Gets next character from file, which has already been checked for
427    --  being in read status, and returns the character read if no error
428    --  occurs. The result is EOF if the end of file was read.
429
430    function Nextc (File : File_Type) return Integer;
431    --  Returns next character from file without skipping past it (i.e. it
432    --  is a combination of Getc followed by an Ungetc).
433
434    procedure Putc (ch : Integer; File : File_Type);
435    --  Outputs the given character to the file, which has already been
436    --  checked for being in output status. Device_Error is raised if the
437    --  character cannot be written.
438
439    procedure Terminate_Line (File : File_Type);
440    --  If the file is in Write_File or Append_File mode, and the current
441    --  line is not terminated, then a line terminator is written using
442    --  New_Line. Note that there is no Terminate_Page routine, because
443    --  the page mark at the end of the file is implied if necessary.
444
445    procedure Ungetc (ch : Integer; File : File_Type);
446    --  Pushes back character into stream, using ungetc. The caller has
447    --  checked that the file is in read status. Device_Error is raised
448    --  if the character cannot be pushed back. An attempt to push back
449    --  and end of file character (EOF) is ignored.
450
451 end Ada.Text_IO;