1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . W I D E _ T E X T _ I O --
9 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Ada
.Exceptions
; use Ada
.Exceptions
;
35 with Ada
.Streams
; use Ada
.Streams
;
36 with Interfaces
.C_Streams
; use Interfaces
.C_Streams
;
40 with System
.WCh_Cnv
; use System
.WCh_Cnv
;
41 with System
.WCh_Con
; use System
.WCh_Con
;
42 with Unchecked_Conversion
;
43 with 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
.Wide_Text_IO
is
50 package FIO
renames System
.File_IO
;
52 subtype AP
is FCB
.AFCB_Ptr
;
54 function To_FCB
is new Unchecked_Conversion
(File_Mode
, FCB
.File_Mode
);
55 function To_TIO
is new Unchecked_Conversion
(FCB
.File_Mode
, File_Mode
);
56 use type FCB
.File_Mode
;
58 WC_Encoding
: Character;
59 pragma Import
(C
, WC_Encoding
, "__gl_wc_encoding");
61 -----------------------
62 -- Local Subprograms --
63 -----------------------
65 function Getc_Immed
(File
: in File_Type
) return int
;
66 -- This routine is identical to Getc, except that the read is done in
67 -- Get_Immediate mode (i.e. without waiting for a line return).
69 function Get_Wide_Char_Immed
72 return Wide_Character;
73 -- This routine is identical to Get_Wide_Char, except that the reads are
74 -- done in Get_Immediate mode (i.e. without waiting for a line return).
76 procedure Set_WCEM
(File
: in out File_Type
);
77 -- Called by Open and Create to set the wide character encoding method
78 -- for the file, processing a WCEM form parameter if one is present.
79 -- File is IN OUT because it may be closed in case of an error.
85 function AFCB_Allocate
86 (Control_Block
: Wide_Text_AFCB
)
89 pragma Warnings
(Off
, Control_Block
);
92 return new Wide_Text_AFCB
;
99 procedure AFCB_Close
(File
: access Wide_Text_AFCB
) is
101 -- If the file being closed is one of the current files, then close
102 -- the corresponding current file. It is not clear that this action
103 -- is required (RM A.10.3(23)) but it seems reasonable, and besides
104 -- ACVC test CE3208A expects this behavior.
106 if File_Type
(File
) = Current_In
then
108 elsif File_Type
(File
) = Current_Out
then
110 elsif File_Type
(File
) = Current_Err
then
114 Terminate_Line
(File_Type
(File
));
121 procedure AFCB_Free
(File
: access Wide_Text_AFCB
) is
122 type FCB_Ptr
is access all Wide_Text_AFCB
;
123 FT
: FCB_Ptr
:= FCB_Ptr
(File
);
125 procedure Free
is new Unchecked_Deallocation
(Wide_Text_AFCB
, FCB_Ptr
);
135 procedure Close
(File
: in out File_Type
) is
137 FIO
.Close
(AP
(File
));
144 -- Note: we assume that it is impossible in practice for the column
145 -- to exceed the value of Count'Last, i.e. no check is required for
146 -- overflow raising layout error.
148 function Col
(File
: in File_Type
) return Positive_Count
is
150 FIO
.Check_File_Open
(AP
(File
));
154 function Col
return Positive_Count
is
156 return Col
(Current_Out
);
164 (File
: in out File_Type
;
165 Mode
: in File_Mode
:= Out_File
;
166 Name
: in String := "";
167 Form
: in String := "")
169 File_Control_Block
: Wide_Text_AFCB
;
172 FIO
.Open
(File_Ptr
=> AP
(File
),
173 Dummy_FCB
=> File_Control_Block
,
174 Mode
=> To_FCB
(Mode
),
187 function Current_Error
return File_Type
is
192 function Current_Error
return File_Access
is
194 return Current_Err
'Access;
201 function Current_Input
return File_Type
is
206 function Current_Input
return File_Access
is
208 return Current_In
'Access;
215 function Current_Output
return File_Type
is
220 function Current_Output
return File_Access
is
222 return Current_Out
'Access;
229 procedure Delete
(File
: in out File_Type
) is
231 FIO
.Delete
(AP
(File
));
238 function End_Of_File
(File
: in File_Type
) return Boolean is
242 FIO
.Check_Read_Status
(AP
(File
));
244 if File
.Before_Wide_Character
then
247 elsif File
.Before_LM
then
249 if File
.Before_LM_PM
then
250 return Nextc
(File
) = EOF
;
264 File
.Before_LM
:= True;
268 -- Here we are just past the line mark with Before_LM set so that we
269 -- do not have to try to back up past the LM, thus avoiding the need
270 -- to back up more than one character.
277 elsif ch
= PM
and then File
.Is_Regular_File
then
278 File
.Before_LM_PM
:= True;
279 return Nextc
(File
) = EOF
;
281 -- Here if neither EOF nor PM followed end of line
290 function End_Of_File
return Boolean is
292 return End_Of_File
(Current_In
);
299 function End_Of_Line
(File
: in File_Type
) return Boolean is
303 FIO
.Check_Read_Status
(AP
(File
));
305 if File
.Before_Wide_Character
then
308 elsif File
.Before_LM
then
324 function End_Of_Line
return Boolean is
326 return End_Of_Line
(Current_In
);
333 function End_Of_Page
(File
: in File_Type
) return Boolean is
337 FIO
.Check_Read_Status
(AP
(File
));
339 if not File
.Is_Regular_File
then
342 elsif File
.Before_Wide_Character
then
345 elsif File
.Before_LM
then
346 if File
.Before_LM_PM
then
361 File
.Before_LM
:= True;
365 -- Here we are just past the line mark with Before_LM set so that we
366 -- do not have to try to back up past the LM, thus avoiding the need
367 -- to back up more than one character.
371 return ch
= PM
or else ch
= EOF
;
374 function End_Of_Page
return Boolean is
376 return End_Of_Page
(Current_In
);
383 procedure Flush
(File
: in File_Type
) is
385 FIO
.Flush
(AP
(File
));
397 function Form
(File
: in File_Type
) return String is
399 return FIO
.Form
(AP
(File
));
407 (File
: in File_Type
;
408 Item
: out Wide_Character)
413 FIO
.Check_Read_Status
(AP
(File
));
415 if File
.Before_Wide_Character
then
416 File
.Before_Wide_Character
:= False;
417 Item
:= File
.Saved_Wide_Character
;
420 Get_Character
(File
, C
);
421 Item
:= Get_Wide_Char
(C
, File
);
425 procedure Get
(Item
: out Wide_Character) is
427 Get
(Current_In
, Item
);
431 (File
: in File_Type
;
432 Item
: out Wide_String)
435 for J
in Item
'Range loop
436 Get
(File
, Item
(J
));
440 procedure Get
(Item
: out Wide_String) is
442 Get
(Current_In
, Item
);
449 procedure Get_Character
450 (File
: in File_Type
;
451 Item
: out Character)
456 if File
.Before_LM
then
457 File
.Before_LM
:= False;
458 File
.Before_LM_PM
:= False;
461 if File
.Before_LM_PM
then
463 File
.Page
:= File
.Page
+ 1;
464 File
.Before_LM_PM
:= False;
467 File
.Line
:= File
.Line
+ 1;
478 File
.Line
:= File
.Line
+ 1;
481 elsif ch
= PM
and then File
.Is_Regular_File
then
482 File
.Page
:= File
.Page
+ 1;
486 Item
:= Character'Val (ch
);
487 File
.Col
:= File
.Col
+ 1;
497 procedure Get_Immediate
498 (File
: in File_Type
;
499 Item
: out Wide_Character)
504 FIO
.Check_Read_Status
(AP
(File
));
506 if File
.Before_Wide_Character
then
507 File
.Before_Wide_Character
:= False;
508 Item
:= File
.Saved_Wide_Character
;
510 elsif File
.Before_LM
then
511 File
.Before_LM
:= False;
512 File
.Before_LM_PM
:= False;
513 Item
:= Wide_Character'Val (LM
);
516 ch
:= Getc_Immed
(File
);
521 Item
:= Get_Wide_Char_Immed
(Character'Val (ch
), File
);
526 procedure Get_Immediate
527 (Item
: out Wide_Character)
530 Get_Immediate
(Current_In
, Item
);
533 procedure Get_Immediate
534 (File
: in File_Type
;
535 Item
: out Wide_Character;
536 Available
: out Boolean)
541 FIO
.Check_Read_Status
(AP
(File
));
544 if File
.Before_Wide_Character
then
545 File
.Before_Wide_Character
:= False;
546 Item
:= File
.Saved_Wide_Character
;
548 elsif File
.Before_LM
then
549 File
.Before_LM
:= False;
550 File
.Before_LM_PM
:= False;
551 Item
:= Wide_Character'Val (LM
);
554 ch
:= Getc_Immed
(File
);
559 Item
:= Get_Wide_Char_Immed
(Character'Val (ch
), File
);
564 procedure Get_Immediate
565 (Item
: out Wide_Character;
566 Available
: out Boolean)
569 Get_Immediate
(Current_In
, Item
, Available
);
577 (File
: in File_Type
;
578 Item
: out Wide_String;
582 FIO
.Check_Read_Status
(AP
(File
));
583 Last
:= Item
'First - 1;
585 -- Immediate exit for null string, this is a case in which we do not
586 -- need to test for end of file and we do not skip a line mark under
587 -- any circumstances.
589 if Last
>= Item
'Last then
593 -- Here we have at least one character, if we are immediately before
594 -- a line mark, then we will just skip past it storing no characters.
596 if File
.Before_LM
then
597 File
.Before_LM
:= False;
598 File
.Before_LM_PM
:= False;
600 -- Otherwise we need to read some characters
603 -- If we are at the end of file now, it means we are trying to
604 -- skip a file terminator and we raise End_Error (RM A.10.7(20))
606 if Nextc
(File
) = EOF
then
610 -- Loop through characters in string
613 -- Exit the loop if read is terminated by encountering line mark
614 -- Note that the use of Skip_Line here ensures we properly deal
615 -- with setting the page and line numbers.
617 if End_Of_Line
(File
) then
622 -- Otherwise store the character, note that we know that ch is
623 -- something other than LM or EOF. It could possibly be a page
624 -- mark if there is a stray page mark in the middle of a line,
625 -- but this is not an official page mark in any case, since
626 -- official page marks can only follow a line mark. The whole
627 -- page business is pretty much nonsense anyway, so we do not
628 -- want to waste time trying to make sense out of non-standard
629 -- page marks in the file! This means that the behavior of
630 -- Get_Line is different from repeated Get of a character, but
631 -- that's too bad. We only promise that page numbers etc make
632 -- sense if the file is formatted in a standard manner.
634 -- Note: we do not adjust the column number because it is quicker
635 -- to adjust it once at the end of the operation than incrementing
636 -- it each time around the loop.
639 Get
(File
, Item
(Last
));
641 -- All done if the string is full, this is the case in which
642 -- we do not skip the following line mark. We need to adjust
643 -- the column number in this case.
645 if Last
= Item
'Last then
646 File
.Col
:= File
.Col
+ Count
(Item
'Length);
650 -- Exit from the loop if we are at the end of file. This happens
651 -- if we have a last line that is not terminated with a line mark.
652 -- In this case we consider that there is an implied line mark;
653 -- this is a non-standard file, but we will treat it nicely.
655 exit when Nextc
(File
) = EOF
;
661 (Item
: out Wide_String;
665 Get_Line
(Current_In
, Item
, Last
);
672 function Get_Wide_Char
675 return Wide_Character
677 function In_Char
return Character;
678 -- Function used to obtain additional characters it the wide character
679 -- sequence is more than one character long.
681 function In_Char
return Character is
682 ch
: constant Integer := Getc
(File
);
688 return Character'Val (ch
);
692 function WC_In
is new Char_Sequence_To_Wide_Char
(In_Char
);
695 return WC_In
(C
, File
.WC_Method
);
698 -------------------------
699 -- Get_Wide_Char_Immed --
700 -------------------------
702 function Get_Wide_Char_Immed
705 return Wide_Character
707 function In_Char
return Character;
708 -- Function used to obtain additional characters it the wide character
709 -- sequence is more than one character long.
711 function In_Char
return Character is
712 ch
: constant Integer := Getc_Immed
(File
);
718 return Character'Val (ch
);
722 function WC_In
is new Char_Sequence_To_Wide_Char
(In_Char
);
725 return WC_In
(C
, File
.WC_Method
);
726 end Get_Wide_Char_Immed
;
732 function Getc
(File
: File_Type
) return int
is
736 ch
:= fgetc
(File
.Stream
);
738 if ch
= EOF
and then ferror
(File
.Stream
) /= 0 then
749 function Getc_Immed
(File
: in File_Type
) return int
is
753 procedure getc_immediate
754 (stream
: FILEs
; ch
: out int
; end_of_file
: out int
);
755 pragma Import
(C
, getc_immediate
, "getc_immediate");
758 FIO
.Check_Read_Status
(AP
(File
));
760 if File
.Before_LM
then
761 File
.Before_LM
:= False;
762 File
.Before_LM_PM
:= False;
766 getc_immediate
(File
.Stream
, ch
, end_of_file
);
768 if ferror
(File
.Stream
) /= 0 then
770 elsif end_of_file
/= 0 then
782 function Is_Open
(File
: in File_Type
) return Boolean is
784 return FIO
.Is_Open
(AP
(File
));
791 -- Note: we assume that it is impossible in practice for the line
792 -- to exceed the value of Count'Last, i.e. no check is required for
793 -- overflow raising layout error.
795 function Line
(File
: in File_Type
) return Positive_Count
is
797 FIO
.Check_File_Open
(AP
(File
));
801 function Line
return Positive_Count
is
803 return Line
(Current_Out
);
810 function Line_Length
(File
: in File_Type
) return Count
is
812 FIO
.Check_Write_Status
(AP
(File
));
813 return File
.Line_Length
;
816 function Line_Length
return Count
is
818 return Line_Length
(Current_Out
);
826 (File
: in File_Type
;
827 Item
: out Wide_Character;
828 End_Of_Line
: out Boolean)
832 -- Start of processing for Look_Ahead
835 FIO
.Check_Read_Status
(AP
(File
));
837 -- If we are logically before a line mark, we can return immediately
839 if File
.Before_LM
then
841 Item
:= Wide_Character'Val (0);
843 -- If we are before a wide character, just return it (this happens
844 -- if there are two calls to Look_Ahead in a row).
846 elsif File
.Before_Wide_Character
then
847 End_Of_Line
:= False;
848 Item
:= File
.Saved_Wide_Character
;
850 -- otherwise we must read a character from the input stream
857 or else (ch
= EOF
and then File
.Is_Regular_File
)
861 Item
:= Wide_Character'Val (0);
863 -- If the character is in the range 16#0000# to 16#007F# it stands
864 -- for itself and occupies a single byte, so we can unget it with
867 elsif ch
<= 16#
0080#
then
868 End_Of_Line
:= False;
870 Item
:= Wide_Character'Val (ch
);
872 -- For a character above this range, we read the character, using
873 -- the Get_Wide_Char routine. It may well occupy more than one byte
874 -- so we can't put it back with ungetc. Instead we save it in the
875 -- control block, setting a flag that everyone interested in reading
876 -- characters must test before reading the stream.
879 Item
:= Get_Wide_Char
(Character'Val (ch
), File
);
880 End_Of_Line
:= False;
881 File
.Saved_Wide_Character
:= Item
;
882 File
.Before_Wide_Character
:= True;
888 (Item
: out Wide_Character;
889 End_Of_Line
: out Boolean)
892 Look_Ahead
(Current_In
, Item
, End_Of_Line
);
899 function Mode
(File
: in File_Type
) return File_Mode
is
901 return To_TIO
(FIO
.Mode
(AP
(File
)));
908 function Name
(File
: in File_Type
) return String is
910 return FIO
.Name
(AP
(File
));
918 (File
: in File_Type
;
919 Spacing
: in Positive_Count
:= 1)
922 -- Raise Constraint_Error if out of range value. The reason for this
923 -- explicit test is that we don't want junk values around, even if
924 -- checks are off in the caller.
926 if Spacing
not in Positive_Count
then
927 raise Constraint_Error
;
930 FIO
.Check_Write_Status
(AP
(File
));
932 for K
in 1 .. Spacing
loop
934 File
.Line
:= File
.Line
+ 1;
936 if File
.Page_Length
/= 0
937 and then File
.Line
> File
.Page_Length
941 File
.Page
:= File
.Page
+ 1;
948 procedure New_Line
(Spacing
: in Positive_Count
:= 1) is
950 New_Line
(Current_Out
, Spacing
);
957 procedure New_Page
(File
: in File_Type
) is
959 FIO
.Check_Write_Status
(AP
(File
));
961 if File
.Col
/= 1 or else File
.Line
= 1 then
966 File
.Page
:= File
.Page
+ 1;
971 procedure New_Page
is
973 New_Page
(Current_Out
);
980 function Nextc
(File
: File_Type
) return int
is
984 ch
:= fgetc
(File
.Stream
);
987 if ferror
(File
.Stream
) /= 0 then
992 if ungetc
(ch
, File
.Stream
) = EOF
then
1005 (File
: in out File_Type
;
1006 Mode
: in File_Mode
;
1008 Form
: in String := "")
1010 File_Control_Block
: Wide_Text_AFCB
;
1013 FIO
.Open
(File_Ptr
=> AP
(File
),
1014 Dummy_FCB
=> File_Control_Block
,
1015 Mode
=> To_FCB
(Mode
),
1028 -- Note: we assume that it is impossible in practice for the page
1029 -- to exceed the value of Count'Last, i.e. no check is required for
1030 -- overflow raising layout error.
1032 function Page
(File
: in File_Type
) return Positive_Count
is
1034 FIO
.Check_File_Open
(AP
(File
));
1038 function Page
return Positive_Count
is
1040 return Page
(Current_Out
);
1047 function Page_Length
(File
: in File_Type
) return Count
is
1049 FIO
.Check_Write_Status
(AP
(File
));
1050 return File
.Page_Length
;
1053 function Page_Length
return Count
is
1055 return Page_Length
(Current_Out
);
1063 (File
: in File_Type
;
1064 Item
: in Wide_Character)
1066 procedure Out_Char
(C
: Character);
1067 -- Procedure to output one character of a wide character sequence
1069 procedure Out_Char
(C
: Character) is
1071 Putc
(Character'Pos (C
), File
);
1074 procedure WC_Out
is new Wide_Char_To_Char_Sequence
(Out_Char
);
1077 WC_Out
(Item
, File
.WC_Method
);
1078 File
.Col
:= File
.Col
+ 1;
1081 procedure Put
(Item
: in Wide_Character) is
1083 Put
(Current_Out
, Item
);
1091 (File
: in File_Type
;
1092 Item
: in Wide_String)
1095 for J
in Item
'Range loop
1096 Put
(File
, Item
(J
));
1100 procedure Put
(Item
: in Wide_String) is
1102 Put
(Current_Out
, Item
);
1110 (File
: in File_Type
;
1111 Item
: in Wide_String)
1118 procedure Put_Line
(Item
: in Wide_String) is
1120 Put
(Current_Out
, Item
);
1121 New_Line
(Current_Out
);
1128 procedure Putc
(ch
: int
; File
: File_Type
) is
1130 if fputc
(ch
, File
.Stream
) = EOF
then
1139 -- This is the primitive Stream Read routine, used when a Text_IO file
1140 -- is treated directly as a stream using Text_IO.Streams.Stream.
1143 (File
: in out Wide_Text_AFCB
;
1144 Item
: out Stream_Element_Array
;
1145 Last
: out Stream_Element_Offset
)
1150 -- Need to deal with Before_Wide_Character ???
1152 if File
.Mode
/= FCB
.In_File
then
1156 -- Deal with case where our logical and physical position do not match
1157 -- because of being after an LM or LM-PM sequence when in fact we are
1158 -- logically positioned before it.
1160 if File
.Before_LM
then
1162 -- If we are before a PM, then it is possible for a stream read
1163 -- to leave us after the LM and before the PM, which is a bit
1164 -- odd. The easiest way to deal with this is to unget the PM,
1165 -- so we are indeed positioned between the characters. This way
1166 -- further stream read operations will work correctly, and the
1167 -- effect on text processing is a little weird, but what can
1168 -- be expected if stream and text input are mixed this way?
1170 if File
.Before_LM_PM
then
1171 ch
:= ungetc
(PM
, File
.Stream
);
1172 File
.Before_LM_PM
:= False;
1175 File
.Before_LM
:= False;
1177 Item
(Item
'First) := Stream_Element
(Character'Pos (ASCII
.LF
));
1179 if Item
'Length = 1 then
1185 Stream_Element_Offset
1186 (fread
(buffer
=> Item
'Address,
1187 index
=> size_t
(Item
'First + 1),
1189 count
=> Item
'Length - 1,
1190 stream
=> File
.Stream
));
1196 -- Now we do the read. Since this is a text file, it is normally in
1197 -- text mode, but stream data must be read in binary mode, so we
1198 -- temporarily set binary mode for the read, resetting it after.
1199 -- These calls have no effect in a system (like Unix) where there is
1200 -- no distinction between text and binary files.
1202 set_binary_mode
(fileno
(File
.Stream
));
1206 Stream_Element_Offset
1207 (fread
(Item
'Address, 1, Item
'Length, File
.Stream
)) - 1;
1209 if Last
< Item
'Last then
1210 if ferror
(File
.Stream
) /= 0 then
1215 set_text_mode
(fileno
(File
.Stream
));
1223 (File
: in out File_Type
;
1224 Mode
: in File_Mode
)
1227 -- Don't allow change of mode for current file (RM A.10.2(5))
1229 if (File
= Current_In
or else
1230 File
= Current_Out
or else
1231 File
= Current_Error
)
1232 and then To_FCB
(Mode
) /= File
.Mode
1237 Terminate_Line
(File
);
1238 FIO
.Reset
(AP
(File
), To_FCB
(Mode
));
1242 File
.Line_Length
:= 0;
1243 File
.Page_Length
:= 0;
1244 File
.Before_LM
:= False;
1245 File
.Before_LM_PM
:= False;
1248 procedure Reset
(File
: in out File_Type
) is
1250 Terminate_Line
(File
);
1251 FIO
.Reset
(AP
(File
));
1255 File
.Line_Length
:= 0;
1256 File
.Page_Length
:= 0;
1257 File
.Before_LM
:= False;
1258 File
.Before_LM_PM
:= False;
1266 (File
: in File_Type
;
1267 To
: in Positive_Count
)
1272 -- Raise Constraint_Error if out of range value. The reason for this
1273 -- explicit test is that we don't want junk values around, even if
1274 -- checks are off in the caller.
1276 if To
not in Positive_Count
then
1277 raise Constraint_Error
;
1280 FIO
.Check_File_Open
(AP
(File
));
1282 if To
= File
.Col
then
1286 if Mode
(File
) >= Out_File
then
1287 if File
.Line_Length
/= 0 and then To
> File
.Line_Length
then
1291 if To
< File
.Col
then
1295 while File
.Col
< To
loop
1307 File
.Line
:= File
.Line
+ 1;
1310 elsif ch
= PM
and then File
.Is_Regular_File
then
1311 File
.Page
:= File
.Page
+ 1;
1315 elsif To
= File
.Col
then
1320 File
.Col
:= File
.Col
+ 1;
1326 procedure Set_Col
(To
: in Positive_Count
) is
1328 Set_Col
(Current_Out
, To
);
1335 procedure Set_Error
(File
: in File_Type
) is
1337 FIO
.Check_Write_Status
(AP
(File
));
1338 Current_Err
:= File
;
1345 procedure Set_Input
(File
: in File_Type
) is
1347 FIO
.Check_Read_Status
(AP
(File
));
1356 (File
: in File_Type
;
1357 To
: in Positive_Count
)
1360 -- Raise Constraint_Error if out of range value. The reason for this
1361 -- explicit test is that we don't want junk values around, even if
1362 -- checks are off in the caller.
1364 if To
not in Positive_Count
then
1365 raise Constraint_Error
;
1368 FIO
.Check_File_Open
(AP
(File
));
1370 if To
= File
.Line
then
1374 if Mode
(File
) >= Out_File
then
1375 if File
.Page_Length
/= 0 and then To
> File
.Page_Length
then
1379 if To
< File
.Line
then
1383 while File
.Line
< To
loop
1388 while To
/= File
.Line
loop
1394 procedure Set_Line
(To
: in Positive_Count
) is
1396 Set_Line
(Current_Out
, To
);
1399 ---------------------
1400 -- Set_Line_Length --
1401 ---------------------
1403 procedure Set_Line_Length
(File
: in File_Type
; To
: in Count
) is
1405 -- Raise Constraint_Error if out of range value. The reason for this
1406 -- explicit test is that we don't want junk values around, even if
1407 -- checks are off in the caller.
1409 if To
not in Count
then
1410 raise Constraint_Error
;
1413 FIO
.Check_Write_Status
(AP
(File
));
1414 File
.Line_Length
:= To
;
1415 end Set_Line_Length
;
1417 procedure Set_Line_Length
(To
: in Count
) is
1419 Set_Line_Length
(Current_Out
, To
);
1420 end Set_Line_Length
;
1426 procedure Set_Output
(File
: in File_Type
) is
1428 FIO
.Check_Write_Status
(AP
(File
));
1429 Current_Out
:= File
;
1432 ---------------------
1433 -- Set_Page_Length --
1434 ---------------------
1436 procedure Set_Page_Length
(File
: in File_Type
; To
: in Count
) is
1438 -- Raise Constraint_Error if out of range value. The reason for this
1439 -- explicit test is that we don't want junk values around, even if
1440 -- checks are off in the caller.
1442 if To
not in Count
then
1443 raise Constraint_Error
;
1446 FIO
.Check_Write_Status
(AP
(File
));
1447 File
.Page_Length
:= To
;
1448 end Set_Page_Length
;
1450 procedure Set_Page_Length
(To
: in Count
) is
1452 Set_Page_Length
(Current_Out
, To
);
1453 end Set_Page_Length
;
1459 procedure Set_WCEM
(File
: in out File_Type
) is
1464 File
.WC_Method
:= WCEM_Brackets
;
1465 FIO
.Form_Parameter
(File
.Form
.all, "wcem", Start
, Stop
);
1468 File
.WC_Method
:= WCEM_Brackets
;
1470 elsif Start
/= 0 then
1471 if Stop
= Start
then
1472 for J
in WC_Encoding_Letters
'Range loop
1473 if File
.Form
(Start
) = WC_Encoding_Letters
(J
) then
1474 File
.WC_Method
:= J
;
1481 Raise_Exception
(Use_Error
'Identity, "invalid WCEM form parameter");
1490 (File
: in File_Type
;
1491 Spacing
: in Positive_Count
:= 1)
1496 -- Raise Constraint_Error if out of range value. The reason for this
1497 -- explicit test is that we don't want junk values around, even if
1498 -- checks are off in the caller.
1500 if Spacing
not in Positive_Count
then
1501 raise Constraint_Error
;
1504 FIO
.Check_Read_Status
(AP
(File
));
1506 for L
in 1 .. Spacing
loop
1507 if File
.Before_LM
then
1508 File
.Before_LM
:= False;
1509 File
.Before_LM_PM
:= False;
1514 -- If at end of file now, then immediately raise End_Error. Note
1515 -- that we can never be positioned between a line mark and a page
1516 -- mark, so if we are at the end of file, we cannot logically be
1517 -- before the implicit page mark that is at the end of the file.
1519 -- For the same reason, we do not need an explicit check for a
1520 -- page mark. If there is a FF in the middle of a line, the file
1521 -- is not in canonical format and we do not care about the page
1522 -- numbers for files other than ones in canonical format.
1528 -- If not at end of file, then loop till we get to an LM or EOF.
1529 -- The latter case happens only in non-canonical files where the
1530 -- last line is not terminated by LM, but we don't want to blow
1531 -- up for such files, so we assume an implicit LM in this case.
1534 exit when ch
= LM
or ch
= EOF
;
1539 -- We have got past a line mark, now, for a regular file only,
1540 -- see if a page mark immediately follows this line mark and
1541 -- if so, skip past the page mark as well. We do not do this
1542 -- for non-regular files, since it would cause an undesirable
1543 -- wait for an additional character.
1546 File
.Line
:= File
.Line
+ 1;
1548 if File
.Before_LM_PM
then
1549 File
.Page
:= File
.Page
+ 1;
1551 File
.Before_LM_PM
:= False;
1553 elsif File
.Is_Regular_File
then
1556 -- Page mark can be explicit, or implied at the end of the file
1558 if (ch
= PM
or else ch
= EOF
)
1559 and then File
.Is_Regular_File
1561 File
.Page
:= File
.Page
+ 1;
1570 File
.Before_Wide_Character
:= False;
1573 procedure Skip_Line
(Spacing
: in Positive_Count
:= 1) is
1575 Skip_Line
(Current_In
, Spacing
);
1582 procedure Skip_Page
(File
: in File_Type
) is
1586 FIO
.Check_Read_Status
(AP
(File
));
1588 -- If at page mark already, just skip it
1590 if File
.Before_LM_PM
then
1591 File
.Before_LM
:= False;
1592 File
.Before_LM_PM
:= False;
1593 File
.Page
:= File
.Page
+ 1;
1599 -- This is a bit tricky, if we are logically before an LM then
1600 -- it is not an error if we are at an end of file now, since we
1601 -- are not really at it.
1603 if File
.Before_LM
then
1604 File
.Before_LM
:= False;
1605 File
.Before_LM_PM
:= False;
1608 -- Otherwise we do raise End_Error if we are at the end of file now
1618 -- Now we can just rumble along to the next page mark, or to the
1619 -- end of file, if that comes first. The latter case happens when
1620 -- the page mark is implied at the end of file.
1624 or else (ch
= PM
and then File
.Is_Regular_File
);
1628 File
.Page
:= File
.Page
+ 1;
1631 File
.Before_Wide_Character
:= False;
1634 procedure Skip_Page
is
1636 Skip_Page
(Current_In
);
1639 --------------------
1640 -- Standard_Error --
1641 --------------------
1643 function Standard_Error
return File_Type
is
1645 return Standard_Err
;
1648 function Standard_Error
return File_Access
is
1650 return Standard_Err
'Access;
1653 --------------------
1654 -- Standard_Input --
1655 --------------------
1657 function Standard_Input
return File_Type
is
1662 function Standard_Input
return File_Access
is
1664 return Standard_In
'Access;
1667 ---------------------
1668 -- Standard_Output --
1669 ---------------------
1671 function Standard_Output
return File_Type
is
1673 return Standard_Out
;
1674 end Standard_Output
;
1676 function Standard_Output
return File_Access
is
1678 return Standard_Out
'Access;
1679 end Standard_Output
;
1681 --------------------
1682 -- Terminate_Line --
1683 --------------------
1685 procedure Terminate_Line
(File
: File_Type
) is
1687 FIO
.Check_File_Open
(AP
(File
));
1689 -- For file other than In_File, test for needing to terminate last line
1691 if Mode
(File
) /= In_File
then
1693 -- If not at start of line definition need new line
1695 if File
.Col
/= 1 then
1698 -- For files other than standard error and standard output, we
1699 -- make sure that an empty file has a single line feed, so that
1700 -- it is properly formatted. We avoid this for the standard files
1701 -- because it is too much of a nuisance to have these odd line
1702 -- feeds when nothing has been written to the file.
1704 elsif (File
/= Standard_Err
and then File
/= Standard_Out
)
1705 and then (File
.Line
= 1 and then File
.Page
= 1)
1716 procedure Ungetc
(ch
: int
; File
: File_Type
) is
1719 if ungetc
(ch
, File
.Stream
) = EOF
then
1729 -- This is the primitive Stream Write routine, used when a Text_IO file
1730 -- is treated directly as a stream using Text_IO.Streams.Stream.
1733 (File
: in out Wide_Text_AFCB
;
1734 Item
: in Stream_Element_Array
)
1736 Siz
: constant size_t
:= Item
'Length;
1739 if File
.Mode
= FCB
.In_File
then
1743 -- Now we do the write. Since this is a text file, it is normally in
1744 -- text mode, but stream data must be written in binary mode, so we
1745 -- temporarily set binary mode for the write, resetting it after.
1746 -- These calls have no effect in a system (like Unix) where there is
1747 -- no distinction between text and binary files.
1749 set_binary_mode
(fileno
(File
.Stream
));
1751 if fwrite
(Item
'Address, 1, Siz
, File
.Stream
) /= Siz
then
1755 set_text_mode
(fileno
(File
.Stream
));
1758 -- Use "preallocated" strings to avoid calling "new" during the
1759 -- elaboration of the run time. This is needed in the tasking case to
1760 -- avoid calling Task_Lock too early. A filename is expected to end with
1761 -- a null character in the runtime, here the null characters are added
1762 -- just to have a correct filename length.
1764 Err_Name
: aliased String := "*stderr" & ASCII
.Nul
;
1765 In_Name
: aliased String := "*stdin" & ASCII
.Nul
;
1766 Out_Name
: aliased String := "*stdout" & ASCII
.Nul
;
1769 -------------------------------
1770 -- Initialize Standard Files --
1771 -------------------------------
1773 for J
in WC_Encoding_Method
loop
1774 if WC_Encoding
= WC_Encoding_Letters
(J
) then
1779 -- Note: the names in these files are bogus, and probably it would be
1780 -- better for these files to have no names, but the ACVC test insist!
1781 -- We use names that are bound to fail in open etc.
1783 Standard_Err
.Stream
:= stderr
;
1784 Standard_Err
.Name
:= Err_Name
'Access;
1785 Standard_Err
.Form
:= Null_Str
'Unrestricted_Access;
1786 Standard_Err
.Mode
:= FCB
.Out_File
;
1787 Standard_Err
.Is_Regular_File
:= is_regular_file
(fileno
(stderr
)) /= 0;
1788 Standard_Err
.Is_Temporary_File
:= False;
1789 Standard_Err
.Is_System_File
:= True;
1790 Standard_Err
.Is_Text_File
:= True;
1791 Standard_Err
.Access_Method
:= 'T';
1792 Standard_Err
.WC_Method
:= Default_WCEM
;
1794 Standard_In
.Stream
:= stdin
;
1795 Standard_In
.Name
:= In_Name
'Access;
1796 Standard_In
.Form
:= Null_Str
'Unrestricted_Access;
1797 Standard_In
.Mode
:= FCB
.In_File
;
1798 Standard_In
.Is_Regular_File
:= is_regular_file
(fileno
(stdin
)) /= 0;
1799 Standard_In
.Is_Temporary_File
:= False;
1800 Standard_In
.Is_System_File
:= True;
1801 Standard_In
.Is_Text_File
:= True;
1802 Standard_In
.Access_Method
:= 'T';
1803 Standard_In
.WC_Method
:= Default_WCEM
;
1805 Standard_Out
.Stream
:= stdout
;
1806 Standard_Out
.Name
:= Out_Name
'Access;
1807 Standard_Out
.Form
:= Null_Str
'Unrestricted_Access;
1808 Standard_Out
.Mode
:= FCB
.Out_File
;
1809 Standard_Out
.Is_Regular_File
:= is_regular_file
(fileno
(stdout
)) /= 0;
1810 Standard_Out
.Is_Temporary_File
:= False;
1811 Standard_Out
.Is_System_File
:= True;
1812 Standard_Out
.Is_Text_File
:= True;
1813 Standard_Out
.Access_Method
:= 'T';
1814 Standard_Out
.WC_Method
:= Default_WCEM
;
1816 FIO
.Chain_File
(AP
(Standard_In
));
1817 FIO
.Chain_File
(AP
(Standard_Out
));
1818 FIO
.Chain_File
(AP
(Standard_Err
));
1820 FIO
.Make_Unbuffered
(AP
(Standard_Out
));
1821 FIO
.Make_Unbuffered
(AP
(Standard_Err
));
1823 end Ada
.Wide_Text_IO
;