1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . T E X T _ I O --
9 -- Copyright (C) 1992-2005 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
;
39 with Unchecked_Conversion
;
40 with Unchecked_Deallocation
;
42 pragma Elaborate_All
(System
.File_IO
);
43 -- Needed because of calls to Chain_File in package body elaboration
45 package body Ada
.Text_IO
is
47 package FIO
renames System
.File_IO
;
49 subtype AP
is FCB
.AFCB_Ptr
;
51 function To_FCB
is new Unchecked_Conversion
(File_Mode
, FCB
.File_Mode
);
52 function To_TIO
is new Unchecked_Conversion
(FCB
.File_Mode
, File_Mode
);
53 use type FCB
.File_Mode
;
55 use type System
.CRTL
.size_t
;
61 function AFCB_Allocate
(Control_Block
: Text_AFCB
) return FCB
.AFCB_Ptr
is
62 pragma Unreferenced
(Control_Block
);
71 procedure AFCB_Close
(File
: access Text_AFCB
) is
73 -- If the file being closed is one of the current files, then close
74 -- the corresponding current file. It is not clear that this action
75 -- is required (RM A.10.3(23)) but it seems reasonable, and besides
76 -- ACVC test CE3208A expects this behavior.
78 if File_Type
(File
) = Current_In
then
80 elsif File_Type
(File
) = Current_Out
then
82 elsif File_Type
(File
) = Current_Err
then
86 Terminate_Line
(File_Type
(File
));
93 procedure AFCB_Free
(File
: access Text_AFCB
) is
94 type FCB_Ptr
is access all Text_AFCB
;
95 FT
: FCB_Ptr
:= FCB_Ptr
(File
);
97 procedure Free
is new Unchecked_Deallocation
(Text_AFCB
, FCB_Ptr
);
107 procedure Close
(File
: in out File_Type
) is
109 FIO
.Close
(AP
(File
));
116 -- Note: we assume that it is impossible in practice for the column
117 -- to exceed the value of Count'Last, i.e. no check is required for
118 -- overflow raising layout error.
120 function Col
(File
: File_Type
) return Positive_Count
is
122 FIO
.Check_File_Open
(AP
(File
));
126 function Col
return Positive_Count
is
128 return Col
(Current_Out
);
136 (File
: in out File_Type
;
137 Mode
: File_Mode
:= Out_File
;
141 Dummy_File_Control_Block
: Text_AFCB
;
142 pragma Warnings
(Off
, Dummy_File_Control_Block
);
143 -- Yes, we know this is never assigned a value, only the tag
144 -- is used for dispatching purposes, so that's expected.
147 FIO
.Open
(File_Ptr
=> AP
(File
),
148 Dummy_FCB
=> Dummy_File_Control_Block
,
149 Mode
=> To_FCB
(Mode
),
163 function Current_Error
return File_Type
is
168 function Current_Error
return File_Access
is
170 return Current_Err
.Self
'Access;
177 function Current_Input
return File_Type
is
182 function Current_Input
return File_Access
is
184 return Current_In
.Self
'Access;
191 function Current_Output
return File_Type
is
196 function Current_Output
return File_Access
is
198 return Current_Out
.Self
'Access;
205 procedure Delete
(File
: in out File_Type
) is
207 FIO
.Delete
(AP
(File
));
214 function End_Of_File
(File
: File_Type
) return Boolean is
218 FIO
.Check_Read_Status
(AP
(File
));
220 if File
.Before_LM
then
222 if File
.Before_LM_PM
then
223 return Nextc
(File
) = EOF
;
237 File
.Before_LM
:= True;
241 -- Here we are just past the line mark with Before_LM set so that we
242 -- do not have to try to back up past the LM, thus avoiding the need
243 -- to back up more than one character.
250 elsif ch
= PM
and then File
.Is_Regular_File
then
251 File
.Before_LM_PM
:= True;
252 return Nextc
(File
) = EOF
;
254 -- Here if neither EOF nor PM followed end of line
263 function End_Of_File
return Boolean is
265 return End_Of_File
(Current_In
);
272 function End_Of_Line
(File
: File_Type
) return Boolean is
276 FIO
.Check_Read_Status
(AP
(File
));
278 if File
.Before_LM
then
294 function End_Of_Line
return Boolean is
296 return End_Of_Line
(Current_In
);
303 function End_Of_Page
(File
: File_Type
) return Boolean is
307 FIO
.Check_Read_Status
(AP
(File
));
309 if not File
.Is_Regular_File
then
312 elsif File
.Before_LM
then
313 if File
.Before_LM_PM
then
328 File
.Before_LM
:= True;
332 -- Here we are just past the line mark with Before_LM set so that we
333 -- do not have to try to back up past the LM, thus avoiding the need
334 -- to back up more than one character.
338 return ch
= PM
or else ch
= EOF
;
341 function End_Of_Page
return Boolean is
343 return End_Of_Page
(Current_In
);
350 function EOF_Char
return Integer is
359 procedure Flush
(File
: File_Type
) is
361 FIO
.Flush
(AP
(File
));
373 function Form
(File
: File_Type
) return String is
375 return FIO
.Form
(AP
(File
));
384 Item
: out Character)
389 FIO
.Check_Read_Status
(AP
(File
));
391 if File
.Before_LM
then
392 File
.Before_LM
:= False;
395 if File
.Before_LM_PM
then
397 File
.Page
:= File
.Page
+ 1;
398 File
.Before_LM_PM
:= False;
400 File
.Line
:= File
.Line
+ 1;
411 File
.Line
:= File
.Line
+ 1;
414 elsif ch
= PM
and then File
.Is_Regular_File
then
415 File
.Page
:= File
.Page
+ 1;
419 Item
:= Character'Val (ch
);
420 File
.Col
:= File
.Col
+ 1;
426 procedure Get
(Item
: out Character) is
428 Get
(Current_In
, Item
);
439 FIO
.Check_Read_Status
(AP
(File
));
441 if File
.Before_LM
then
442 File
.Before_LM
:= False;
443 File
.Before_LM_PM
:= False;
446 if File
.Before_LM_PM
then
448 File
.Page
:= File
.Page
+ 1;
449 File
.Before_LM_PM
:= False;
452 File
.Line
:= File
.Line
+ 1;
457 while J
<= Item
'Last loop
464 File
.Line
:= File
.Line
+ 1;
467 elsif ch
= PM
and then File
.Is_Regular_File
then
468 File
.Page
:= File
.Page
+ 1;
472 Item
(J
) := Character'Val (ch
);
474 File
.Col
:= File
.Col
+ 1;
479 procedure Get
(Item
: out String) is
481 Get
(Current_In
, Item
);
488 -- More work required here ???
490 procedure Get_Immediate
492 Item
: out Character)
497 procedure getc_immediate
500 end_of_file
: out int
);
501 pragma Import
(C
, getc_immediate
, "getc_immediate");
504 FIO
.Check_Read_Status
(AP
(File
));
506 if File
.Before_LM
then
507 File
.Before_LM
:= False;
508 File
.Before_LM_PM
:= False;
512 getc_immediate
(File
.Stream
, ch
, end_of_file
);
514 if ferror
(File
.Stream
) /= 0 then
516 elsif end_of_file
/= 0 then
521 Item
:= Character'Val (ch
);
524 procedure Get_Immediate
525 (Item
: out Character)
528 Get_Immediate
(Current_In
, Item
);
531 procedure Get_Immediate
533 Item
: out Character;
534 Available
: out Boolean)
540 procedure getc_immediate_nowait
543 end_of_file
: out int
;
545 pragma Import
(C
, getc_immediate_nowait
, "getc_immediate_nowait");
548 FIO
.Check_Read_Status
(AP
(File
));
550 -- If we are logically before an end of line, but physically after it,
551 -- then we just return the end of line character, no I/O is necessary.
553 if File
.Before_LM
then
554 File
.Before_LM
:= False;
555 File
.Before_LM_PM
:= False;
558 Item
:= Character'Val (LM
);
560 -- Normal case where a read operation is required
563 getc_immediate_nowait
(File
.Stream
, ch
, end_of_file
, avail
);
565 if ferror
(File
.Stream
) /= 0 then
568 elsif end_of_file
/= 0 then
577 Item
:= Character'Val (ch
);
583 procedure Get_Immediate
584 (Item
: out Character;
585 Available
: out Boolean)
588 Get_Immediate
(Current_In
, Item
, Available
);
603 FIO
.Check_Read_Status
(AP
(File
));
604 Last
:= Item
'First - 1;
606 -- Immediate exit for null string, this is a case in which we do not
607 -- need to test for end of file and we do not skip a line mark under
608 -- any circumstances.
610 if Last
>= Item
'Last then
614 -- Here we have at least one character, if we are immediately before
615 -- a line mark, then we will just skip past it storing no characters.
617 if File
.Before_LM
then
618 File
.Before_LM
:= False;
619 File
.Before_LM_PM
:= False;
621 -- Otherwise we need to read some characters
626 -- If we are at the end of file now, it means we are trying to
627 -- skip a file terminator and we raise End_Error (RM A.10.7(20))
633 -- Loop through characters. Don't bother if we hit a page mark,
634 -- since in normal files, page marks can only follow line marks
635 -- in any case and we only promise to treat the page nonsense
636 -- correctly in the absense of such rogue page marks.
639 -- Exit the loop if read is terminated by encountering line mark
643 -- Otherwise store the character, note that we know that ch is
644 -- something other than LM or EOF. It could possibly be a page
645 -- mark if there is a stray page mark in the middle of a line,
646 -- but this is not an official page mark in any case, since
647 -- official page marks can only follow a line mark. The whole
648 -- page business is pretty much nonsense anyway, so we do not
649 -- want to waste time trying to make sense out of non-standard
650 -- page marks in the file! This means that the behavior of
651 -- Get_Line is different from repeated Get of a character, but
652 -- that's too bad. We only promise that page numbers etc make
653 -- sense if the file is formatted in a standard manner.
655 -- Note: we do not adjust the column number because it is quicker
656 -- to adjust it once at the end of the operation than incrementing
657 -- it each time around the loop.
660 Item
(Last
) := Character'Val (ch
);
662 -- All done if the string is full, this is the case in which
663 -- we do not skip the following line mark. We need to adjust
664 -- the column number in this case.
666 if Last
= Item
'Last then
667 File
.Col
:= File
.Col
+ Count
(Item
'Length);
671 -- Otherwise read next character. We also exit from the loop if
672 -- we read an end of file. This is the case where the last line
673 -- is not terminated with a line mark, and we consider that there
674 -- is an implied line mark in this case (this is a non-standard
675 -- file, but it is nice to treat it reasonably).
682 -- We have skipped past, but not stored, a line mark. Skip following
683 -- page mark if one follows, but do not do this for a non-regular
684 -- file (since otherwise we get annoying wait for an extra character)
686 File
.Line
:= File
.Line
+ 1;
689 if File
.Before_LM_PM
then
691 File
.Before_LM_PM
:= False;
692 File
.Page
:= File
.Page
+ 1;
694 elsif File
.Is_Regular_File
then
697 if ch
= PM
and then File
.Is_Regular_File
then
699 File
.Page
:= File
.Page
+ 1;
711 Get_Line
(Current_In
, Item
, Last
);
714 function Get_Line
(File
: File_Type
) return String is
715 Buffer
: String (1 .. 500);
718 function Get_Rest
(S
: String) return String;
719 -- This is a recursive function that reads the rest of the line and
720 -- returns it. S is the part read so far.
726 function Get_Rest
(S
: String) return String is
728 -- Each time we allocate a buffer the same size as what we have
729 -- read so far. This limits us to a logarithmic number of calls
730 -- to Get_Rest and also ensures only a linear use of stack space.
732 Buffer
: String (1 .. S
'Length);
736 Get_Line
(File
, Buffer
, Last
);
739 R
: constant String := S
& Buffer
(1 .. Last
);
741 if Last
< Buffer
'Last then
749 -- Start of processing for Get_Line
752 Get_Line
(File
, Buffer
, Last
);
754 if Last
< Buffer
'Last then
755 return Buffer
(1 .. Last
);
757 return Get_Rest
(Buffer
(1 .. Last
));
761 function Get_Line
return String is
763 return Get_Line
(Current_In
);
770 function Getc
(File
: File_Type
) return int
is
774 ch
:= fgetc
(File
.Stream
);
776 if ch
= EOF
and then ferror
(File
.Stream
) /= 0 then
787 function Is_Open
(File
: File_Type
) return Boolean is
789 return FIO
.Is_Open
(AP
(File
));
796 -- Note: we assume that it is impossible in practice for the line
797 -- to exceed the value of Count'Last, i.e. no check is required for
798 -- overflow raising layout error.
800 function Line
(File
: File_Type
) return Positive_Count
is
802 FIO
.Check_File_Open
(AP
(File
));
806 function Line
return Positive_Count
is
808 return Line
(Current_Out
);
815 function Line_Length
(File
: File_Type
) return Count
is
817 FIO
.Check_Write_Status
(AP
(File
));
818 return File
.Line_Length
;
821 function Line_Length
return Count
is
823 return Line_Length
(Current_Out
);
832 Item
: out Character;
833 End_Of_Line
: out Boolean)
838 FIO
.Check_Read_Status
(AP
(File
));
840 if File
.Before_LM
then
849 or else (ch
= PM
and then File
.Is_Regular_File
)
854 End_Of_Line
:= False;
855 Item
:= Character'Val (ch
);
861 (Item
: out Character;
862 End_Of_Line
: out Boolean)
865 Look_Ahead
(Current_In
, Item
, End_Of_Line
);
872 function Mode
(File
: File_Type
) return File_Mode
is
874 return To_TIO
(FIO
.Mode
(AP
(File
)));
881 function Name
(File
: File_Type
) return String is
883 return FIO
.Name
(AP
(File
));
892 Spacing
: Positive_Count
:= 1)
895 -- Raise Constraint_Error if out of range value. The reason for this
896 -- explicit test is that we don't want junk values around, even if
897 -- checks are off in the caller.
899 if Spacing
not in Positive_Count
then
900 raise Constraint_Error
;
903 FIO
.Check_Write_Status
(AP
(File
));
905 for K
in 1 .. Spacing
loop
907 File
.Line
:= File
.Line
+ 1;
909 if File
.Page_Length
/= 0
910 and then File
.Line
> File
.Page_Length
914 File
.Page
:= File
.Page
+ 1;
921 procedure New_Line
(Spacing
: Positive_Count
:= 1) is
923 New_Line
(Current_Out
, Spacing
);
930 procedure New_Page
(File
: File_Type
) is
932 FIO
.Check_Write_Status
(AP
(File
));
934 if File
.Col
/= 1 or else File
.Line
= 1 then
939 File
.Page
:= File
.Page
+ 1;
944 procedure New_Page
is
946 New_Page
(Current_Out
);
953 function Nextc
(File
: File_Type
) return int
is
957 ch
:= fgetc
(File
.Stream
);
960 if ferror
(File
.Stream
) /= 0 then
965 if ungetc
(ch
, File
.Stream
) = EOF
then
978 (File
: in out File_Type
;
983 Dummy_File_Control_Block
: Text_AFCB
;
984 pragma Warnings
(Off
, Dummy_File_Control_Block
);
985 -- Yes, we know this is never assigned a value, only the tag
986 -- is used for dispatching purposes, so that's expected.
989 FIO
.Open
(File_Ptr
=> AP
(File
),
990 Dummy_FCB
=> Dummy_File_Control_Block
,
991 Mode
=> To_FCB
(Mode
),
1005 -- Note: we assume that it is impossible in practice for the page
1006 -- to exceed the value of Count'Last, i.e. no check is required for
1007 -- overflow raising layout error.
1009 function Page
(File
: File_Type
) return Positive_Count
is
1011 FIO
.Check_File_Open
(AP
(File
));
1015 function Page
return Positive_Count
is
1017 return Page
(Current_Out
);
1024 function Page_Length
(File
: File_Type
) return Count
is
1026 FIO
.Check_Write_Status
(AP
(File
));
1027 return File
.Page_Length
;
1030 function Page_Length
return Count
is
1032 return Page_Length
(Current_Out
);
1044 FIO
.Check_Write_Status
(AP
(File
));
1046 if File
.Line_Length
/= 0 and then File
.Col
> File
.Line_Length
then
1050 if fputc
(Character'Pos (Item
), File
.Stream
) = EOF
then
1054 File
.Col
:= File
.Col
+ 1;
1057 procedure Put
(Item
: Character) is
1059 FIO
.Check_Write_Status
(AP
(Current_Out
));
1061 if Current_Out
.Line_Length
/= 0
1062 and then Current_Out
.Col
> Current_Out
.Line_Length
1064 New_Line
(Current_Out
);
1067 if fputc
(Character'Pos (Item
), Current_Out
.Stream
) = EOF
then
1071 Current_Out
.Col
:= Current_Out
.Col
+ 1;
1083 FIO
.Check_Write_Status
(AP
(File
));
1085 if Item
'Length > 0 then
1087 -- If we have bounded lines, then do things character by
1088 -- character (this seems a rare case anyway!)
1090 if File
.Line_Length
/= 0 then
1091 for J
in Item
'Range loop
1092 Put
(File
, Item
(J
));
1095 -- Otherwise we can output the entire string at once. Note that if
1096 -- there are LF or FF characters in the string, we do not bother to
1097 -- count them as line or page terminators.
1100 FIO
.Write_Buf
(AP
(File
), Item
'Address, Item
'Length);
1101 File
.Col
:= File
.Col
+ Item
'Length;
1106 procedure Put
(Item
: String) is
1108 Put
(Current_Out
, Item
);
1119 Ilen
: Natural := Item
'Length;
1120 Istart
: Natural := Item
'First;
1123 FIO
.Check_Write_Status
(AP
(File
));
1125 -- If we have bounded lines, then just do a put and a new line. In
1126 -- this case we will end up doing things character by character in
1127 -- any case, and it is a rare situation.
1129 if File
.Line_Length
/= 0 then
1135 -- We setup a single string that has the necessary terminators and
1136 -- then write it with a single call. The reason for doing this is
1137 -- that it gives better behavior for the use of Put_Line in multi-
1138 -- tasking programs, since often the OS will treat the entire put
1139 -- operation as an atomic operation.
1141 -- We only do this if the message is 512 characters or less in length,
1142 -- since otherwise Put_Line would use an unbounded amount of stack
1143 -- space and could cause undetected stack overflow. If we have a
1144 -- longer string, then output the first part separately to avoid this.
1147 FIO
.Write_Buf
(AP
(File
), Item
'Address, size_t
(Ilen
- 512));
1148 Istart
:= Istart
+ Ilen
- 512;
1152 -- Now prepare the string with its terminator
1155 Buffer
: String (1 .. Ilen
+ 2);
1159 Buffer
(1 .. Ilen
) := Item
(Istart
.. Item
'Last);
1160 Buffer
(Ilen
+ 1) := Character'Val (LM
);
1162 if File
.Page_Length
/= 0
1163 and then File
.Line
> File
.Page_Length
1165 Buffer
(Ilen
+ 2) := Character'Val (PM
);
1166 Plen
:= size_t
(Ilen
) + 2;
1168 File
.Page
:= File
.Page
+ 1;
1171 Plen
:= size_t
(Ilen
) + 1;
1172 File
.Line
:= File
.Line
+ 1;
1175 FIO
.Write_Buf
(AP
(File
), Buffer
'Address, Plen
);
1181 procedure Put_Line
(Item
: String) is
1183 Put_Line
(Current_Out
, Item
);
1190 procedure Putc
(ch
: int
; File
: File_Type
) is
1192 if fputc
(ch
, File
.Stream
) = EOF
then
1201 -- This is the primitive Stream Read routine, used when a Text_IO file
1202 -- is treated directly as a stream using Text_IO.Streams.Stream.
1205 (File
: in out Text_AFCB
;
1206 Item
: out Stream_Element_Array
;
1207 Last
: out Stream_Element_Offset
)
1210 pragma Warnings
(Off
, Discard_ch
);
1213 if File
.Mode
/= FCB
.In_File
then
1217 -- Deal with case where our logical and physical position do not match
1218 -- because of being after an LM or LM-PM sequence when in fact we are
1219 -- logically positioned before it.
1221 if File
.Before_LM
then
1223 -- If we are before a PM, then it is possible for a stream read
1224 -- to leave us after the LM and before the PM, which is a bit
1225 -- odd. The easiest way to deal with this is to unget the PM,
1226 -- so we are indeed positioned between the characters. This way
1227 -- further stream read operations will work correctly, and the
1228 -- effect on text processing is a little weird, but what can
1229 -- be expected if stream and text input are mixed this way?
1231 if File
.Before_LM_PM
then
1232 Discard_ch
:= ungetc
(PM
, File
.Stream
);
1233 File
.Before_LM_PM
:= False;
1236 File
.Before_LM
:= False;
1238 Item
(Item
'First) := Stream_Element
(Character'Pos (ASCII
.LF
));
1240 if Item
'Length = 1 then
1246 Stream_Element_Offset
1247 (fread
(buffer
=> Item
'Address,
1248 index
=> size_t
(Item
'First + 1),
1250 count
=> Item
'Length - 1,
1251 stream
=> File
.Stream
));
1257 -- Now we do the read. Since this is a text file, it is normally in
1258 -- text mode, but stream data must be read in binary mode, so we
1259 -- temporarily set binary mode for the read, resetting it after.
1260 -- These calls have no effect in a system (like Unix) where there is
1261 -- no distinction between text and binary files.
1263 set_binary_mode
(fileno
(File
.Stream
));
1267 Stream_Element_Offset
1268 (fread
(Item
'Address, 1, Item
'Length, File
.Stream
)) - 1;
1270 if Last
< Item
'Last then
1271 if ferror
(File
.Stream
) /= 0 then
1276 set_text_mode
(fileno
(File
.Stream
));
1284 (File
: in out File_Type
;
1288 -- Don't allow change of mode for current file (RM A.10.2(5))
1290 if (File
= Current_In
or else
1291 File
= Current_Out
or else
1292 File
= Current_Error
)
1293 and then To_FCB
(Mode
) /= File
.Mode
1298 Terminate_Line
(File
);
1299 FIO
.Reset
(AP
(File
), To_FCB
(Mode
));
1303 File
.Line_Length
:= 0;
1304 File
.Page_Length
:= 0;
1305 File
.Before_LM
:= False;
1306 File
.Before_LM_PM
:= False;
1309 procedure Reset
(File
: in out File_Type
) is
1311 Terminate_Line
(File
);
1312 FIO
.Reset
(AP
(File
));
1316 File
.Line_Length
:= 0;
1317 File
.Page_Length
:= 0;
1318 File
.Before_LM
:= False;
1319 File
.Before_LM_PM
:= False;
1328 To
: Positive_Count
)
1333 -- Raise Constraint_Error if out of range value. The reason for this
1334 -- explicit test is that we don't want junk values around, even if
1335 -- checks are off in the caller.
1337 if To
not in Positive_Count
then
1338 raise Constraint_Error
;
1341 FIO
.Check_File_Open
(AP
(File
));
1343 if To
= File
.Col
then
1347 if Mode
(File
) >= Out_File
then
1348 if File
.Line_Length
/= 0 and then To
> File
.Line_Length
then
1352 if To
< File
.Col
then
1356 while File
.Col
< To
loop
1368 File
.Line
:= File
.Line
+ 1;
1371 elsif ch
= PM
and then File
.Is_Regular_File
then
1372 File
.Page
:= File
.Page
+ 1;
1376 elsif To
= File
.Col
then
1381 File
.Col
:= File
.Col
+ 1;
1387 procedure Set_Col
(To
: Positive_Count
) is
1389 Set_Col
(Current_Out
, To
);
1396 procedure Set_Error
(File
: File_Type
) is
1398 FIO
.Check_Write_Status
(AP
(File
));
1399 Current_Err
:= File
;
1406 procedure Set_Input
(File
: File_Type
) is
1408 FIO
.Check_Read_Status
(AP
(File
));
1418 To
: Positive_Count
)
1421 -- Raise Constraint_Error if out of range value. The reason for this
1422 -- explicit test is that we don't want junk values around, even if
1423 -- checks are off in the caller.
1425 if To
not in Positive_Count
then
1426 raise Constraint_Error
;
1429 FIO
.Check_File_Open
(AP
(File
));
1431 if To
= File
.Line
then
1435 if Mode
(File
) >= Out_File
then
1436 if File
.Page_Length
/= 0 and then To
> File
.Page_Length
then
1440 if To
< File
.Line
then
1444 while File
.Line
< To
loop
1449 while To
/= File
.Line
loop
1455 procedure Set_Line
(To
: Positive_Count
) is
1457 Set_Line
(Current_Out
, To
);
1460 ---------------------
1461 -- Set_Line_Length --
1462 ---------------------
1464 procedure Set_Line_Length
(File
: File_Type
; To
: Count
) is
1466 -- Raise Constraint_Error if out of range value. The reason for this
1467 -- explicit test is that we don't want junk values around, even if
1468 -- checks are off in the caller.
1470 if To
not in Count
then
1471 raise Constraint_Error
;
1474 FIO
.Check_Write_Status
(AP
(File
));
1475 File
.Line_Length
:= To
;
1476 end Set_Line_Length
;
1478 procedure Set_Line_Length
(To
: Count
) is
1480 Set_Line_Length
(Current_Out
, To
);
1481 end Set_Line_Length
;
1487 procedure Set_Output
(File
: File_Type
) is
1489 FIO
.Check_Write_Status
(AP
(File
));
1490 Current_Out
:= File
;
1493 ---------------------
1494 -- Set_Page_Length --
1495 ---------------------
1497 procedure Set_Page_Length
(File
: File_Type
; To
: Count
) is
1499 -- Raise Constraint_Error if out of range value. The reason for this
1500 -- explicit test is that we don't want junk values around, even if
1501 -- checks are off in the caller.
1503 if To
not in Count
then
1504 raise Constraint_Error
;
1507 FIO
.Check_Write_Status
(AP
(File
));
1508 File
.Page_Length
:= To
;
1509 end Set_Page_Length
;
1511 procedure Set_Page_Length
(To
: Count
) is
1513 Set_Page_Length
(Current_Out
, To
);
1514 end Set_Page_Length
;
1522 Spacing
: Positive_Count
:= 1)
1527 -- Raise Constraint_Error if out of range value. The reason for this
1528 -- explicit test is that we don't want junk values around, even if
1529 -- checks are off in the caller.
1531 if Spacing
not in Positive_Count
then
1532 raise Constraint_Error
;
1535 FIO
.Check_Read_Status
(AP
(File
));
1537 for L
in 1 .. Spacing
loop
1538 if File
.Before_LM
then
1539 File
.Before_LM
:= False;
1540 File
.Before_LM_PM
:= False;
1545 -- If at end of file now, then immediately raise End_Error. Note
1546 -- that we can never be positioned between a line mark and a page
1547 -- mark, so if we are at the end of file, we cannot logically be
1548 -- before the implicit page mark that is at the end of the file.
1550 -- For the same reason, we do not need an explicit check for a
1551 -- page mark. If there is a FF in the middle of a line, the file
1552 -- is not in canonical format and we do not care about the page
1553 -- numbers for files other than ones in canonical format.
1559 -- If not at end of file, then loop till we get to an LM or EOF.
1560 -- The latter case happens only in non-canonical files where the
1561 -- last line is not terminated by LM, but we don't want to blow
1562 -- up for such files, so we assume an implicit LM in this case.
1565 exit when ch
= LM
or ch
= EOF
;
1570 -- We have got past a line mark, now, for a regular file only,
1571 -- see if a page mark immediately follows this line mark and
1572 -- if so, skip past the page mark as well. We do not do this
1573 -- for non-regular files, since it would cause an undesirable
1574 -- wait for an additional character.
1577 File
.Line
:= File
.Line
+ 1;
1579 if File
.Before_LM_PM
then
1580 File
.Page
:= File
.Page
+ 1;
1582 File
.Before_LM_PM
:= False;
1584 elsif File
.Is_Regular_File
then
1587 -- Page mark can be explicit, or implied at the end of the file
1589 if (ch
= PM
or else ch
= EOF
)
1590 and then File
.Is_Regular_File
1592 File
.Page
:= File
.Page
+ 1;
1602 procedure Skip_Line
(Spacing
: Positive_Count
:= 1) is
1604 Skip_Line
(Current_In
, Spacing
);
1611 procedure Skip_Page
(File
: File_Type
) is
1615 FIO
.Check_Read_Status
(AP
(File
));
1617 -- If at page mark already, just skip it
1619 if File
.Before_LM_PM
then
1620 File
.Before_LM
:= False;
1621 File
.Before_LM_PM
:= False;
1622 File
.Page
:= File
.Page
+ 1;
1628 -- This is a bit tricky, if we are logically before an LM then
1629 -- it is not an error if we are at an end of file now, since we
1630 -- are not really at it.
1632 if File
.Before_LM
then
1633 File
.Before_LM
:= False;
1634 File
.Before_LM_PM
:= False;
1637 -- Otherwise we do raise End_Error if we are at the end of file now
1647 -- Now we can just rumble along to the next page mark, or to the
1648 -- end of file, if that comes first. The latter case happens when
1649 -- the page mark is implied at the end of file.
1653 or else (ch
= PM
and then File
.Is_Regular_File
);
1657 File
.Page
:= File
.Page
+ 1;
1662 procedure Skip_Page
is
1664 Skip_Page
(Current_In
);
1667 --------------------
1668 -- Standard_Error --
1669 --------------------
1671 function Standard_Error
return File_Type
is
1673 return Standard_Err
;
1676 function Standard_Error
return File_Access
is
1678 return Standard_Err
'Access;
1681 --------------------
1682 -- Standard_Input --
1683 --------------------
1685 function Standard_Input
return File_Type
is
1690 function Standard_Input
return File_Access
is
1692 return Standard_In
'Access;
1695 ---------------------
1696 -- Standard_Output --
1697 ---------------------
1699 function Standard_Output
return File_Type
is
1701 return Standard_Out
;
1702 end Standard_Output
;
1704 function Standard_Output
return File_Access
is
1706 return Standard_Out
'Access;
1707 end Standard_Output
;
1709 --------------------
1710 -- Terminate_Line --
1711 --------------------
1713 procedure Terminate_Line
(File
: File_Type
) is
1715 FIO
.Check_File_Open
(AP
(File
));
1717 -- For file other than In_File, test for needing to terminate last line
1719 if Mode
(File
) /= In_File
then
1721 -- If not at start of line definition need new line
1723 if File
.Col
/= 1 then
1726 -- For files other than standard error and standard output, we
1727 -- make sure that an empty file has a single line feed, so that
1728 -- it is properly formatted. We avoid this for the standard files
1729 -- because it is too much of a nuisance to have these odd line
1730 -- feeds when nothing has been written to the file.
1732 -- We also avoid this for files opened in append mode, in
1733 -- accordance with (RM A.8.2(10))
1735 elsif (File
/= Standard_Err
and then File
/= Standard_Out
)
1736 and then (File
.Line
= 1 and then File
.Page
= 1)
1737 and then Mode
(File
) = Out_File
1748 procedure Ungetc
(ch
: int
; File
: File_Type
) is
1751 if ungetc
(ch
, File
.Stream
) = EOF
then
1761 -- This is the primitive Stream Write routine, used when a Text_IO file
1762 -- is treated directly as a stream using Text_IO.Streams.Stream.
1765 (File
: in out Text_AFCB
;
1766 Item
: Stream_Element_Array
)
1769 function Has_Translated_Characters
return Boolean;
1770 -- return True if Item array contains a character which will be
1771 -- translated under the text file mode. There is only one such
1772 -- character under DOS based systems which is character 10.
1774 text_translation_required
: Boolean;
1775 pragma Import
(C
, text_translation_required
,
1776 "__gnat_text_translation_required");
1778 Siz
: constant size_t
:= Item
'Length;
1780 function Has_Translated_Characters
return Boolean is
1782 for K
in Item
'Range loop
1783 if Item
(K
) = 10 then
1788 end Has_Translated_Characters
;
1790 Needs_Binary_Write
: constant Boolean :=
1791 text_translation_required
and then Has_Translated_Characters
;
1794 if File
.Mode
= FCB
.In_File
then
1798 -- Now we do the write. Since this is a text file, it is normally in
1799 -- text mode, but stream data must be written in binary mode, so we
1800 -- temporarily set binary mode for the write, resetting it after. This
1801 -- is done only if needed (i.e. there is some characters in Item which
1802 -- needs to be written using the binary mode).
1803 -- These calls have no effect in a system (like Unix) where there is
1804 -- no distinction between text and binary files.
1806 -- Since the character translation is done at the time the buffer is
1807 -- written (this is true under Windows) we first flush current buffer
1808 -- with text mode if needed.
1810 if Needs_Binary_Write
then
1812 if fflush
(File
.Stream
) = -1 then
1816 set_binary_mode
(fileno
(File
.Stream
));
1819 if fwrite
(Item
'Address, 1, Siz
, File
.Stream
) /= Siz
then
1823 -- At this point we need to flush the buffer using the binary mode then
1824 -- we reset to text mode.
1826 if Needs_Binary_Write
then
1828 if fflush
(File
.Stream
) = -1 then
1832 set_text_mode
(fileno
(File
.Stream
));
1836 -- Use "preallocated" strings to avoid calling "new" during the
1837 -- elaboration of the run time. This is needed in the tasking case to
1838 -- avoid calling Task_Lock too early. A filename is expected to end with a
1839 -- null character in the runtime, here the null characters are added just
1840 -- to have a correct filename length.
1842 Err_Name
: aliased String := "*stderr" & ASCII
.Nul
;
1843 In_Name
: aliased String := "*stdin" & ASCII
.Nul
;
1844 Out_Name
: aliased String := "*stdout" & ASCII
.Nul
;
1846 -------------------------------
1847 -- Initialize Standard Files --
1848 -------------------------------
1850 -- Note: the names in these files are bogus, and probably it would be
1851 -- better for these files to have no names, but the ACVC test insist!
1852 -- We use names that are bound to fail in open etc.
1854 Standard_Err
.Stream
:= stderr
;
1855 Standard_Err
.Name
:= Err_Name
'Access;
1856 Standard_Err
.Form
:= Null_Str
'Unrestricted_Access;
1857 Standard_Err
.Mode
:= FCB
.Out_File
;
1858 Standard_Err
.Is_Regular_File
:= is_regular_file
(fileno
(stderr
)) /= 0;
1859 Standard_Err
.Is_Temporary_File
:= False;
1860 Standard_Err
.Is_System_File
:= True;
1861 Standard_Err
.Is_Text_File
:= True;
1862 Standard_Err
.Access_Method
:= 'T';
1863 Standard_Err
.Self
:= Standard_Err
;
1865 Standard_In
.Stream
:= stdin
;
1866 Standard_In
.Name
:= In_Name
'Access;
1867 Standard_In
.Form
:= Null_Str
'Unrestricted_Access;
1868 Standard_In
.Mode
:= FCB
.In_File
;
1869 Standard_In
.Is_Regular_File
:= is_regular_file
(fileno
(stdin
)) /= 0;
1870 Standard_In
.Is_Temporary_File
:= False;
1871 Standard_In
.Is_System_File
:= True;
1872 Standard_In
.Is_Text_File
:= True;
1873 Standard_In
.Access_Method
:= 'T';
1874 Standard_In
.Self
:= Standard_In
;
1876 Standard_Out
.Stream
:= stdout
;
1877 Standard_Out
.Name
:= Out_Name
'Access;
1878 Standard_Out
.Form
:= Null_Str
'Unrestricted_Access;
1879 Standard_Out
.Mode
:= FCB
.Out_File
;
1880 Standard_Out
.Is_Regular_File
:= is_regular_file
(fileno
(stdout
)) /= 0;
1881 Standard_Out
.Is_Temporary_File
:= False;
1882 Standard_Out
.Is_System_File
:= True;
1883 Standard_Out
.Is_Text_File
:= True;
1884 Standard_Out
.Access_Method
:= 'T';
1885 Standard_Out
.Self
:= Standard_Out
;
1887 FIO
.Chain_File
(AP
(Standard_In
));
1888 FIO
.Chain_File
(AP
(Standard_Out
));
1889 FIO
.Chain_File
(AP
(Standard_Err
));
1891 FIO
.Make_Unbuffered
(AP
(Standard_Out
));
1892 FIO
.Make_Unbuffered
(AP
(Standard_Err
));