1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . T E X T _ I O --
9 -- Copyright (C) 1992-2006, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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
;
40 with Unchecked_Conversion
;
41 with Unchecked_Deallocation
;
43 pragma Elaborate_All
(System
.File_IO
);
44 -- Needed because of calls to Chain_File in package body elaboration
46 package body Ada
.Text_IO
is
48 package FIO
renames System
.File_IO
;
50 subtype AP
is FCB
.AFCB_Ptr
;
52 function To_FCB
is new Unchecked_Conversion
(File_Mode
, FCB
.File_Mode
);
53 function To_TIO
is new Unchecked_Conversion
(FCB
.File_Mode
, File_Mode
);
54 use type FCB
.File_Mode
;
56 use type System
.CRTL
.size_t
;
62 function AFCB_Allocate
(Control_Block
: Text_AFCB
) return FCB
.AFCB_Ptr
is
63 pragma Unreferenced
(Control_Block
);
72 procedure AFCB_Close
(File
: access Text_AFCB
) is
74 -- If the file being closed is one of the current files, then close
75 -- the corresponding current file. It is not clear that this action
76 -- is required (RM A.10.3(23)) but it seems reasonable, and besides
77 -- ACVC test CE3208A expects this behavior.
79 if File_Type
(File
) = Current_In
then
81 elsif File_Type
(File
) = Current_Out
then
83 elsif File_Type
(File
) = Current_Err
then
87 Terminate_Line
(File_Type
(File
));
94 procedure AFCB_Free
(File
: access Text_AFCB
) is
95 type FCB_Ptr
is access all Text_AFCB
;
96 FT
: FCB_Ptr
:= FCB_Ptr
(File
);
98 procedure Free
is new Unchecked_Deallocation
(Text_AFCB
, FCB_Ptr
);
108 procedure Close
(File
: in out File_Type
) is
110 FIO
.Close
(AP
(File
));
117 -- Note: we assume that it is impossible in practice for the column
118 -- to exceed the value of Count'Last, i.e. no check is required for
119 -- overflow raising layout error.
121 function Col
(File
: File_Type
) return Positive_Count
is
123 FIO
.Check_File_Open
(AP
(File
));
127 function Col
return Positive_Count
is
129 return Col
(Current_Out
);
137 (File
: in out File_Type
;
138 Mode
: File_Mode
:= Out_File
;
142 Dummy_File_Control_Block
: Text_AFCB
;
143 pragma Warnings
(Off
, Dummy_File_Control_Block
);
144 -- Yes, we know this is never assigned a value, only the tag
145 -- is used for dispatching purposes, so that's expected.
148 FIO
.Open
(File_Ptr
=> AP
(File
),
149 Dummy_FCB
=> Dummy_File_Control_Block
,
150 Mode
=> To_FCB
(Mode
),
164 function Current_Error
return File_Type
is
169 function Current_Error
return File_Access
is
171 return Current_Err
.Self
'Access;
178 function Current_Input
return File_Type
is
183 function Current_Input
return File_Access
is
185 return Current_In
.Self
'Access;
192 function Current_Output
return File_Type
is
197 function Current_Output
return File_Access
is
199 return Current_Out
.Self
'Access;
206 procedure Delete
(File
: in out File_Type
) is
208 FIO
.Delete
(AP
(File
));
215 function End_Of_File
(File
: File_Type
) return Boolean is
219 FIO
.Check_Read_Status
(AP
(File
));
221 if File
.Before_LM
then
223 if File
.Before_LM_PM
then
224 return Nextc
(File
) = EOF
;
238 File
.Before_LM
:= True;
242 -- Here we are just past the line mark with Before_LM set so that we
243 -- do not have to try to back up past the LM, thus avoiding the need
244 -- to back up more than one character.
251 elsif ch
= PM
and then File
.Is_Regular_File
then
252 File
.Before_LM_PM
:= True;
253 return Nextc
(File
) = EOF
;
255 -- Here if neither EOF nor PM followed end of line
264 function End_Of_File
return Boolean is
266 return End_Of_File
(Current_In
);
273 function End_Of_Line
(File
: File_Type
) return Boolean is
277 FIO
.Check_Read_Status
(AP
(File
));
279 if File
.Before_LM
then
295 function End_Of_Line
return Boolean is
297 return End_Of_Line
(Current_In
);
304 function End_Of_Page
(File
: File_Type
) return Boolean is
308 FIO
.Check_Read_Status
(AP
(File
));
310 if not File
.Is_Regular_File
then
313 elsif File
.Before_LM
then
314 if File
.Before_LM_PM
then
329 File
.Before_LM
:= True;
333 -- Here we are just past the line mark with Before_LM set so that we
334 -- do not have to try to back up past the LM, thus avoiding the need
335 -- to back up more than one character.
339 return ch
= PM
or else ch
= EOF
;
342 function End_Of_Page
return Boolean is
344 return End_Of_Page
(Current_In
);
351 function EOF_Char
return Integer is
360 procedure Flush
(File
: File_Type
) is
362 FIO
.Flush
(AP
(File
));
374 function Form
(File
: File_Type
) return String is
376 return FIO
.Form
(AP
(File
));
385 Item
: out Character)
390 FIO
.Check_Read_Status
(AP
(File
));
392 if File
.Before_LM
then
393 File
.Before_LM
:= False;
396 if File
.Before_LM_PM
then
398 File
.Page
:= File
.Page
+ 1;
399 File
.Before_LM_PM
:= False;
401 File
.Line
:= File
.Line
+ 1;
412 File
.Line
:= File
.Line
+ 1;
415 elsif ch
= PM
and then File
.Is_Regular_File
then
416 File
.Page
:= File
.Page
+ 1;
420 Item
:= Character'Val (ch
);
421 File
.Col
:= File
.Col
+ 1;
427 procedure Get
(Item
: out Character) is
429 Get
(Current_In
, Item
);
440 FIO
.Check_Read_Status
(AP
(File
));
442 if File
.Before_LM
then
443 File
.Before_LM
:= False;
444 File
.Before_LM_PM
:= False;
447 if File
.Before_LM_PM
then
449 File
.Page
:= File
.Page
+ 1;
450 File
.Before_LM_PM
:= False;
453 File
.Line
:= File
.Line
+ 1;
458 while J
<= Item
'Last loop
465 File
.Line
:= File
.Line
+ 1;
468 elsif ch
= PM
and then File
.Is_Regular_File
then
469 File
.Page
:= File
.Page
+ 1;
473 Item
(J
) := Character'Val (ch
);
475 File
.Col
:= File
.Col
+ 1;
480 procedure Get
(Item
: out String) is
482 Get
(Current_In
, Item
);
489 -- More work required here ???
491 procedure Get_Immediate
493 Item
: out Character)
498 procedure getc_immediate
501 end_of_file
: out int
);
502 pragma Import
(C
, getc_immediate
, "getc_immediate");
505 FIO
.Check_Read_Status
(AP
(File
));
507 if File
.Before_LM
then
508 File
.Before_LM
:= False;
509 File
.Before_LM_PM
:= False;
513 getc_immediate
(File
.Stream
, ch
, end_of_file
);
515 if ferror
(File
.Stream
) /= 0 then
517 elsif end_of_file
/= 0 then
522 Item
:= Character'Val (ch
);
525 procedure Get_Immediate
526 (Item
: out Character)
529 Get_Immediate
(Current_In
, Item
);
532 procedure Get_Immediate
534 Item
: out Character;
535 Available
: out Boolean)
541 procedure getc_immediate_nowait
544 end_of_file
: out int
;
546 pragma Import
(C
, getc_immediate_nowait
, "getc_immediate_nowait");
549 FIO
.Check_Read_Status
(AP
(File
));
551 -- If we are logically before an end of line, but physically after it,
552 -- then we just return the end of line character, no I/O is necessary.
554 if File
.Before_LM
then
555 File
.Before_LM
:= False;
556 File
.Before_LM_PM
:= False;
559 Item
:= Character'Val (LM
);
561 -- Normal case where a read operation is required
564 getc_immediate_nowait
(File
.Stream
, ch
, end_of_file
, avail
);
566 if ferror
(File
.Stream
) /= 0 then
569 elsif end_of_file
/= 0 then
578 Item
:= Character'Val (ch
);
584 procedure Get_Immediate
585 (Item
: out Character;
586 Available
: out Boolean)
589 Get_Immediate
(Current_In
, Item
, Available
);
604 FIO
.Check_Read_Status
(AP
(File
));
605 Last
:= Item
'First - 1;
607 -- Immediate exit for null string, this is a case in which we do not
608 -- need to test for end of file and we do not skip a line mark under
609 -- any circumstances.
611 if Last
>= Item
'Last then
615 -- Here we have at least one character, if we are immediately before
616 -- a line mark, then we will just skip past it storing no characters.
618 if File
.Before_LM
then
619 File
.Before_LM
:= False;
620 File
.Before_LM_PM
:= False;
622 -- Otherwise we need to read some characters
627 -- If we are at the end of file now, it means we are trying to
628 -- skip a file terminator and we raise End_Error (RM A.10.7(20))
634 -- Loop through characters. Don't bother if we hit a page mark,
635 -- since in normal files, page marks can only follow line marks
636 -- in any case and we only promise to treat the page nonsense
637 -- correctly in the absense of such rogue page marks.
640 -- Exit the loop if read is terminated by encountering line mark
644 -- Otherwise store the character, note that we know that ch is
645 -- something other than LM or EOF. It could possibly be a page
646 -- mark if there is a stray page mark in the middle of a line,
647 -- but this is not an official page mark in any case, since
648 -- official page marks can only follow a line mark. The whole
649 -- page business is pretty much nonsense anyway, so we do not
650 -- want to waste time trying to make sense out of non-standard
651 -- page marks in the file! This means that the behavior of
652 -- Get_Line is different from repeated Get of a character, but
653 -- that's too bad. We only promise that page numbers etc make
654 -- sense if the file is formatted in a standard manner.
656 -- Note: we do not adjust the column number because it is quicker
657 -- to adjust it once at the end of the operation than incrementing
658 -- it each time around the loop.
661 Item
(Last
) := Character'Val (ch
);
663 -- All done if the string is full, this is the case in which
664 -- we do not skip the following line mark. We need to adjust
665 -- the column number in this case.
667 if Last
= Item
'Last then
668 File
.Col
:= File
.Col
+ Count
(Item
'Length);
672 -- Otherwise read next character. We also exit from the loop if
673 -- we read an end of file. This is the case where the last line
674 -- is not terminated with a line mark, and we consider that there
675 -- is an implied line mark in this case (this is a non-standard
676 -- file, but it is nice to treat it reasonably).
683 -- We have skipped past, but not stored, a line mark. Skip following
684 -- page mark if one follows, but do not do this for a non-regular
685 -- file (since otherwise we get annoying wait for an extra character)
687 File
.Line
:= File
.Line
+ 1;
690 if File
.Before_LM_PM
then
692 File
.Before_LM_PM
:= False;
693 File
.Page
:= File
.Page
+ 1;
695 elsif File
.Is_Regular_File
then
698 if ch
= PM
and then File
.Is_Regular_File
then
700 File
.Page
:= File
.Page
+ 1;
712 Get_Line
(Current_In
, Item
, Last
);
715 function Get_Line
(File
: File_Type
) return String is
716 Buffer
: String (1 .. 500);
719 function Get_Rest
(S
: String) return String;
720 -- This is a recursive function that reads the rest of the line and
721 -- returns it. S is the part read so far.
727 function Get_Rest
(S
: String) return String is
729 -- Each time we allocate a buffer the same size as what we have
730 -- read so far. This limits us to a logarithmic number of calls
731 -- to Get_Rest and also ensures only a linear use of stack space.
733 Buffer
: String (1 .. S
'Length);
737 Get_Line
(File
, Buffer
, Last
);
740 R
: constant String := S
& Buffer
(1 .. Last
);
742 if Last
< Buffer
'Last then
750 -- Start of processing for Get_Line
753 Get_Line
(File
, Buffer
, Last
);
755 if Last
< Buffer
'Last then
756 return Buffer
(1 .. Last
);
758 return Get_Rest
(Buffer
(1 .. Last
));
762 function Get_Line
return String is
764 return Get_Line
(Current_In
);
771 function Getc
(File
: File_Type
) return int
is
775 ch
:= fgetc
(File
.Stream
);
777 if ch
= EOF
and then ferror
(File
.Stream
) /= 0 then
788 function Is_Open
(File
: File_Type
) return Boolean is
790 return FIO
.Is_Open
(AP
(File
));
797 -- Note: we assume that it is impossible in practice for the line
798 -- to exceed the value of Count'Last, i.e. no check is required for
799 -- overflow raising layout error.
801 function Line
(File
: File_Type
) return Positive_Count
is
803 FIO
.Check_File_Open
(AP
(File
));
807 function Line
return Positive_Count
is
809 return Line
(Current_Out
);
816 function Line_Length
(File
: File_Type
) return Count
is
818 FIO
.Check_Write_Status
(AP
(File
));
819 return File
.Line_Length
;
822 function Line_Length
return Count
is
824 return Line_Length
(Current_Out
);
833 Item
: out Character;
834 End_Of_Line
: out Boolean)
839 FIO
.Check_Read_Status
(AP
(File
));
841 if File
.Before_LM
then
850 or else (ch
= PM
and then File
.Is_Regular_File
)
855 End_Of_Line
:= False;
856 Item
:= Character'Val (ch
);
862 (Item
: out Character;
863 End_Of_Line
: out Boolean)
866 Look_Ahead
(Current_In
, Item
, End_Of_Line
);
873 function Mode
(File
: File_Type
) return File_Mode
is
875 return To_TIO
(FIO
.Mode
(AP
(File
)));
882 function Name
(File
: File_Type
) return String is
884 return FIO
.Name
(AP
(File
));
893 Spacing
: Positive_Count
:= 1)
896 -- Raise Constraint_Error if out of range value. The reason for this
897 -- explicit test is that we don't want junk values around, even if
898 -- checks are off in the caller.
900 if not Spacing
'Valid then
901 raise Constraint_Error
;
904 FIO
.Check_Write_Status
(AP
(File
));
906 for K
in 1 .. Spacing
loop
908 File
.Line
:= File
.Line
+ 1;
910 if File
.Page_Length
/= 0
911 and then File
.Line
> File
.Page_Length
915 File
.Page
:= File
.Page
+ 1;
922 procedure New_Line
(Spacing
: Positive_Count
:= 1) is
924 New_Line
(Current_Out
, Spacing
);
931 procedure New_Page
(File
: File_Type
) is
933 FIO
.Check_Write_Status
(AP
(File
));
935 if File
.Col
/= 1 or else File
.Line
= 1 then
940 File
.Page
:= File
.Page
+ 1;
945 procedure New_Page
is
947 New_Page
(Current_Out
);
954 function Nextc
(File
: File_Type
) return int
is
958 ch
:= fgetc
(File
.Stream
);
961 if ferror
(File
.Stream
) /= 0 then
966 if ungetc
(ch
, File
.Stream
) = EOF
then
979 (File
: in out File_Type
;
984 Dummy_File_Control_Block
: Text_AFCB
;
985 pragma Warnings
(Off
, Dummy_File_Control_Block
);
986 -- Yes, we know this is never assigned a value, only the tag
987 -- is used for dispatching purposes, so that's expected.
990 FIO
.Open
(File_Ptr
=> AP
(File
),
991 Dummy_FCB
=> Dummy_File_Control_Block
,
992 Mode
=> To_FCB
(Mode
),
1006 -- Note: we assume that it is impossible in practice for the page
1007 -- to exceed the value of Count'Last, i.e. no check is required for
1008 -- overflow raising layout error.
1010 function Page
(File
: File_Type
) return Positive_Count
is
1012 FIO
.Check_File_Open
(AP
(File
));
1016 function Page
return Positive_Count
is
1018 return Page
(Current_Out
);
1025 function Page_Length
(File
: File_Type
) return Count
is
1027 FIO
.Check_Write_Status
(AP
(File
));
1028 return File
.Page_Length
;
1031 function Page_Length
return Count
is
1033 return Page_Length
(Current_Out
);
1045 FIO
.Check_Write_Status
(AP
(File
));
1047 if File
.Line_Length
/= 0 and then File
.Col
> File
.Line_Length
then
1051 if fputc
(Character'Pos (Item
), File
.Stream
) = EOF
then
1055 File
.Col
:= File
.Col
+ 1;
1058 procedure Put
(Item
: Character) is
1060 FIO
.Check_Write_Status
(AP
(Current_Out
));
1062 if Current_Out
.Line_Length
/= 0
1063 and then Current_Out
.Col
> Current_Out
.Line_Length
1065 New_Line
(Current_Out
);
1068 if fputc
(Character'Pos (Item
), Current_Out
.Stream
) = EOF
then
1072 Current_Out
.Col
:= Current_Out
.Col
+ 1;
1084 FIO
.Check_Write_Status
(AP
(File
));
1086 if Item
'Length > 0 then
1088 -- If we have bounded lines, then do things character by
1089 -- character (this seems a rare case anyway!)
1091 if File
.Line_Length
/= 0 then
1092 for J
in Item
'Range loop
1093 Put
(File
, Item
(J
));
1096 -- Otherwise we can output the entire string at once. Note that if
1097 -- there are LF or FF characters in the string, we do not bother to
1098 -- count them as line or page terminators.
1101 FIO
.Write_Buf
(AP
(File
), Item
'Address, Item
'Length);
1102 File
.Col
:= File
.Col
+ Item
'Length;
1107 procedure Put
(Item
: String) is
1109 Put
(Current_Out
, Item
);
1120 Ilen
: Natural := Item
'Length;
1121 Istart
: Natural := Item
'First;
1124 FIO
.Check_Write_Status
(AP
(File
));
1126 -- If we have bounded lines, then just do a put and a new line. In
1127 -- this case we will end up doing things character by character in
1128 -- any case, and it is a rare situation.
1130 if File
.Line_Length
/= 0 then
1136 -- We setup a single string that has the necessary terminators and
1137 -- then write it with a single call. The reason for doing this is
1138 -- that it gives better behavior for the use of Put_Line in multi-
1139 -- tasking programs, since often the OS will treat the entire put
1140 -- operation as an atomic operation.
1142 -- We only do this if the message is 512 characters or less in length,
1143 -- since otherwise Put_Line would use an unbounded amount of stack
1144 -- space and could cause undetected stack overflow. If we have a
1145 -- longer string, then output the first part separately to avoid this.
1148 FIO
.Write_Buf
(AP
(File
), Item
'Address, size_t
(Ilen
- 512));
1149 Istart
:= Istart
+ Ilen
- 512;
1153 -- Now prepare the string with its terminator
1156 Buffer
: String (1 .. Ilen
+ 2);
1160 Buffer
(1 .. Ilen
) := Item
(Istart
.. Item
'Last);
1161 Buffer
(Ilen
+ 1) := Character'Val (LM
);
1163 if File
.Page_Length
/= 0
1164 and then File
.Line
> File
.Page_Length
1166 Buffer
(Ilen
+ 2) := Character'Val (PM
);
1167 Plen
:= size_t
(Ilen
) + 2;
1169 File
.Page
:= File
.Page
+ 1;
1172 Plen
:= size_t
(Ilen
) + 1;
1173 File
.Line
:= File
.Line
+ 1;
1176 FIO
.Write_Buf
(AP
(File
), Buffer
'Address, Plen
);
1182 procedure Put_Line
(Item
: String) is
1184 Put_Line
(Current_Out
, Item
);
1191 procedure Putc
(ch
: int
; File
: File_Type
) is
1193 if fputc
(ch
, File
.Stream
) = EOF
then
1202 -- This is the primitive Stream Read routine, used when a Text_IO file
1203 -- is treated directly as a stream using Text_IO.Streams.Stream.
1206 (File
: in out Text_AFCB
;
1207 Item
: out Stream_Element_Array
;
1208 Last
: out Stream_Element_Offset
)
1211 pragma Warnings
(Off
, Discard_ch
);
1214 if File
.Mode
/= FCB
.In_File
then
1218 -- Deal with case where our logical and physical position do not match
1219 -- because of being after an LM or LM-PM sequence when in fact we are
1220 -- logically positioned before it.
1222 if File
.Before_LM
then
1224 -- If we are before a PM, then it is possible for a stream read
1225 -- to leave us after the LM and before the PM, which is a bit
1226 -- odd. The easiest way to deal with this is to unget the PM,
1227 -- so we are indeed positioned between the characters. This way
1228 -- further stream read operations will work correctly, and the
1229 -- effect on text processing is a little weird, but what can
1230 -- be expected if stream and text input are mixed this way?
1232 if File
.Before_LM_PM
then
1233 Discard_ch
:= ungetc
(PM
, File
.Stream
);
1234 File
.Before_LM_PM
:= False;
1237 File
.Before_LM
:= False;
1239 Item
(Item
'First) := Stream_Element
(Character'Pos (ASCII
.LF
));
1241 if Item
'Length = 1 then
1247 Stream_Element_Offset
1248 (fread
(buffer
=> Item
'Address,
1249 index
=> size_t
(Item
'First + 1),
1251 count
=> Item
'Length - 1,
1252 stream
=> File
.Stream
));
1258 -- Now we do the read. Since this is a text file, it is normally in
1259 -- text mode, but stream data must be read in binary mode, so we
1260 -- temporarily set binary mode for the read, resetting it after.
1261 -- These calls have no effect in a system (like Unix) where there is
1262 -- no distinction between text and binary files.
1264 set_binary_mode
(fileno
(File
.Stream
));
1268 Stream_Element_Offset
1269 (fread
(Item
'Address, 1, Item
'Length, File
.Stream
)) - 1;
1271 if Last
< Item
'Last then
1272 if ferror
(File
.Stream
) /= 0 then
1277 set_text_mode
(fileno
(File
.Stream
));
1285 (File
: in out File_Type
;
1289 -- Don't allow change of mode for current file (RM A.10.2(5))
1291 if (File
= Current_In
or else
1292 File
= Current_Out
or else
1293 File
= Current_Error
)
1294 and then To_FCB
(Mode
) /= File
.Mode
1299 Terminate_Line
(File
);
1300 FIO
.Reset
(AP
(File
), To_FCB
(Mode
));
1304 File
.Line_Length
:= 0;
1305 File
.Page_Length
:= 0;
1306 File
.Before_LM
:= False;
1307 File
.Before_LM_PM
:= False;
1310 procedure Reset
(File
: in out File_Type
) is
1312 Terminate_Line
(File
);
1313 FIO
.Reset
(AP
(File
));
1317 File
.Line_Length
:= 0;
1318 File
.Page_Length
:= 0;
1319 File
.Before_LM
:= False;
1320 File
.Before_LM_PM
:= False;
1329 To
: Positive_Count
)
1334 -- Raise Constraint_Error if out of range value. The reason for this
1335 -- explicit test is that we don't want junk values around, even if
1336 -- checks are off in the caller.
1338 if not To
'Valid then
1339 raise Constraint_Error
;
1342 FIO
.Check_File_Open
(AP
(File
));
1346 if Mode
(File
) >= Out_File
then
1348 -- Error if we attempt to set Col to a value greater than the
1349 -- maximum permissible line length.
1351 if File
.Line_Length
/= 0 and then To
> File
.Line_Length
then
1355 -- If we are behind current position, then go to start of new line
1357 if To
< File
.Col
then
1361 -- Loop to output blanks till we are at the required column
1363 while File
.Col
< To
loop
1370 -- If we are logically before a LM, but physically after it, the
1371 -- file position still reflects the position before the LM, so eat
1372 -- it now and adjust the file position appropriately.
1374 if File
.Before_LM
then
1375 File
.Before_LM
:= False;
1376 File
.Before_LM_PM
:= False;
1377 File
.Line
:= File
.Line
+ 1;
1381 -- Loop reading characters till we get one at the required Col value
1384 -- Read next character. The reason we have to read ahead is to
1385 -- skip formatting characters, the effect of Set_Col is to set
1386 -- us to a real character with the right Col value, and format
1387 -- characters don't count.
1391 -- Error if we hit an end of file
1396 -- If line mark, eat it and adjust file position
1399 File
.Line
:= File
.Line
+ 1;
1402 -- If recognized page mark, eat it, and adjust file position
1404 elsif ch
= PM
and then File
.Is_Regular_File
then
1405 File
.Page
:= File
.Page
+ 1;
1409 -- Otherwise this is the character we are looking for, so put it
1410 -- back in the input stream (we have not adjusted the file
1411 -- position yet, so everything is set right after this ungetc).
1413 elsif To
= File
.Col
then
1417 -- Keep skipping characters if we are not there yet, updating the
1418 -- file position past the skipped character.
1421 File
.Col
:= File
.Col
+ 1;
1427 procedure Set_Col
(To
: Positive_Count
) is
1429 Set_Col
(Current_Out
, To
);
1436 procedure Set_Error
(File
: File_Type
) is
1438 FIO
.Check_Write_Status
(AP
(File
));
1439 Current_Err
:= File
;
1446 procedure Set_Input
(File
: File_Type
) is
1448 FIO
.Check_Read_Status
(AP
(File
));
1458 To
: Positive_Count
)
1461 -- Raise Constraint_Error if out of range value. The reason for this
1462 -- explicit test is that we don't want junk values around, even if
1463 -- checks are off in the caller.
1465 if not To
'Valid then
1466 raise Constraint_Error
;
1469 FIO
.Check_File_Open
(AP
(File
));
1471 if To
= File
.Line
then
1475 if Mode
(File
) >= Out_File
then
1476 if File
.Page_Length
/= 0 and then To
> File
.Page_Length
then
1480 if To
< File
.Line
then
1484 while File
.Line
< To
loop
1489 while To
/= File
.Line
loop
1495 procedure Set_Line
(To
: Positive_Count
) is
1497 Set_Line
(Current_Out
, To
);
1500 ---------------------
1501 -- Set_Line_Length --
1502 ---------------------
1504 procedure Set_Line_Length
(File
: File_Type
; To
: Count
) is
1506 -- Raise Constraint_Error if out of range value. The reason for this
1507 -- explicit test is that we don't want junk values around, even if
1508 -- checks are off in the caller.
1510 if not To
'Valid then
1511 raise Constraint_Error
;
1514 FIO
.Check_Write_Status
(AP
(File
));
1515 File
.Line_Length
:= To
;
1516 end Set_Line_Length
;
1518 procedure Set_Line_Length
(To
: Count
) is
1520 Set_Line_Length
(Current_Out
, To
);
1521 end Set_Line_Length
;
1527 procedure Set_Output
(File
: File_Type
) is
1529 FIO
.Check_Write_Status
(AP
(File
));
1530 Current_Out
:= File
;
1533 ---------------------
1534 -- Set_Page_Length --
1535 ---------------------
1537 procedure Set_Page_Length
(File
: File_Type
; To
: Count
) is
1539 -- Raise Constraint_Error if out of range value. The reason for this
1540 -- explicit test is that we don't want junk values around, even if
1541 -- checks are off in the caller.
1543 if not To
'Valid then
1544 raise Constraint_Error
;
1547 FIO
.Check_Write_Status
(AP
(File
));
1548 File
.Page_Length
:= To
;
1549 end Set_Page_Length
;
1551 procedure Set_Page_Length
(To
: Count
) is
1553 Set_Page_Length
(Current_Out
, To
);
1554 end Set_Page_Length
;
1562 Spacing
: Positive_Count
:= 1)
1567 -- Raise Constraint_Error if out of range value. The reason for this
1568 -- explicit test is that we don't want junk values around, even if
1569 -- checks are off in the caller.
1571 if not Spacing
'Valid then
1572 raise Constraint_Error
;
1575 FIO
.Check_Read_Status
(AP
(File
));
1577 for L
in 1 .. Spacing
loop
1578 if File
.Before_LM
then
1579 File
.Before_LM
:= False;
1580 File
.Before_LM_PM
:= False;
1585 -- If at end of file now, then immediately raise End_Error. Note
1586 -- that we can never be positioned between a line mark and a page
1587 -- mark, so if we are at the end of file, we cannot logically be
1588 -- before the implicit page mark that is at the end of the file.
1590 -- For the same reason, we do not need an explicit check for a
1591 -- page mark. If there is a FF in the middle of a line, the file
1592 -- is not in canonical format and we do not care about the page
1593 -- numbers for files other than ones in canonical format.
1599 -- If not at end of file, then loop till we get to an LM or EOF.
1600 -- The latter case happens only in non-canonical files where the
1601 -- last line is not terminated by LM, but we don't want to blow
1602 -- up for such files, so we assume an implicit LM in this case.
1605 exit when ch
= LM
or ch
= EOF
;
1610 -- We have got past a line mark, now, for a regular file only,
1611 -- see if a page mark immediately follows this line mark and
1612 -- if so, skip past the page mark as well. We do not do this
1613 -- for non-regular files, since it would cause an undesirable
1614 -- wait for an additional character.
1617 File
.Line
:= File
.Line
+ 1;
1619 if File
.Before_LM_PM
then
1620 File
.Page
:= File
.Page
+ 1;
1622 File
.Before_LM_PM
:= False;
1624 elsif File
.Is_Regular_File
then
1627 -- Page mark can be explicit, or implied at the end of the file
1629 if (ch
= PM
or else ch
= EOF
)
1630 and then File
.Is_Regular_File
1632 File
.Page
:= File
.Page
+ 1;
1642 procedure Skip_Line
(Spacing
: Positive_Count
:= 1) is
1644 Skip_Line
(Current_In
, Spacing
);
1651 procedure Skip_Page
(File
: File_Type
) is
1655 FIO
.Check_Read_Status
(AP
(File
));
1657 -- If at page mark already, just skip it
1659 if File
.Before_LM_PM
then
1660 File
.Before_LM
:= False;
1661 File
.Before_LM_PM
:= False;
1662 File
.Page
:= File
.Page
+ 1;
1668 -- This is a bit tricky, if we are logically before an LM then
1669 -- it is not an error if we are at an end of file now, since we
1670 -- are not really at it.
1672 if File
.Before_LM
then
1673 File
.Before_LM
:= False;
1674 File
.Before_LM_PM
:= False;
1677 -- Otherwise we do raise End_Error if we are at the end of file now
1687 -- Now we can just rumble along to the next page mark, or to the
1688 -- end of file, if that comes first. The latter case happens when
1689 -- the page mark is implied at the end of file.
1693 or else (ch
= PM
and then File
.Is_Regular_File
);
1697 File
.Page
:= File
.Page
+ 1;
1702 procedure Skip_Page
is
1704 Skip_Page
(Current_In
);
1707 --------------------
1708 -- Standard_Error --
1709 --------------------
1711 function Standard_Error
return File_Type
is
1713 return Standard_Err
;
1716 function Standard_Error
return File_Access
is
1718 return Standard_Err
'Access;
1721 --------------------
1722 -- Standard_Input --
1723 --------------------
1725 function Standard_Input
return File_Type
is
1730 function Standard_Input
return File_Access
is
1732 return Standard_In
'Access;
1735 ---------------------
1736 -- Standard_Output --
1737 ---------------------
1739 function Standard_Output
return File_Type
is
1741 return Standard_Out
;
1742 end Standard_Output
;
1744 function Standard_Output
return File_Access
is
1746 return Standard_Out
'Access;
1747 end Standard_Output
;
1749 --------------------
1750 -- Terminate_Line --
1751 --------------------
1753 procedure Terminate_Line
(File
: File_Type
) is
1755 FIO
.Check_File_Open
(AP
(File
));
1757 -- For file other than In_File, test for needing to terminate last line
1759 if Mode
(File
) /= In_File
then
1761 -- If not at start of line definition need new line
1763 if File
.Col
/= 1 then
1766 -- For files other than standard error and standard output, we
1767 -- make sure that an empty file has a single line feed, so that
1768 -- it is properly formatted. We avoid this for the standard files
1769 -- because it is too much of a nuisance to have these odd line
1770 -- feeds when nothing has been written to the file.
1772 -- We also avoid this for files opened in append mode, in
1773 -- accordance with (RM A.8.2(10))
1775 elsif (File
/= Standard_Err
and then File
/= Standard_Out
)
1776 and then (File
.Line
= 1 and then File
.Page
= 1)
1777 and then Mode
(File
) = Out_File
1788 procedure Ungetc
(ch
: int
; File
: File_Type
) is
1791 if ungetc
(ch
, File
.Stream
) = EOF
then
1801 -- This is the primitive Stream Write routine, used when a Text_IO file
1802 -- is treated directly as a stream using Text_IO.Streams.Stream.
1805 (File
: in out Text_AFCB
;
1806 Item
: Stream_Element_Array
)
1809 function Has_Translated_Characters
return Boolean;
1810 -- return True if Item array contains a character which will be
1811 -- translated under the text file mode. There is only one such
1812 -- character under DOS based systems which is character 10.
1814 text_translation_required
: Boolean;
1815 pragma Import
(C
, text_translation_required
,
1816 "__gnat_text_translation_required");
1818 Siz
: constant size_t
:= Item
'Length;
1820 function Has_Translated_Characters
return Boolean is
1822 for K
in Item
'Range loop
1823 if Item
(K
) = 10 then
1828 end Has_Translated_Characters
;
1830 Needs_Binary_Write
: constant Boolean :=
1831 text_translation_required
and then Has_Translated_Characters
;
1834 if File
.Mode
= FCB
.In_File
then
1838 -- Now we do the write. Since this is a text file, it is normally in
1839 -- text mode, but stream data must be written in binary mode, so we
1840 -- temporarily set binary mode for the write, resetting it after. This
1841 -- is done only if needed (i.e. there is some characters in Item which
1842 -- needs to be written using the binary mode).
1843 -- These calls have no effect in a system (like Unix) where there is
1844 -- no distinction between text and binary files.
1846 -- Since the character translation is done at the time the buffer is
1847 -- written (this is true under Windows) we first flush current buffer
1848 -- with text mode if needed.
1850 if Needs_Binary_Write
then
1852 if fflush
(File
.Stream
) = -1 then
1856 set_binary_mode
(fileno
(File
.Stream
));
1859 if fwrite
(Item
'Address, 1, Siz
, File
.Stream
) /= Siz
then
1863 -- At this point we need to flush the buffer using the binary mode then
1864 -- we reset to text mode.
1866 if Needs_Binary_Write
then
1868 if fflush
(File
.Stream
) = -1 then
1872 set_text_mode
(fileno
(File
.Stream
));
1876 -- Use "preallocated" strings to avoid calling "new" during the
1877 -- elaboration of the run time. This is needed in the tasking case to
1878 -- avoid calling Task_Lock too early. A filename is expected to end with a
1879 -- null character in the runtime, here the null characters are added just
1880 -- to have a correct filename length.
1882 Err_Name
: aliased String := "*stderr" & ASCII
.Nul
;
1883 In_Name
: aliased String := "*stdin" & ASCII
.Nul
;
1884 Out_Name
: aliased String := "*stdout" & ASCII
.Nul
;
1886 -------------------------------
1887 -- Initialize Standard Files --
1888 -------------------------------
1890 -- Note: the names in these files are bogus, and probably it would be
1891 -- better for these files to have no names, but the ACVC test insist!
1892 -- We use names that are bound to fail in open etc.
1894 Standard_Err
.Stream
:= stderr
;
1895 Standard_Err
.Name
:= Err_Name
'Access;
1896 Standard_Err
.Form
:= Null_Str
'Unrestricted_Access;
1897 Standard_Err
.Mode
:= FCB
.Out_File
;
1898 Standard_Err
.Is_Regular_File
:= is_regular_file
(fileno
(stderr
)) /= 0;
1899 Standard_Err
.Is_Temporary_File
:= False;
1900 Standard_Err
.Is_System_File
:= True;
1901 Standard_Err
.Is_Text_File
:= True;
1902 Standard_Err
.Access_Method
:= 'T';
1903 Standard_Err
.Self
:= Standard_Err
;
1905 Standard_In
.Stream
:= stdin
;
1906 Standard_In
.Name
:= In_Name
'Access;
1907 Standard_In
.Form
:= Null_Str
'Unrestricted_Access;
1908 Standard_In
.Mode
:= FCB
.In_File
;
1909 Standard_In
.Is_Regular_File
:= is_regular_file
(fileno
(stdin
)) /= 0;
1910 Standard_In
.Is_Temporary_File
:= False;
1911 Standard_In
.Is_System_File
:= True;
1912 Standard_In
.Is_Text_File
:= True;
1913 Standard_In
.Access_Method
:= 'T';
1914 Standard_In
.Self
:= Standard_In
;
1916 Standard_Out
.Stream
:= stdout
;
1917 Standard_Out
.Name
:= Out_Name
'Access;
1918 Standard_Out
.Form
:= Null_Str
'Unrestricted_Access;
1919 Standard_Out
.Mode
:= FCB
.Out_File
;
1920 Standard_Out
.Is_Regular_File
:= is_regular_file
(fileno
(stdout
)) /= 0;
1921 Standard_Out
.Is_Temporary_File
:= False;
1922 Standard_Out
.Is_System_File
:= True;
1923 Standard_Out
.Is_Text_File
:= True;
1924 Standard_Out
.Access_Method
:= 'T';
1925 Standard_Out
.Self
:= Standard_Out
;
1927 FIO
.Chain_File
(AP
(Standard_In
));
1928 FIO
.Chain_File
(AP
(Standard_Out
));
1929 FIO
.Chain_File
(AP
(Standard_Err
));
1931 FIO
.Make_Unbuffered
(AP
(Standard_Out
));
1932 FIO
.Make_Unbuffered
(AP
(Standard_Err
));