1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . T E X T _ I O --
9 -- Copyright (C) 1992-2003 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 Unreferenced
(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 Dummy_File_Control_Block
: Text_AFCB
;
140 pragma Warnings
(Off
, Dummy_File_Control_Block
);
141 -- Yes, we know this is never assigned a value, only the tag
142 -- is used for dispatching purposes, so that's expected.
145 FIO
.Open
(File_Ptr
=> AP
(File
),
146 Dummy_FCB
=> Dummy_File_Control_Block
,
147 Mode
=> To_FCB
(Mode
),
161 function Current_Error
return File_Type
is
166 function Current_Error
return File_Access
is
168 return Current_Err
.Self
'Access;
175 function Current_Input
return File_Type
is
180 function Current_Input
return File_Access
is
182 return Current_In
.Self
'Access;
189 function Current_Output
return File_Type
is
194 function Current_Output
return File_Access
is
196 return Current_Out
.Self
'Access;
203 procedure Delete
(File
: in out File_Type
) is
205 FIO
.Delete
(AP
(File
));
212 function End_Of_File
(File
: in File_Type
) return Boolean is
216 FIO
.Check_Read_Status
(AP
(File
));
218 if File
.Before_LM
then
220 if File
.Before_LM_PM
then
221 return Nextc
(File
) = EOF
;
235 File
.Before_LM
:= True;
239 -- Here we are just past the line mark with Before_LM set so that we
240 -- do not have to try to back up past the LM, thus avoiding the need
241 -- to back up more than one character.
248 elsif ch
= PM
and then File
.Is_Regular_File
then
249 File
.Before_LM_PM
:= True;
250 return Nextc
(File
) = EOF
;
252 -- Here if neither EOF nor PM followed end of line
261 function End_Of_File
return Boolean is
263 return End_Of_File
(Current_In
);
270 function End_Of_Line
(File
: in File_Type
) return Boolean is
274 FIO
.Check_Read_Status
(AP
(File
));
276 if File
.Before_LM
then
292 function End_Of_Line
return Boolean is
294 return End_Of_Line
(Current_In
);
301 function End_Of_Page
(File
: in File_Type
) return Boolean is
305 FIO
.Check_Read_Status
(AP
(File
));
307 if not File
.Is_Regular_File
then
310 elsif File
.Before_LM
then
311 if File
.Before_LM_PM
then
326 File
.Before_LM
:= True;
330 -- Here we are just past the line mark with Before_LM set so that we
331 -- do not have to try to back up past the LM, thus avoiding the need
332 -- to back up more than one character.
336 return ch
= PM
or else ch
= EOF
;
339 function End_Of_Page
return Boolean is
341 return End_Of_Page
(Current_In
);
348 function EOF_Char
return Integer is
357 procedure Flush
(File
: in File_Type
) is
359 FIO
.Flush
(AP
(File
));
371 function Form
(File
: in File_Type
) return String is
373 return FIO
.Form
(AP
(File
));
381 (File
: in File_Type
;
382 Item
: out Character)
387 FIO
.Check_Read_Status
(AP
(File
));
389 if File
.Before_LM
then
390 File
.Before_LM
:= False;
393 if File
.Before_LM_PM
then
395 File
.Page
:= File
.Page
+ 1;
396 File
.Before_LM_PM
:= False;
398 File
.Line
:= File
.Line
+ 1;
409 File
.Line
:= File
.Line
+ 1;
412 elsif ch
= PM
and then File
.Is_Regular_File
then
413 File
.Page
:= File
.Page
+ 1;
417 Item
:= Character'Val (ch
);
418 File
.Col
:= File
.Col
+ 1;
424 procedure Get
(Item
: out Character) is
426 Get
(Current_In
, Item
);
430 (File
: in File_Type
;
437 FIO
.Check_Read_Status
(AP
(File
));
439 if File
.Before_LM
then
440 File
.Before_LM
:= False;
441 File
.Before_LM_PM
:= False;
444 if File
.Before_LM_PM
then
446 File
.Page
:= File
.Page
+ 1;
447 File
.Before_LM_PM
:= False;
450 File
.Line
:= File
.Line
+ 1;
455 while J
<= Item
'Last loop
462 File
.Line
:= File
.Line
+ 1;
465 elsif ch
= PM
and then File
.Is_Regular_File
then
466 File
.Page
:= File
.Page
+ 1;
470 Item
(J
) := Character'Val (ch
);
472 File
.Col
:= File
.Col
+ 1;
477 procedure Get
(Item
: out String) is
479 Get
(Current_In
, Item
);
486 -- More work required here ???
488 procedure Get_Immediate
489 (File
: in File_Type
;
490 Item
: out Character)
495 procedure getc_immediate
498 end_of_file
: out int
);
499 pragma Import
(C
, getc_immediate
, "getc_immediate");
502 FIO
.Check_Read_Status
(AP
(File
));
504 if File
.Before_LM
then
505 File
.Before_LM
:= False;
506 File
.Before_LM_PM
:= False;
510 getc_immediate
(File
.Stream
, ch
, end_of_file
);
512 if ferror
(File
.Stream
) /= 0 then
514 elsif end_of_file
/= 0 then
519 Item
:= Character'Val (ch
);
522 procedure Get_Immediate
523 (Item
: out Character)
526 Get_Immediate
(Current_In
, Item
);
529 procedure Get_Immediate
530 (File
: in File_Type
;
531 Item
: out Character;
532 Available
: out Boolean)
538 procedure getc_immediate_nowait
541 end_of_file
: out int
;
543 pragma Import
(C
, getc_immediate_nowait
, "getc_immediate_nowait");
546 FIO
.Check_Read_Status
(AP
(File
));
548 -- If we are logically before an end of line, but physically after it,
549 -- then we just return the end of line character, no I/O is necessary.
551 if File
.Before_LM
then
552 File
.Before_LM
:= False;
553 File
.Before_LM_PM
:= False;
556 Item
:= Character'Val (LM
);
558 -- Normal case where a read operation is required
561 getc_immediate_nowait
(File
.Stream
, ch
, end_of_file
, avail
);
563 if ferror
(File
.Stream
) /= 0 then
566 elsif end_of_file
/= 0 then
575 Item
:= Character'Val (ch
);
581 procedure Get_Immediate
582 (Item
: out Character;
583 Available
: out Boolean)
586 Get_Immediate
(Current_In
, Item
, Available
);
594 (File
: in File_Type
;
601 FIO
.Check_Read_Status
(AP
(File
));
602 Last
:= Item
'First - 1;
604 -- Immediate exit for null string, this is a case in which we do not
605 -- need to test for end of file and we do not skip a line mark under
606 -- any circumstances.
608 if Last
>= Item
'Last then
612 -- Here we have at least one character, if we are immediately before
613 -- a line mark, then we will just skip past it storing no characters.
615 if File
.Before_LM
then
616 File
.Before_LM
:= False;
617 File
.Before_LM_PM
:= False;
619 -- Otherwise we need to read some characters
624 -- If we are at the end of file now, it means we are trying to
625 -- skip a file terminator and we raise End_Error (RM A.10.7(20))
631 -- Loop through characters. Don't bother if we hit a page mark,
632 -- since in normal files, page marks can only follow line marks
633 -- in any case and we only promise to treat the page nonsense
634 -- correctly in the absense of such rogue page marks.
637 -- Exit the loop if read is terminated by encountering line mark
641 -- Otherwise store the character, note that we know that ch is
642 -- something other than LM or EOF. It could possibly be a page
643 -- mark if there is a stray page mark in the middle of a line,
644 -- but this is not an official page mark in any case, since
645 -- official page marks can only follow a line mark. The whole
646 -- page business is pretty much nonsense anyway, so we do not
647 -- want to waste time trying to make sense out of non-standard
648 -- page marks in the file! This means that the behavior of
649 -- Get_Line is different from repeated Get of a character, but
650 -- that's too bad. We only promise that page numbers etc make
651 -- sense if the file is formatted in a standard manner.
653 -- Note: we do not adjust the column number because it is quicker
654 -- to adjust it once at the end of the operation than incrementing
655 -- it each time around the loop.
658 Item
(Last
) := Character'Val (ch
);
660 -- All done if the string is full, this is the case in which
661 -- we do not skip the following line mark. We need to adjust
662 -- the column number in this case.
664 if Last
= Item
'Last then
665 File
.Col
:= File
.Col
+ Count
(Item
'Length);
669 -- Otherwise read next character. We also exit from the loop if
670 -- we read an end of file. This is the case where the last line
671 -- is not terminated with a line mark, and we consider that there
672 -- is an implied line mark in this case (this is a non-standard
673 -- file, but it is nice to treat it reasonably).
680 -- We have skipped past, but not stored, a line mark. Skip following
681 -- page mark if one follows, but do not do this for a non-regular
682 -- file (since otherwise we get annoying wait for an extra character)
684 File
.Line
:= File
.Line
+ 1;
687 if File
.Before_LM_PM
then
689 File
.Before_LM_PM
:= False;
690 File
.Page
:= File
.Page
+ 1;
692 elsif File
.Is_Regular_File
then
695 if ch
= PM
and then File
.Is_Regular_File
then
697 File
.Page
:= File
.Page
+ 1;
709 Get_Line
(Current_In
, Item
, Last
);
716 function Getc
(File
: File_Type
) return int
is
720 ch
:= fgetc
(File
.Stream
);
722 if ch
= EOF
and then ferror
(File
.Stream
) /= 0 then
733 function Is_Open
(File
: in File_Type
) return Boolean is
735 return FIO
.Is_Open
(AP
(File
));
742 -- Note: we assume that it is impossible in practice for the line
743 -- to exceed the value of Count'Last, i.e. no check is required for
744 -- overflow raising layout error.
746 function Line
(File
: in File_Type
) return Positive_Count
is
748 FIO
.Check_File_Open
(AP
(File
));
752 function Line
return Positive_Count
is
754 return Line
(Current_Out
);
761 function Line_Length
(File
: in File_Type
) return Count
is
763 FIO
.Check_Write_Status
(AP
(File
));
764 return File
.Line_Length
;
767 function Line_Length
return Count
is
769 return Line_Length
(Current_Out
);
777 (File
: in File_Type
;
778 Item
: out Character;
779 End_Of_Line
: out Boolean)
784 FIO
.Check_Read_Status
(AP
(File
));
786 if File
.Before_LM
then
795 or else (ch
= PM
and then File
.Is_Regular_File
)
800 End_Of_Line
:= False;
801 Item
:= Character'Val (ch
);
807 (Item
: out Character;
808 End_Of_Line
: out Boolean)
811 Look_Ahead
(Current_In
, Item
, End_Of_Line
);
818 function Mode
(File
: in File_Type
) return File_Mode
is
820 return To_TIO
(FIO
.Mode
(AP
(File
)));
827 function Name
(File
: in File_Type
) return String is
829 return FIO
.Name
(AP
(File
));
837 (File
: in File_Type
;
838 Spacing
: in Positive_Count
:= 1)
841 -- Raise Constraint_Error if out of range value. The reason for this
842 -- explicit test is that we don't want junk values around, even if
843 -- checks are off in the caller.
845 if Spacing
not in Positive_Count
then
846 raise Constraint_Error
;
849 FIO
.Check_Write_Status
(AP
(File
));
851 for K
in 1 .. Spacing
loop
853 File
.Line
:= File
.Line
+ 1;
855 if File
.Page_Length
/= 0
856 and then File
.Line
> File
.Page_Length
860 File
.Page
:= File
.Page
+ 1;
867 procedure New_Line
(Spacing
: in Positive_Count
:= 1) is
869 New_Line
(Current_Out
, Spacing
);
876 procedure New_Page
(File
: in File_Type
) is
878 FIO
.Check_Write_Status
(AP
(File
));
880 if File
.Col
/= 1 or else File
.Line
= 1 then
885 File
.Page
:= File
.Page
+ 1;
890 procedure New_Page
is
892 New_Page
(Current_Out
);
899 function Nextc
(File
: File_Type
) return int
is
903 ch
:= fgetc
(File
.Stream
);
906 if ferror
(File
.Stream
) /= 0 then
911 if ungetc
(ch
, File
.Stream
) = EOF
then
924 (File
: in out File_Type
;
927 Form
: in String := "")
929 Dummy_File_Control_Block
: Text_AFCB
;
930 pragma Warnings
(Off
, Dummy_File_Control_Block
);
931 -- Yes, we know this is never assigned a value, only the tag
932 -- is used for dispatching purposes, so that's expected.
935 FIO
.Open
(File_Ptr
=> AP
(File
),
936 Dummy_FCB
=> Dummy_File_Control_Block
,
937 Mode
=> To_FCB
(Mode
),
951 -- Note: we assume that it is impossible in practice for the page
952 -- to exceed the value of Count'Last, i.e. no check is required for
953 -- overflow raising layout error.
955 function Page
(File
: in File_Type
) return Positive_Count
is
957 FIO
.Check_File_Open
(AP
(File
));
961 function Page
return Positive_Count
is
963 return Page
(Current_Out
);
970 function Page_Length
(File
: in File_Type
) return Count
is
972 FIO
.Check_Write_Status
(AP
(File
));
973 return File
.Page_Length
;
976 function Page_Length
return Count
is
978 return Page_Length
(Current_Out
);
986 (File
: in File_Type
;
990 FIO
.Check_Write_Status
(AP
(File
));
992 if File
.Line_Length
/= 0 and then File
.Col
> File
.Line_Length
then
996 if fputc
(Character'Pos (Item
), File
.Stream
) = EOF
then
1000 File
.Col
:= File
.Col
+ 1;
1003 procedure Put
(Item
: in Character) is
1005 FIO
.Check_Write_Status
(AP
(Current_Out
));
1007 if Current_Out
.Line_Length
/= 0
1008 and then Current_Out
.Col
> Current_Out
.Line_Length
1010 New_Line
(Current_Out
);
1013 if fputc
(Character'Pos (Item
), Current_Out
.Stream
) = EOF
then
1017 Current_Out
.Col
:= Current_Out
.Col
+ 1;
1025 (File
: in File_Type
;
1029 FIO
.Check_Write_Status
(AP
(File
));
1031 if Item
'Length > 0 then
1033 -- If we have bounded lines, then do things character by
1034 -- character (this seems a rare case anyway!)
1036 if File
.Line_Length
/= 0 then
1037 for J
in Item
'Range loop
1038 Put
(File
, Item
(J
));
1041 -- Otherwise we can output the entire string at once. Note that if
1042 -- there are LF or FF characters in the string, we do not bother to
1043 -- count them as line or page terminators.
1046 FIO
.Write_Buf
(AP
(File
), Item
'Address, Item
'Length);
1047 File
.Col
:= File
.Col
+ Item
'Length;
1052 procedure Put
(Item
: in String) is
1054 Put
(Current_Out
, Item
);
1062 (File
: in File_Type
;
1065 Ilen
: Natural := Item
'Length;
1066 Istart
: Natural := Item
'First;
1069 FIO
.Check_Write_Status
(AP
(File
));
1071 -- If we have bounded lines, then just do a put and a new line. In
1072 -- this case we will end up doing things character by character in
1073 -- any case, and it is a rare situation.
1075 if File
.Line_Length
/= 0 then
1081 -- We setup a single string that has the necessary terminators and
1082 -- then write it with a single call. The reason for doing this is
1083 -- that it gives better behavior for the use of Put_Line in multi-
1084 -- tasking programs, since often the OS will treat the entire put
1085 -- operation as an atomic operation.
1087 -- We only do this if the message is 512 characters or less in length,
1088 -- since otherwise Put_Line would use an unbounded amount of stack
1089 -- space and could cause undetected stack overflow. If we have a
1090 -- longer string, then output the first part separately to avoid this.
1093 FIO
.Write_Buf
(AP
(File
), Item
'Address, size_t
(Ilen
- 512));
1094 Istart
:= Istart
+ Ilen
- 512;
1098 -- Now prepare the string with its terminator
1101 Buffer
: String (1 .. Ilen
+ 2);
1105 Buffer
(1 .. Ilen
) := Item
(Istart
.. Item
'Last);
1106 Buffer
(Ilen
+ 1) := Character'Val (LM
);
1108 if File
.Page_Length
/= 0
1109 and then File
.Line
> File
.Page_Length
1111 Buffer
(Ilen
+ 2) := Character'Val (PM
);
1112 Plen
:= size_t
(Ilen
) + 2;
1114 File
.Page
:= File
.Page
+ 1;
1117 Plen
:= size_t
(Ilen
) + 1;
1118 File
.Line
:= File
.Line
+ 1;
1121 FIO
.Write_Buf
(AP
(File
), Buffer
'Address, Plen
);
1127 procedure Put_Line
(Item
: in String) is
1129 Put_Line
(Current_Out
, Item
);
1136 procedure Putc
(ch
: int
; File
: File_Type
) is
1138 if fputc
(ch
, File
.Stream
) = EOF
then
1147 -- This is the primitive Stream Read routine, used when a Text_IO file
1148 -- is treated directly as a stream using Text_IO.Streams.Stream.
1151 (File
: in out Text_AFCB
;
1152 Item
: out Stream_Element_Array
;
1153 Last
: out Stream_Element_Offset
)
1156 pragma Warnings
(Off
, Discard_ch
);
1159 if File
.Mode
/= FCB
.In_File
then
1163 -- Deal with case where our logical and physical position do not match
1164 -- because of being after an LM or LM-PM sequence when in fact we are
1165 -- logically positioned before it.
1167 if File
.Before_LM
then
1169 -- If we are before a PM, then it is possible for a stream read
1170 -- to leave us after the LM and before the PM, which is a bit
1171 -- odd. The easiest way to deal with this is to unget the PM,
1172 -- so we are indeed positioned between the characters. This way
1173 -- further stream read operations will work correctly, and the
1174 -- effect on text processing is a little weird, but what can
1175 -- be expected if stream and text input are mixed this way?
1177 if File
.Before_LM_PM
then
1178 Discard_ch
:= ungetc
(PM
, File
.Stream
);
1179 File
.Before_LM_PM
:= False;
1182 File
.Before_LM
:= False;
1184 Item
(Item
'First) := Stream_Element
(Character'Pos (ASCII
.LF
));
1186 if Item
'Length = 1 then
1192 Stream_Element_Offset
1193 (fread
(buffer
=> Item
'Address,
1194 index
=> size_t
(Item
'First + 1),
1196 count
=> Item
'Length - 1,
1197 stream
=> File
.Stream
));
1203 -- Now we do the read. Since this is a text file, it is normally in
1204 -- text mode, but stream data must be read in binary mode, so we
1205 -- temporarily set binary mode for the read, resetting it after.
1206 -- These calls have no effect in a system (like Unix) where there is
1207 -- no distinction between text and binary files.
1209 set_binary_mode
(fileno
(File
.Stream
));
1213 Stream_Element_Offset
1214 (fread
(Item
'Address, 1, Item
'Length, File
.Stream
)) - 1;
1216 if Last
< Item
'Last then
1217 if ferror
(File
.Stream
) /= 0 then
1222 set_text_mode
(fileno
(File
.Stream
));
1230 (File
: in out File_Type
;
1231 Mode
: in File_Mode
)
1234 -- Don't allow change of mode for current file (RM A.10.2(5))
1236 if (File
= Current_In
or else
1237 File
= Current_Out
or else
1238 File
= Current_Error
)
1239 and then To_FCB
(Mode
) /= File
.Mode
1244 Terminate_Line
(File
);
1245 FIO
.Reset
(AP
(File
), To_FCB
(Mode
));
1249 File
.Line_Length
:= 0;
1250 File
.Page_Length
:= 0;
1251 File
.Before_LM
:= False;
1252 File
.Before_LM_PM
:= False;
1255 procedure Reset
(File
: in out File_Type
) is
1257 Terminate_Line
(File
);
1258 FIO
.Reset
(AP
(File
));
1262 File
.Line_Length
:= 0;
1263 File
.Page_Length
:= 0;
1264 File
.Before_LM
:= False;
1265 File
.Before_LM_PM
:= False;
1273 (File
: in File_Type
;
1274 To
: in Positive_Count
)
1279 -- Raise Constraint_Error if out of range value. The reason for this
1280 -- explicit test is that we don't want junk values around, even if
1281 -- checks are off in the caller.
1283 if To
not in Positive_Count
then
1284 raise Constraint_Error
;
1287 FIO
.Check_File_Open
(AP
(File
));
1289 if To
= File
.Col
then
1293 if Mode
(File
) >= Out_File
then
1294 if File
.Line_Length
/= 0 and then To
> File
.Line_Length
then
1298 if To
< File
.Col
then
1302 while File
.Col
< To
loop
1314 File
.Line
:= File
.Line
+ 1;
1317 elsif ch
= PM
and then File
.Is_Regular_File
then
1318 File
.Page
:= File
.Page
+ 1;
1322 elsif To
= File
.Col
then
1327 File
.Col
:= File
.Col
+ 1;
1333 procedure Set_Col
(To
: in Positive_Count
) is
1335 Set_Col
(Current_Out
, To
);
1342 procedure Set_Error
(File
: in File_Type
) is
1344 FIO
.Check_Write_Status
(AP
(File
));
1345 Current_Err
:= File
;
1352 procedure Set_Input
(File
: in File_Type
) is
1354 FIO
.Check_Read_Status
(AP
(File
));
1363 (File
: in File_Type
;
1364 To
: in Positive_Count
)
1367 -- Raise Constraint_Error if out of range value. The reason for this
1368 -- explicit test is that we don't want junk values around, even if
1369 -- checks are off in the caller.
1371 if To
not in Positive_Count
then
1372 raise Constraint_Error
;
1375 FIO
.Check_File_Open
(AP
(File
));
1377 if To
= File
.Line
then
1381 if Mode
(File
) >= Out_File
then
1382 if File
.Page_Length
/= 0 and then To
> File
.Page_Length
then
1386 if To
< File
.Line
then
1390 while File
.Line
< To
loop
1395 while To
/= File
.Line
loop
1401 procedure Set_Line
(To
: in Positive_Count
) is
1403 Set_Line
(Current_Out
, To
);
1406 ---------------------
1407 -- Set_Line_Length --
1408 ---------------------
1410 procedure Set_Line_Length
(File
: in File_Type
; To
: in Count
) is
1412 -- Raise Constraint_Error if out of range value. The reason for this
1413 -- explicit test is that we don't want junk values around, even if
1414 -- checks are off in the caller.
1416 if To
not in Count
then
1417 raise Constraint_Error
;
1420 FIO
.Check_Write_Status
(AP
(File
));
1421 File
.Line_Length
:= To
;
1422 end Set_Line_Length
;
1424 procedure Set_Line_Length
(To
: in Count
) is
1426 Set_Line_Length
(Current_Out
, To
);
1427 end Set_Line_Length
;
1433 procedure Set_Output
(File
: in File_Type
) is
1435 FIO
.Check_Write_Status
(AP
(File
));
1436 Current_Out
:= File
;
1439 ---------------------
1440 -- Set_Page_Length --
1441 ---------------------
1443 procedure Set_Page_Length
(File
: in File_Type
; To
: in Count
) is
1445 -- Raise Constraint_Error if out of range value. The reason for this
1446 -- explicit test is that we don't want junk values around, even if
1447 -- checks are off in the caller.
1449 if To
not in Count
then
1450 raise Constraint_Error
;
1453 FIO
.Check_Write_Status
(AP
(File
));
1454 File
.Page_Length
:= To
;
1455 end Set_Page_Length
;
1457 procedure Set_Page_Length
(To
: in Count
) is
1459 Set_Page_Length
(Current_Out
, To
);
1460 end Set_Page_Length
;
1467 (File
: in File_Type
;
1468 Spacing
: in Positive_Count
:= 1)
1473 -- Raise Constraint_Error if out of range value. The reason for this
1474 -- explicit test is that we don't want junk values around, even if
1475 -- checks are off in the caller.
1477 if Spacing
not in Positive_Count
then
1478 raise Constraint_Error
;
1481 FIO
.Check_Read_Status
(AP
(File
));
1483 for L
in 1 .. Spacing
loop
1484 if File
.Before_LM
then
1485 File
.Before_LM
:= False;
1486 File
.Before_LM_PM
:= False;
1491 -- If at end of file now, then immediately raise End_Error. Note
1492 -- that we can never be positioned between a line mark and a page
1493 -- mark, so if we are at the end of file, we cannot logically be
1494 -- before the implicit page mark that is at the end of the file.
1496 -- For the same reason, we do not need an explicit check for a
1497 -- page mark. If there is a FF in the middle of a line, the file
1498 -- is not in canonical format and we do not care about the page
1499 -- numbers for files other than ones in canonical format.
1505 -- If not at end of file, then loop till we get to an LM or EOF.
1506 -- The latter case happens only in non-canonical files where the
1507 -- last line is not terminated by LM, but we don't want to blow
1508 -- up for such files, so we assume an implicit LM in this case.
1511 exit when ch
= LM
or ch
= EOF
;
1516 -- We have got past a line mark, now, for a regular file only,
1517 -- see if a page mark immediately follows this line mark and
1518 -- if so, skip past the page mark as well. We do not do this
1519 -- for non-regular files, since it would cause an undesirable
1520 -- wait for an additional character.
1523 File
.Line
:= File
.Line
+ 1;
1525 if File
.Before_LM_PM
then
1526 File
.Page
:= File
.Page
+ 1;
1528 File
.Before_LM_PM
:= False;
1530 elsif File
.Is_Regular_File
then
1533 -- Page mark can be explicit, or implied at the end of the file
1535 if (ch
= PM
or else ch
= EOF
)
1536 and then File
.Is_Regular_File
1538 File
.Page
:= File
.Page
+ 1;
1548 procedure Skip_Line
(Spacing
: in Positive_Count
:= 1) is
1550 Skip_Line
(Current_In
, Spacing
);
1557 procedure Skip_Page
(File
: in File_Type
) is
1561 FIO
.Check_Read_Status
(AP
(File
));
1563 -- If at page mark already, just skip it
1565 if File
.Before_LM_PM
then
1566 File
.Before_LM
:= False;
1567 File
.Before_LM_PM
:= False;
1568 File
.Page
:= File
.Page
+ 1;
1574 -- This is a bit tricky, if we are logically before an LM then
1575 -- it is not an error if we are at an end of file now, since we
1576 -- are not really at it.
1578 if File
.Before_LM
then
1579 File
.Before_LM
:= False;
1580 File
.Before_LM_PM
:= False;
1583 -- Otherwise we do raise End_Error if we are at the end of file now
1593 -- Now we can just rumble along to the next page mark, or to the
1594 -- end of file, if that comes first. The latter case happens when
1595 -- the page mark is implied at the end of file.
1599 or else (ch
= PM
and then File
.Is_Regular_File
);
1603 File
.Page
:= File
.Page
+ 1;
1608 procedure Skip_Page
is
1610 Skip_Page
(Current_In
);
1613 --------------------
1614 -- Standard_Error --
1615 --------------------
1617 function Standard_Error
return File_Type
is
1619 return Standard_Err
;
1622 function Standard_Error
return File_Access
is
1624 return Standard_Err
'Access;
1627 --------------------
1628 -- Standard_Input --
1629 --------------------
1631 function Standard_Input
return File_Type
is
1636 function Standard_Input
return File_Access
is
1638 return Standard_In
'Access;
1641 ---------------------
1642 -- Standard_Output --
1643 ---------------------
1645 function Standard_Output
return File_Type
is
1647 return Standard_Out
;
1648 end Standard_Output
;
1650 function Standard_Output
return File_Access
is
1652 return Standard_Out
'Access;
1653 end Standard_Output
;
1655 --------------------
1656 -- Terminate_Line --
1657 --------------------
1659 procedure Terminate_Line
(File
: File_Type
) is
1661 FIO
.Check_File_Open
(AP
(File
));
1663 -- For file other than In_File, test for needing to terminate last line
1665 if Mode
(File
) /= In_File
then
1667 -- If not at start of line definition need new line
1669 if File
.Col
/= 1 then
1672 -- For files other than standard error and standard output, we
1673 -- make sure that an empty file has a single line feed, so that
1674 -- it is properly formatted. We avoid this for the standard files
1675 -- because it is too much of a nuisance to have these odd line
1676 -- feeds when nothing has been written to the file.
1678 elsif (File
/= Standard_Err
and then File
/= Standard_Out
)
1679 and then (File
.Line
= 1 and then File
.Page
= 1)
1690 procedure Ungetc
(ch
: int
; File
: File_Type
) is
1693 if ungetc
(ch
, File
.Stream
) = EOF
then
1703 -- This is the primitive Stream Write routine, used when a Text_IO file
1704 -- is treated directly as a stream using Text_IO.Streams.Stream.
1707 (File
: in out Text_AFCB
;
1708 Item
: in Stream_Element_Array
)
1711 function Has_Translated_Characters
return Boolean;
1712 -- return True if Item array contains a character which will be
1713 -- translated under the text file mode. There is only one such
1714 -- character under DOS based systems which is character 10.
1716 text_translation_required
: Boolean;
1717 pragma Import
(C
, text_translation_required
,
1718 "__gnat_text_translation_required");
1720 Siz
: constant size_t
:= Item
'Length;
1722 function Has_Translated_Characters
return Boolean is
1724 for K
in Item
'Range loop
1725 if Item
(K
) = 10 then
1730 end Has_Translated_Characters
;
1732 Needs_Binary_Write
: constant Boolean :=
1733 text_translation_required
and then Has_Translated_Characters
;
1736 if File
.Mode
= FCB
.In_File
then
1740 -- Now we do the write. Since this is a text file, it is normally in
1741 -- text mode, but stream data must be written in binary mode, so we
1742 -- temporarily set binary mode for the write, resetting it after. This
1743 -- is done only if needed (i.e. there is some characters in Item which
1744 -- needs to be written using the binary mode).
1745 -- These calls have no effect in a system (like Unix) where there is
1746 -- no distinction between text and binary files.
1748 -- Since the character translation is done at the time the buffer is
1749 -- written (this is true under Windows) we first flush current buffer
1750 -- with text mode if needed.
1752 if Needs_Binary_Write
then
1754 if fflush
(File
.Stream
) = -1 then
1758 set_binary_mode
(fileno
(File
.Stream
));
1761 if fwrite
(Item
'Address, 1, Siz
, File
.Stream
) /= Siz
then
1765 -- At this point we need to flush the buffer using the binary mode then
1766 -- we reset to text mode.
1768 if Needs_Binary_Write
then
1770 if fflush
(File
.Stream
) = -1 then
1774 set_text_mode
(fileno
(File
.Stream
));
1778 -- Use "preallocated" strings to avoid calling "new" during the
1779 -- elaboration of the run time. This is needed in the tasking case to
1780 -- avoid calling Task_Lock too early. A filename is expected to end with a
1781 -- null character in the runtime, here the null characters are added just
1782 -- to have a correct filename length.
1784 Err_Name
: aliased String := "*stderr" & ASCII
.Nul
;
1785 In_Name
: aliased String := "*stdin" & ASCII
.Nul
;
1786 Out_Name
: aliased String := "*stdout" & ASCII
.Nul
;
1788 -------------------------------
1789 -- Initialize Standard Files --
1790 -------------------------------
1792 -- Note: the names in these files are bogus, and probably it would be
1793 -- better for these files to have no names, but the ACVC test insist!
1794 -- We use names that are bound to fail in open etc.
1796 Standard_Err
.Stream
:= stderr
;
1797 Standard_Err
.Name
:= Err_Name
'Access;
1798 Standard_Err
.Form
:= Null_Str
'Unrestricted_Access;
1799 Standard_Err
.Mode
:= FCB
.Out_File
;
1800 Standard_Err
.Is_Regular_File
:= is_regular_file
(fileno
(stderr
)) /= 0;
1801 Standard_Err
.Is_Temporary_File
:= False;
1802 Standard_Err
.Is_System_File
:= True;
1803 Standard_Err
.Is_Text_File
:= True;
1804 Standard_Err
.Access_Method
:= 'T';
1805 Standard_Err
.Self
:= Standard_Err
;
1807 Standard_In
.Stream
:= stdin
;
1808 Standard_In
.Name
:= In_Name
'Access;
1809 Standard_In
.Form
:= Null_Str
'Unrestricted_Access;
1810 Standard_In
.Mode
:= FCB
.In_File
;
1811 Standard_In
.Is_Regular_File
:= is_regular_file
(fileno
(stdin
)) /= 0;
1812 Standard_In
.Is_Temporary_File
:= False;
1813 Standard_In
.Is_System_File
:= True;
1814 Standard_In
.Is_Text_File
:= True;
1815 Standard_In
.Access_Method
:= 'T';
1816 Standard_In
.Self
:= Standard_In
;
1818 Standard_Out
.Stream
:= stdout
;
1819 Standard_Out
.Name
:= Out_Name
'Access;
1820 Standard_Out
.Form
:= Null_Str
'Unrestricted_Access;
1821 Standard_Out
.Mode
:= FCB
.Out_File
;
1822 Standard_Out
.Is_Regular_File
:= is_regular_file
(fileno
(stdout
)) /= 0;
1823 Standard_Out
.Is_Temporary_File
:= False;
1824 Standard_Out
.Is_System_File
:= True;
1825 Standard_Out
.Is_Text_File
:= True;
1826 Standard_Out
.Access_Method
:= 'T';
1827 Standard_Out
.Self
:= Standard_Out
;
1829 FIO
.Chain_File
(AP
(Standard_In
));
1830 FIO
.Chain_File
(AP
(Standard_Out
));
1831 FIO
.Chain_File
(AP
(Standard_Err
));
1833 FIO
.Make_Unbuffered
(AP
(Standard_Out
));
1834 FIO
.Make_Unbuffered
(AP
(Standard_Err
));