1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . T E X T _ I O --
9 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Ada.Streams; use Ada.Streams;
35 with Interfaces.C_Streams; use Interfaces.C_Streams;
38 with Unchecked_Conversion;
39 with Unchecked_Deallocation;
41 pragma Elaborate_All (System.File_IO);
42 -- Needed because of calls to Chain_File in package body elaboration
44 package body Ada.Text_IO is
46 package FIO renames System.File_IO;
48 subtype AP is FCB.AFCB_Ptr;
50 function To_FCB is new Unchecked_Conversion (File_Mode, FCB.File_Mode);
51 function To_TIO is new Unchecked_Conversion (FCB.File_Mode, File_Mode);
52 use type FCB.File_Mode;
58 function AFCB_Allocate (Control_Block : Text_AFCB) return FCB.AFCB_Ptr is
59 pragma Warnings (Off, Control_Block);
69 procedure AFCB_Close (File : access Text_AFCB) is
71 -- If the file being closed is one of the current files, then close
72 -- the corresponding current file. It is not clear that this action
73 -- is required (RM A.10.3(23)) but it seems reasonable, and besides
74 -- ACVC test CE3208A expects this behavior.
76 if File_Type (File) = Current_In then
78 elsif File_Type (File) = Current_Out then
80 elsif File_Type (File) = Current_Err then
84 Terminate_Line (File_Type (File));
91 procedure AFCB_Free (File : access Text_AFCB) is
92 type FCB_Ptr is access all Text_AFCB;
93 FT : FCB_Ptr := FCB_Ptr (File);
95 procedure Free is new Unchecked_Deallocation (Text_AFCB, FCB_Ptr);
105 procedure Close (File : in out File_Type) is
107 FIO.Close (AP (File));
114 -- Note: we assume that it is impossible in practice for the column
115 -- to exceed the value of Count'Last, i.e. no check is required for
116 -- overflow raising layout error.
118 function Col (File : in File_Type) return Positive_Count is
120 FIO.Check_File_Open (AP (File));
124 function Col return Positive_Count is
126 return Col (Current_Out);
134 (File : in out File_Type;
135 Mode : in File_Mode := Out_File;
136 Name : in String := "";
137 Form : in String := "")
139 File_Control_Block : Text_AFCB;
142 FIO.Open (File_Ptr => AP (File),
143 Dummy_FCB => File_Control_Block,
144 Mode => To_FCB (Mode),
158 function Current_Error return File_Type is
163 function Current_Error return File_Access is
165 return Current_Err.Self'Access;
172 function Current_Input return File_Type is
177 function Current_Input return File_Access is
179 return Current_In.Self'Access;
186 function Current_Output return File_Type is
191 function Current_Output return File_Access is
193 return Current_Out.Self'Access;
200 procedure Delete (File : in out File_Type) is
202 FIO.Delete (AP (File));
209 function End_Of_File (File : in File_Type) return Boolean is
213 FIO.Check_Read_Status (AP (File));
215 if File.Before_LM then
217 if File.Before_LM_PM then
218 return Nextc (File) = EOF;
232 File.Before_LM := True;
236 -- Here we are just past the line mark with Before_LM set so that we
237 -- do not have to try to back up past the LM, thus avoiding the need
238 -- to back up more than one character.
245 elsif ch = PM and then File.Is_Regular_File then
246 File.Before_LM_PM := True;
247 return Nextc (File) = EOF;
249 -- Here if neither EOF nor PM followed end of line
258 function End_Of_File return Boolean is
260 return End_Of_File (Current_In);
267 function End_Of_Line (File : in File_Type) return Boolean is
271 FIO.Check_Read_Status (AP (File));
273 if File.Before_LM then
289 function End_Of_Line return Boolean is
291 return End_Of_Line (Current_In);
298 function End_Of_Page (File : in File_Type) return Boolean is
302 FIO.Check_Read_Status (AP (File));
304 if not File.Is_Regular_File then
307 elsif File.Before_LM then
308 if File.Before_LM_PM then
323 File.Before_LM := True;
327 -- Here we are just past the line mark with Before_LM set so that we
328 -- do not have to try to back up past the LM, thus avoiding the need
329 -- to back up more than one character.
333 return ch = PM or else ch = EOF;
336 function End_Of_Page return Boolean is
338 return End_Of_Page (Current_In);
345 procedure Flush (File : in File_Type) is
347 FIO.Flush (AP (File));
359 function Form (File : in File_Type) return String is
361 return FIO.Form (AP (File));
369 (File : in File_Type;
370 Item : out Character)
375 FIO.Check_Read_Status (AP (File));
377 if File.Before_LM then
378 File.Before_LM := False;
381 if File.Before_LM_PM then
383 File.Page := File.Page + 1;
384 File.Before_LM_PM := False;
386 File.Line := File.Line + 1;
397 File.Line := File.Line + 1;
400 elsif ch = PM and then File.Is_Regular_File then
401 File.Page := File.Page + 1;
405 Item := Character'Val (ch);
406 File.Col := File.Col + 1;
412 procedure Get (Item : out Character) is
414 Get (Current_In, Item);
418 (File : in File_Type;
425 FIO.Check_Read_Status (AP (File));
427 if File.Before_LM then
428 File.Before_LM := False;
429 File.Before_LM_PM := False;
432 if File.Before_LM_PM then
434 File.Page := File.Page + 1;
435 File.Before_LM_PM := False;
438 File.Line := File.Line + 1;
443 while J <= Item'Last loop
450 File.Line := File.Line + 1;
453 elsif ch = PM and then File.Is_Regular_File then
454 File.Page := File.Page + 1;
458 Item (J) := Character'Val (ch);
460 File.Col := File.Col + 1;
465 procedure Get (Item : out String) is
467 Get (Current_In, Item);
474 -- More work required here ???
476 procedure Get_Immediate
477 (File : in File_Type;
478 Item : out Character)
483 procedure getc_immediate
484 (stream : FILEs; ch : out int; end_of_file : out int);
485 pragma Import (C, getc_immediate, "getc_immediate");
488 FIO.Check_Read_Status (AP (File));
490 if File.Before_LM then
491 File.Before_LM := False;
492 File.Before_LM_PM := False;
496 getc_immediate (File.Stream, ch, end_of_file);
498 if ferror (File.Stream) /= 0 then
500 elsif end_of_file /= 0 then
505 Item := Character'Val (ch);
509 procedure Get_Immediate
510 (Item : out Character)
513 Get_Immediate (Current_In, Item);
516 procedure Get_Immediate
517 (File : in File_Type;
518 Item : out Character;
519 Available : out Boolean)
525 procedure getc_immediate_nowait
528 end_of_file : out int;
530 pragma Import (C, getc_immediate_nowait, "getc_immediate_nowait");
533 FIO.Check_Read_Status (AP (File));
535 -- If we are logically before an end of line, but physically after it,
536 -- then we just return the end of line character, no I/O is necessary.
538 if File.Before_LM then
539 File.Before_LM := False;
540 File.Before_LM_PM := False;
543 Item := Character'Val (LM);
545 -- Normal case where a read operation is required
548 getc_immediate_nowait (File.Stream, ch, end_of_file, avail);
550 if ferror (File.Stream) /= 0 then
553 elsif end_of_file /= 0 then
562 Item := Character'Val (ch);
568 procedure Get_Immediate
569 (Item : out Character;
570 Available : out Boolean)
573 Get_Immediate (Current_In, Item, Available);
581 (File : in File_Type;
588 FIO.Check_Read_Status (AP (File));
589 Last := Item'First - 1;
591 -- Immediate exit for null string, this is a case in which we do not
592 -- need to test for end of file and we do not skip a line mark under
593 -- any circumstances.
595 if Last >= Item'Last then
599 -- Here we have at least one character, if we are immediately before
600 -- a line mark, then we will just skip past it storing no characters.
602 if File.Before_LM then
603 File.Before_LM := False;
604 File.Before_LM_PM := False;
606 -- Otherwise we need to read some characters
611 -- If we are at the end of file now, it means we are trying to
612 -- skip a file terminator and we raise End_Error (RM A.10.7(20))
618 -- Loop through characters. Don't bother if we hit a page mark,
619 -- since in normal files, page marks can only follow line marks
620 -- in any case and we only promise to treat the page nonsense
621 -- correctly in the absense of such rogue page marks.
624 -- Exit the loop if read is terminated by encountering line mark
628 -- Otherwise store the character, note that we know that ch is
629 -- something other than LM or EOF. It could possibly be a page
630 -- mark if there is a stray page mark in the middle of a line,
631 -- but this is not an official page mark in any case, since
632 -- official page marks can only follow a line mark. The whole
633 -- page business is pretty much nonsense anyway, so we do not
634 -- want to waste time trying to make sense out of non-standard
635 -- page marks in the file! This means that the behavior of
636 -- Get_Line is different from repeated Get of a character, but
637 -- that's too bad. We only promise that page numbers etc make
638 -- sense if the file is formatted in a standard manner.
640 -- Note: we do not adjust the column number because it is quicker
641 -- to adjust it once at the end of the operation than incrementing
642 -- it each time around the loop.
645 Item (Last) := Character'Val (ch);
647 -- All done if the string is full, this is the case in which
648 -- we do not skip the following line mark. We need to adjust
649 -- the column number in this case.
651 if Last = Item'Last then
652 File.Col := File.Col + Count (Item'Length);
656 -- Otherwise read next character. We also exit from the loop if
657 -- we read an end of file. This is the case where the last line
658 -- is not terminated with a line mark, and we consider that there
659 -- is an implied line mark in this case (this is a non-standard
660 -- file, but it is nice to treat it reasonably).
667 -- We have skipped past, but not stored, a line mark. Skip following
668 -- page mark if one follows, but do not do this for a non-regular
669 -- file (since otherwise we get annoying wait for an extra character)
671 File.Line := File.Line + 1;
674 if File.Before_LM_PM then
676 File.Before_LM_PM := False;
677 File.Page := File.Page + 1;
679 elsif File.Is_Regular_File then
682 if ch = PM and then File.Is_Regular_File then
684 File.Page := File.Page + 1;
696 Get_Line (Current_In, Item, Last);
703 function Getc (File : File_Type) return int is
707 ch := fgetc (File.Stream);
709 if ch = EOF and then ferror (File.Stream) /= 0 then
720 function Is_Open (File : in File_Type) return Boolean is
722 return FIO.Is_Open (AP (File));
729 -- Note: we assume that it is impossible in practice for the line
730 -- to exceed the value of Count'Last, i.e. no check is required for
731 -- overflow raising layout error.
733 function Line (File : in File_Type) return Positive_Count is
735 FIO.Check_File_Open (AP (File));
739 function Line return Positive_Count is
741 return Line (Current_Out);
748 function Line_Length (File : in File_Type) return Count is
750 FIO.Check_Write_Status (AP (File));
751 return File.Line_Length;
754 function Line_Length return Count is
756 return Line_Length (Current_Out);
764 (File : in File_Type;
765 Item : out Character;
766 End_Of_Line : out Boolean)
771 FIO.Check_Read_Status (AP (File));
773 if File.Before_LM then
782 or else (ch = PM and then File.Is_Regular_File)
787 End_Of_Line := False;
788 Item := Character'Val (ch);
794 (Item : out Character;
795 End_Of_Line : out Boolean)
798 Look_Ahead (Current_In, Item, End_Of_Line);
805 function Mode (File : in File_Type) return File_Mode is
807 return To_TIO (FIO.Mode (AP (File)));
814 function Name (File : in File_Type) return String is
816 return FIO.Name (AP (File));
824 (File : in File_Type;
825 Spacing : in Positive_Count := 1)
828 -- Raise Constraint_Error if out of range value. The reason for this
829 -- explicit test is that we don't want junk values around, even if
830 -- checks are off in the caller.
832 if Spacing not in Positive_Count then
833 raise Constraint_Error;
836 FIO.Check_Write_Status (AP (File));
838 for K in 1 .. Spacing loop
840 File.Line := File.Line + 1;
842 if File.Page_Length /= 0
843 and then File.Line > File.Page_Length
847 File.Page := File.Page + 1;
854 procedure New_Line (Spacing : in Positive_Count := 1) is
856 New_Line (Current_Out, Spacing);
863 procedure New_Page (File : in File_Type) is
865 FIO.Check_Write_Status (AP (File));
867 if File.Col /= 1 or else File.Line = 1 then
872 File.Page := File.Page + 1;
877 procedure New_Page is
879 New_Page (Current_Out);
886 function Nextc (File : File_Type) return int is
890 ch := fgetc (File.Stream);
893 if ferror (File.Stream) /= 0 then
898 if ungetc (ch, File.Stream) = EOF then
911 (File : in out File_Type;
914 Form : in String := "")
916 File_Control_Block : Text_AFCB;
919 FIO.Open (File_Ptr => AP (File),
920 Dummy_FCB => File_Control_Block,
921 Mode => To_FCB (Mode),
935 -- Note: we assume that it is impossible in practice for the page
936 -- to exceed the value of Count'Last, i.e. no check is required for
937 -- overflow raising layout error.
939 function Page (File : in File_Type) return Positive_Count is
941 FIO.Check_File_Open (AP (File));
945 function Page return Positive_Count is
947 return Page (Current_Out);
954 function Page_Length (File : in File_Type) return Count is
956 FIO.Check_Write_Status (AP (File));
957 return File.Page_Length;
960 function Page_Length return Count is
962 return Page_Length (Current_Out);
970 (File : in File_Type;
974 FIO.Check_Write_Status (AP (File));
976 if File.Line_Length /= 0 and then File.Col > File.Line_Length then
980 if fputc (Character'Pos (Item), File.Stream) = EOF then
984 File.Col := File.Col + 1;
987 procedure Put (Item : in Character) is
989 FIO.Check_Write_Status (AP (Current_Out));
991 if Current_Out.Line_Length /= 0
992 and then Current_Out.Col > Current_Out.Line_Length
994 New_Line (Current_Out);
997 if fputc (Character'Pos (Item), Current_Out.Stream) = EOF then
1001 Current_Out.Col := Current_Out.Col + 1;
1009 (File : in File_Type;
1013 FIO.Check_Write_Status (AP (File));
1015 if Item'Length > 0 then
1017 -- If we have bounded lines, then do things character by
1018 -- character (this seems a rare case anyway!)
1020 if File.Line_Length /= 0 then
1021 for J in Item'Range loop
1022 Put (File, Item (J));
1025 -- Otherwise we can output the entire string at once. Note that if
1026 -- there are LF or FF characters in the string, we do not bother to
1027 -- count them as line or page terminators.
1030 FIO.Write_Buf (AP (File), Item'Address, Item'Length);
1031 File.Col := File.Col + Item'Length;
1036 procedure Put (Item : in String) is
1038 Put (Current_Out, Item);
1046 (File : in File_Type;
1050 FIO.Check_Write_Status (AP (File));
1052 -- If we have bounded lines, then just do a put and a new line. In
1053 -- this case we will end up doing things character by character in
1054 -- any case, and it is a rare situation.
1056 if File.Line_Length /= 0 then
1062 -- We setup a single string that has the necessary terminators and
1063 -- then write it with a single call. The reason for doing this is
1064 -- that it gives better behavior for the use of Put_Line in multi-
1065 -- tasking programs, since often the OS will treat the entire put
1066 -- operation as an atomic operation.
1069 Ilen : constant Natural := Item'Length;
1070 Buffer : String (1 .. Ilen + 2);
1074 Buffer (1 .. Ilen) := Item;
1075 Buffer (Ilen + 1) := Character'Val (LM);
1077 if File.Page_Length /= 0
1078 and then File.Line > File.Page_Length
1080 Buffer (Ilen + 2) := Character'Val (PM);
1081 Plen := size_t (Ilen) + 2;
1083 File.Page := File.Page + 1;
1086 Plen := size_t (Ilen) + 1;
1087 File.Line := File.Line + 1;
1090 FIO.Write_Buf (AP (File), Buffer'Address, Plen);
1096 procedure Put_Line (Item : in String) is
1098 Put_Line (Current_Out, Item);
1105 procedure Putc (ch : int; File : File_Type) is
1107 if fputc (ch, File.Stream) = EOF then
1116 -- This is the primitive Stream Read routine, used when a Text_IO file
1117 -- is treated directly as a stream using Text_IO.Streams.Stream.
1120 (File : in out Text_AFCB;
1121 Item : out Stream_Element_Array;
1122 Last : out Stream_Element_Offset)
1127 if File.Mode /= FCB.In_File then
1131 -- Deal with case where our logical and physical position do not match
1132 -- because of being after an LM or LM-PM sequence when in fact we are
1133 -- logically positioned before it.
1135 if File.Before_LM then
1137 -- If we are before a PM, then it is possible for a stream read
1138 -- to leave us after the LM and before the PM, which is a bit
1139 -- odd. The easiest way to deal with this is to unget the PM,
1140 -- so we are indeed positioned between the characters. This way
1141 -- further stream read operations will work correctly, and the
1142 -- effect on text processing is a little weird, but what can
1143 -- be expected if stream and text input are mixed this way?
1145 if File.Before_LM_PM then
1146 ch := ungetc (PM, File.Stream);
1147 File.Before_LM_PM := False;
1150 File.Before_LM := False;
1152 Item (Item'First) := Stream_Element (Character'Pos (ASCII.LF));
1154 if Item'Length = 1 then
1160 Stream_Element_Offset
1161 (fread (buffer => Item'Address,
1162 index => size_t (Item'First + 1),
1164 count => Item'Length - 1,
1165 stream => File.Stream));
1171 -- Now we do the read. Since this is a text file, it is normally in
1172 -- text mode, but stream data must be read in binary mode, so we
1173 -- temporarily set binary mode for the read, resetting it after.
1174 -- These calls have no effect in a system (like Unix) where there is
1175 -- no distinction between text and binary files.
1177 set_binary_mode (fileno (File.Stream));
1181 Stream_Element_Offset
1182 (fread (Item'Address, 1, Item'Length, File.Stream)) - 1;
1184 if Last < Item'Last then
1185 if ferror (File.Stream) /= 0 then
1190 set_text_mode (fileno (File.Stream));
1198 (File : in out File_Type;
1199 Mode : in File_Mode)
1202 -- Don't allow change of mode for current file (RM A.10.2(5))
1204 if (File = Current_In or else
1205 File = Current_Out or else
1206 File = Current_Error)
1207 and then To_FCB (Mode) /= File.Mode
1212 Terminate_Line (File);
1213 FIO.Reset (AP (File), To_FCB (Mode));
1217 File.Line_Length := 0;
1218 File.Page_Length := 0;
1219 File.Before_LM := False;
1220 File.Before_LM_PM := False;
1223 procedure Reset (File : in out File_Type) is
1225 Terminate_Line (File);
1226 FIO.Reset (AP (File));
1230 File.Line_Length := 0;
1231 File.Page_Length := 0;
1232 File.Before_LM := False;
1233 File.Before_LM_PM := False;
1241 (File : in File_Type;
1242 To : in Positive_Count)
1247 -- Raise Constraint_Error if out of range value. The reason for this
1248 -- explicit test is that we don't want junk values around, even if
1249 -- checks are off in the caller.
1251 if To not in Positive_Count then
1252 raise Constraint_Error;
1255 FIO.Check_File_Open (AP (File));
1257 if To = File.Col then
1261 if Mode (File) >= Out_File then
1262 if File.Line_Length /= 0 and then To > File.Line_Length then
1266 if To < File.Col then
1270 while File.Col < To loop
1282 File.Line := File.Line + 1;
1285 elsif ch = PM and then File.Is_Regular_File then
1286 File.Page := File.Page + 1;
1290 elsif To = File.Col then
1295 File.Col := File.Col + 1;
1301 procedure Set_Col (To : in Positive_Count) is
1303 Set_Col (Current_Out, To);
1310 procedure Set_Error (File : in File_Type) is
1312 FIO.Check_Write_Status (AP (File));
1313 Current_Err := File;
1320 procedure Set_Input (File : in File_Type) is
1322 FIO.Check_Read_Status (AP (File));
1331 (File : in File_Type;
1332 To : in Positive_Count)
1335 -- Raise Constraint_Error if out of range value. The reason for this
1336 -- explicit test is that we don't want junk values around, even if
1337 -- checks are off in the caller.
1339 if To not in Positive_Count then
1340 raise Constraint_Error;
1343 FIO.Check_File_Open (AP (File));
1345 if To = File.Line then
1349 if Mode (File) >= Out_File then
1350 if File.Page_Length /= 0 and then To > File.Page_Length then
1354 if To < File.Line then
1358 while File.Line < To loop
1363 while To /= File.Line loop
1369 procedure Set_Line (To : in Positive_Count) is
1371 Set_Line (Current_Out, To);
1374 ---------------------
1375 -- Set_Line_Length --
1376 ---------------------
1378 procedure Set_Line_Length (File : in File_Type; To : in Count) is
1380 -- Raise Constraint_Error if out of range value. The reason for this
1381 -- explicit test is that we don't want junk values around, even if
1382 -- checks are off in the caller.
1384 if To not in Count then
1385 raise Constraint_Error;
1388 FIO.Check_Write_Status (AP (File));
1389 File.Line_Length := To;
1390 end Set_Line_Length;
1392 procedure Set_Line_Length (To : in Count) is
1394 Set_Line_Length (Current_Out, To);
1395 end Set_Line_Length;
1401 procedure Set_Output (File : in File_Type) is
1403 FIO.Check_Write_Status (AP (File));
1404 Current_Out := File;
1407 ---------------------
1408 -- Set_Page_Length --
1409 ---------------------
1411 procedure Set_Page_Length (File : in File_Type; To : in Count) is
1413 -- Raise Constraint_Error if out of range value. The reason for this
1414 -- explicit test is that we don't want junk values around, even if
1415 -- checks are off in the caller.
1417 if To not in Count then
1418 raise Constraint_Error;
1421 FIO.Check_Write_Status (AP (File));
1422 File.Page_Length := To;
1423 end Set_Page_Length;
1425 procedure Set_Page_Length (To : in Count) is
1427 Set_Page_Length (Current_Out, To);
1428 end Set_Page_Length;
1435 (File : in File_Type;
1436 Spacing : in Positive_Count := 1)
1441 -- Raise Constraint_Error if out of range value. The reason for this
1442 -- explicit test is that we don't want junk values around, even if
1443 -- checks are off in the caller.
1445 if Spacing not in Positive_Count then
1446 raise Constraint_Error;
1449 FIO.Check_Read_Status (AP (File));
1451 for L in 1 .. Spacing loop
1452 if File.Before_LM then
1453 File.Before_LM := False;
1454 File.Before_LM_PM := False;
1459 -- If at end of file now, then immediately raise End_Error. Note
1460 -- that we can never be positioned between a line mark and a page
1461 -- mark, so if we are at the end of file, we cannot logically be
1462 -- before the implicit page mark that is at the end of the file.
1464 -- For the same reason, we do not need an explicit check for a
1465 -- page mark. If there is a FF in the middle of a line, the file
1466 -- is not in canonical format and we do not care about the page
1467 -- numbers for files other than ones in canonical format.
1473 -- If not at end of file, then loop till we get to an LM or EOF.
1474 -- The latter case happens only in non-canonical files where the
1475 -- last line is not terminated by LM, but we don't want to blow
1476 -- up for such files, so we assume an implicit LM in this case.
1479 exit when ch = LM or ch = EOF;
1484 -- We have got past a line mark, now, for a regular file only,
1485 -- see if a page mark immediately follows this line mark and
1486 -- if so, skip past the page mark as well. We do not do this
1487 -- for non-regular files, since it would cause an undesirable
1488 -- wait for an additional character.
1491 File.Line := File.Line + 1;
1493 if File.Before_LM_PM then
1494 File.Page := File.Page + 1;
1496 File.Before_LM_PM := False;
1498 elsif File.Is_Regular_File then
1501 -- Page mark can be explicit, or implied at the end of the file
1503 if (ch = PM or else ch = EOF)
1504 and then File.Is_Regular_File
1506 File.Page := File.Page + 1;
1516 procedure Skip_Line (Spacing : in Positive_Count := 1) is
1518 Skip_Line (Current_In, Spacing);
1525 procedure Skip_Page (File : in File_Type) is
1529 FIO.Check_Read_Status (AP (File));
1531 -- If at page mark already, just skip it
1533 if File.Before_LM_PM then
1534 File.Before_LM := False;
1535 File.Before_LM_PM := False;
1536 File.Page := File.Page + 1;
1542 -- This is a bit tricky, if we are logically before an LM then
1543 -- it is not an error if we are at an end of file now, since we
1544 -- are not really at it.
1546 if File.Before_LM then
1547 File.Before_LM := False;
1548 File.Before_LM_PM := False;
1551 -- Otherwise we do raise End_Error if we are at the end of file now
1561 -- Now we can just rumble along to the next page mark, or to the
1562 -- end of file, if that comes first. The latter case happens when
1563 -- the page mark is implied at the end of file.
1567 or else (ch = PM and then File.Is_Regular_File);
1571 File.Page := File.Page + 1;
1576 procedure Skip_Page is
1578 Skip_Page (Current_In);
1581 --------------------
1582 -- Standard_Error --
1583 --------------------
1585 function Standard_Error return File_Type is
1587 return Standard_Err;
1590 function Standard_Error return File_Access is
1592 return Standard_Err'Access;
1595 --------------------
1596 -- Standard_Input --
1597 --------------------
1599 function Standard_Input return File_Type is
1604 function Standard_Input return File_Access is
1606 return Standard_In'Access;
1609 ---------------------
1610 -- Standard_Output --
1611 ---------------------
1613 function Standard_Output return File_Type is
1615 return Standard_Out;
1616 end Standard_Output;
1618 function Standard_Output return File_Access is
1620 return Standard_Out'Access;
1621 end Standard_Output;
1623 --------------------
1624 -- Terminate_Line --
1625 --------------------
1627 procedure Terminate_Line (File : File_Type) is
1629 FIO.Check_File_Open (AP (File));
1631 -- For file other than In_File, test for needing to terminate last line
1633 if Mode (File) /= In_File then
1635 -- If not at start of line definition need new line
1637 if File.Col /= 1 then
1640 -- For files other than standard error and standard output, we
1641 -- make sure that an empty file has a single line feed, so that
1642 -- it is properly formatted. We avoid this for the standard files
1643 -- because it is too much of a nuisance to have these odd line
1644 -- feeds when nothing has been written to the file.
1646 elsif (File /= Standard_Err and then File /= Standard_Out)
1647 and then (File.Line = 1 and then File.Page = 1)
1658 procedure Ungetc (ch : int; File : File_Type) is
1661 if ungetc (ch, File.Stream) = EOF then
1671 -- This is the primitive Stream Write routine, used when a Text_IO file
1672 -- is treated directly as a stream using Text_IO.Streams.Stream.
1675 (File : in out Text_AFCB;
1676 Item : in Stream_Element_Array)
1679 function Has_Translated_Characters return Boolean;
1680 -- return True if Item array contains a character which will be
1681 -- translated under the text file mode. There is only one such
1682 -- character under DOS based systems which is character 10.
1684 text_translation_required : Boolean;
1685 pragma Import (C, text_translation_required,
1686 "__gnat_text_translation_required");
1688 Siz : constant size_t := Item'Length;
1690 function Has_Translated_Characters return Boolean is
1692 for K in Item'Range loop
1693 if Item (K) = 10 then
1698 end Has_Translated_Characters;
1700 Needs_Binary_Write : constant Boolean :=
1701 text_translation_required and then Has_Translated_Characters;
1704 if File.Mode = FCB.In_File then
1708 -- Now we do the write. Since this is a text file, it is normally in
1709 -- text mode, but stream data must be written in binary mode, so we
1710 -- temporarily set binary mode for the write, resetting it after. This
1711 -- is done only if needed (i.e. there is some characters in Item which
1712 -- needs to be written using the binary mode).
1713 -- These calls have no effect in a system (like Unix) where there is
1714 -- no distinction between text and binary files.
1716 -- Since the character translation is done at the time the buffer is
1717 -- written (this is true under Windows) we first flush current buffer
1718 -- with text mode if needed.
1720 if Needs_Binary_Write then
1722 if fflush (File.Stream) = -1 then
1726 set_binary_mode (fileno (File.Stream));
1729 if fwrite (Item'Address, 1, Siz, File.Stream) /= Siz then
1733 -- At this point we need to flush the buffer using the binary mode then
1734 -- we reset to text mode.
1736 if Needs_Binary_Write then
1738 if fflush (File.Stream) = -1 then
1742 set_text_mode (fileno (File.Stream));
1746 -- Use "preallocated" strings to avoid calling "new" during the
1747 -- elaboration of the run time. This is needed in the tasking case to
1748 -- avoid calling Task_Lock too early. A filename is expected to end with a
1749 -- null character in the runtime, here the null characters are added just
1750 -- to have a correct filename length.
1752 Err_Name : aliased String := "*stderr" & ASCII.Nul;
1753 In_Name : aliased String := "*stdin" & ASCII.Nul;
1754 Out_Name : aliased String := "*stdout" & ASCII.Nul;
1756 -------------------------------
1757 -- Initialize Standard Files --
1758 -------------------------------
1760 -- Note: the names in these files are bogus, and probably it would be
1761 -- better for these files to have no names, but the ACVC test insist!
1762 -- We use names that are bound to fail in open etc.
1764 Standard_Err.Stream := stderr;
1765 Standard_Err.Name := Err_Name'Access;
1766 Standard_Err.Form := Null_Str'Unrestricted_Access;
1767 Standard_Err.Mode := FCB.Out_File;
1768 Standard_Err.Is_Regular_File := is_regular_file (fileno (stderr)) /= 0;
1769 Standard_Err.Is_Temporary_File := False;
1770 Standard_Err.Is_System_File := True;
1771 Standard_Err.Is_Text_File := True;
1772 Standard_Err.Access_Method := 'T';
1773 Standard_Err.Self := Standard_Err;
1775 Standard_In.Stream := stdin;
1776 Standard_In.Name := In_Name'Access;
1777 Standard_In.Form := Null_Str'Unrestricted_Access;
1778 Standard_In.Mode := FCB.In_File;
1779 Standard_In.Is_Regular_File := is_regular_file (fileno (stdin)) /= 0;
1780 Standard_In.Is_Temporary_File := False;
1781 Standard_In.Is_System_File := True;
1782 Standard_In.Is_Text_File := True;
1783 Standard_In.Access_Method := 'T';
1784 Standard_In.Self := Standard_In;
1786 Standard_Out.Stream := stdout;
1787 Standard_Out.Name := Out_Name'Access;
1788 Standard_Out.Form := Null_Str'Unrestricted_Access;
1789 Standard_Out.Mode := FCB.Out_File;
1790 Standard_Out.Is_Regular_File := is_regular_file (fileno (stdout)) /= 0;
1791 Standard_Out.Is_Temporary_File := False;
1792 Standard_Out.Is_System_File := True;
1793 Standard_Out.Is_Text_File := True;
1794 Standard_Out.Access_Method := 'T';
1795 Standard_Out.Self := Standard_Out;
1797 FIO.Chain_File (AP (Standard_In));
1798 FIO.Chain_File (AP (Standard_Out));
1799 FIO.Chain_File (AP (Standard_Err));
1801 FIO.Make_Unbuffered (AP (Standard_Out));
1802 FIO.Make_Unbuffered (AP (Standard_Err));