1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . T E X T _ I O --
9 -- Copyright (C) 1992-2013, 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 3, 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. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with Ada
.Streams
; use Ada
.Streams
;
33 with Interfaces
.C_Streams
; use Interfaces
.C_Streams
;
37 with System
.WCh_Cnv
; use System
.WCh_Cnv
;
38 with System
.WCh_Con
; use System
.WCh_Con
;
40 with Ada
.Unchecked_Conversion
;
41 with Ada
.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 Ada
.Unchecked_Conversion
(File_Mode
, FCB
.File_Mode
);
53 function To_TIO
is new Ada
.Unchecked_Conversion
(FCB
.File_Mode
, File_Mode
);
54 use type FCB
.File_Mode
;
56 use type System
.CRTL
.size_t
;
58 WC_Encoding
: Character;
59 pragma Import
(C
, WC_Encoding
, "__gl_wc_encoding");
60 -- Default wide character encoding
62 Err_Name
: aliased String := "*stderr" & ASCII
.NUL
;
63 In_Name
: aliased String := "*stdin" & ASCII
.NUL
;
64 Out_Name
: aliased String := "*stdout" & ASCII
.NUL
;
65 -- Names of standard files
67 -- Use "preallocated" strings to avoid calling "new" during the elaboration
68 -- of the run time. This is needed in the tasking case to avoid calling
69 -- Task_Lock too early. A filename is expected to end with a null character
70 -- in the runtime, here the null characters are added just to have a
71 -- correct filename length.
73 -- Note: the names for these files are bogus, and probably it would be
74 -- better for these files to have no names, but the ACVC tests insist.
75 -- We use names that are bound to fail in open etc.
77 Null_Str
: aliased constant String := "";
78 -- Used as form string for standard files
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
84 function Get_Upper_Half_Char
86 File
: File_Type
) return Character;
87 -- This function is shared by Get and Get_Immediate to extract an encoded
88 -- upper half character value from the given File. The first byte has
89 -- already been read and is passed in C. The character value is returned as
90 -- the result, and the file pointer is bumped past the character.
91 -- Constraint_Error is raised if the encoded value is outside the bounds of
94 function Get_Upper_Half_Char_Immed
96 File
: File_Type
) return Character;
97 -- This routine is identical to Get_Upper_Half_Char, except that the reads
98 -- are done in Get_Immediate mode (i.e. without waiting for a line return).
100 function Getc
(File
: File_Type
) return int
;
101 -- Gets next character from file, which has already been checked for being
102 -- in read status, and returns the character read if no error occurs. The
103 -- result is EOF if the end of file was read.
105 function Getc_Immed
(File
: File_Type
) return int
;
106 -- This routine is identical to Getc, except that the read is done in
107 -- Get_Immediate mode (i.e. without waiting for a line return).
109 function Has_Upper_Half_Character
(Item
: String) return Boolean;
110 -- Returns True if any of the characters is in the range 16#80#-16#FF#
112 function Nextc
(File
: File_Type
) return int
;
113 -- Returns next character from file without skipping past it (i.e. it is a
114 -- combination of Getc followed by an Ungetc).
116 procedure Put_Encoded
(File
: File_Type
; Char
: Character);
117 -- Called to output a character Char to the given File, when the encoding
118 -- method for the file is other than brackets, and Char is upper half.
120 procedure Putc
(ch
: int
; File
: File_Type
);
121 -- Outputs the given character to the file, which has already been checked
122 -- for being in output status. Device_Error is raised if the character
123 -- cannot be written.
125 procedure Set_WCEM
(File
: in out File_Type
);
126 -- Called by Open and Create to set the wide character encoding method for
127 -- the file, processing a WCEM form parameter if one is present. File is
128 -- IN OUT because it may be closed in case of an error.
130 procedure Terminate_Line
(File
: File_Type
);
131 -- If the file is in Write_File or Append_File mode, and the current line
132 -- is not terminated, then a line terminator is written using New_Line.
133 -- Note that there is no Terminate_Page routine, because the page mark at
134 -- the end of the file is implied if necessary.
136 procedure Ungetc
(ch
: int
; File
: File_Type
);
137 -- Pushes back character into stream, using ungetc. The caller has checked
138 -- that the file is in read status. Device_Error is raised if the character
139 -- cannot be pushed back. An attempt to push back and end of file character
146 function AFCB_Allocate
(Control_Block
: Text_AFCB
) return FCB
.AFCB_Ptr
is
147 pragma Unreferenced
(Control_Block
);
149 return new Text_AFCB
;
156 procedure AFCB_Close
(File
: not null access Text_AFCB
) is
158 -- If the file being closed is one of the current files, then close
159 -- the corresponding current file. It is not clear that this action
160 -- is required (RM A.10.3(23)) but it seems reasonable, and besides
161 -- ACVC test CE3208A expects this behavior.
163 if File_Type
(File
) = Current_In
then
165 elsif File_Type
(File
) = Current_Out
then
167 elsif File_Type
(File
) = Current_Err
then
171 Terminate_Line
(File_Type
(File
));
178 procedure AFCB_Free
(File
: not null access Text_AFCB
) is
179 type FCB_Ptr
is access all Text_AFCB
;
180 FT
: FCB_Ptr
:= FCB_Ptr
(File
);
182 procedure Free
is new Ada
.Unchecked_Deallocation
(Text_AFCB
, FCB_Ptr
);
192 procedure Close
(File
: in out File_Type
) is
194 FIO
.Close
(AP
(File
)'Unrestricted_Access);
201 -- Note: we assume that it is impossible in practice for the column
202 -- to exceed the value of Count'Last, i.e. no check is required for
203 -- overflow raising layout error.
205 function Col
(File
: File_Type
) return Positive_Count
is
207 FIO
.Check_File_Open
(AP
(File
));
211 function Col
return Positive_Count
is
213 return Col
(Current_Out
);
221 (File
: in out File_Type
;
222 Mode
: File_Mode
:= Out_File
;
226 Dummy_File_Control_Block
: Text_AFCB
;
227 pragma Warnings
(Off
, Dummy_File_Control_Block
);
228 -- Yes, we know this is never assigned a value, only the tag
229 -- is used for dispatching purposes, so that's expected.
232 FIO
.Open
(File_Ptr
=> AP
(File
),
233 Dummy_FCB
=> Dummy_File_Control_Block
,
234 Mode
=> To_FCB
(Mode
),
249 function Current_Error
return File_Type
is
254 function Current_Error
return File_Access
is
256 return Current_Err
.Self
'Access;
263 function Current_Input
return File_Type
is
268 function Current_Input
return File_Access
is
270 return Current_In
.Self
'Access;
277 function Current_Output
return File_Type
is
282 function Current_Output
return File_Access
is
284 return Current_Out
.Self
'Access;
291 procedure Delete
(File
: in out File_Type
) is
293 FIO
.Delete
(AP
(File
)'Unrestricted_Access);
300 function End_Of_File
(File
: File_Type
) return Boolean is
304 FIO
.Check_Read_Status
(AP
(File
));
306 if File
.Before_Upper_Half_Character
then
309 elsif File
.Before_LM
then
310 if File
.Before_LM_PM
then
311 return Nextc
(File
) = EOF
;
325 File
.Before_LM
:= True;
329 -- Here we are just past the line mark with Before_LM set so that we
330 -- do not have to try to back up past the LM, thus avoiding the need
331 -- to back up more than one character.
338 elsif ch
= PM
and then File
.Is_Regular_File
then
339 File
.Before_LM_PM
:= True;
340 return Nextc
(File
) = EOF
;
342 -- Here if neither EOF nor PM followed end of line
351 function End_Of_File
return Boolean is
353 return End_Of_File
(Current_In
);
360 function End_Of_Line
(File
: File_Type
) return Boolean is
364 FIO
.Check_Read_Status
(AP
(File
));
366 if File
.Before_Upper_Half_Character
then
369 elsif File
.Before_LM
then
385 function End_Of_Line
return Boolean is
387 return End_Of_Line
(Current_In
);
394 function End_Of_Page
(File
: File_Type
) return Boolean is
398 FIO
.Check_Read_Status
(AP
(File
));
400 if not File
.Is_Regular_File
then
403 elsif File
.Before_Upper_Half_Character
then
406 elsif File
.Before_LM
then
407 if File
.Before_LM_PM
then
422 File
.Before_LM
:= True;
426 -- Here we are just past the line mark with Before_LM set so that we
427 -- do not have to try to back up past the LM, thus avoiding the need
428 -- to back up more than one character.
432 return ch
= PM
or else ch
= EOF
;
435 function End_Of_Page
return Boolean is
437 return End_Of_Page
(Current_In
);
444 function EOF_Char
return Integer is
453 procedure Flush
(File
: File_Type
) is
455 FIO
.Flush
(AP
(File
));
467 function Form
(File
: File_Type
) return String is
469 return FIO
.Form
(AP
(File
));
478 Item
: out Character)
483 FIO
.Check_Read_Status
(AP
(File
));
485 if File
.Before_Upper_Half_Character
then
486 File
.Before_Upper_Half_Character
:= False;
487 Item
:= File
.Saved_Upper_Half_Character
;
489 elsif File
.Before_LM
then
490 File
.Before_LM
:= False;
493 if File
.Before_LM_PM
then
495 File
.Page
:= File
.Page
+ 1;
496 File
.Before_LM_PM
:= False;
498 File
.Line
:= File
.Line
+ 1;
509 File
.Line
:= File
.Line
+ 1;
512 elsif ch
= PM
and then File
.Is_Regular_File
then
513 File
.Page
:= File
.Page
+ 1;
517 Item
:= Character'Val (ch
);
518 File
.Col
:= File
.Col
+ 1;
524 procedure Get
(Item
: out Character) is
526 Get
(Current_In
, Item
);
537 FIO
.Check_Read_Status
(AP
(File
));
539 if File
.Before_LM
then
540 File
.Before_LM
:= False;
541 File
.Before_LM_PM
:= False;
544 if File
.Before_LM_PM
then
546 File
.Page
:= File
.Page
+ 1;
547 File
.Before_LM_PM
:= False;
550 File
.Line
:= File
.Line
+ 1;
555 while J
<= Item
'Last loop
562 File
.Line
:= File
.Line
+ 1;
565 elsif ch
= PM
and then File
.Is_Regular_File
then
566 File
.Page
:= File
.Page
+ 1;
570 Item
(J
) := Character'Val (ch
);
572 File
.Col
:= File
.Col
+ 1;
577 procedure Get
(Item
: out String) is
579 Get
(Current_In
, Item
);
586 procedure Get_Immediate
588 Item
: out Character)
593 FIO
.Check_Read_Status
(AP
(File
));
595 if File
.Before_Upper_Half_Character
then
596 File
.Before_Upper_Half_Character
:= False;
597 Item
:= File
.Saved_Upper_Half_Character
;
599 elsif File
.Before_LM
then
600 File
.Before_LM
:= False;
601 File
.Before_LM_PM
:= False;
602 Item
:= Character'Val (LM
);
605 ch
:= Getc_Immed
(File
);
611 (if not Is_Start_Of_Encoding
(Character'Val (ch
), File
.WC_Method
)
612 then Character'Val (ch
)
613 else Get_Upper_Half_Char_Immed
(Character'Val (ch
), File
));
618 procedure Get_Immediate
619 (Item
: out Character)
622 Get_Immediate
(Current_In
, Item
);
625 procedure Get_Immediate
627 Item
: out Character;
628 Available
: out Boolean)
634 procedure getc_immediate_nowait
637 end_of_file
: out int
;
639 pragma Import
(C
, getc_immediate_nowait
, "getc_immediate_nowait");
642 FIO
.Check_Read_Status
(AP
(File
));
645 if File
.Before_Upper_Half_Character
then
646 File
.Before_Upper_Half_Character
:= False;
647 Item
:= File
.Saved_Upper_Half_Character
;
649 elsif File
.Before_LM
then
650 File
.Before_LM
:= False;
651 File
.Before_LM_PM
:= False;
652 Item
:= Character'Val (LM
);
655 getc_immediate_nowait
(File
.Stream
, ch
, end_of_file
, avail
);
657 if ferror
(File
.Stream
) /= 0 then
660 elsif end_of_file
/= 0 then
671 (if Is_Start_Of_Encoding
(Character'Val (ch
), File
.WC_Method
)
672 then Character'Val (ch
)
673 else Get_Upper_Half_Char_Immed
(Character'Val (ch
), File
));
679 procedure Get_Immediate
680 (Item
: out Character;
681 Available
: out Boolean)
684 Get_Immediate
(Current_In
, Item
, Available
);
694 Last
: out Natural) is separate;
695 -- The implementation of Ada.Text_IO.Get_Line is split into a subunit so
696 -- that different implementations can be used on different systems. In
697 -- particular the standard implementation uses low level stuff that is
698 -- not appropriate for the JVM and .NET implementations.
705 Get_Line
(Current_In
, Item
, Last
);
708 function Get_Line
(File
: File_Type
) return String is
709 Buffer
: String (1 .. 500);
712 function Get_Rest
(S
: String) return String;
713 -- This is a recursive function that reads the rest of the line and
714 -- returns it. S is the part read so far.
720 function Get_Rest
(S
: String) return String is
722 -- Each time we allocate a buffer the same size as what we have
723 -- read so far. This limits us to a logarithmic number of calls
724 -- to Get_Rest and also ensures only a linear use of stack space.
726 Buffer
: String (1 .. S
'Length);
730 Get_Line
(File
, Buffer
, Last
);
733 R
: constant String := S
& Buffer
(1 .. Last
);
735 if Last
< Buffer
'Last then
743 -- Start of processing for Get_Line
746 Get_Line
(File
, Buffer
, Last
);
748 if Last
< Buffer
'Last then
749 return Buffer
(1 .. Last
);
751 return Get_Rest
(Buffer
(1 .. Last
));
755 function Get_Line
return String is
757 return Get_Line
(Current_In
);
760 -------------------------
761 -- Get_Upper_Half_Char --
762 -------------------------
764 function Get_Upper_Half_Char
766 File
: File_Type
) return Character
768 Result
: Wide_Character;
770 function In_Char
return Character;
771 -- Function used to obtain additional characters it the wide character
772 -- sequence is more than one character long.
774 function WC_In
is new Char_Sequence_To_Wide_Char
(In_Char
);
780 function In_Char
return Character is
781 ch
: constant Integer := Getc
(File
);
786 return Character'Val (ch
);
790 -- Start of processing for Get_Upper_Half_Char
793 Result
:= WC_In
(C
, File
.WC_Method
);
795 if Wide_Character'Pos (Result
) > 16#FF#
then
796 raise Constraint_Error
with
797 "invalid wide character in Text_'I'O input";
799 return Character'Val (Wide_Character'Pos (Result
));
801 end Get_Upper_Half_Char
;
803 -------------------------------
804 -- Get_Upper_Half_Char_Immed --
805 -------------------------------
807 function Get_Upper_Half_Char_Immed
809 File
: File_Type
) return Character
811 Result
: Wide_Character;
813 function In_Char
return Character;
814 -- Function used to obtain additional characters it the wide character
815 -- sequence is more than one character long.
817 function WC_In
is new Char_Sequence_To_Wide_Char
(In_Char
);
823 function In_Char
return Character is
824 ch
: constant Integer := Getc_Immed
(File
);
829 return Character'Val (ch
);
833 -- Start of processing for Get_Upper_Half_Char_Immed
836 Result
:= WC_In
(C
, File
.WC_Method
);
838 if Wide_Character'Pos (Result
) > 16#FF#
then
839 raise Constraint_Error
with
840 "invalid wide character in Text_'I'O input";
842 return Character'Val (Wide_Character'Pos (Result
));
844 end Get_Upper_Half_Char_Immed
;
850 function Getc
(File
: File_Type
) return int
is
854 ch
:= fgetc
(File
.Stream
);
856 if ch
= EOF
and then ferror
(File
.Stream
) /= 0 then
867 function Getc_Immed
(File
: File_Type
) return int
is
871 procedure getc_immediate
872 (stream
: FILEs
; ch
: out int
; end_of_file
: out int
);
873 pragma Import
(C
, getc_immediate
, "getc_immediate");
876 FIO
.Check_Read_Status
(AP
(File
));
878 if File
.Before_LM
then
879 File
.Before_LM
:= False;
880 File
.Before_LM_PM
:= False;
884 getc_immediate
(File
.Stream
, ch
, end_of_file
);
886 if ferror
(File
.Stream
) /= 0 then
888 elsif end_of_file
/= 0 then
896 ------------------------------
897 -- Has_Upper_Half_Character --
898 ------------------------------
900 function Has_Upper_Half_Character
(Item
: String) return Boolean is
902 for J
in Item
'Range loop
903 if Character'Pos (Item
(J
)) >= 16#
80#
then
909 end Has_Upper_Half_Character
;
911 -------------------------------
912 -- Initialize_Standard_Files --
913 -------------------------------
915 procedure Initialize_Standard_Files
is
917 Standard_Err
.Stream
:= stderr
;
918 Standard_Err
.Name
:= Err_Name
'Access;
919 Standard_Err
.Form
:= Null_Str
'Unrestricted_Access;
920 Standard_Err
.Mode
:= FCB
.Out_File
;
921 Standard_Err
.Is_Regular_File
:= is_regular_file
(fileno
(stderr
)) /= 0;
922 Standard_Err
.Is_Temporary_File
:= False;
923 Standard_Err
.Is_System_File
:= True;
924 Standard_Err
.Is_Text_File
:= True;
925 Standard_Err
.Access_Method
:= 'T';
926 Standard_Err
.Self
:= Standard_Err
;
927 Standard_Err
.WC_Method
:= Default_WCEM
;
929 Standard_In
.Stream
:= stdin
;
930 Standard_In
.Name
:= In_Name
'Access;
931 Standard_In
.Form
:= Null_Str
'Unrestricted_Access;
932 Standard_In
.Mode
:= FCB
.In_File
;
933 Standard_In
.Is_Regular_File
:= is_regular_file
(fileno
(stdin
)) /= 0;
934 Standard_In
.Is_Temporary_File
:= False;
935 Standard_In
.Is_System_File
:= True;
936 Standard_In
.Is_Text_File
:= True;
937 Standard_In
.Access_Method
:= 'T';
938 Standard_In
.Self
:= Standard_In
;
939 Standard_In
.WC_Method
:= Default_WCEM
;
941 Standard_Out
.Stream
:= stdout
;
942 Standard_Out
.Name
:= Out_Name
'Access;
943 Standard_Out
.Form
:= Null_Str
'Unrestricted_Access;
944 Standard_Out
.Mode
:= FCB
.Out_File
;
945 Standard_Out
.Is_Regular_File
:= is_regular_file
(fileno
(stdout
)) /= 0;
946 Standard_Out
.Is_Temporary_File
:= False;
947 Standard_Out
.Is_System_File
:= True;
948 Standard_Out
.Is_Text_File
:= True;
949 Standard_Out
.Access_Method
:= 'T';
950 Standard_Out
.Self
:= Standard_Out
;
951 Standard_Out
.WC_Method
:= Default_WCEM
;
953 FIO
.Make_Unbuffered
(AP
(Standard_Out
));
954 FIO
.Make_Unbuffered
(AP
(Standard_Err
));
955 end Initialize_Standard_Files
;
961 function Is_Open
(File
: File_Type
) return Boolean is
963 return FIO
.Is_Open
(AP
(File
));
970 -- Note: we assume that it is impossible in practice for the line
971 -- to exceed the value of Count'Last, i.e. no check is required for
972 -- overflow raising layout error.
974 function Line
(File
: File_Type
) return Positive_Count
is
976 FIO
.Check_File_Open
(AP
(File
));
980 function Line
return Positive_Count
is
982 return Line
(Current_Out
);
989 function Line_Length
(File
: File_Type
) return Count
is
991 FIO
.Check_Write_Status
(AP
(File
));
992 return File
.Line_Length
;
995 function Line_Length
return Count
is
997 return Line_Length
(Current_Out
);
1004 procedure Look_Ahead
1006 Item
: out Character;
1007 End_Of_Line
: out Boolean)
1012 FIO
.Check_Read_Status
(AP
(File
));
1014 -- If we are logically before a line mark, we can return immediately
1016 if File
.Before_LM
then
1017 End_Of_Line
:= True;
1020 -- If we are before an upper half character just return it (this can
1021 -- happen if there are two calls to Look_Ahead in a row).
1023 elsif File
.Before_Upper_Half_Character
then
1024 End_Of_Line
:= False;
1025 Item
:= File
.Saved_Upper_Half_Character
;
1027 -- Otherwise we must read a character from the input stream
1034 or else (ch
= PM
and then File
.Is_Regular_File
)
1036 End_Of_Line
:= True;
1040 -- Case where character obtained does not represent the start of an
1041 -- encoded sequence so it stands for itself and we can unget it with
1044 elsif not Is_Start_Of_Encoding
1045 (Character'Val (ch
), File
.WC_Method
)
1047 End_Of_Line
:= False;
1049 Item
:= Character'Val (ch
);
1051 -- For the start of an encoding, we read the character using the
1052 -- Get_Upper_Half_Char routine. It will occupy more than one byte
1053 -- so we can't put it back with ungetc. Instead we save it in the
1054 -- control block, setting a flag that everyone interested in reading
1055 -- characters must test before reading the stream.
1058 Item
:= Get_Upper_Half_Char
(Character'Val (ch
), File
);
1059 End_Of_Line
:= False;
1060 File
.Saved_Upper_Half_Character
:= Item
;
1061 File
.Before_Upper_Half_Character
:= True;
1066 procedure Look_Ahead
1067 (Item
: out Character;
1068 End_Of_Line
: out Boolean)
1071 Look_Ahead
(Current_In
, Item
, End_Of_Line
);
1078 function Mode
(File
: File_Type
) return File_Mode
is
1080 return To_TIO
(FIO
.Mode
(AP
(File
)));
1087 function Name
(File
: File_Type
) return String is
1089 return FIO
.Name
(AP
(File
));
1098 Spacing
: Positive_Count
:= 1)
1101 -- Raise Constraint_Error if out of range value. The reason for this
1102 -- explicit test is that we don't want junk values around, even if
1103 -- checks are off in the caller.
1105 if not Spacing
'Valid then
1106 raise Constraint_Error
;
1109 FIO
.Check_Write_Status
(AP
(File
));
1111 for K
in 1 .. Spacing
loop
1113 File
.Line
:= File
.Line
+ 1;
1115 if File
.Page_Length
/= 0
1116 and then File
.Line
> File
.Page_Length
1120 File
.Page
:= File
.Page
+ 1;
1127 procedure New_Line
(Spacing
: Positive_Count
:= 1) is
1129 New_Line
(Current_Out
, Spacing
);
1136 procedure New_Page
(File
: File_Type
) is
1138 FIO
.Check_Write_Status
(AP
(File
));
1140 if File
.Col
/= 1 or else File
.Line
= 1 then
1145 File
.Page
:= File
.Page
+ 1;
1150 procedure New_Page
is
1152 New_Page
(Current_Out
);
1159 function Nextc
(File
: File_Type
) return int
is
1163 ch
:= fgetc
(File
.Stream
);
1166 if ferror
(File
.Stream
) /= 0 then
1171 if ungetc
(ch
, File
.Stream
) = EOF
then
1184 (File
: in out File_Type
;
1187 Form
: String := "")
1189 Dummy_File_Control_Block
: Text_AFCB
;
1190 pragma Warnings
(Off
, Dummy_File_Control_Block
);
1191 -- Yes, we know this is never assigned a value, only the tag
1192 -- is used for dispatching purposes, so that's expected.
1195 FIO
.Open
(File_Ptr
=> AP
(File
),
1196 Dummy_FCB
=> Dummy_File_Control_Block
,
1197 Mode
=> To_FCB
(Mode
),
1212 -- Note: we assume that it is impossible in practice for the page
1213 -- to exceed the value of Count'Last, i.e. no check is required for
1214 -- overflow raising layout error.
1216 function Page
(File
: File_Type
) return Positive_Count
is
1218 FIO
.Check_File_Open
(AP
(File
));
1222 function Page
return Positive_Count
is
1224 return Page
(Current_Out
);
1231 function Page_Length
(File
: File_Type
) return Count
is
1233 FIO
.Check_Write_Status
(AP
(File
));
1234 return File
.Page_Length
;
1237 function Page_Length
return Count
is
1239 return Page_Length
(Current_Out
);
1251 FIO
.Check_Write_Status
(AP
(File
));
1253 if File
.Line_Length
/= 0 and then File
.Col
> File
.Line_Length
then
1257 -- If lower half character, or brackets encoding, output directly
1259 if Character'Pos (Item
) < 16#
80#
1260 or else File
.WC_Method
= WCEM_Brackets
1262 if fputc
(Character'Pos (Item
), File
.Stream
) = EOF
then
1266 -- Case of upper half character with non-brackets encoding
1269 Put_Encoded
(File
, Item
);
1272 File
.Col
:= File
.Col
+ 1;
1275 procedure Put
(Item
: Character) is
1277 Put
(Current_Out
, Item
);
1289 FIO
.Check_Write_Status
(AP
(File
));
1291 -- Only have something to do if string is non-null
1293 if Item
'Length > 0 then
1295 -- If we have bounded lines, or if the file encoding is other than
1296 -- Brackets and the string has at least one upper half character,
1297 -- then output the string character by character.
1299 if File
.Line_Length
/= 0
1300 or else (File
.WC_Method
/= WCEM_Brackets
1301 and then Has_Upper_Half_Character
(Item
))
1303 for J
in Item
'Range loop
1304 Put
(File
, Item
(J
));
1307 -- Otherwise we can output the entire string at once. Note that if
1308 -- there are LF or FF characters in the string, we do not bother to
1309 -- count them as line or page terminators.
1312 FIO
.Write_Buf
(AP
(File
), Item
'Address, Item
'Length);
1313 File
.Col
:= File
.Col
+ Item
'Length;
1318 procedure Put
(Item
: String) is
1320 Put
(Current_Out
, Item
);
1327 procedure Put_Encoded
(File
: File_Type
; Char
: Character) is
1328 procedure Out_Char
(C
: Character);
1329 -- Procedure to output one character of an upper half encoded sequence
1331 procedure WC_Out
is new Wide_Char_To_Char_Sequence
(Out_Char
);
1337 procedure Out_Char
(C
: Character) is
1339 Putc
(Character'Pos (C
), File
);
1342 -- Start of processing for Put_Encoded
1345 WC_Out
(Wide_Character'Val (Character'Pos (Char
)), File
.WC_Method
);
1356 Ilen
: Natural := Item
'Length;
1357 Istart
: Natural := Item
'First;
1360 FIO
.Check_Write_Status
(AP
(File
));
1362 -- If we have bounded lines, or if the file encoding is other than
1363 -- Brackets and the string has at least one upper half character, then
1364 -- output the string character by character.
1366 if File
.Line_Length
/= 0
1367 or else (File
.WC_Method
/= WCEM_Brackets
1368 and then Has_Upper_Half_Character
(Item
))
1370 for J
in Item
'Range loop
1371 Put
(File
, Item
(J
));
1378 -- Normal case where we do not need to output character by character
1380 -- We setup a single string that has the necessary terminators and
1381 -- then write it with a single call. The reason for doing this is
1382 -- that it gives better behavior for the use of Put_Line in multi-
1383 -- tasking programs, since often the OS will treat the entire put
1384 -- operation as an atomic operation.
1386 -- We only do this if the message is 512 characters or less in length,
1387 -- since otherwise Put_Line would use an unbounded amount of stack
1388 -- space and could cause undetected stack overflow. If we have a
1389 -- longer string, then output the first part separately to avoid this.
1392 FIO
.Write_Buf
(AP
(File
), Item
'Address, size_t
(Ilen
- 512));
1393 Istart
:= Istart
+ Ilen
- 512;
1397 -- Now prepare the string with its terminator
1400 Buffer
: String (1 .. Ilen
+ 2);
1404 Buffer
(1 .. Ilen
) := Item
(Istart
.. Item
'Last);
1405 Buffer
(Ilen
+ 1) := Character'Val (LM
);
1407 if File
.Page_Length
/= 0
1408 and then File
.Line
> File
.Page_Length
1410 Buffer
(Ilen
+ 2) := Character'Val (PM
);
1411 Plen
:= size_t
(Ilen
) + 2;
1413 File
.Page
:= File
.Page
+ 1;
1416 Plen
:= size_t
(Ilen
) + 1;
1417 File
.Line
:= File
.Line
+ 1;
1420 FIO
.Write_Buf
(AP
(File
), Buffer
'Address, Plen
);
1426 procedure Put_Line
(Item
: String) is
1428 Put_Line
(Current_Out
, Item
);
1435 procedure Putc
(ch
: int
; File
: File_Type
) is
1437 if fputc
(ch
, File
.Stream
) = EOF
then
1446 -- This is the primitive Stream Read routine, used when a Text_IO file
1447 -- is treated directly as a stream using Text_IO.Streams.Stream.
1450 (File
: in out Text_AFCB
;
1451 Item
: out Stream_Element_Array
;
1452 Last
: out Stream_Element_Offset
)
1455 pragma Warnings
(Off
, Discard_ch
);
1458 -- Need to deal with Before_Upper_Half_Character ???
1460 if File
.Mode
/= FCB
.In_File
then
1464 -- Deal with case where our logical and physical position do not match
1465 -- because of being after an LM or LM-PM sequence when in fact we are
1466 -- logically positioned before it.
1468 if File
.Before_LM
then
1470 -- If we are before a PM, then it is possible for a stream read
1471 -- to leave us after the LM and before the PM, which is a bit
1472 -- odd. The easiest way to deal with this is to unget the PM,
1473 -- so we are indeed positioned between the characters. This way
1474 -- further stream read operations will work correctly, and the
1475 -- effect on text processing is a little weird, but what can
1476 -- be expected if stream and text input are mixed this way?
1478 if File
.Before_LM_PM
then
1479 Discard_ch
:= ungetc
(PM
, File
.Stream
);
1480 File
.Before_LM_PM
:= False;
1483 File
.Before_LM
:= False;
1485 Item
(Item
'First) := Stream_Element
(Character'Pos (ASCII
.LF
));
1487 if Item
'Length = 1 then
1493 Stream_Element_Offset
1494 (fread
(buffer
=> Item
'Address,
1495 index
=> size_t
(Item
'First + 1),
1497 count
=> Item
'Length - 1,
1498 stream
=> File
.Stream
));
1504 -- Now we do the read. Since this is a text file, it is normally in
1505 -- text mode, but stream data must be read in binary mode, so we
1506 -- temporarily set binary mode for the read, resetting it after.
1507 -- These calls have no effect in a system (like Unix) where there is
1508 -- no distinction between text and binary files.
1510 set_binary_mode
(fileno
(File
.Stream
));
1514 Stream_Element_Offset
1515 (fread
(Item
'Address, 1, Item
'Length, File
.Stream
)) - 1;
1517 if Last
< Item
'Last then
1518 if ferror
(File
.Stream
) /= 0 then
1523 set_text_mode
(fileno
(File
.Stream
));
1531 (File
: in out File_Type
;
1535 -- Don't allow change of mode for current file (RM A.10.2(5))
1537 if (File
= Current_In
or else
1538 File
= Current_Out
or else
1539 File
= Current_Error
)
1540 and then To_FCB
(Mode
) /= File
.Mode
1545 Terminate_Line
(File
);
1546 FIO
.Reset
(AP
(File
)'Unrestricted_Access, To_FCB
(Mode
));
1550 File
.Line_Length
:= 0;
1551 File
.Page_Length
:= 0;
1552 File
.Before_LM
:= False;
1553 File
.Before_LM_PM
:= False;
1556 procedure Reset
(File
: in out File_Type
) is
1558 Terminate_Line
(File
);
1559 FIO
.Reset
(AP
(File
)'Unrestricted_Access);
1563 File
.Line_Length
:= 0;
1564 File
.Page_Length
:= 0;
1565 File
.Before_LM
:= False;
1566 File
.Before_LM_PM
:= False;
1575 To
: Positive_Count
)
1580 -- Raise Constraint_Error if out of range value. The reason for this
1581 -- explicit test is that we don't want junk values around, even if
1582 -- checks are off in the caller.
1584 if not To
'Valid then
1585 raise Constraint_Error
;
1588 FIO
.Check_File_Open
(AP
(File
));
1592 if Mode
(File
) >= Out_File
then
1594 -- Error if we attempt to set Col to a value greater than the
1595 -- maximum permissible line length.
1597 if File
.Line_Length
/= 0 and then To
> File
.Line_Length
then
1601 -- If we are behind current position, then go to start of new line
1603 if To
< File
.Col
then
1607 -- Loop to output blanks till we are at the required column
1609 while File
.Col
< To
loop
1616 -- If we are logically before a LM, but physically after it, the
1617 -- file position still reflects the position before the LM, so eat
1618 -- it now and adjust the file position appropriately.
1620 if File
.Before_LM
then
1621 File
.Before_LM
:= False;
1622 File
.Before_LM_PM
:= False;
1623 File
.Line
:= File
.Line
+ 1;
1627 -- Loop reading characters till we get one at the required Col value
1630 -- Read next character. The reason we have to read ahead is to
1631 -- skip formatting characters, the effect of Set_Col is to set
1632 -- us to a real character with the right Col value, and format
1633 -- characters don't count.
1637 -- Error if we hit an end of file
1642 -- If line mark, eat it and adjust file position
1645 File
.Line
:= File
.Line
+ 1;
1648 -- If recognized page mark, eat it, and adjust file position
1650 elsif ch
= PM
and then File
.Is_Regular_File
then
1651 File
.Page
:= File
.Page
+ 1;
1655 -- Otherwise this is the character we are looking for, so put it
1656 -- back in the input stream (we have not adjusted the file
1657 -- position yet, so everything is set right after this ungetc).
1659 elsif To
= File
.Col
then
1663 -- Keep skipping characters if we are not there yet, updating the
1664 -- file position past the skipped character.
1667 File
.Col
:= File
.Col
+ 1;
1673 procedure Set_Col
(To
: Positive_Count
) is
1675 Set_Col
(Current_Out
, To
);
1682 procedure Set_Error
(File
: File_Type
) is
1684 FIO
.Check_Write_Status
(AP
(File
));
1685 Current_Err
:= File
;
1692 procedure Set_Input
(File
: File_Type
) is
1694 FIO
.Check_Read_Status
(AP
(File
));
1704 To
: Positive_Count
)
1707 -- Raise Constraint_Error if out of range value. The reason for this
1708 -- explicit test is that we don't want junk values around, even if
1709 -- checks are off in the caller.
1711 if not To
'Valid then
1712 raise Constraint_Error
;
1715 FIO
.Check_File_Open
(AP
(File
));
1717 if To
= File
.Line
then
1721 if Mode
(File
) >= Out_File
then
1722 if File
.Page_Length
/= 0 and then To
> File
.Page_Length
then
1726 if To
< File
.Line
then
1730 while File
.Line
< To
loop
1735 while To
/= File
.Line
loop
1741 procedure Set_Line
(To
: Positive_Count
) is
1743 Set_Line
(Current_Out
, To
);
1746 ---------------------
1747 -- Set_Line_Length --
1748 ---------------------
1750 procedure Set_Line_Length
(File
: File_Type
; To
: Count
) is
1752 -- Raise Constraint_Error if out of range value. The reason for this
1753 -- explicit test is that we don't want junk values around, even if
1754 -- checks are off in the caller.
1756 if not To
'Valid then
1757 raise Constraint_Error
;
1760 FIO
.Check_Write_Status
(AP
(File
));
1761 File
.Line_Length
:= To
;
1762 end Set_Line_Length
;
1764 procedure Set_Line_Length
(To
: Count
) is
1766 Set_Line_Length
(Current_Out
, To
);
1767 end Set_Line_Length
;
1773 procedure Set_Output
(File
: File_Type
) is
1775 FIO
.Check_Write_Status
(AP
(File
));
1776 Current_Out
:= File
;
1779 ---------------------
1780 -- Set_Page_Length --
1781 ---------------------
1783 procedure Set_Page_Length
(File
: File_Type
; To
: Count
) is
1785 -- Raise Constraint_Error if out of range value. The reason for this
1786 -- explicit test is that we don't want junk values around, even if
1787 -- checks are off in the caller.
1789 if not To
'Valid then
1790 raise Constraint_Error
;
1793 FIO
.Check_Write_Status
(AP
(File
));
1794 File
.Page_Length
:= To
;
1795 end Set_Page_Length
;
1797 procedure Set_Page_Length
(To
: Count
) is
1799 Set_Page_Length
(Current_Out
, To
);
1800 end Set_Page_Length
;
1806 procedure Set_WCEM
(File
: in out File_Type
) is
1811 File
.WC_Method
:= WCEM_Brackets
;
1812 FIO
.Form_Parameter
(File
.Form
.all, "wcem", Start
, Stop
);
1815 File
.WC_Method
:= WCEM_Brackets
;
1818 if Stop
= Start
then
1819 for J
in WC_Encoding_Letters
'Range loop
1820 if File
.Form
(Start
) = WC_Encoding_Letters
(J
) then
1821 File
.WC_Method
:= J
;
1828 raise Use_Error
with "invalid WCEM form parameter";
1838 Spacing
: Positive_Count
:= 1)
1843 -- Raise Constraint_Error if out of range value. The reason for this
1844 -- explicit test is that we don't want junk values around, even if
1845 -- checks are off in the caller.
1847 if not Spacing
'Valid then
1848 raise Constraint_Error
;
1851 FIO
.Check_Read_Status
(AP
(File
));
1853 for L
in 1 .. Spacing
loop
1854 if File
.Before_LM
then
1855 File
.Before_LM
:= False;
1857 -- Note that if File.Before_LM_PM is currently set, we also have
1858 -- to reset it (because it makes sense for Before_LM_PM to be set
1859 -- only when Before_LM is also set). This is done later on in this
1860 -- subprogram, as soon as Before_LM_PM has been taken into account
1861 -- for the purpose of page and line counts.
1866 -- If at end of file now, then immediately raise End_Error. Note
1867 -- that we can never be positioned between a line mark and a page
1868 -- mark, so if we are at the end of file, we cannot logically be
1869 -- before the implicit page mark that is at the end of the file.
1871 -- For the same reason, we do not need an explicit check for a
1872 -- page mark. If there is a FF in the middle of a line, the file
1873 -- is not in canonical format and we do not care about the page
1874 -- numbers for files other than ones in canonical format.
1880 -- If not at end of file, then loop till we get to an LM or EOF.
1881 -- The latter case happens only in non-canonical files where the
1882 -- last line is not terminated by LM, but we don't want to blow
1883 -- up for such files, so we assume an implicit LM in this case.
1886 exit when ch
= LM
or else ch
= EOF
;
1891 -- We have got past a line mark, now, for a regular file only,
1892 -- see if a page mark immediately follows this line mark and
1893 -- if so, skip past the page mark as well. We do not do this
1894 -- for non-regular files, since it would cause an undesirable
1895 -- wait for an additional character.
1898 File
.Line
:= File
.Line
+ 1;
1900 if File
.Before_LM_PM
then
1901 File
.Page
:= File
.Page
+ 1;
1903 File
.Before_LM_PM
:= False;
1905 elsif File
.Is_Regular_File
then
1908 -- Page mark can be explicit, or implied at the end of the file
1910 if (ch
= PM
or else ch
= EOF
)
1911 and then File
.Is_Regular_File
1913 File
.Page
:= File
.Page
+ 1;
1921 File
.Before_Upper_Half_Character
:= False;
1924 procedure Skip_Line
(Spacing
: Positive_Count
:= 1) is
1926 Skip_Line
(Current_In
, Spacing
);
1933 procedure Skip_Page
(File
: File_Type
) is
1937 FIO
.Check_Read_Status
(AP
(File
));
1939 -- If at page mark already, just skip it
1941 if File
.Before_LM_PM
then
1942 File
.Before_LM
:= False;
1943 File
.Before_LM_PM
:= False;
1944 File
.Page
:= File
.Page
+ 1;
1950 -- This is a bit tricky, if we are logically before an LM then
1951 -- it is not an error if we are at an end of file now, since we
1952 -- are not really at it.
1954 if File
.Before_LM
then
1955 File
.Before_LM
:= False;
1956 File
.Before_LM_PM
:= False;
1959 -- Otherwise we do raise End_Error if we are at the end of file now
1969 -- Now we can just rumble along to the next page mark, or to the
1970 -- end of file, if that comes first. The latter case happens when
1971 -- the page mark is implied at the end of file.
1975 or else (ch
= PM
and then File
.Is_Regular_File
);
1979 File
.Page
:= File
.Page
+ 1;
1982 File
.Before_Upper_Half_Character
:= False;
1985 procedure Skip_Page
is
1987 Skip_Page
(Current_In
);
1990 --------------------
1991 -- Standard_Error --
1992 --------------------
1994 function Standard_Error
return File_Type
is
1996 return Standard_Err
;
1999 function Standard_Error
return File_Access
is
2001 return Standard_Err
'Access;
2004 --------------------
2005 -- Standard_Input --
2006 --------------------
2008 function Standard_Input
return File_Type
is
2013 function Standard_Input
return File_Access
is
2015 return Standard_In
'Access;
2018 ---------------------
2019 -- Standard_Output --
2020 ---------------------
2022 function Standard_Output
return File_Type
is
2024 return Standard_Out
;
2025 end Standard_Output
;
2027 function Standard_Output
return File_Access
is
2029 return Standard_Out
'Access;
2030 end Standard_Output
;
2032 --------------------
2033 -- Terminate_Line --
2034 --------------------
2036 procedure Terminate_Line
(File
: File_Type
) is
2038 FIO
.Check_File_Open
(AP
(File
));
2040 -- For file other than In_File, test for needing to terminate last line
2042 if Mode
(File
) /= In_File
then
2044 -- If not at start of line definition need new line
2046 if File
.Col
/= 1 then
2049 -- For files other than standard error and standard output, we
2050 -- make sure that an empty file has a single line feed, so that
2051 -- it is properly formatted. We avoid this for the standard files
2052 -- because it is too much of a nuisance to have these odd line
2053 -- feeds when nothing has been written to the file.
2055 -- We also avoid this for files opened in append mode, in
2056 -- accordance with (RM A.8.2(10))
2058 elsif (File
/= Standard_Err
and then File
/= Standard_Out
)
2059 and then (File
.Line
= 1 and then File
.Page
= 1)
2060 and then Mode
(File
) = Out_File
2071 procedure Ungetc
(ch
: int
; File
: File_Type
) is
2074 if ungetc
(ch
, File
.Stream
) = EOF
then
2084 -- This is the primitive Stream Write routine, used when a Text_IO file
2085 -- is treated directly as a stream using Text_IO.Streams.Stream.
2088 (File
: in out Text_AFCB
;
2089 Item
: Stream_Element_Array
)
2091 pragma Warnings
(Off
, File
);
2092 -- Because in this implementation we don't need IN OUT, we only read
2094 function Has_Translated_Characters
return Boolean;
2095 -- return True if Item array contains a character which will be
2096 -- translated under the text file mode. There is only one such
2097 -- character under DOS based systems which is character 10.
2099 text_translation_required
: Boolean;
2100 for text_translation_required
'Size use Character'Size;
2101 pragma Import
(C
, text_translation_required
,
2102 "__gnat_text_translation_required");
2104 Siz
: constant size_t
:= Item
'Length;
2106 -------------------------------
2107 -- Has_Translated_Characters --
2108 -------------------------------
2110 function Has_Translated_Characters
return Boolean is
2112 for K
in Item
'Range loop
2113 if Item
(K
) = 10 then
2118 end Has_Translated_Characters
;
2120 Needs_Binary_Write
: constant Boolean :=
2121 text_translation_required
and then Has_Translated_Characters
;
2123 -- Start of processing for Write
2126 if File
.Mode
= FCB
.In_File
then
2130 -- Now we do the write. Since this is a text file, it is normally in
2131 -- text mode, but stream data must be written in binary mode, so we
2132 -- temporarily set binary mode for the write, resetting it after. This
2133 -- is done only if needed (i.e. there is some characters in Item which
2134 -- needs to be written using the binary mode).
2135 -- These calls have no effect in a system (like Unix) where there is
2136 -- no distinction between text and binary files.
2138 -- Since the character translation is done at the time the buffer is
2139 -- written (this is true under Windows) we first flush current buffer
2140 -- with text mode if needed.
2142 if Needs_Binary_Write
then
2143 if fflush
(File
.Stream
) = -1 then
2147 set_binary_mode
(fileno
(File
.Stream
));
2150 if fwrite
(Item
'Address, 1, Siz
, File
.Stream
) /= Siz
then
2154 -- At this point we need to flush the buffer using the binary mode then
2155 -- we reset to text mode.
2157 if Needs_Binary_Write
then
2158 if fflush
(File
.Stream
) = -1 then
2162 set_text_mode
(fileno
(File
.Stream
));
2167 -- Initialize Standard Files
2169 for J
in WC_Encoding_Method
loop
2170 if WC_Encoding
= WC_Encoding_Letters
(J
) then
2175 Initialize_Standard_Files
;
2177 FIO
.Chain_File
(AP
(Standard_In
));
2178 FIO
.Chain_File
(AP
(Standard_Out
));
2179 FIO
.Chain_File
(AP
(Standard_Err
));