1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . W I D E _ T E X T _ I O --
9 -- Copyright (C) 1992-2000 Free Software Foundation, Inc. --
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. --
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. --
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. --
33 -- GNAT was originally developed by the GNAT team at New York University. --
34 -- Extensive contributions were provided by Ada Core Technologies Inc. --
36 ------------------------------------------------------------------------------
38 -- Note: the generic subpackages of Wide_Text_IO (Integer_IO, Float_IO,
39 -- Fixed_IO, Modular_IO, Decimal_IO and Enumeration_IO) appear as private
40 -- children in GNAT. These children are with'ed automatically if they are
41 -- referenced, so this rearrangement is invisible to user programs, but has
42 -- the advantage that only the needed parts of Wide_Text_IO are processed
45 with Ada
.IO_Exceptions
;
48 with System
.File_Control_Block
;
51 package Ada
.Wide_Text_IO
is
53 package WCh_Con
renames System
.WCh_Con
;
55 type File_Type
is limited private;
56 type File_Mode
is (In_File
, Out_File
, Append_File
);
58 -- The following representation clause allows the use of unchecked
59 -- conversion for rapid translation between the File_Mode type
60 -- used in this package and System.File_IO.
63 (In_File
=> 0, -- System.FIle_IO.File_Mode'Pos (In_File)
64 Out_File
=> 2, -- System.File_IO.File_Mode'Pos (Out_File)
65 Append_File
=> 3); -- System.File_IO.File_Mode'Pos (Append_File)
67 type Count
is range 0 .. Natural'Last;
68 -- The value of Count'Last must be large enough so that the assumption
69 -- enough so that the assumption that the Line, Column and Page
70 -- counts can never exceed this value is a valid assumption.
72 subtype Positive_Count
is Count
range 1 .. Count
'Last;
74 Unbounded
: constant Count
:= 0;
75 -- Line and page length
77 subtype Field
is Integer range 0 .. 255;
78 -- Note: if for any reason, there is a need to increase this value,
79 -- then it will be necessary to change the corresponding value in
80 -- System.Img_Real in file s-imgrea.adb.
82 subtype Number_Base
is Integer range 2 .. 16;
84 type Type_Set
is (Lower_Case
, Upper_Case
);
91 (File
: in out File_Type
;
92 Mode
: in File_Mode
:= Out_File
;
93 Name
: in String := "";
94 Form
: in String := "");
97 (File
: in out File_Type
;
100 Form
: in String := "");
102 procedure Close
(File
: in out File_Type
);
103 procedure Delete
(File
: in out File_Type
);
104 procedure Reset
(File
: in out File_Type
; Mode
: in File_Mode
);
105 procedure Reset
(File
: in out File_Type
);
107 function Mode
(File
: in File_Type
) return File_Mode
;
108 function Name
(File
: in File_Type
) return String;
109 function Form
(File
: in File_Type
) return String;
111 function Is_Open
(File
: in File_Type
) return Boolean;
113 ------------------------------------------------------
114 -- Control of default input, output and error files --
115 ------------------------------------------------------
117 procedure Set_Input
(File
: in File_Type
);
118 procedure Set_Output
(File
: in File_Type
);
119 procedure Set_Error
(File
: in File_Type
);
121 function Standard_Input
return File_Type
;
122 function Standard_Output
return File_Type
;
123 function Standard_Error
return File_Type
;
125 function Current_Input
return File_Type
;
126 function Current_Output
return File_Type
;
127 function Current_Error
return File_Type
;
129 type File_Access
is access constant File_Type
;
131 function Standard_Input
return File_Access
;
132 function Standard_Output
return File_Access
;
133 function Standard_Error
return File_Access
;
135 function Current_Input
return File_Access
;
136 function Current_Output
return File_Access
;
137 function Current_Error
return File_Access
;
143 -- Note: The paramter file is in out in the RM, but as pointed out
144 -- in <<95-5166.a Tucker Taft 95-6-23>> this is clearly an oversight.
146 procedure Flush
(File
: in File_Type
);
149 --------------------------------------------
150 -- Specification of line and page lengths --
151 --------------------------------------------
153 procedure Set_Line_Length
(File
: in File_Type
; To
: in Count
);
154 procedure Set_Line_Length
(To
: in Count
);
156 procedure Set_Page_Length
(File
: in File_Type
; To
: in Count
);
157 procedure Set_Page_Length
(To
: in Count
);
159 function Line_Length
(File
: in File_Type
) return Count
;
160 function Line_Length
return Count
;
162 function Page_Length
(File
: in File_Type
) return Count
;
163 function Page_Length
return Count
;
165 ------------------------------------
166 -- Column, Line, and Page Control --
167 ------------------------------------
169 procedure New_Line
(File
: in File_Type
; Spacing
: in Positive_Count
:= 1);
170 procedure New_Line
(Spacing
: in Positive_Count
:= 1);
172 procedure Skip_Line
(File
: in File_Type
; Spacing
: in Positive_Count
:= 1);
173 procedure Skip_Line
(Spacing
: in Positive_Count
:= 1);
175 function End_Of_Line
(File
: in File_Type
) return Boolean;
176 function End_Of_Line
return Boolean;
178 procedure New_Page
(File
: in File_Type
);
181 procedure Skip_Page
(File
: in File_Type
);
184 function End_Of_Page
(File
: in File_Type
) return Boolean;
185 function End_Of_Page
return Boolean;
187 function End_Of_File
(File
: in File_Type
) return Boolean;
188 function End_Of_File
return Boolean;
190 procedure Set_Col
(File
: in File_Type
; To
: in Positive_Count
);
191 procedure Set_Col
(To
: in Positive_Count
);
193 procedure Set_Line
(File
: in File_Type
; To
: in Positive_Count
);
194 procedure Set_Line
(To
: in Positive_Count
);
196 function Col
(File
: in File_Type
) return Positive_Count
;
197 function Col
return Positive_Count
;
199 function Line
(File
: in File_Type
) return Positive_Count
;
200 function Line
return Positive_Count
;
202 function Page
(File
: in File_Type
) return Positive_Count
;
203 function Page
return Positive_Count
;
205 ----------------------------
206 -- Character Input-Output --
207 ----------------------------
209 procedure Get
(File
: in File_Type
; Item
: out Wide_Character);
210 procedure Get
(Item
: out Wide_Character);
211 procedure Put
(File
: in File_Type
; Item
: in Wide_Character);
212 procedure Put
(Item
: in Wide_Character);
215 (File
: in File_Type
;
216 Item
: out Wide_Character;
217 End_Of_Line
: out Boolean);
220 (Item
: out Wide_Character;
221 End_Of_Line
: out Boolean);
223 procedure Get_Immediate
224 (File
: in File_Type
;
225 Item
: out Wide_Character);
227 procedure Get_Immediate
228 (Item
: out Wide_Character);
230 procedure Get_Immediate
231 (File
: in File_Type
;
232 Item
: out Wide_Character;
233 Available
: out Boolean);
235 procedure Get_Immediate
236 (Item
: out Wide_Character;
237 Available
: out Boolean);
239 -------------------------
240 -- String Input-Output --
241 -------------------------
243 procedure Get
(File
: in File_Type
; Item
: out Wide_String);
244 procedure Get
(Item
: out Wide_String);
245 procedure Put
(File
: in File_Type
; Item
: in Wide_String);
246 procedure Put
(Item
: in Wide_String);
249 (File
: in File_Type
;
250 Item
: out Wide_String;
254 (Item
: out Wide_String;
258 (File
: in File_Type
;
259 Item
: in Wide_String);
262 (Item
: in Wide_String);
264 ---------------------------------------
265 -- Generic packages for Input-Output --
266 ---------------------------------------
268 -- The generic packages:
270 -- Ada.Wide_Text_IO.Integer_IO
271 -- Ada.Wide_Text_IO.Modular_IO
272 -- Ada.Wide_Text_IO.Float_IO
273 -- Ada.Wide_Text_IO.Fixed_IO
274 -- Ada.Wide_Text_IO.Decimal_IO
275 -- Ada.Wide_Text_IO.Enumeration_IO
277 -- are implemented as separate child packages in GNAT, so the
278 -- spec and body of these packages are to be found in separate
279 -- child units. This implementation detail is hidden from the
280 -- Ada programmer by special circuitry in the compiler that
281 -- treats these child packages as though they were nested in
282 -- Text_IO. The advantage of this special processing is that
283 -- the subsidiary routines needed if these generics are used
284 -- are not loaded when they are not used.
290 Status_Error
: exception renames IO_Exceptions
.Status_Error
;
291 Mode_Error
: exception renames IO_Exceptions
.Mode_Error
;
292 Name_Error
: exception renames IO_Exceptions
.Name_Error
;
293 Use_Error
: exception renames IO_Exceptions
.Use_Error
;
294 Device_Error
: exception renames IO_Exceptions
.Device_Error
;
295 End_Error
: exception renames IO_Exceptions
.End_Error
;
296 Data_Error
: exception renames IO_Exceptions
.Data_Error
;
297 Layout_Error
: exception renames IO_Exceptions
.Layout_Error
;
300 -----------------------------------
301 -- Handling of Format Characters --
302 -----------------------------------
304 -- Line marks are represented by the single character ASCII.LF (16#0A#).
305 -- In DOS and similar systems, underlying file translation takes care
306 -- of translating this to and from the standard CR/LF sequences used in
307 -- these operating systems to mark the end of a line. On output there is
308 -- always a line mark at the end of the last line, but on input, this
309 -- line mark can be omitted, and is implied by the end of file.
311 -- Page marks are represented by the single character ASCII.FF (16#0C#),
312 -- The page mark at the end of the file may be omitted, and is normally
313 -- omitted on output unless an explicit New_Page call is made before
314 -- closing the file. No page mark is added when a file is appended to,
315 -- so, in accordance with the permission in (RM A.10.2(4)), there may
316 -- or may not be a page mark separating preexising text in the file
317 -- from the new text to be written.
319 -- A file mark is marked by the physical end of file. In DOS translation
320 -- mode on input, an EOF character (SUB = 16#1A#) gets translated to the
321 -- physical end of file, so in effect this character is recognized as
322 -- marking the end of file in DOS and similar systems.
324 LM
: constant := Character'Pos (ASCII
.LF
);
327 PM
: constant := Character'Pos (ASCII
.FF
);
328 -- Used as page mark, except at end of file where it is implied
330 -------------------------------------
331 -- Wide_Text_IO File Control Block --
332 -------------------------------------
334 Default_WCEM
: WCh_Con
.WC_Encoding_Method
:= WCh_Con
.WCEM_UTF8
;
335 -- This gets modified during initialization (see body) using
336 -- the default value established in the call to Set_Globals.
338 package FCB
renames System
.File_Control_Block
;
340 type Wide_Text_AFCB
is new FCB
.AFCB
with record
344 Line_Length
: Count
:= 0;
345 Page_Length
: Count
:= 0;
347 Before_LM
: Boolean := False;
348 -- This flag is used to deal with the anomolies introduced by the
349 -- peculiar definition of End_Of_File and End_Of_Page in Ada. These
350 -- functions require looking ahead more than one character. Since
351 -- there is no convenient way of backing up more than one character,
352 -- what we do is to leave ourselves positioned past the LM, but set
353 -- this flag, so that we know that from an Ada point of view we are
354 -- in front of the LM, not after it. A bit of a kludge, but it works!
356 Before_LM_PM
: Boolean := False;
357 -- This flag similarly handles the case of being physically positioned
358 -- after a LM-PM sequence when logically we are before the LM-PM. This
359 -- flag can only be set if Before_LM is also set.
361 WC_Method
: WCh_Con
.WC_Encoding_Method
:= Default_WCEM
;
362 -- Encoding method to be used for this file
364 Before_Wide_Character
: Boolean := False;
365 -- This flag is set to indicate that a wide character in the input has
366 -- been read by Wide_Text_IO.Look_Ahead. If it is set to True, then it
367 -- means that the stream is logically positioned before the character
368 -- but is physically positioned after it. The character involved must
369 -- not be in the range 16#00#-16#7F#, i.e. if the flag is set, then
370 -- we know the next character has a code greater than 16#7F#, and the
371 -- value of this character is saved in Saved_Wide_Character.
373 Saved_Wide_Character
: Wide_Character;
374 -- This field is valid only if Before_Wide_Character is set. It
375 -- contains a wide character read by Look_Ahead. If Look_Ahead
376 -- reads a character in the range 16#0000# to 16#007F#, then it
377 -- can use ungetc to put it back, but ungetc cannot be called
378 -- more than once, so for characters above this range, we don't
379 -- try to back up the file. Instead we save the character in this
380 -- field and set the flag Before_Wide_Character to indicate that
381 -- we are logically positioned before this character even though
382 -- the stream is physically positioned after it.
386 type File_Type
is access all Wide_Text_AFCB
;
388 function AFCB_Allocate
(Control_Block
: Wide_Text_AFCB
) return FCB
.AFCB_Ptr
;
390 procedure AFCB_Close
(File
: access Wide_Text_AFCB
);
391 procedure AFCB_Free
(File
: access Wide_Text_AFCB
);
394 (File
: in out Wide_Text_AFCB
;
395 Item
: out Ada
.Streams
.Stream_Element_Array
;
396 Last
: out Ada
.Streams
.Stream_Element_Offset
);
397 -- Read operation used when Wide_Text_IO file is treated as a Stream
400 (File
: in out Wide_Text_AFCB
;
401 Item
: in Ada
.Streams
.Stream_Element_Array
);
402 -- Write operation used when Wide_Text_IO file is treated as a Stream
404 ------------------------
405 -- The Standard Files --
406 ------------------------
408 Null_Str
: aliased constant String := "";
409 -- Used as name and form of standard files
411 Standard_Err_AFCB
: aliased Wide_Text_AFCB
;
412 Standard_In_AFCB
: aliased Wide_Text_AFCB
;
413 Standard_Out_AFCB
: aliased Wide_Text_AFCB
;
415 Standard_Err
: aliased File_Type
:= Standard_Err_AFCB
'Access;
416 Standard_In
: aliased File_Type
:= Standard_In_AFCB
'Access;
417 Standard_Out
: aliased File_Type
:= Standard_Out_AFCB
'Access;
420 Current_In
: aliased File_Type
:= Standard_In
;
421 Current_Out
: aliased File_Type
:= Standard_Out
;
422 Current_Err
: aliased File_Type
:= Standard_Err
;
425 -----------------------
426 -- Local Subprograms --
427 -----------------------
429 -- These subprograms are in the private part of the spec so that they can
430 -- be shared by the routines in the body of Ada.Text_IO.Wide_Text_IO.
432 -- Note: we use Integer in these declarations instead of the more accurate
433 -- Interfaces.C_Streams.int, because we do not want to drag in the spec of
434 -- this interfaces package with the spec of Ada.Text_IO, and we know that
435 -- in fact these types are identical
437 function Getc
(File
: File_Type
) return Integer;
438 -- Gets next character from file, which has already been checked for
439 -- being in read status, and returns the character read if no error
440 -- occurs. The result is EOF if the end of file was read.
442 procedure Get_Character
443 (File
: in File_Type
;
444 Item
: out Character);
445 -- This is essentially a copy of the normal Get routine from Text_IO. It
446 -- obtains a single character from the input file File, and places it in
447 -- Item. This character may be the leading character of a Wide_Character
448 -- sequence, but that is up to the caller to deal with.
450 function Get_Wide_Char
453 return Wide_Character;
454 -- This function is shared by Get and Get_Immediate to extract a wide
455 -- character value from the given File. The first byte has already been
456 -- read and is passed in C. The wide character value is returned as the
457 -- result, and the file pointer is bumped past the character.
459 function Nextc
(File
: File_Type
) return Integer;
460 -- Returns next character from file without skipping past it (i.e. it
461 -- is a combination of Getc followed by an Ungetc).
463 procedure Putc
(ch
: Integer; File
: File_Type
);
464 -- Outputs the given character to the file, which has already been
465 -- checked for being in output status. Device_Error is raised if the
466 -- character cannot be written.
468 procedure Terminate_Line
(File
: File_Type
);
469 -- If the file is in Write_File or Append_File mode, and the current
470 -- line is not terminated, then a line terminator is written using
471 -- New_Line. Note that there is no Terminate_Page routine, because
472 -- the page mark at the end of the file is implied if necessary.
474 procedure Ungetc
(ch
: Integer; File
: File_Type
);
475 -- Pushes back character into stream, using ungetc. The caller has
476 -- checked that the file is in read status. Device_Error is raised
477 -- if the character cannot be pushed back. An attempt to push back
478 -- and end of file character (EOF) is ignored.
480 end Ada
.Wide_Text_IO
;