PR target/16201
[official-gcc.git] / gcc / ada / a-textio.ads
blob4f38370c77a8cc3b80769d7746407e77bebfa6a9
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-2002 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 ------------------------------------------------------------------------------
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.
44 with Ada.IO_Exceptions;
45 with Ada.Streams;
46 with System;
47 with System.File_Control_Block;
49 package Ada.Text_IO is
50 pragma Elaborate_Body (Text_IO);
52 type File_Type is limited private;
53 type File_Mode is (In_File, Out_File, Append_File);
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.
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)
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.
69 subtype Positive_Count is Count range 1 .. Count'Last;
71 Unbounded : constant Count := 0;
72 -- Line and page length
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.
79 subtype Number_Base is Integer range 2 .. 16;
81 type Type_Set is (Lower_Case, Upper_Case);
83 ---------------------
84 -- File Management --
85 ---------------------
87 procedure Create
88 (File : in out File_Type;
89 Mode : in File_Mode := Out_File;
90 Name : in String := "";
91 Form : in String := "");
93 procedure Open
94 (File : in out File_Type;
95 Mode : in File_Mode;
96 Name : in String;
97 Form : in String := "");
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 : in File_Mode);
102 procedure Reset (File : in out File_Type);
104 function Mode (File : in File_Type) return File_Mode;
105 function Name (File : in File_Type) return String;
106 function Form (File : in File_Type) return String;
108 function Is_Open (File : in File_Type) return Boolean;
110 ------------------------------------------------------
111 -- Control of default input, output and error files --
112 ------------------------------------------------------
114 procedure Set_Input (File : in File_Type);
115 procedure Set_Output (File : in File_Type);
116 procedure Set_Error (File : in File_Type);
118 function Standard_Input return File_Type;
119 function Standard_Output return File_Type;
120 function Standard_Error return File_Type;
122 function Current_Input return File_Type;
123 function Current_Output return File_Type;
124 function Current_Error return File_Type;
126 type File_Access is access constant File_Type;
128 function Standard_Input return File_Access;
129 function Standard_Output return File_Access;
130 function Standard_Error return File_Access;
132 function Current_Input return File_Access;
133 function Current_Output return File_Access;
134 function Current_Error return File_Access;
136 --------------------
137 -- Buffer control --
138 --------------------
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.
143 procedure Flush (File : in File_Type);
144 procedure Flush;
146 --------------------------------------------
147 -- Specification of line and page lengths --
148 --------------------------------------------
150 procedure Set_Line_Length (File : in File_Type; To : in Count);
151 procedure Set_Line_Length (To : in Count);
153 procedure Set_Page_Length (File : in File_Type; To : in Count);
154 procedure Set_Page_Length (To : in Count);
156 function Line_Length (File : in File_Type) return Count;
157 function Line_Length return Count;
159 function Page_Length (File : in File_Type) return Count;
160 function Page_Length return Count;
162 ------------------------------------
163 -- Column, Line, and Page Control --
164 ------------------------------------
166 procedure New_Line (File : in File_Type; Spacing : in Positive_Count := 1);
167 procedure New_Line (Spacing : in Positive_Count := 1);
169 procedure Skip_Line (File : in File_Type; Spacing : in Positive_Count := 1);
170 procedure Skip_Line (Spacing : in Positive_Count := 1);
172 function End_Of_Line (File : in File_Type) return Boolean;
173 function End_Of_Line return Boolean;
175 procedure New_Page (File : in File_Type);
176 procedure New_Page;
178 procedure Skip_Page (File : in File_Type);
179 procedure Skip_Page;
181 function End_Of_Page (File : in File_Type) return Boolean;
182 function End_Of_Page return Boolean;
184 function End_Of_File (File : in File_Type) return Boolean;
185 function End_Of_File return Boolean;
187 procedure Set_Col (File : in File_Type; To : in Positive_Count);
188 procedure Set_Col (To : in Positive_Count);
190 procedure Set_Line (File : in File_Type; To : in Positive_Count);
191 procedure Set_Line (To : in Positive_Count);
193 function Col (File : in File_Type) return Positive_Count;
194 function Col return Positive_Count;
196 function Line (File : in File_Type) return Positive_Count;
197 function Line return Positive_Count;
199 function Page (File : in File_Type) return Positive_Count;
200 function Page return Positive_Count;
202 ----------------------------
203 -- Character Input-Output --
204 ----------------------------
206 procedure Get (File : in File_Type; Item : out Character);
207 procedure Get (Item : out Character);
208 procedure Put (File : in File_Type; Item : in Character);
209 procedure Put (Item : in Character);
211 procedure Look_Ahead
212 (File : in File_Type;
213 Item : out Character;
214 End_Of_Line : out Boolean);
216 procedure Look_Ahead
217 (Item : out Character;
218 End_Of_Line : out Boolean);
220 procedure Get_Immediate
221 (File : in File_Type;
222 Item : out Character);
224 procedure Get_Immediate
225 (Item : out Character);
227 procedure Get_Immediate
228 (File : in File_Type;
229 Item : out Character;
230 Available : out Boolean);
232 procedure Get_Immediate
233 (Item : out Character;
234 Available : out Boolean);
236 -------------------------
237 -- String Input-Output --
238 -------------------------
240 procedure Get (File : in File_Type; Item : out String);
241 procedure Get (Item : out String);
242 procedure Put (File : in File_Type; Item : in String);
243 procedure Put (Item : in String);
245 procedure Get_Line
246 (File : in File_Type;
247 Item : out String;
248 Last : out Natural);
250 procedure Get_Line
251 (Item : out String;
252 Last : out Natural);
254 procedure Put_Line
255 (File : in File_Type;
256 Item : in String);
258 procedure Put_Line
259 (Item : in String);
261 ---------------------------------------
262 -- Generic packages for Input-Output --
263 ---------------------------------------
265 -- The generic packages:
267 -- Ada.Text_IO.Integer_IO
268 -- Ada.Text_IO.Modular_IO
269 -- Ada.Text_IO.Float_IO
270 -- Ada.Text_IO.Fixed_IO
271 -- Ada.Text_IO.Decimal_IO
272 -- Ada.Text_IO.Enumeration_IO
274 -- are implemented as separate child packages in GNAT, so the
275 -- spec and body of these packages are to be found in separate
276 -- child units. This implementation detail is hidden from the
277 -- Ada programmer by special circuitry in the compiler that
278 -- treats these child packages as though they were nested in
279 -- Text_IO. The advantage of this special processing is that
280 -- the subsidiary routines needed if these generics are used
281 -- are not loaded when they are not used.
283 ----------------
284 -- Exceptions --
285 ----------------
287 Status_Error : exception renames IO_Exceptions.Status_Error;
288 Mode_Error : exception renames IO_Exceptions.Mode_Error;
289 Name_Error : exception renames IO_Exceptions.Name_Error;
290 Use_Error : exception renames IO_Exceptions.Use_Error;
291 Device_Error : exception renames IO_Exceptions.Device_Error;
292 End_Error : exception renames IO_Exceptions.End_Error;
293 Data_Error : exception renames IO_Exceptions.Data_Error;
294 Layout_Error : exception renames IO_Exceptions.Layout_Error;
296 private
297 -----------------------------------
298 -- Handling of Format Characters --
299 -----------------------------------
301 -- Line marks are represented by the single character ASCII.LF (16#0A#).
302 -- In DOS and similar systems, underlying file translation takes care
303 -- of translating this to and from the standard CR/LF sequences used in
304 -- these operating systems to mark the end of a line. On output there is
305 -- always a line mark at the end of the last line, but on input, this
306 -- line mark can be omitted, and is implied by the end of file.
308 -- Page marks are represented by the single character ASCII.FF (16#0C#),
309 -- The page mark at the end of the file may be omitted, and is normally
310 -- omitted on output unless an explicit New_Page call is made before
311 -- closing the file. No page mark is added when a file is appended to,
312 -- so, in accordance with the permission in (RM A.10.2(4)), there may
313 -- or may not be a page mark separating preexising text in the file
314 -- from the new text to be written.
316 -- A file mark is marked by the physical end of file. In DOS translation
317 -- mode on input, an EOF character (SUB = 16#1A#) gets translated to the
318 -- physical end of file, so in effect this character is recognized as
319 -- marking the end of file in DOS and similar systems.
321 LM : constant := Character'Pos (ASCII.LF);
322 -- Used as line mark
324 PM : constant := Character'Pos (ASCII.FF);
325 -- Used as page mark, except at end of file where it is implied
327 --------------------------------
328 -- Text_IO File Control Block --
329 --------------------------------
331 package FCB renames System.File_Control_Block;
333 type Text_AFCB;
334 type File_Type is access all Text_AFCB;
336 type Text_AFCB is new FCB.AFCB with record
337 Page : Count := 1;
338 Line : Count := 1;
339 Col : Count := 1;
340 Line_Length : Count := 0;
341 Page_Length : Count := 0;
343 Self : aliased File_Type;
344 -- Set to point to the containing Text_AFCB block. This is used to
345 -- implement the Current_{Error,Input,Ouput} functions which return
346 -- a File_Access, the file access value returned is a pointer to
347 -- the Self field of the corresponding file.
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!
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.
363 end record;
365 function AFCB_Allocate (Control_Block : Text_AFCB) return FCB.AFCB_Ptr;
367 procedure AFCB_Close (File : access Text_AFCB);
368 procedure AFCB_Free (File : access Text_AFCB);
370 procedure Read
371 (File : in out Text_AFCB;
372 Item : out Ada.Streams.Stream_Element_Array;
373 Last : out Ada.Streams.Stream_Element_Offset);
374 -- Read operation used when Text_IO file is treated directly as Stream
376 procedure Write
377 (File : in out Text_AFCB;
378 Item : in Ada.Streams.Stream_Element_Array);
379 -- Write operation used when Text_IO file is treated directly as Stream
381 ------------------------
382 -- The Standard Files --
383 ------------------------
385 Null_Str : aliased constant String := "";
386 -- Used as name and form of standard files
388 Standard_Err_AFCB : aliased Text_AFCB;
389 Standard_In_AFCB : aliased Text_AFCB;
390 Standard_Out_AFCB : aliased Text_AFCB;
392 Standard_Err : aliased File_Type := Standard_Err_AFCB'Access;
393 Standard_In : aliased File_Type := Standard_In_AFCB'Access;
394 Standard_Out : aliased File_Type := Standard_Out_AFCB'Access;
395 -- Standard files
397 Current_In : aliased File_Type := Standard_In;
398 Current_Out : aliased File_Type := Standard_Out;
399 Current_Err : aliased File_Type := Standard_Err;
400 -- Current files
402 -----------------------
403 -- Local Subprograms --
404 -----------------------
406 -- These subprograms are in the private part of the spec so that they can
407 -- be shared by the routines in the body of Ada.Text_IO.Wide_Text_IO.
409 -- Note: we use Integer in these declarations instead of the more accurate
410 -- Interfaces.C_Streams.int, because we do not want to drag in the spec of
411 -- this interfaces package with the spec of Ada.Text_IO, and we know that
412 -- in fact these types are identical
414 function EOF_Char return Integer;
415 -- Returns the system-specific character indicating the end of a text file.
416 -- This is exported for use by child packages such as Enumeration_Aux to
417 -- eliminate their needing to depend directly on Interfaces.C_Streams.
419 function Getc (File : File_Type) return Integer;
420 -- Gets next character from file, which has already been checked for
421 -- being in read status, and returns the character read if no error
422 -- occurs. The result is EOF if the end of file was read.
424 function Nextc (File : File_Type) return Integer;
425 -- Returns next character from file without skipping past it (i.e. it
426 -- is a combination of Getc followed by an Ungetc).
428 procedure Putc (ch : Integer; File : File_Type);
429 -- Outputs the given character to the file, which has already been
430 -- checked for being in output status. Device_Error is raised if the
431 -- character cannot be written.
433 procedure Terminate_Line (File : File_Type);
434 -- If the file is in Write_File or Append_File mode, and the current
435 -- line is not terminated, then a line terminator is written using
436 -- New_Line. Note that there is no Terminate_Page routine, because
437 -- the page mark at the end of the file is implied if necessary.
439 procedure Ungetc (ch : Integer; File : File_Type);
440 -- Pushes back character into stream, using ungetc. The caller has
441 -- checked that the file is in read status. Device_Error is raised
442 -- if the character cannot be pushed back. An attempt to push back
443 -- and end of file character (EOF) is ignored.
445 end Ada.Text_IO;