1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . T E X T _ I O --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 with Ada
.Streams
; use Ada
.Streams
;
36 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
;
59 function AFCB_Allocate
(Control_Block
: Text_AFCB
) return FCB
.AFCB_Ptr
is
60 pragma Warnings
(Off
, Control_Block
);
70 procedure AFCB_Close
(File
: access Text_AFCB
) is
72 -- If the file being closed is one of the current files, then close
73 -- the corresponding current file. It is not clear that this action
74 -- is required (RM A.10.3(23)) but it seems reasonable, and besides
75 -- ACVC test CE3208A expects this behavior.
77 if File_Type
(File
) = Current_In
then
79 elsif File_Type
(File
) = Current_Out
then
81 elsif File_Type
(File
) = Current_Err
then
85 Terminate_Line
(File_Type
(File
));
92 procedure AFCB_Free
(File
: access Text_AFCB
) is
93 type FCB_Ptr
is access all Text_AFCB
;
94 FT
: FCB_Ptr
:= FCB_Ptr
(File
);
96 procedure Free
is new Unchecked_Deallocation
(Text_AFCB
, FCB_Ptr
);
106 procedure Close
(File
: in out File_Type
) is
108 FIO
.Close
(AP
(File
));
115 -- Note: we assume that it is impossible in practice for the column
116 -- to exceed the value of Count'Last, i.e. no check is required for
117 -- overflow raising layout error.
119 function Col
(File
: in File_Type
) return Positive_Count
is
121 FIO
.Check_File_Open
(AP
(File
));
125 function Col
return Positive_Count
is
127 return Col
(Current_Out
);
135 (File
: in out File_Type
;
136 Mode
: in File_Mode
:= Out_File
;
137 Name
: in String := "";
138 Form
: in String := "")
140 File_Control_Block
: Text_AFCB
;
143 FIO
.Open
(File_Ptr
=> AP
(File
),
144 Dummy_FCB
=> File_Control_Block
,
145 Mode
=> To_FCB
(Mode
),
159 function Current_Error
return File_Type
is
164 function Current_Error
return File_Access
is
166 return Current_Err
.Self
'Access;
173 function Current_Input
return File_Type
is
178 function Current_Input
return File_Access
is
180 return Current_In
.Self
'Access;
187 function Current_Output
return File_Type
is
192 function Current_Output
return File_Access
is
194 return Current_Out
.Self
'Access;
201 procedure Delete
(File
: in out File_Type
) is
203 FIO
.Delete
(AP
(File
));
210 function End_Of_File
(File
: in File_Type
) return Boolean is
214 FIO
.Check_Read_Status
(AP
(File
));
216 if File
.Before_LM
then
218 if File
.Before_LM_PM
then
219 return Nextc
(File
) = EOF
;
233 File
.Before_LM
:= True;
237 -- Here we are just past the line mark with Before_LM set so that we
238 -- do not have to try to back up past the LM, thus avoiding the need
239 -- to back up more than one character.
246 elsif ch
= PM
and then File
.Is_Regular_File
then
247 File
.Before_LM_PM
:= True;
248 return Nextc
(File
) = EOF
;
250 -- Here if neither EOF nor PM followed end of line
259 function End_Of_File
return Boolean is
261 return End_Of_File
(Current_In
);
268 function End_Of_Line
(File
: in File_Type
) return Boolean is
272 FIO
.Check_Read_Status
(AP
(File
));
274 if File
.Before_LM
then
290 function End_Of_Line
return Boolean is
292 return End_Of_Line
(Current_In
);
299 function End_Of_Page
(File
: in File_Type
) return Boolean is
303 FIO
.Check_Read_Status
(AP
(File
));
305 if not File
.Is_Regular_File
then
308 elsif File
.Before_LM
then
309 if File
.Before_LM_PM
then
324 File
.Before_LM
:= True;
328 -- Here we are just past the line mark with Before_LM set so that we
329 -- do not have to try to back up past the LM, thus avoiding the need
330 -- to back up more than one character.
334 return ch
= PM
or else ch
= EOF
;
337 function End_Of_Page
return Boolean is
339 return End_Of_Page
(Current_In
);
346 procedure Flush
(File
: in File_Type
) is
348 FIO
.Flush
(AP
(File
));
360 function Form
(File
: in File_Type
) return String is
362 return FIO
.Form
(AP
(File
));
370 (File
: in File_Type
;
371 Item
: out Character)
376 FIO
.Check_Read_Status
(AP
(File
));
378 if File
.Before_LM
then
379 File
.Before_LM
:= False;
382 if File
.Before_LM_PM
then
384 File
.Page
:= File
.Page
+ 1;
385 File
.Before_LM_PM
:= False;
387 File
.Line
:= File
.Line
+ 1;
398 File
.Line
:= File
.Line
+ 1;
401 elsif ch
= PM
and then File
.Is_Regular_File
then
402 File
.Page
:= File
.Page
+ 1;
406 Item
:= Character'Val (ch
);
407 File
.Col
:= File
.Col
+ 1;
413 procedure Get
(Item
: out Character) is
415 Get
(Current_In
, Item
);
419 (File
: in File_Type
;
426 FIO
.Check_Read_Status
(AP
(File
));
428 if File
.Before_LM
then
429 File
.Before_LM
:= False;
430 File
.Before_LM_PM
:= False;
433 if File
.Before_LM_PM
then
435 File
.Page
:= File
.Page
+ 1;
436 File
.Before_LM_PM
:= False;
439 File
.Line
:= File
.Line
+ 1;
444 while J
<= Item
'Last loop
451 File
.Line
:= File
.Line
+ 1;
454 elsif ch
= PM
and then File
.Is_Regular_File
then
455 File
.Page
:= File
.Page
+ 1;
459 Item
(J
) := Character'Val (ch
);
461 File
.Col
:= File
.Col
+ 1;
466 procedure Get
(Item
: out String) is
468 Get
(Current_In
, Item
);
475 -- More work required here ???
477 procedure Get_Immediate
478 (File
: in File_Type
;
479 Item
: out Character)
484 procedure getc_immediate
485 (stream
: FILEs
; ch
: out int
; end_of_file
: out int
);
486 pragma Import
(C
, getc_immediate
, "getc_immediate");
489 FIO
.Check_Read_Status
(AP
(File
));
491 if File
.Before_LM
then
492 File
.Before_LM
:= False;
493 File
.Before_LM_PM
:= False;
497 getc_immediate
(File
.Stream
, ch
, end_of_file
);
499 if ferror
(File
.Stream
) /= 0 then
501 elsif end_of_file
/= 0 then
506 Item
:= Character'Val (ch
);
510 procedure Get_Immediate
511 (Item
: out Character)
514 Get_Immediate
(Current_In
, Item
);
517 procedure Get_Immediate
518 (File
: in File_Type
;
519 Item
: out Character;
520 Available
: out Boolean)
526 procedure getc_immediate_nowait
529 end_of_file
: out int
;
531 pragma Import
(C
, getc_immediate_nowait
, "getc_immediate_nowait");
534 FIO
.Check_Read_Status
(AP
(File
));
536 -- If we are logically before an end of line, but physically after it,
537 -- then we just return the end of line character, no I/O is necessary.
539 if File
.Before_LM
then
540 File
.Before_LM
:= False;
541 File
.Before_LM_PM
:= False;
544 Item
:= Character'Val (LM
);
546 -- Normal case where a read operation is required
549 getc_immediate_nowait
(File
.Stream
, ch
, end_of_file
, avail
);
551 if ferror
(File
.Stream
) /= 0 then
554 elsif end_of_file
/= 0 then
563 Item
:= Character'Val (ch
);
569 procedure Get_Immediate
570 (Item
: out Character;
571 Available
: out Boolean)
574 Get_Immediate
(Current_In
, Item
, Available
);
582 (File
: in File_Type
;
589 FIO
.Check_Read_Status
(AP
(File
));
590 Last
:= Item
'First - 1;
592 -- Immediate exit for null string, this is a case in which we do not
593 -- need to test for end of file and we do not skip a line mark under
594 -- any circumstances.
596 if Last
>= Item
'Last then
600 -- Here we have at least one character, if we are immediately before
601 -- a line mark, then we will just skip past it storing no characters.
603 if File
.Before_LM
then
604 File
.Before_LM
:= False;
605 File
.Before_LM_PM
:= False;
607 -- Otherwise we need to read some characters
612 -- If we are at the end of file now, it means we are trying to
613 -- skip a file terminator and we raise End_Error (RM A.10.7(20))
619 -- Loop through characters. Don't bother if we hit a page mark,
620 -- since in normal files, page marks can only follow line marks
621 -- in any case and we only promise to treat the page nonsense
622 -- correctly in the absense of such rogue page marks.
625 -- Exit the loop if read is terminated by encountering line mark
629 -- Otherwise store the character, note that we know that ch is
630 -- something other than LM or EOF. It could possibly be a page
631 -- mark if there is a stray page mark in the middle of a line,
632 -- but this is not an official page mark in any case, since
633 -- official page marks can only follow a line mark. The whole
634 -- page business is pretty much nonsense anyway, so we do not
635 -- want to waste time trying to make sense out of non-standard
636 -- page marks in the file! This means that the behavior of
637 -- Get_Line is different from repeated Get of a character, but
638 -- that's too bad. We only promise that page numbers etc make
639 -- sense if the file is formatted in a standard manner.
641 -- Note: we do not adjust the column number because it is quicker
642 -- to adjust it once at the end of the operation than incrementing
643 -- it each time around the loop.
646 Item
(Last
) := Character'Val (ch
);
648 -- All done if the string is full, this is the case in which
649 -- we do not skip the following line mark. We need to adjust
650 -- the column number in this case.
652 if Last
= Item
'Last then
653 File
.Col
:= File
.Col
+ Count
(Item
'Length);
657 -- Otherwise read next character. We also exit from the loop if
658 -- we read an end of file. This is the case where the last line
659 -- is not terminated with a line mark, and we consider that there
660 -- is an implied line mark in this case (this is a non-standard
661 -- file, but it is nice to treat it reasonably).
668 -- We have skipped past, but not stored, a line mark. Skip following
669 -- page mark if one follows, but do not do this for a non-regular
670 -- file (since otherwise we get annoying wait for an extra character)
672 File
.Line
:= File
.Line
+ 1;
675 if File
.Before_LM_PM
then
677 File
.Before_LM_PM
:= False;
678 File
.Page
:= File
.Page
+ 1;
680 elsif File
.Is_Regular_File
then
683 if ch
= PM
and then File
.Is_Regular_File
then
685 File
.Page
:= File
.Page
+ 1;
697 Get_Line
(Current_In
, Item
, Last
);
704 function Getc
(File
: File_Type
) return int
is
708 ch
:= fgetc
(File
.Stream
);
710 if ch
= EOF
and then ferror
(File
.Stream
) /= 0 then
721 function Is_Open
(File
: in File_Type
) return Boolean is
723 return FIO
.Is_Open
(AP
(File
));
730 -- Note: we assume that it is impossible in practice for the line
731 -- to exceed the value of Count'Last, i.e. no check is required for
732 -- overflow raising layout error.
734 function Line
(File
: in File_Type
) return Positive_Count
is
736 FIO
.Check_File_Open
(AP
(File
));
740 function Line
return Positive_Count
is
742 return Line
(Current_Out
);
749 function Line_Length
(File
: in File_Type
) return Count
is
751 FIO
.Check_Write_Status
(AP
(File
));
752 return File
.Line_Length
;
755 function Line_Length
return Count
is
757 return Line_Length
(Current_Out
);
765 (File
: in File_Type
;
766 Item
: out Character;
767 End_Of_Line
: out Boolean)
772 FIO
.Check_Read_Status
(AP
(File
));
774 if File
.Before_LM
then
783 or else (ch
= PM
and then File
.Is_Regular_File
)
788 End_Of_Line
:= False;
789 Item
:= Character'Val (ch
);
795 (Item
: out Character;
796 End_Of_Line
: out Boolean)
799 Look_Ahead
(Current_In
, Item
, End_Of_Line
);
806 function Mode
(File
: in File_Type
) return File_Mode
is
808 return To_TIO
(FIO
.Mode
(AP
(File
)));
815 function Name
(File
: in File_Type
) return String is
817 return FIO
.Name
(AP
(File
));
825 (File
: in File_Type
;
826 Spacing
: in Positive_Count
:= 1)
829 -- Raise Constraint_Error if out of range value. The reason for this
830 -- explicit test is that we don't want junk values around, even if
831 -- checks are off in the caller.
833 if Spacing
not in Positive_Count
then
834 raise Constraint_Error
;
837 FIO
.Check_Write_Status
(AP
(File
));
839 for K
in 1 .. Spacing
loop
841 File
.Line
:= File
.Line
+ 1;
843 if File
.Page_Length
/= 0
844 and then File
.Line
> File
.Page_Length
848 File
.Page
:= File
.Page
+ 1;
855 procedure New_Line
(Spacing
: in Positive_Count
:= 1) is
857 New_Line
(Current_Out
, Spacing
);
864 procedure New_Page
(File
: in File_Type
) is
866 FIO
.Check_Write_Status
(AP
(File
));
868 if File
.Col
/= 1 or else File
.Line
= 1 then
873 File
.Page
:= File
.Page
+ 1;
878 procedure New_Page
is
880 New_Page
(Current_Out
);
887 function Nextc
(File
: File_Type
) return int
is
891 ch
:= fgetc
(File
.Stream
);
894 if ferror
(File
.Stream
) /= 0 then
899 if ungetc
(ch
, File
.Stream
) = EOF
then
912 (File
: in out File_Type
;
915 Form
: in String := "")
917 File_Control_Block
: Text_AFCB
;
920 FIO
.Open
(File_Ptr
=> AP
(File
),
921 Dummy_FCB
=> File_Control_Block
,
922 Mode
=> To_FCB
(Mode
),
936 -- Note: we assume that it is impossible in practice for the page
937 -- to exceed the value of Count'Last, i.e. no check is required for
938 -- overflow raising layout error.
940 function Page
(File
: in File_Type
) return Positive_Count
is
942 FIO
.Check_File_Open
(AP
(File
));
946 function Page
return Positive_Count
is
948 return Page
(Current_Out
);
955 function Page_Length
(File
: in File_Type
) return Count
is
957 FIO
.Check_Write_Status
(AP
(File
));
958 return File
.Page_Length
;
961 function Page_Length
return Count
is
963 return Page_Length
(Current_Out
);
971 (File
: in File_Type
;
975 FIO
.Check_Write_Status
(AP
(File
));
977 if File
.Line_Length
/= 0 and then File
.Col
> File
.Line_Length
then
981 if fputc
(Character'Pos (Item
), File
.Stream
) = EOF
then
985 File
.Col
:= File
.Col
+ 1;
988 procedure Put
(Item
: in Character) is
990 FIO
.Check_Write_Status
(AP
(Current_Out
));
992 if Current_Out
.Line_Length
/= 0
993 and then Current_Out
.Col
> Current_Out
.Line_Length
995 New_Line
(Current_Out
);
998 if fputc
(Character'Pos (Item
), Current_Out
.Stream
) = EOF
then
1002 Current_Out
.Col
:= Current_Out
.Col
+ 1;
1010 (File
: in File_Type
;
1014 FIO
.Check_Write_Status
(AP
(File
));
1016 if Item
'Length > 0 then
1018 -- If we have bounded lines, then do things character by
1019 -- character (this seems a rare case anyway!)
1021 if File
.Line_Length
/= 0 then
1022 for J
in Item
'Range loop
1023 Put
(File
, Item
(J
));
1026 -- Otherwise we can output the entire string at once. Note that if
1027 -- there are LF or FF characters in the string, we do not bother to
1028 -- count them as line or page terminators.
1031 FIO
.Write_Buf
(AP
(File
), Item
'Address, Item
'Length);
1032 File
.Col
:= File
.Col
+ Item
'Length;
1037 procedure Put
(Item
: in String) is
1039 Put
(Current_Out
, Item
);
1047 (File
: in File_Type
;
1051 FIO
.Check_Write_Status
(AP
(File
));
1053 -- If we have bounded lines, then just do a put and a new line. In
1054 -- this case we will end up doing things character by character in
1055 -- any case, and it is a rare situation.
1057 if File
.Line_Length
/= 0 then
1063 -- We setup a single string that has the necessary terminators and
1064 -- then write it with a single call. The reason for doing this is
1065 -- that it gives better behavior for the use of Put_Line in multi-
1066 -- tasking programs, since often the OS will treat the entire put
1067 -- operation as an atomic operation.
1070 Ilen
: constant Natural := Item
'Length;
1071 Buffer
: String (1 .. Ilen
+ 2);
1075 Buffer
(1 .. Ilen
) := Item
;
1076 Buffer
(Ilen
+ 1) := Character'Val (LM
);
1078 if File
.Page_Length
/= 0
1079 and then File
.Line
> File
.Page_Length
1081 Buffer
(Ilen
+ 2) := Character'Val (PM
);
1082 Plen
:= size_t
(Ilen
) + 2;
1084 File
.Page
:= File
.Page
+ 1;
1087 Plen
:= size_t
(Ilen
) + 1;
1088 File
.Line
:= File
.Line
+ 1;
1091 FIO
.Write_Buf
(AP
(File
), Buffer
'Address, Plen
);
1097 procedure Put_Line
(Item
: in String) is
1099 Put_Line
(Current_Out
, Item
);
1106 procedure Putc
(ch
: int
; File
: File_Type
) is
1108 if fputc
(ch
, File
.Stream
) = EOF
then
1117 -- This is the primitive Stream Read routine, used when a Text_IO file
1118 -- is treated directly as a stream using Text_IO.Streams.Stream.
1121 (File
: in out Text_AFCB
;
1122 Item
: out Stream_Element_Array
;
1123 Last
: out Stream_Element_Offset
)
1128 if File
.Mode
/= FCB
.In_File
then
1132 -- Deal with case where our logical and physical position do not match
1133 -- because of being after an LM or LM-PM sequence when in fact we are
1134 -- logically positioned before it.
1136 if File
.Before_LM
then
1138 -- If we are before a PM, then it is possible for a stream read
1139 -- to leave us after the LM and before the PM, which is a bit
1140 -- odd. The easiest way to deal with this is to unget the PM,
1141 -- so we are indeed positioned between the characters. This way
1142 -- further stream read operations will work correctly, and the
1143 -- effect on text processing is a little weird, but what can
1144 -- be expected if stream and text input are mixed this way?
1146 if File
.Before_LM_PM
then
1147 ch
:= ungetc
(PM
, File
.Stream
);
1148 File
.Before_LM_PM
:= False;
1151 File
.Before_LM
:= False;
1153 Item
(Item
'First) := Stream_Element
(Character'Pos (ASCII
.LF
));
1155 if Item
'Length = 1 then
1161 Stream_Element_Offset
1162 (fread
(buffer
=> Item
'Address,
1163 index
=> size_t
(Item
'First + 1),
1165 count
=> Item
'Length - 1,
1166 stream
=> File
.Stream
));
1172 -- Now we do the read. Since this is a text file, it is normally in
1173 -- text mode, but stream data must be read in binary mode, so we
1174 -- temporarily set binary mode for the read, resetting it after.
1175 -- These calls have no effect in a system (like Unix) where there is
1176 -- no distinction between text and binary files.
1178 set_binary_mode
(fileno
(File
.Stream
));
1182 Stream_Element_Offset
1183 (fread
(Item
'Address, 1, Item
'Length, File
.Stream
)) - 1;
1185 if Last
< Item
'Last then
1186 if ferror
(File
.Stream
) /= 0 then
1191 set_text_mode
(fileno
(File
.Stream
));
1199 (File
: in out File_Type
;
1200 Mode
: in File_Mode
)
1203 -- Don't allow change of mode for current file (RM A.10.2(5))
1205 if (File
= Current_In
or else
1206 File
= Current_Out
or else
1207 File
= Current_Error
)
1208 and then To_FCB
(Mode
) /= File
.Mode
1213 Terminate_Line
(File
);
1214 FIO
.Reset
(AP
(File
), To_FCB
(Mode
));
1218 File
.Line_Length
:= 0;
1219 File
.Page_Length
:= 0;
1220 File
.Before_LM
:= False;
1221 File
.Before_LM_PM
:= False;
1224 procedure Reset
(File
: in out File_Type
) is
1226 Terminate_Line
(File
);
1227 FIO
.Reset
(AP
(File
));
1231 File
.Line_Length
:= 0;
1232 File
.Page_Length
:= 0;
1233 File
.Before_LM
:= False;
1234 File
.Before_LM_PM
:= False;
1242 (File
: in File_Type
;
1243 To
: in Positive_Count
)
1248 -- Raise Constraint_Error if out of range value. The reason for this
1249 -- explicit test is that we don't want junk values around, even if
1250 -- checks are off in the caller.
1252 if To
not in Positive_Count
then
1253 raise Constraint_Error
;
1256 FIO
.Check_File_Open
(AP
(File
));
1258 if To
= File
.Col
then
1262 if Mode
(File
) >= Out_File
then
1263 if File
.Line_Length
/= 0 and then To
> File
.Line_Length
then
1267 if To
< File
.Col
then
1271 while File
.Col
< To
loop
1283 File
.Line
:= File
.Line
+ 1;
1286 elsif ch
= PM
and then File
.Is_Regular_File
then
1287 File
.Page
:= File
.Page
+ 1;
1291 elsif To
= File
.Col
then
1296 File
.Col
:= File
.Col
+ 1;
1302 procedure Set_Col
(To
: in Positive_Count
) is
1304 Set_Col
(Current_Out
, To
);
1311 procedure Set_Error
(File
: in File_Type
) is
1313 FIO
.Check_Write_Status
(AP
(File
));
1314 Current_Err
:= File
;
1321 procedure Set_Input
(File
: in File_Type
) is
1323 FIO
.Check_Read_Status
(AP
(File
));
1332 (File
: in File_Type
;
1333 To
: in Positive_Count
)
1336 -- Raise Constraint_Error if out of range value. The reason for this
1337 -- explicit test is that we don't want junk values around, even if
1338 -- checks are off in the caller.
1340 if To
not in Positive_Count
then
1341 raise Constraint_Error
;
1344 FIO
.Check_File_Open
(AP
(File
));
1346 if To
= File
.Line
then
1350 if Mode
(File
) >= Out_File
then
1351 if File
.Page_Length
/= 0 and then To
> File
.Page_Length
then
1355 if To
< File
.Line
then
1359 while File
.Line
< To
loop
1364 while To
/= File
.Line
loop
1370 procedure Set_Line
(To
: in Positive_Count
) is
1372 Set_Line
(Current_Out
, To
);
1375 ---------------------
1376 -- Set_Line_Length --
1377 ---------------------
1379 procedure Set_Line_Length
(File
: in File_Type
; To
: in Count
) is
1381 -- Raise Constraint_Error if out of range value. The reason for this
1382 -- explicit test is that we don't want junk values around, even if
1383 -- checks are off in the caller.
1385 if To
not in Count
then
1386 raise Constraint_Error
;
1389 FIO
.Check_Write_Status
(AP
(File
));
1390 File
.Line_Length
:= To
;
1391 end Set_Line_Length
;
1393 procedure Set_Line_Length
(To
: in Count
) is
1395 Set_Line_Length
(Current_Out
, To
);
1396 end Set_Line_Length
;
1402 procedure Set_Output
(File
: in File_Type
) is
1404 FIO
.Check_Write_Status
(AP
(File
));
1405 Current_Out
:= File
;
1408 ---------------------
1409 -- Set_Page_Length --
1410 ---------------------
1412 procedure Set_Page_Length
(File
: in File_Type
; To
: in Count
) is
1414 -- Raise Constraint_Error if out of range value. The reason for this
1415 -- explicit test is that we don't want junk values around, even if
1416 -- checks are off in the caller.
1418 if To
not in Count
then
1419 raise Constraint_Error
;
1422 FIO
.Check_Write_Status
(AP
(File
));
1423 File
.Page_Length
:= To
;
1424 end Set_Page_Length
;
1426 procedure Set_Page_Length
(To
: in Count
) is
1428 Set_Page_Length
(Current_Out
, To
);
1429 end Set_Page_Length
;
1436 (File
: in File_Type
;
1437 Spacing
: in Positive_Count
:= 1)
1442 -- Raise Constraint_Error if out of range value. The reason for this
1443 -- explicit test is that we don't want junk values around, even if
1444 -- checks are off in the caller.
1446 if Spacing
not in Positive_Count
then
1447 raise Constraint_Error
;
1450 FIO
.Check_Read_Status
(AP
(File
));
1452 for L
in 1 .. Spacing
loop
1453 if File
.Before_LM
then
1454 File
.Before_LM
:= False;
1455 File
.Before_LM_PM
:= False;
1460 -- If at end of file now, then immediately raise End_Error. Note
1461 -- that we can never be positioned between a line mark and a page
1462 -- mark, so if we are at the end of file, we cannot logically be
1463 -- before the implicit page mark that is at the end of the file.
1465 -- For the same reason, we do not need an explicit check for a
1466 -- page mark. If there is a FF in the middle of a line, the file
1467 -- is not in canonical format and we do not care about the page
1468 -- numbers for files other than ones in canonical format.
1474 -- If not at end of file, then loop till we get to an LM or EOF.
1475 -- The latter case happens only in non-canonical files where the
1476 -- last line is not terminated by LM, but we don't want to blow
1477 -- up for such files, so we assume an implicit LM in this case.
1480 exit when ch
= LM
or ch
= EOF
;
1485 -- We have got past a line mark, now, for a regular file only,
1486 -- see if a page mark immediately follows this line mark and
1487 -- if so, skip past the page mark as well. We do not do this
1488 -- for non-regular files, since it would cause an undesirable
1489 -- wait for an additional character.
1492 File
.Line
:= File
.Line
+ 1;
1494 if File
.Before_LM_PM
then
1495 File
.Page
:= File
.Page
+ 1;
1497 File
.Before_LM_PM
:= False;
1499 elsif File
.Is_Regular_File
then
1502 -- Page mark can be explicit, or implied at the end of the file
1504 if (ch
= PM
or else ch
= EOF
)
1505 and then File
.Is_Regular_File
1507 File
.Page
:= File
.Page
+ 1;
1517 procedure Skip_Line
(Spacing
: in Positive_Count
:= 1) is
1519 Skip_Line
(Current_In
, Spacing
);
1526 procedure Skip_Page
(File
: in File_Type
) is
1530 FIO
.Check_Read_Status
(AP
(File
));
1532 -- If at page mark already, just skip it
1534 if File
.Before_LM_PM
then
1535 File
.Before_LM
:= False;
1536 File
.Before_LM_PM
:= False;
1537 File
.Page
:= File
.Page
+ 1;
1543 -- This is a bit tricky, if we are logically before an LM then
1544 -- it is not an error if we are at an end of file now, since we
1545 -- are not really at it.
1547 if File
.Before_LM
then
1548 File
.Before_LM
:= False;
1549 File
.Before_LM_PM
:= False;
1552 -- Otherwise we do raise End_Error if we are at the end of file now
1562 -- Now we can just rumble along to the next page mark, or to the
1563 -- end of file, if that comes first. The latter case happens when
1564 -- the page mark is implied at the end of file.
1568 or else (ch
= PM
and then File
.Is_Regular_File
);
1572 File
.Page
:= File
.Page
+ 1;
1577 procedure Skip_Page
is
1579 Skip_Page
(Current_In
);
1582 --------------------
1583 -- Standard_Error --
1584 --------------------
1586 function Standard_Error
return File_Type
is
1588 return Standard_Err
;
1591 function Standard_Error
return File_Access
is
1593 return Standard_Err
'Access;
1596 --------------------
1597 -- Standard_Input --
1598 --------------------
1600 function Standard_Input
return File_Type
is
1605 function Standard_Input
return File_Access
is
1607 return Standard_In
'Access;
1610 ---------------------
1611 -- Standard_Output --
1612 ---------------------
1614 function Standard_Output
return File_Type
is
1616 return Standard_Out
;
1617 end Standard_Output
;
1619 function Standard_Output
return File_Access
is
1621 return Standard_Out
'Access;
1622 end Standard_Output
;
1624 --------------------
1625 -- Terminate_Line --
1626 --------------------
1628 procedure Terminate_Line
(File
: File_Type
) is
1630 FIO
.Check_File_Open
(AP
(File
));
1632 -- For file other than In_File, test for needing to terminate last line
1634 if Mode
(File
) /= In_File
then
1636 -- If not at start of line definition need new line
1638 if File
.Col
/= 1 then
1641 -- For files other than standard error and standard output, we
1642 -- make sure that an empty file has a single line feed, so that
1643 -- it is properly formatted. We avoid this for the standard files
1644 -- because it is too much of a nuisance to have these odd line
1645 -- feeds when nothing has been written to the file.
1647 elsif (File
/= Standard_Err
and then File
/= Standard_Out
)
1648 and then (File
.Line
= 1 and then File
.Page
= 1)
1659 procedure Ungetc
(ch
: int
; File
: File_Type
) is
1662 if ungetc
(ch
, File
.Stream
) = EOF
then
1672 -- This is the primitive Stream Write routine, used when a Text_IO file
1673 -- is treated directly as a stream using Text_IO.Streams.Stream.
1676 (File
: in out Text_AFCB
;
1677 Item
: in Stream_Element_Array
)
1680 function Has_Translated_Characters
return Boolean;
1681 -- return True if Item array contains a character which will be
1682 -- translated under the text file mode. There is only one such
1683 -- character under DOS based systems which is character 10.
1685 text_translation_required
: Boolean;
1686 pragma Import
(C
, text_translation_required
,
1687 "__gnat_text_translation_required");
1689 Siz
: constant size_t
:= Item
'Length;
1691 function Has_Translated_Characters
return Boolean is
1693 for K
in Item
'Range loop
1694 if Item
(K
) = 10 then
1699 end Has_Translated_Characters
;
1701 Needs_Binary_Write
: constant Boolean :=
1702 text_translation_required
and then Has_Translated_Characters
;
1705 if File
.Mode
= FCB
.In_File
then
1709 -- Now we do the write. Since this is a text file, it is normally in
1710 -- text mode, but stream data must be written in binary mode, so we
1711 -- temporarily set binary mode for the write, resetting it after. This
1712 -- is done only if needed (i.e. there is some characters in Item which
1713 -- needs to be written using the binary mode).
1714 -- These calls have no effect in a system (like Unix) where there is
1715 -- no distinction between text and binary files.
1717 -- Since the character translation is done at the time the buffer is
1718 -- written (this is true under Windows) we first flush current buffer
1719 -- with text mode if needed.
1721 if Needs_Binary_Write
then
1723 if fflush
(File
.Stream
) = -1 then
1727 set_binary_mode
(fileno
(File
.Stream
));
1730 if fwrite
(Item
'Address, 1, Siz
, File
.Stream
) /= Siz
then
1734 -- At this point we need to flush the buffer using the binary mode then
1735 -- we reset to text mode.
1737 if Needs_Binary_Write
then
1739 if fflush
(File
.Stream
) = -1 then
1743 set_text_mode
(fileno
(File
.Stream
));
1747 -- Use "preallocated" strings to avoid calling "new" during the
1748 -- elaboration of the run time. This is needed in the tasking case to
1749 -- avoid calling Task_Lock too early. A filename is expected to end with a
1750 -- null character in the runtime, here the null characters are added just
1751 -- to have a correct filename length.
1753 Err_Name
: aliased String := "*stderr" & ASCII
.Nul
;
1754 In_Name
: aliased String := "*stdin" & ASCII
.Nul
;
1755 Out_Name
: aliased String := "*stdout" & ASCII
.Nul
;
1757 -------------------------------
1758 -- Initialize Standard Files --
1759 -------------------------------
1761 -- Note: the names in these files are bogus, and probably it would be
1762 -- better for these files to have no names, but the ACVC test insist!
1763 -- We use names that are bound to fail in open etc.
1765 Standard_Err
.Stream
:= stderr
;
1766 Standard_Err
.Name
:= Err_Name
'Access;
1767 Standard_Err
.Form
:= Null_Str
'Unrestricted_Access;
1768 Standard_Err
.Mode
:= FCB
.Out_File
;
1769 Standard_Err
.Is_Regular_File
:= is_regular_file
(fileno
(stderr
)) /= 0;
1770 Standard_Err
.Is_Temporary_File
:= False;
1771 Standard_Err
.Is_System_File
:= True;
1772 Standard_Err
.Is_Text_File
:= True;
1773 Standard_Err
.Access_Method
:= 'T';
1774 Standard_Err
.Self
:= Standard_Err
;
1776 Standard_In
.Stream
:= stdin
;
1777 Standard_In
.Name
:= In_Name
'Access;
1778 Standard_In
.Form
:= Null_Str
'Unrestricted_Access;
1779 Standard_In
.Mode
:= FCB
.In_File
;
1780 Standard_In
.Is_Regular_File
:= is_regular_file
(fileno
(stdin
)) /= 0;
1781 Standard_In
.Is_Temporary_File
:= False;
1782 Standard_In
.Is_System_File
:= True;
1783 Standard_In
.Is_Text_File
:= True;
1784 Standard_In
.Access_Method
:= 'T';
1785 Standard_In
.Self
:= Standard_In
;
1787 Standard_Out
.Stream
:= stdout
;
1788 Standard_Out
.Name
:= Out_Name
'Access;
1789 Standard_Out
.Form
:= Null_Str
'Unrestricted_Access;
1790 Standard_Out
.Mode
:= FCB
.Out_File
;
1791 Standard_Out
.Is_Regular_File
:= is_regular_file
(fileno
(stdout
)) /= 0;
1792 Standard_Out
.Is_Temporary_File
:= False;
1793 Standard_Out
.Is_System_File
:= True;
1794 Standard_Out
.Is_Text_File
:= True;
1795 Standard_Out
.Access_Method
:= 'T';
1796 Standard_Out
.Self
:= Standard_Out
;
1798 FIO
.Chain_File
(AP
(Standard_In
));
1799 FIO
.Chain_File
(AP
(Standard_Out
));
1800 FIO
.Chain_File
(AP
(Standard_Err
));
1802 FIO
.Make_Unbuffered
(AP
(Standard_Out
));
1803 FIO
.Make_Unbuffered
(AP
(Standard_Err
));