1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . T E X T _ I O --
9 -- Copyright (C) 1992-2008, 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
;
39 with System
.WCh_Cnv
; use System
.WCh_Cnv
;
40 with System
.WCh_Con
; use System
.WCh_Con
;
42 with Ada
.Unchecked_Conversion
;
43 with Ada
.Unchecked_Deallocation
;
45 pragma Elaborate_All
(System
.File_IO
);
46 -- Needed because of calls to Chain_File in package body elaboration
48 package body Ada
.Text_IO
is
50 package FIO
renames System
.File_IO
;
52 subtype AP
is FCB
.AFCB_Ptr
;
54 function To_FCB
is new Ada
.Unchecked_Conversion
(File_Mode
, FCB
.File_Mode
);
55 function To_TIO
is new Ada
.Unchecked_Conversion
(FCB
.File_Mode
, File_Mode
);
56 use type FCB
.File_Mode
;
58 use type System
.CRTL
.size_t
;
60 WC_Encoding
: Character;
61 pragma Import
(C
, WC_Encoding
, "__gl_wc_encoding");
63 -----------------------
64 -- Local Subprograms --
65 -----------------------
67 function Getc_Immed
(File
: File_Type
) return int
;
68 -- This routine is identical to Getc, except that the read is done in
69 -- Get_Immediate mode (i.e. without waiting for a line return).
71 function Get_Upper_Half_Char
73 File
: File_Type
) return Character;
74 -- This function is shared by Get and Get_Immediate to extract an encoded
75 -- upper half character value from the given File. The first byte has
76 -- already been read and is passed in C. The character value is returned as
77 -- the result, and the file pointer is bumped past the character.
78 -- Constraint_Error is raised if the encoded value is outside the bounds of
81 function Get_Upper_Half_Char_Immed
83 File
: File_Type
) return Character;
84 -- This routine is identical to Get_Upper_Half_Char, except that the reads
85 -- are done in Get_Immediate mode (i.e. without waiting for a line return).
87 function Has_Upper_Half_Character
(Item
: String) return Boolean;
88 -- Returns True if any of the characters is in the range 16#80#-16#FF#
90 procedure Put_Encoded
(File
: File_Type
; Char
: Character);
91 -- Called to output a character Char to the given File, when the encoding
92 -- method for the file is other than brackets, and Char is upper half.
94 procedure Set_WCEM
(File
: in out File_Type
);
95 -- Called by Open and Create to set the wide character encoding method for
96 -- the file, processing a WCEM form parameter if one is present. File is
97 -- IN OUT because it may be closed in case of an error.
103 function AFCB_Allocate
(Control_Block
: Text_AFCB
) return FCB
.AFCB_Ptr
is
104 pragma Unreferenced
(Control_Block
);
106 return new Text_AFCB
;
113 procedure AFCB_Close
(File
: not null access Text_AFCB
) is
115 -- If the file being closed is one of the current files, then close
116 -- the corresponding current file. It is not clear that this action
117 -- is required (RM A.10.3(23)) but it seems reasonable, and besides
118 -- ACVC test CE3208A expects this behavior.
120 if File_Type
(File
) = Current_In
then
122 elsif File_Type
(File
) = Current_Out
then
124 elsif File_Type
(File
) = Current_Err
then
128 Terminate_Line
(File_Type
(File
));
135 procedure AFCB_Free
(File
: not null access Text_AFCB
) is
136 type FCB_Ptr
is access all Text_AFCB
;
137 FT
: FCB_Ptr
:= FCB_Ptr
(File
);
139 procedure Free
is new Ada
.Unchecked_Deallocation
(Text_AFCB
, FCB_Ptr
);
149 procedure Close
(File
: in out File_Type
) is
151 FIO
.Close
(AP
(File
)'Unrestricted_Access);
158 -- Note: we assume that it is impossible in practice for the column
159 -- to exceed the value of Count'Last, i.e. no check is required for
160 -- overflow raising layout error.
162 function Col
(File
: File_Type
) return Positive_Count
is
164 FIO
.Check_File_Open
(AP
(File
));
168 function Col
return Positive_Count
is
170 return Col
(Current_Out
);
178 (File
: in out File_Type
;
179 Mode
: File_Mode
:= Out_File
;
183 Dummy_File_Control_Block
: Text_AFCB
;
184 pragma Warnings
(Off
, Dummy_File_Control_Block
);
185 -- Yes, we know this is never assigned a value, only the tag
186 -- is used for dispatching purposes, so that's expected.
189 FIO
.Open
(File_Ptr
=> AP
(File
),
190 Dummy_FCB
=> Dummy_File_Control_Block
,
191 Mode
=> To_FCB
(Mode
),
206 function Current_Error
return File_Type
is
211 function Current_Error
return File_Access
is
213 return Current_Err
.Self
'Access;
220 function Current_Input
return File_Type
is
225 function Current_Input
return File_Access
is
227 return Current_In
.Self
'Access;
234 function Current_Output
return File_Type
is
239 function Current_Output
return File_Access
is
241 return Current_Out
.Self
'Access;
248 procedure Delete
(File
: in out File_Type
) is
250 FIO
.Delete
(AP
(File
)'Unrestricted_Access);
257 function End_Of_File
(File
: File_Type
) return Boolean is
261 FIO
.Check_Read_Status
(AP
(File
));
263 if File
.Before_Upper_Half_Character
then
266 elsif File
.Before_LM
then
267 if File
.Before_LM_PM
then
268 return Nextc
(File
) = EOF
;
282 File
.Before_LM
:= True;
286 -- Here we are just past the line mark with Before_LM set so that we
287 -- do not have to try to back up past the LM, thus avoiding the need
288 -- to back up more than one character.
295 elsif ch
= PM
and then File
.Is_Regular_File
then
296 File
.Before_LM_PM
:= True;
297 return Nextc
(File
) = EOF
;
299 -- Here if neither EOF nor PM followed end of line
308 function End_Of_File
return Boolean is
310 return End_Of_File
(Current_In
);
317 function End_Of_Line
(File
: File_Type
) return Boolean is
321 FIO
.Check_Read_Status
(AP
(File
));
323 if File
.Before_Upper_Half_Character
then
326 elsif File
.Before_LM
then
342 function End_Of_Line
return Boolean is
344 return End_Of_Line
(Current_In
);
351 function End_Of_Page
(File
: File_Type
) return Boolean is
355 FIO
.Check_Read_Status
(AP
(File
));
357 if not File
.Is_Regular_File
then
360 elsif File
.Before_Upper_Half_Character
then
363 elsif File
.Before_LM
then
364 if File
.Before_LM_PM
then
379 File
.Before_LM
:= True;
383 -- Here we are just past the line mark with Before_LM set so that we
384 -- do not have to try to back up past the LM, thus avoiding the need
385 -- to back up more than one character.
389 return ch
= PM
or else ch
= EOF
;
392 function End_Of_Page
return Boolean is
394 return End_Of_Page
(Current_In
);
401 function EOF_Char
return Integer is
410 procedure Flush
(File
: File_Type
) is
412 FIO
.Flush
(AP
(File
));
424 function Form
(File
: File_Type
) return String is
426 return FIO
.Form
(AP
(File
));
435 Item
: out Character)
440 FIO
.Check_Read_Status
(AP
(File
));
442 if File
.Before_Upper_Half_Character
then
443 File
.Before_Upper_Half_Character
:= False;
444 Item
:= File
.Saved_Upper_Half_Character
;
446 elsif File
.Before_LM
then
447 File
.Before_LM
:= False;
450 if File
.Before_LM_PM
then
452 File
.Page
:= File
.Page
+ 1;
453 File
.Before_LM_PM
:= False;
455 File
.Line
:= File
.Line
+ 1;
466 File
.Line
:= File
.Line
+ 1;
469 elsif ch
= PM
and then File
.Is_Regular_File
then
470 File
.Page
:= File
.Page
+ 1;
474 Item
:= Character'Val (ch
);
475 File
.Col
:= File
.Col
+ 1;
481 procedure Get
(Item
: out Character) is
483 Get
(Current_In
, Item
);
494 FIO
.Check_Read_Status
(AP
(File
));
496 if File
.Before_LM
then
497 File
.Before_LM
:= False;
498 File
.Before_LM_PM
:= False;
501 if File
.Before_LM_PM
then
503 File
.Page
:= File
.Page
+ 1;
504 File
.Before_LM_PM
:= False;
507 File
.Line
:= File
.Line
+ 1;
512 while J
<= Item
'Last loop
519 File
.Line
:= File
.Line
+ 1;
522 elsif ch
= PM
and then File
.Is_Regular_File
then
523 File
.Page
:= File
.Page
+ 1;
527 Item
(J
) := Character'Val (ch
);
529 File
.Col
:= File
.Col
+ 1;
534 procedure Get
(Item
: out String) is
536 Get
(Current_In
, Item
);
543 procedure Get_Immediate
545 Item
: out Character)
550 FIO
.Check_Read_Status
(AP
(File
));
552 if File
.Before_Upper_Half_Character
then
553 File
.Before_Upper_Half_Character
:= False;
554 Item
:= File
.Saved_Upper_Half_Character
;
556 elsif File
.Before_LM
then
557 File
.Before_LM
:= False;
558 File
.Before_LM_PM
:= False;
559 Item
:= Character'Val (LM
);
562 ch
:= Getc_Immed
(File
);
567 if not Is_Start_Of_Encoding
568 (Character'Val (ch
), File
.WC_Method
)
570 Item
:= Character'Val (ch
);
572 Item
:= Get_Upper_Half_Char_Immed
(Character'Val (ch
), File
);
578 procedure Get_Immediate
579 (Item
: out Character)
582 Get_Immediate
(Current_In
, Item
);
585 procedure Get_Immediate
587 Item
: out Character;
588 Available
: out Boolean)
594 procedure getc_immediate_nowait
597 end_of_file
: out int
;
599 pragma Import
(C
, getc_immediate_nowait
, "getc_immediate_nowait");
602 FIO
.Check_Read_Status
(AP
(File
));
605 if File
.Before_Upper_Half_Character
then
606 File
.Before_Upper_Half_Character
:= False;
607 Item
:= File
.Saved_Upper_Half_Character
;
609 elsif File
.Before_LM
then
610 File
.Before_LM
:= False;
611 File
.Before_LM_PM
:= False;
612 Item
:= Character'Val (LM
);
615 getc_immediate_nowait
(File
.Stream
, ch
, end_of_file
, avail
);
617 if ferror
(File
.Stream
) /= 0 then
620 elsif end_of_file
/= 0 then
630 if Is_Start_Of_Encoding
631 (Character'Val (ch
), File
.WC_Method
)
633 Item
:= Character'Val (ch
);
635 Item
:= Get_Upper_Half_Char_Immed
(Character'Val (ch
), File
);
642 procedure Get_Immediate
643 (Item
: out Character;
644 Available
: out Boolean)
647 Get_Immediate
(Current_In
, Item
, Available
);
662 FIO
.Check_Read_Status
(AP
(File
));
663 Last
:= Item
'First - 1;
665 -- Immediate exit for null string, this is a case in which we do not
666 -- need to test for end of file and we do not skip a line mark under
667 -- any circumstances.
669 if Last
>= Item
'Last then
673 -- Here we have at least one character, if we are immediately before
674 -- a line mark, then we will just skip past it storing no characters.
676 if File
.Before_LM
then
677 File
.Before_LM
:= False;
678 File
.Before_LM_PM
:= False;
680 -- Otherwise we need to read some characters
685 -- If we are at the end of file now, it means we are trying to
686 -- skip a file terminator and we raise End_Error (RM A.10.7(20))
692 -- Loop through characters. Don't bother if we hit a page mark,
693 -- since in normal files, page marks can only follow line marks
694 -- in any case and we only promise to treat the page nonsense
695 -- correctly in the absense of such rogue page marks.
698 -- Exit the loop if read is terminated by encountering line mark
702 -- Otherwise store the character, note that we know that ch is
703 -- something other than LM or EOF. It could possibly be a page
704 -- mark if there is a stray page mark in the middle of a line,
705 -- but this is not an official page mark in any case, since
706 -- official page marks can only follow a line mark. The whole
707 -- page business is pretty much nonsense anyway, so we do not
708 -- want to waste time trying to make sense out of non-standard
709 -- page marks in the file! This means that the behavior of
710 -- Get_Line is different from repeated Get of a character, but
711 -- that's too bad. We only promise that page numbers etc make
712 -- sense if the file is formatted in a standard manner.
714 -- Note: we do not adjust the column number because it is quicker
715 -- to adjust it once at the end of the operation than incrementing
716 -- it each time around the loop.
719 Item
(Last
) := Character'Val (ch
);
721 -- All done if the string is full, this is the case in which
722 -- we do not skip the following line mark. We need to adjust
723 -- the column number in this case.
725 if Last
= Item
'Last then
726 File
.Col
:= File
.Col
+ Count
(Item
'Length);
730 -- Otherwise read next character. We also exit from the loop if
731 -- we read an end of file. This is the case where the last line
732 -- is not terminated with a line mark, and we consider that there
733 -- is an implied line mark in this case (this is a non-standard
734 -- file, but it is nice to treat it reasonably).
741 -- We have skipped past, but not stored, a line mark. Skip following
742 -- page mark if one follows, but do not do this for a non-regular
743 -- file (since otherwise we get annoying wait for an extra character)
745 File
.Line
:= File
.Line
+ 1;
748 if File
.Before_LM_PM
then
750 File
.Before_LM_PM
:= False;
751 File
.Page
:= File
.Page
+ 1;
753 elsif File
.Is_Regular_File
then
756 if ch
= PM
and then File
.Is_Regular_File
then
758 File
.Page
:= File
.Page
+ 1;
770 Get_Line
(Current_In
, Item
, Last
);
773 function Get_Line
(File
: File_Type
) return String is
774 Buffer
: String (1 .. 500);
777 function Get_Rest
(S
: String) return String;
778 -- This is a recursive function that reads the rest of the line and
779 -- returns it. S is the part read so far.
785 function Get_Rest
(S
: String) return String is
787 -- Each time we allocate a buffer the same size as what we have
788 -- read so far. This limits us to a logarithmic number of calls
789 -- to Get_Rest and also ensures only a linear use of stack space.
791 Buffer
: String (1 .. S
'Length);
795 Get_Line
(File
, Buffer
, Last
);
798 R
: constant String := S
& Buffer
(1 .. Last
);
800 if Last
< Buffer
'Last then
808 -- Start of processing for Get_Line
811 Get_Line
(File
, Buffer
, Last
);
813 if Last
< Buffer
'Last then
814 return Buffer
(1 .. Last
);
816 return Get_Rest
(Buffer
(1 .. Last
));
820 function Get_Line
return String is
822 return Get_Line
(Current_In
);
825 -------------------------
826 -- Get_Upper_Half_Char --
827 -------------------------
829 function Get_Upper_Half_Char
831 File
: File_Type
) return Character
833 Result
: Wide_Character;
835 function In_Char
return Character;
836 -- Function used to obtain additional characters it the wide character
837 -- sequence is more than one character long.
839 function WC_In
is new Char_Sequence_To_Wide_Char
(In_Char
);
845 function In_Char
return Character is
846 ch
: constant Integer := Getc
(File
);
851 return Character'Val (ch
);
855 -- Start of processing for Get_Upper_Half_Char
858 Result
:= WC_In
(C
, File
.WC_Method
);
860 if Wide_Character'Pos (Result
) > 16#FF#
then
861 raise Constraint_Error
with
862 "invalid wide character in Text_'I'O input";
864 return Character'Val (Wide_Character'Pos (Result
));
866 end Get_Upper_Half_Char
;
868 -------------------------------
869 -- Get_Upper_Half_Char_Immed --
870 -------------------------------
872 function Get_Upper_Half_Char_Immed
874 File
: File_Type
) return Character
876 Result
: Wide_Character;
878 function In_Char
return Character;
879 -- Function used to obtain additional characters it the wide character
880 -- sequence is more than one character long.
882 function WC_In
is new Char_Sequence_To_Wide_Char
(In_Char
);
888 function In_Char
return Character is
889 ch
: constant Integer := Getc_Immed
(File
);
894 return Character'Val (ch
);
898 -- Start of processing for Get_Upper_Half_Char_Immed
901 Result
:= WC_In
(C
, File
.WC_Method
);
903 if Wide_Character'Pos (Result
) > 16#FF#
then
904 raise Constraint_Error
with
905 "invalid wide character in Text_'I'O input";
907 return Character'Val (Wide_Character'Pos (Result
));
909 end Get_Upper_Half_Char_Immed
;
915 function Getc
(File
: File_Type
) return int
is
919 ch
:= fgetc
(File
.Stream
);
921 if ch
= EOF
and then ferror
(File
.Stream
) /= 0 then
932 function Getc_Immed
(File
: File_Type
) return int
is
936 procedure getc_immediate
937 (stream
: FILEs
; ch
: out int
; end_of_file
: out int
);
938 pragma Import
(C
, getc_immediate
, "getc_immediate");
941 FIO
.Check_Read_Status
(AP
(File
));
943 if File
.Before_LM
then
944 File
.Before_LM
:= False;
945 File
.Before_LM_PM
:= False;
949 getc_immediate
(File
.Stream
, ch
, end_of_file
);
951 if ferror
(File
.Stream
) /= 0 then
953 elsif end_of_file
/= 0 then
961 ------------------------------
962 -- Has_Upper_Half_Character --
963 ------------------------------
965 function Has_Upper_Half_Character
(Item
: String) return Boolean is
967 for J
in Item
'Range loop
968 if Character'Pos (Item
(J
)) >= 16#
80#
then
974 end Has_Upper_Half_Character
;
980 function Is_Open
(File
: File_Type
) return Boolean is
982 return FIO
.Is_Open
(AP
(File
));
989 -- Note: we assume that it is impossible in practice for the line
990 -- to exceed the value of Count'Last, i.e. no check is required for
991 -- overflow raising layout error.
993 function Line
(File
: File_Type
) return Positive_Count
is
995 FIO
.Check_File_Open
(AP
(File
));
999 function Line
return Positive_Count
is
1001 return Line
(Current_Out
);
1008 function Line_Length
(File
: File_Type
) return Count
is
1010 FIO
.Check_Write_Status
(AP
(File
));
1011 return File
.Line_Length
;
1014 function Line_Length
return Count
is
1016 return Line_Length
(Current_Out
);
1023 procedure Look_Ahead
1025 Item
: out Character;
1026 End_Of_Line
: out Boolean)
1031 FIO
.Check_Read_Status
(AP
(File
));
1033 -- If we are logically before a line mark, we can return immediately
1035 if File
.Before_LM
then
1036 End_Of_Line
:= True;
1039 -- If we are before an upper half character just return it (this can
1040 -- happen if there are two calls to Look_Ahead in a row).
1042 elsif File
.Before_Upper_Half_Character
then
1043 End_Of_Line
:= False;
1044 Item
:= File
.Saved_Upper_Half_Character
;
1046 -- Otherwise we must read a character from the input stream
1053 or else (ch
= PM
and then File
.Is_Regular_File
)
1055 End_Of_Line
:= True;
1059 -- Case where character obtained does not represent the start of an
1060 -- encoded sequence so it stands for itself and we can unget it with
1063 elsif not Is_Start_Of_Encoding
1064 (Character'Val (ch
), File
.WC_Method
)
1066 End_Of_Line
:= False;
1068 Item
:= Character'Val (ch
);
1070 -- For the start of an encoding, we read the character using the
1071 -- Get_Upper_Half_Char routine. It will occupy more than one byte
1072 -- so we can't put it back with ungetc. Instead we save it in the
1073 -- control block, setting a flag that everyone interested in reading
1074 -- characters must test before reading the stream.
1077 Item
:= Get_Upper_Half_Char
(Character'Val (ch
), File
);
1078 End_Of_Line
:= False;
1079 File
.Saved_Upper_Half_Character
:= Item
;
1080 File
.Before_Upper_Half_Character
:= True;
1085 procedure Look_Ahead
1086 (Item
: out Character;
1087 End_Of_Line
: out Boolean)
1090 Look_Ahead
(Current_In
, Item
, End_Of_Line
);
1097 function Mode
(File
: File_Type
) return File_Mode
is
1099 return To_TIO
(FIO
.Mode
(AP
(File
)));
1106 function Name
(File
: File_Type
) return String is
1108 return FIO
.Name
(AP
(File
));
1117 Spacing
: Positive_Count
:= 1)
1120 -- Raise Constraint_Error if out of range value. The reason for this
1121 -- explicit test is that we don't want junk values around, even if
1122 -- checks are off in the caller.
1124 if not Spacing
'Valid then
1125 raise Constraint_Error
;
1128 FIO
.Check_Write_Status
(AP
(File
));
1130 for K
in 1 .. Spacing
loop
1132 File
.Line
:= File
.Line
+ 1;
1134 if File
.Page_Length
/= 0
1135 and then File
.Line
> File
.Page_Length
1139 File
.Page
:= File
.Page
+ 1;
1146 procedure New_Line
(Spacing
: Positive_Count
:= 1) is
1148 New_Line
(Current_Out
, Spacing
);
1155 procedure New_Page
(File
: File_Type
) is
1157 FIO
.Check_Write_Status
(AP
(File
));
1159 if File
.Col
/= 1 or else File
.Line
= 1 then
1164 File
.Page
:= File
.Page
+ 1;
1169 procedure New_Page
is
1171 New_Page
(Current_Out
);
1178 function Nextc
(File
: File_Type
) return int
is
1182 ch
:= fgetc
(File
.Stream
);
1185 if ferror
(File
.Stream
) /= 0 then
1190 if ungetc
(ch
, File
.Stream
) = EOF
then
1203 (File
: in out File_Type
;
1206 Form
: String := "")
1208 Dummy_File_Control_Block
: Text_AFCB
;
1209 pragma Warnings
(Off
, Dummy_File_Control_Block
);
1210 -- Yes, we know this is never assigned a value, only the tag
1211 -- is used for dispatching purposes, so that's expected.
1214 FIO
.Open
(File_Ptr
=> AP
(File
),
1215 Dummy_FCB
=> Dummy_File_Control_Block
,
1216 Mode
=> To_FCB
(Mode
),
1231 -- Note: we assume that it is impossible in practice for the page
1232 -- to exceed the value of Count'Last, i.e. no check is required for
1233 -- overflow raising layout error.
1235 function Page
(File
: File_Type
) return Positive_Count
is
1237 FIO
.Check_File_Open
(AP
(File
));
1241 function Page
return Positive_Count
is
1243 return Page
(Current_Out
);
1250 function Page_Length
(File
: File_Type
) return Count
is
1252 FIO
.Check_Write_Status
(AP
(File
));
1253 return File
.Page_Length
;
1256 function Page_Length
return Count
is
1258 return Page_Length
(Current_Out
);
1270 FIO
.Check_Write_Status
(AP
(File
));
1272 if File
.Line_Length
/= 0 and then File
.Col
> File
.Line_Length
then
1276 -- If lower half character, or brackets encoding, output directly
1278 if Character'Pos (Item
) < 16#
80#
1279 or else File
.WC_Method
= WCEM_Brackets
1281 if fputc
(Character'Pos (Item
), File
.Stream
) = EOF
then
1285 -- Case of upper half character with non-brackets encoding
1288 Put_Encoded
(File
, Item
);
1291 File
.Col
:= File
.Col
+ 1;
1294 procedure Put
(Item
: Character) is
1296 FIO
.Check_Write_Status
(AP
(Current_Out
));
1298 if Current_Out
.Line_Length
/= 0
1299 and then Current_Out
.Col
> Current_Out
.Line_Length
1301 New_Line
(Current_Out
);
1304 -- If lower half character, or brackets encoding, output directly
1306 if Character'Pos (Item
) < 16#
80#
1307 or else Default_WCEM
= WCEM_Brackets
1309 if fputc
(Character'Pos (Item
), Current_Out
.Stream
) = EOF
then
1313 -- Case of upper half character with non-brackets encoding
1316 Put_Encoded
(Current_Out
, Item
);
1319 Current_Out
.Col
:= Current_Out
.Col
+ 1;
1331 FIO
.Check_Write_Status
(AP
(File
));
1333 -- Only have something to do if string is non-null
1335 if Item
'Length > 0 then
1337 -- If we have bounded lines, or if the file encoding is other than
1338 -- Brackets and the string has at least one upper half character,
1339 -- then output the string character by character.
1341 if File
.Line_Length
/= 0
1342 or else (File
.WC_Method
/= WCEM_Brackets
1343 and then Has_Upper_Half_Character
(Item
))
1345 for J
in Item
'Range loop
1346 Put
(File
, Item
(J
));
1349 -- Otherwise we can output the entire string at once. Note that if
1350 -- there are LF or FF characters in the string, we do not bother to
1351 -- count them as line or page terminators.
1354 FIO
.Write_Buf
(AP
(File
), Item
'Address, Item
'Length);
1355 File
.Col
:= File
.Col
+ Item
'Length;
1360 procedure Put
(Item
: String) is
1362 Put
(Current_Out
, Item
);
1369 procedure Put_Encoded
(File
: File_Type
; Char
: Character) is
1370 procedure Out_Char
(C
: Character);
1371 -- Procedure to output one character of an upper half encoded sequence
1373 procedure WC_Out
is new Wide_Char_To_Char_Sequence
(Out_Char
);
1379 procedure Out_Char
(C
: Character) is
1381 Putc
(Character'Pos (C
), File
);
1384 -- Start of processing for Put_Encoded
1387 WC_Out
(Wide_Character'Val (Character'Pos (Char
)), File
.WC_Method
);
1398 Ilen
: Natural := Item
'Length;
1399 Istart
: Natural := Item
'First;
1402 FIO
.Check_Write_Status
(AP
(File
));
1404 -- If we have bounded lines, or if the file encoding is other than
1405 -- Brackets and the string has at least one upper half character, then
1406 -- output the string character by character.
1408 if File
.Line_Length
/= 0
1409 or else (File
.WC_Method
/= WCEM_Brackets
1410 and then Has_Upper_Half_Character
(Item
))
1412 for J
in Item
'Range loop
1413 Put
(File
, Item
(J
));
1420 -- Normal case where we do not need to output character by character
1422 -- We setup a single string that has the necessary terminators and
1423 -- then write it with a single call. The reason for doing this is
1424 -- that it gives better behavior for the use of Put_Line in multi-
1425 -- tasking programs, since often the OS will treat the entire put
1426 -- operation as an atomic operation.
1428 -- We only do this if the message is 512 characters or less in length,
1429 -- since otherwise Put_Line would use an unbounded amount of stack
1430 -- space and could cause undetected stack overflow. If we have a
1431 -- longer string, then output the first part separately to avoid this.
1434 FIO
.Write_Buf
(AP
(File
), Item
'Address, size_t
(Ilen
- 512));
1435 Istart
:= Istart
+ Ilen
- 512;
1439 -- Now prepare the string with its terminator
1442 Buffer
: String (1 .. Ilen
+ 2);
1446 Buffer
(1 .. Ilen
) := Item
(Istart
.. Item
'Last);
1447 Buffer
(Ilen
+ 1) := Character'Val (LM
);
1449 if File
.Page_Length
/= 0
1450 and then File
.Line
> File
.Page_Length
1452 Buffer
(Ilen
+ 2) := Character'Val (PM
);
1453 Plen
:= size_t
(Ilen
) + 2;
1455 File
.Page
:= File
.Page
+ 1;
1458 Plen
:= size_t
(Ilen
) + 1;
1459 File
.Line
:= File
.Line
+ 1;
1462 FIO
.Write_Buf
(AP
(File
), Buffer
'Address, Plen
);
1468 procedure Put_Line
(Item
: String) is
1470 Put_Line
(Current_Out
, Item
);
1477 procedure Putc
(ch
: int
; File
: File_Type
) is
1479 if fputc
(ch
, File
.Stream
) = EOF
then
1488 -- This is the primitive Stream Read routine, used when a Text_IO file
1489 -- is treated directly as a stream using Text_IO.Streams.Stream.
1492 (File
: in out Text_AFCB
;
1493 Item
: out Stream_Element_Array
;
1494 Last
: out Stream_Element_Offset
)
1497 pragma Warnings
(Off
, Discard_ch
);
1500 -- Need to deal with Before_Upper_Half_Character ???
1502 if File
.Mode
/= FCB
.In_File
then
1506 -- Deal with case where our logical and physical position do not match
1507 -- because of being after an LM or LM-PM sequence when in fact we are
1508 -- logically positioned before it.
1510 if File
.Before_LM
then
1512 -- If we are before a PM, then it is possible for a stream read
1513 -- to leave us after the LM and before the PM, which is a bit
1514 -- odd. The easiest way to deal with this is to unget the PM,
1515 -- so we are indeed positioned between the characters. This way
1516 -- further stream read operations will work correctly, and the
1517 -- effect on text processing is a little weird, but what can
1518 -- be expected if stream and text input are mixed this way?
1520 if File
.Before_LM_PM
then
1521 Discard_ch
:= ungetc
(PM
, File
.Stream
);
1522 File
.Before_LM_PM
:= False;
1525 File
.Before_LM
:= False;
1527 Item
(Item
'First) := Stream_Element
(Character'Pos (ASCII
.LF
));
1529 if Item
'Length = 1 then
1535 Stream_Element_Offset
1536 (fread
(buffer
=> Item
'Address,
1537 index
=> size_t
(Item
'First + 1),
1539 count
=> Item
'Length - 1,
1540 stream
=> File
.Stream
));
1546 -- Now we do the read. Since this is a text file, it is normally in
1547 -- text mode, but stream data must be read in binary mode, so we
1548 -- temporarily set binary mode for the read, resetting it after.
1549 -- These calls have no effect in a system (like Unix) where there is
1550 -- no distinction between text and binary files.
1552 set_binary_mode
(fileno
(File
.Stream
));
1556 Stream_Element_Offset
1557 (fread
(Item
'Address, 1, Item
'Length, File
.Stream
)) - 1;
1559 if Last
< Item
'Last then
1560 if ferror
(File
.Stream
) /= 0 then
1565 set_text_mode
(fileno
(File
.Stream
));
1573 (File
: in out File_Type
;
1577 -- Don't allow change of mode for current file (RM A.10.2(5))
1579 if (File
= Current_In
or else
1580 File
= Current_Out
or else
1581 File
= Current_Error
)
1582 and then To_FCB
(Mode
) /= File
.Mode
1587 Terminate_Line
(File
);
1588 FIO
.Reset
(AP
(File
)'Unrestricted_Access, To_FCB
(Mode
));
1592 File
.Line_Length
:= 0;
1593 File
.Page_Length
:= 0;
1594 File
.Before_LM
:= False;
1595 File
.Before_LM_PM
:= False;
1598 procedure Reset
(File
: in out File_Type
) is
1600 Terminate_Line
(File
);
1601 FIO
.Reset
(AP
(File
)'Unrestricted_Access);
1605 File
.Line_Length
:= 0;
1606 File
.Page_Length
:= 0;
1607 File
.Before_LM
:= False;
1608 File
.Before_LM_PM
:= False;
1617 To
: Positive_Count
)
1622 -- Raise Constraint_Error if out of range value. The reason for this
1623 -- explicit test is that we don't want junk values around, even if
1624 -- checks are off in the caller.
1626 if not To
'Valid then
1627 raise Constraint_Error
;
1630 FIO
.Check_File_Open
(AP
(File
));
1634 if Mode
(File
) >= Out_File
then
1636 -- Error if we attempt to set Col to a value greater than the
1637 -- maximum permissible line length.
1639 if File
.Line_Length
/= 0 and then To
> File
.Line_Length
then
1643 -- If we are behind current position, then go to start of new line
1645 if To
< File
.Col
then
1649 -- Loop to output blanks till we are at the required column
1651 while File
.Col
< To
loop
1658 -- If we are logically before a LM, but physically after it, the
1659 -- file position still reflects the position before the LM, so eat
1660 -- it now and adjust the file position appropriately.
1662 if File
.Before_LM
then
1663 File
.Before_LM
:= False;
1664 File
.Before_LM_PM
:= False;
1665 File
.Line
:= File
.Line
+ 1;
1669 -- Loop reading characters till we get one at the required Col value
1672 -- Read next character. The reason we have to read ahead is to
1673 -- skip formatting characters, the effect of Set_Col is to set
1674 -- us to a real character with the right Col value, and format
1675 -- characters don't count.
1679 -- Error if we hit an end of file
1684 -- If line mark, eat it and adjust file position
1687 File
.Line
:= File
.Line
+ 1;
1690 -- If recognized page mark, eat it, and adjust file position
1692 elsif ch
= PM
and then File
.Is_Regular_File
then
1693 File
.Page
:= File
.Page
+ 1;
1697 -- Otherwise this is the character we are looking for, so put it
1698 -- back in the input stream (we have not adjusted the file
1699 -- position yet, so everything is set right after this ungetc).
1701 elsif To
= File
.Col
then
1705 -- Keep skipping characters if we are not there yet, updating the
1706 -- file position past the skipped character.
1709 File
.Col
:= File
.Col
+ 1;
1715 procedure Set_Col
(To
: Positive_Count
) is
1717 Set_Col
(Current_Out
, To
);
1724 procedure Set_Error
(File
: File_Type
) is
1726 FIO
.Check_Write_Status
(AP
(File
));
1727 Current_Err
:= File
;
1734 procedure Set_Input
(File
: File_Type
) is
1736 FIO
.Check_Read_Status
(AP
(File
));
1746 To
: Positive_Count
)
1749 -- Raise Constraint_Error if out of range value. The reason for this
1750 -- explicit test is that we don't want junk values around, even if
1751 -- checks are off in the caller.
1753 if not To
'Valid then
1754 raise Constraint_Error
;
1757 FIO
.Check_File_Open
(AP
(File
));
1759 if To
= File
.Line
then
1763 if Mode
(File
) >= Out_File
then
1764 if File
.Page_Length
/= 0 and then To
> File
.Page_Length
then
1768 if To
< File
.Line
then
1772 while File
.Line
< To
loop
1777 while To
/= File
.Line
loop
1783 procedure Set_Line
(To
: Positive_Count
) is
1785 Set_Line
(Current_Out
, To
);
1788 ---------------------
1789 -- Set_Line_Length --
1790 ---------------------
1792 procedure Set_Line_Length
(File
: File_Type
; To
: Count
) is
1794 -- Raise Constraint_Error if out of range value. The reason for this
1795 -- explicit test is that we don't want junk values around, even if
1796 -- checks are off in the caller.
1798 if not To
'Valid then
1799 raise Constraint_Error
;
1802 FIO
.Check_Write_Status
(AP
(File
));
1803 File
.Line_Length
:= To
;
1804 end Set_Line_Length
;
1806 procedure Set_Line_Length
(To
: Count
) is
1808 Set_Line_Length
(Current_Out
, To
);
1809 end Set_Line_Length
;
1815 procedure Set_Output
(File
: File_Type
) is
1817 FIO
.Check_Write_Status
(AP
(File
));
1818 Current_Out
:= File
;
1821 ---------------------
1822 -- Set_Page_Length --
1823 ---------------------
1825 procedure Set_Page_Length
(File
: File_Type
; To
: Count
) is
1827 -- Raise Constraint_Error if out of range value. The reason for this
1828 -- explicit test is that we don't want junk values around, even if
1829 -- checks are off in the caller.
1831 if not To
'Valid then
1832 raise Constraint_Error
;
1835 FIO
.Check_Write_Status
(AP
(File
));
1836 File
.Page_Length
:= To
;
1837 end Set_Page_Length
;
1839 procedure Set_Page_Length
(To
: Count
) is
1841 Set_Page_Length
(Current_Out
, To
);
1842 end Set_Page_Length
;
1848 procedure Set_WCEM
(File
: in out File_Type
) is
1853 File
.WC_Method
:= WCEM_Brackets
;
1854 FIO
.Form_Parameter
(File
.Form
.all, "wcem", Start
, Stop
);
1857 File
.WC_Method
:= WCEM_Brackets
;
1859 elsif Start
/= 0 then
1860 if Stop
= Start
then
1861 for J
in WC_Encoding_Letters
'Range loop
1862 if File
.Form
(Start
) = WC_Encoding_Letters
(J
) then
1863 File
.WC_Method
:= J
;
1870 raise Use_Error
with "invalid WCEM form parameter";
1880 Spacing
: Positive_Count
:= 1)
1885 -- Raise Constraint_Error if out of range value. The reason for this
1886 -- explicit test is that we don't want junk values around, even if
1887 -- checks are off in the caller.
1889 if not Spacing
'Valid then
1890 raise Constraint_Error
;
1893 FIO
.Check_Read_Status
(AP
(File
));
1895 for L
in 1 .. Spacing
loop
1896 if File
.Before_LM
then
1897 File
.Before_LM
:= False;
1899 -- Note that if File.Before_LM_PM is currently set, we also have
1900 -- to reset it (because it makes sense for Before_LM_PM to be set
1901 -- only when Before_LM is also set). This is done later on in this
1902 -- subprogram, as soon as Before_LM_PM has been taken into account
1903 -- for the purpose of page and line counts.
1908 -- If at end of file now, then immediately raise End_Error. Note
1909 -- that we can never be positioned between a line mark and a page
1910 -- mark, so if we are at the end of file, we cannot logically be
1911 -- before the implicit page mark that is at the end of the file.
1913 -- For the same reason, we do not need an explicit check for a
1914 -- page mark. If there is a FF in the middle of a line, the file
1915 -- is not in canonical format and we do not care about the page
1916 -- numbers for files other than ones in canonical format.
1922 -- If not at end of file, then loop till we get to an LM or EOF.
1923 -- The latter case happens only in non-canonical files where the
1924 -- last line is not terminated by LM, but we don't want to blow
1925 -- up for such files, so we assume an implicit LM in this case.
1928 exit when ch
= LM
or ch
= EOF
;
1933 -- We have got past a line mark, now, for a regular file only,
1934 -- see if a page mark immediately follows this line mark and
1935 -- if so, skip past the page mark as well. We do not do this
1936 -- for non-regular files, since it would cause an undesirable
1937 -- wait for an additional character.
1940 File
.Line
:= File
.Line
+ 1;
1942 if File
.Before_LM_PM
then
1943 File
.Page
:= File
.Page
+ 1;
1945 File
.Before_LM_PM
:= False;
1947 elsif File
.Is_Regular_File
then
1950 -- Page mark can be explicit, or implied at the end of the file
1952 if (ch
= PM
or else ch
= EOF
)
1953 and then File
.Is_Regular_File
1955 File
.Page
:= File
.Page
+ 1;
1963 File
.Before_Upper_Half_Character
:= False;
1966 procedure Skip_Line
(Spacing
: Positive_Count
:= 1) is
1968 Skip_Line
(Current_In
, Spacing
);
1975 procedure Skip_Page
(File
: File_Type
) is
1979 FIO
.Check_Read_Status
(AP
(File
));
1981 -- If at page mark already, just skip it
1983 if File
.Before_LM_PM
then
1984 File
.Before_LM
:= False;
1985 File
.Before_LM_PM
:= False;
1986 File
.Page
:= File
.Page
+ 1;
1992 -- This is a bit tricky, if we are logically before an LM then
1993 -- it is not an error if we are at an end of file now, since we
1994 -- are not really at it.
1996 if File
.Before_LM
then
1997 File
.Before_LM
:= False;
1998 File
.Before_LM_PM
:= False;
2001 -- Otherwise we do raise End_Error if we are at the end of file now
2011 -- Now we can just rumble along to the next page mark, or to the
2012 -- end of file, if that comes first. The latter case happens when
2013 -- the page mark is implied at the end of file.
2017 or else (ch
= PM
and then File
.Is_Regular_File
);
2021 File
.Page
:= File
.Page
+ 1;
2024 File
.Before_Upper_Half_Character
:= False;
2027 procedure Skip_Page
is
2029 Skip_Page
(Current_In
);
2032 --------------------
2033 -- Standard_Error --
2034 --------------------
2036 function Standard_Error
return File_Type
is
2038 return Standard_Err
;
2041 function Standard_Error
return File_Access
is
2043 return Standard_Err
'Access;
2046 --------------------
2047 -- Standard_Input --
2048 --------------------
2050 function Standard_Input
return File_Type
is
2055 function Standard_Input
return File_Access
is
2057 return Standard_In
'Access;
2060 ---------------------
2061 -- Standard_Output --
2062 ---------------------
2064 function Standard_Output
return File_Type
is
2066 return Standard_Out
;
2067 end Standard_Output
;
2069 function Standard_Output
return File_Access
is
2071 return Standard_Out
'Access;
2072 end Standard_Output
;
2074 --------------------
2075 -- Terminate_Line --
2076 --------------------
2078 procedure Terminate_Line
(File
: File_Type
) is
2080 FIO
.Check_File_Open
(AP
(File
));
2082 -- For file other than In_File, test for needing to terminate last line
2084 if Mode
(File
) /= In_File
then
2086 -- If not at start of line definition need new line
2088 if File
.Col
/= 1 then
2091 -- For files other than standard error and standard output, we
2092 -- make sure that an empty file has a single line feed, so that
2093 -- it is properly formatted. We avoid this for the standard files
2094 -- because it is too much of a nuisance to have these odd line
2095 -- feeds when nothing has been written to the file.
2097 -- We also avoid this for files opened in append mode, in
2098 -- accordance with (RM A.8.2(10))
2100 elsif (File
/= Standard_Err
and then File
/= Standard_Out
)
2101 and then (File
.Line
= 1 and then File
.Page
= 1)
2102 and then Mode
(File
) = Out_File
2113 procedure Ungetc
(ch
: int
; File
: File_Type
) is
2116 if ungetc
(ch
, File
.Stream
) = EOF
then
2126 -- This is the primitive Stream Write routine, used when a Text_IO file
2127 -- is treated directly as a stream using Text_IO.Streams.Stream.
2130 (File
: in out Text_AFCB
;
2131 Item
: Stream_Element_Array
)
2133 pragma Warnings
(Off
, File
);
2134 -- Because in this implementation we don't need IN OUT, we only read
2136 function Has_Translated_Characters
return Boolean;
2137 -- return True if Item array contains a character which will be
2138 -- translated under the text file mode. There is only one such
2139 -- character under DOS based systems which is character 10.
2141 text_translation_required
: Boolean;
2142 for text_translation_required
'Size use Character'Size;
2143 pragma Import
(C
, text_translation_required
,
2144 "__gnat_text_translation_required");
2146 Siz
: constant size_t
:= Item
'Length;
2148 -------------------------------
2149 -- Has_Translated_Characters --
2150 -------------------------------
2152 function Has_Translated_Characters
return Boolean is
2154 for K
in Item
'Range loop
2155 if Item
(K
) = 10 then
2160 end Has_Translated_Characters
;
2162 Needs_Binary_Write
: constant Boolean :=
2163 text_translation_required
2164 and then Has_Translated_Characters
;
2166 -- Start of processing for Write
2169 if File
.Mode
= FCB
.In_File
then
2173 -- Now we do the write. Since this is a text file, it is normally in
2174 -- text mode, but stream data must be written in binary mode, so we
2175 -- temporarily set binary mode for the write, resetting it after. This
2176 -- is done only if needed (i.e. there is some characters in Item which
2177 -- needs to be written using the binary mode).
2178 -- These calls have no effect in a system (like Unix) where there is
2179 -- no distinction between text and binary files.
2181 -- Since the character translation is done at the time the buffer is
2182 -- written (this is true under Windows) we first flush current buffer
2183 -- with text mode if needed.
2185 if Needs_Binary_Write
then
2186 if fflush
(File
.Stream
) = -1 then
2190 set_binary_mode
(fileno
(File
.Stream
));
2193 if fwrite
(Item
'Address, 1, Siz
, File
.Stream
) /= Siz
then
2197 -- At this point we need to flush the buffer using the binary mode then
2198 -- we reset to text mode.
2200 if Needs_Binary_Write
then
2201 if fflush
(File
.Stream
) = -1 then
2205 set_text_mode
(fileno
(File
.Stream
));
2209 -- Use "preallocated" strings to avoid calling "new" during the
2210 -- elaboration of the run time. This is needed in the tasking case to
2211 -- avoid calling Task_Lock too early. A filename is expected to end with a
2212 -- null character in the runtime, here the null characters are added just
2213 -- to have a correct filename length.
2215 Err_Name
: aliased String := "*stderr" & ASCII
.NUL
;
2216 In_Name
: aliased String := "*stdin" & ASCII
.NUL
;
2217 Out_Name
: aliased String := "*stdout" & ASCII
.NUL
;
2220 -------------------------------
2221 -- Initialize Standard Files --
2222 -------------------------------
2224 for J
in WC_Encoding_Method
loop
2225 if WC_Encoding
= WC_Encoding_Letters
(J
) then
2230 -- Note: the names in these files are bogus, and probably it would be
2231 -- better for these files to have no names, but the ACVC test insist!
2232 -- We use names that are bound to fail in open etc.
2234 Standard_Err
.Stream
:= stderr
;
2235 Standard_Err
.Name
:= Err_Name
'Access;
2236 Standard_Err
.Form
:= Null_Str
'Unrestricted_Access;
2237 Standard_Err
.Mode
:= FCB
.Out_File
;
2238 Standard_Err
.Is_Regular_File
:= is_regular_file
(fileno
(stderr
)) /= 0;
2239 Standard_Err
.Is_Temporary_File
:= False;
2240 Standard_Err
.Is_System_File
:= True;
2241 Standard_Err
.Is_Text_File
:= True;
2242 Standard_Err
.Access_Method
:= 'T';
2243 Standard_Err
.Self
:= Standard_Err
;
2244 Standard_Err
.WC_Method
:= Default_WCEM
;
2246 Standard_In
.Stream
:= stdin
;
2247 Standard_In
.Name
:= In_Name
'Access;
2248 Standard_In
.Form
:= Null_Str
'Unrestricted_Access;
2249 Standard_In
.Mode
:= FCB
.In_File
;
2250 Standard_In
.Is_Regular_File
:= is_regular_file
(fileno
(stdin
)) /= 0;
2251 Standard_In
.Is_Temporary_File
:= False;
2252 Standard_In
.Is_System_File
:= True;
2253 Standard_In
.Is_Text_File
:= True;
2254 Standard_In
.Access_Method
:= 'T';
2255 Standard_In
.Self
:= Standard_In
;
2256 Standard_In
.WC_Method
:= Default_WCEM
;
2258 Standard_Out
.Stream
:= stdout
;
2259 Standard_Out
.Name
:= Out_Name
'Access;
2260 Standard_Out
.Form
:= Null_Str
'Unrestricted_Access;
2261 Standard_Out
.Mode
:= FCB
.Out_File
;
2262 Standard_Out
.Is_Regular_File
:= is_regular_file
(fileno
(stdout
)) /= 0;
2263 Standard_Out
.Is_Temporary_File
:= False;
2264 Standard_Out
.Is_System_File
:= True;
2265 Standard_Out
.Is_Text_File
:= True;
2266 Standard_Out
.Access_Method
:= 'T';
2267 Standard_Out
.Self
:= Standard_Out
;
2268 Standard_Out
.WC_Method
:= Default_WCEM
;
2270 FIO
.Chain_File
(AP
(Standard_In
));
2271 FIO
.Chain_File
(AP
(Standard_Out
));
2272 FIO
.Chain_File
(AP
(Standard_Err
));
2274 FIO
.Make_Unbuffered
(AP
(Standard_Out
));
2275 FIO
.Make_Unbuffered
(AP
(Standard_Err
));