1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . W I D E _ T E X T _ I O --
9 -- Copyright (C) 1992-2003 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
;
41 with System
.WCh_Cnv
; use System
.WCh_Cnv
;
42 with System
.WCh_Con
; use System
.WCh_Con
;
43 with Unchecked_Conversion
;
44 with Unchecked_Deallocation
;
46 pragma Elaborate_All
(System
.File_IO
);
47 -- Needed because of calls to Chain_File in package body elaboration
49 package body Ada
.Wide_Text_IO
is
51 package FIO
renames System
.File_IO
;
53 subtype AP
is FCB
.AFCB_Ptr
;
55 function To_FCB
is new Unchecked_Conversion
(File_Mode
, FCB
.File_Mode
);
56 function To_TIO
is new Unchecked_Conversion
(FCB
.File_Mode
, File_Mode
);
57 use type FCB
.File_Mode
;
59 use type System
.CRTL
.size_t
;
61 WC_Encoding
: Character;
62 pragma Import
(C
, WC_Encoding
, "__gl_wc_encoding");
64 -----------------------
65 -- Local Subprograms --
66 -----------------------
68 function Getc_Immed
(File
: in File_Type
) return int
;
69 -- This routine is identical to Getc, except that the read is done in
70 -- Get_Immediate mode (i.e. without waiting for a line return).
72 function Get_Wide_Char_Immed
75 return Wide_Character;
76 -- This routine is identical to Get_Wide_Char, except that the reads are
77 -- done in Get_Immediate mode (i.e. without waiting for a line return).
79 procedure Set_WCEM
(File
: in out File_Type
);
80 -- Called by Open and Create to set the wide character encoding method
81 -- for the file, processing a WCEM form parameter if one is present.
82 -- File is IN OUT because it may be closed in case of an error.
88 function AFCB_Allocate
89 (Control_Block
: Wide_Text_AFCB
)
92 pragma Unreferenced
(Control_Block
);
95 return new Wide_Text_AFCB
;
102 procedure AFCB_Close
(File
: access Wide_Text_AFCB
) is
104 -- If the file being closed is one of the current files, then close
105 -- the corresponding current file. It is not clear that this action
106 -- is required (RM A.10.3(23)) but it seems reasonable, and besides
107 -- ACVC test CE3208A expects this behavior.
109 if File_Type
(File
) = Current_In
then
111 elsif File_Type
(File
) = Current_Out
then
113 elsif File_Type
(File
) = Current_Err
then
117 Terminate_Line
(File_Type
(File
));
124 procedure AFCB_Free
(File
: access Wide_Text_AFCB
) is
125 type FCB_Ptr
is access all Wide_Text_AFCB
;
126 FT
: FCB_Ptr
:= FCB_Ptr
(File
);
128 procedure Free
is new Unchecked_Deallocation
(Wide_Text_AFCB
, FCB_Ptr
);
138 procedure Close
(File
: in out File_Type
) is
140 FIO
.Close
(AP
(File
));
147 -- Note: we assume that it is impossible in practice for the column
148 -- to exceed the value of Count'Last, i.e. no check is required for
149 -- overflow raising layout error.
151 function Col
(File
: in File_Type
) return Positive_Count
is
153 FIO
.Check_File_Open
(AP
(File
));
157 function Col
return Positive_Count
is
159 return Col
(Current_Out
);
167 (File
: in out File_Type
;
168 Mode
: in File_Mode
:= Out_File
;
169 Name
: in String := "";
170 Form
: in String := "")
172 Dummy_File_Control_Block
: Wide_Text_AFCB
;
173 pragma Warnings
(Off
, Dummy_File_Control_Block
);
174 -- Yes, we know this is never assigned a value, only the tag
175 -- is used for dispatching purposes, so that's expected.
178 FIO
.Open
(File_Ptr
=> AP
(File
),
179 Dummy_FCB
=> Dummy_File_Control_Block
,
180 Mode
=> To_FCB
(Mode
),
193 function Current_Error
return File_Type
is
198 function Current_Error
return File_Access
is
200 return Current_Err
'Access;
207 function Current_Input
return File_Type
is
212 function Current_Input
return File_Access
is
214 return Current_In
'Access;
221 function Current_Output
return File_Type
is
226 function Current_Output
return File_Access
is
228 return Current_Out
'Access;
235 procedure Delete
(File
: in out File_Type
) is
237 FIO
.Delete
(AP
(File
));
244 function End_Of_File
(File
: in File_Type
) return Boolean is
248 FIO
.Check_Read_Status
(AP
(File
));
250 if File
.Before_Wide_Character
then
253 elsif File
.Before_LM
then
255 if File
.Before_LM_PM
then
256 return Nextc
(File
) = EOF
;
270 File
.Before_LM
:= True;
274 -- Here we are just past the line mark with Before_LM set so that we
275 -- do not have to try to back up past the LM, thus avoiding the need
276 -- to back up more than one character.
283 elsif ch
= PM
and then File
.Is_Regular_File
then
284 File
.Before_LM_PM
:= True;
285 return Nextc
(File
) = EOF
;
287 -- Here if neither EOF nor PM followed end of line
296 function End_Of_File
return Boolean is
298 return End_Of_File
(Current_In
);
305 function End_Of_Line
(File
: in File_Type
) return Boolean is
309 FIO
.Check_Read_Status
(AP
(File
));
311 if File
.Before_Wide_Character
then
314 elsif File
.Before_LM
then
330 function End_Of_Line
return Boolean is
332 return End_Of_Line
(Current_In
);
339 function End_Of_Page
(File
: in File_Type
) return Boolean is
343 FIO
.Check_Read_Status
(AP
(File
));
345 if not File
.Is_Regular_File
then
348 elsif File
.Before_Wide_Character
then
351 elsif File
.Before_LM
then
352 if File
.Before_LM_PM
then
367 File
.Before_LM
:= True;
371 -- Here we are just past the line mark with Before_LM set so that we
372 -- do not have to try to back up past the LM, thus avoiding the need
373 -- to back up more than one character.
377 return ch
= PM
or else ch
= EOF
;
380 function End_Of_Page
return Boolean is
382 return End_Of_Page
(Current_In
);
389 procedure Flush
(File
: in File_Type
) is
391 FIO
.Flush
(AP
(File
));
403 function Form
(File
: in File_Type
) return String is
405 return FIO
.Form
(AP
(File
));
413 (File
: in File_Type
;
414 Item
: out Wide_Character)
419 FIO
.Check_Read_Status
(AP
(File
));
421 if File
.Before_Wide_Character
then
422 File
.Before_Wide_Character
:= False;
423 Item
:= File
.Saved_Wide_Character
;
426 Get_Character
(File
, C
);
427 Item
:= Get_Wide_Char
(C
, File
);
431 procedure Get
(Item
: out Wide_Character) is
433 Get
(Current_In
, Item
);
437 (File
: in File_Type
;
438 Item
: out Wide_String)
441 for J
in Item
'Range loop
442 Get
(File
, Item
(J
));
446 procedure Get
(Item
: out Wide_String) is
448 Get
(Current_In
, Item
);
455 procedure Get_Character
456 (File
: in File_Type
;
457 Item
: out Character)
462 if File
.Before_LM
then
463 File
.Before_LM
:= False;
464 File
.Before_LM_PM
:= False;
467 if File
.Before_LM_PM
then
469 File
.Page
:= File
.Page
+ 1;
470 File
.Before_LM_PM
:= False;
473 File
.Line
:= File
.Line
+ 1;
484 File
.Line
:= File
.Line
+ 1;
487 elsif ch
= PM
and then File
.Is_Regular_File
then
488 File
.Page
:= File
.Page
+ 1;
492 Item
:= Character'Val (ch
);
493 File
.Col
:= File
.Col
+ 1;
503 procedure Get_Immediate
504 (File
: in File_Type
;
505 Item
: out Wide_Character)
510 FIO
.Check_Read_Status
(AP
(File
));
512 if File
.Before_Wide_Character
then
513 File
.Before_Wide_Character
:= False;
514 Item
:= File
.Saved_Wide_Character
;
516 elsif File
.Before_LM
then
517 File
.Before_LM
:= False;
518 File
.Before_LM_PM
:= False;
519 Item
:= Wide_Character'Val (LM
);
522 ch
:= Getc_Immed
(File
);
527 Item
:= Get_Wide_Char_Immed
(Character'Val (ch
), File
);
532 procedure Get_Immediate
533 (Item
: out Wide_Character)
536 Get_Immediate
(Current_In
, Item
);
539 procedure Get_Immediate
540 (File
: in File_Type
;
541 Item
: out Wide_Character;
542 Available
: out Boolean)
547 FIO
.Check_Read_Status
(AP
(File
));
550 if File
.Before_Wide_Character
then
551 File
.Before_Wide_Character
:= False;
552 Item
:= File
.Saved_Wide_Character
;
554 elsif File
.Before_LM
then
555 File
.Before_LM
:= False;
556 File
.Before_LM_PM
:= False;
557 Item
:= Wide_Character'Val (LM
);
560 ch
:= Getc_Immed
(File
);
565 Item
:= Get_Wide_Char_Immed
(Character'Val (ch
), File
);
570 procedure Get_Immediate
571 (Item
: out Wide_Character;
572 Available
: out Boolean)
575 Get_Immediate
(Current_In
, Item
, Available
);
583 (File
: in File_Type
;
584 Item
: out Wide_String;
588 FIO
.Check_Read_Status
(AP
(File
));
589 Last
:= Item
'First - 1;
591 -- Immediate exit for null string, this is a case in which we do not
592 -- need to test for end of file and we do not skip a line mark under
593 -- any circumstances.
595 if Last
>= Item
'Last then
599 -- Here we have at least one character, if we are immediately before
600 -- a line mark, then we will just skip past it storing no characters.
602 if File
.Before_LM
then
603 File
.Before_LM
:= False;
604 File
.Before_LM_PM
:= False;
606 -- Otherwise we need to read some characters
609 -- If we are at the end of file now, it means we are trying to
610 -- skip a file terminator and we raise End_Error (RM A.10.7(20))
612 if Nextc
(File
) = EOF
then
616 -- Loop through characters in string
619 -- Exit the loop if read is terminated by encountering line mark
620 -- Note that the use of Skip_Line here ensures we properly deal
621 -- with setting the page and line numbers.
623 if End_Of_Line
(File
) then
628 -- Otherwise store the character, note that we know that ch is
629 -- something other than LM or EOF. It could possibly be a page
630 -- mark if there is a stray page mark in the middle of a line,
631 -- but this is not an official page mark in any case, since
632 -- official page marks can only follow a line mark. The whole
633 -- page business is pretty much nonsense anyway, so we do not
634 -- want to waste time trying to make sense out of non-standard
635 -- page marks in the file! This means that the behavior of
636 -- Get_Line is different from repeated Get of a character, but
637 -- that's too bad. We only promise that page numbers etc make
638 -- sense if the file is formatted in a standard manner.
640 -- Note: we do not adjust the column number because it is quicker
641 -- to adjust it once at the end of the operation than incrementing
642 -- it each time around the loop.
645 Get
(File
, Item
(Last
));
647 -- All done if the string is full, this is the case in which
648 -- we do not skip the following line mark. We need to adjust
649 -- the column number in this case.
651 if Last
= Item
'Last then
652 File
.Col
:= File
.Col
+ Count
(Item
'Length);
656 -- Exit from the loop if we are at the end of file. This happens
657 -- if we have a last line that is not terminated with a line mark.
658 -- In this case we consider that there is an implied line mark;
659 -- this is a non-standard file, but we will treat it nicely.
661 exit when Nextc
(File
) = EOF
;
667 (Item
: out Wide_String;
671 Get_Line
(Current_In
, Item
, Last
);
678 function Get_Wide_Char
681 return Wide_Character
683 function In_Char
return Character;
684 -- Function used to obtain additional characters it the wide character
685 -- sequence is more than one character long.
687 function In_Char
return Character is
688 ch
: constant Integer := Getc
(File
);
694 return Character'Val (ch
);
698 function WC_In
is new Char_Sequence_To_Wide_Char
(In_Char
);
701 return WC_In
(C
, File
.WC_Method
);
704 -------------------------
705 -- Get_Wide_Char_Immed --
706 -------------------------
708 function Get_Wide_Char_Immed
711 return Wide_Character
713 function In_Char
return Character;
714 -- Function used to obtain additional characters it the wide character
715 -- sequence is more than one character long.
717 function In_Char
return Character is
718 ch
: constant Integer := Getc_Immed
(File
);
724 return Character'Val (ch
);
728 function WC_In
is new Char_Sequence_To_Wide_Char
(In_Char
);
731 return WC_In
(C
, File
.WC_Method
);
732 end Get_Wide_Char_Immed
;
738 function Getc
(File
: File_Type
) return int
is
742 ch
:= fgetc
(File
.Stream
);
744 if ch
= EOF
and then ferror
(File
.Stream
) /= 0 then
755 function Getc_Immed
(File
: in File_Type
) return int
is
759 procedure getc_immediate
760 (stream
: FILEs
; ch
: out int
; end_of_file
: out int
);
761 pragma Import
(C
, getc_immediate
, "getc_immediate");
764 FIO
.Check_Read_Status
(AP
(File
));
766 if File
.Before_LM
then
767 File
.Before_LM
:= False;
768 File
.Before_LM_PM
:= False;
772 getc_immediate
(File
.Stream
, ch
, end_of_file
);
774 if ferror
(File
.Stream
) /= 0 then
776 elsif end_of_file
/= 0 then
788 function Is_Open
(File
: in File_Type
) return Boolean is
790 return FIO
.Is_Open
(AP
(File
));
797 -- Note: we assume that it is impossible in practice for the line
798 -- to exceed the value of Count'Last, i.e. no check is required for
799 -- overflow raising layout error.
801 function Line
(File
: in File_Type
) return Positive_Count
is
803 FIO
.Check_File_Open
(AP
(File
));
807 function Line
return Positive_Count
is
809 return Line
(Current_Out
);
816 function Line_Length
(File
: in File_Type
) return Count
is
818 FIO
.Check_Write_Status
(AP
(File
));
819 return File
.Line_Length
;
822 function Line_Length
return Count
is
824 return Line_Length
(Current_Out
);
832 (File
: in File_Type
;
833 Item
: out Wide_Character;
834 End_Of_Line
: out Boolean)
838 -- Start of processing for Look_Ahead
841 FIO
.Check_Read_Status
(AP
(File
));
843 -- If we are logically before a line mark, we can return immediately
845 if File
.Before_LM
then
847 Item
:= Wide_Character'Val (0);
849 -- If we are before a wide character, just return it (this happens
850 -- if there are two calls to Look_Ahead in a row).
852 elsif File
.Before_Wide_Character
then
853 End_Of_Line
:= False;
854 Item
:= File
.Saved_Wide_Character
;
856 -- otherwise we must read a character from the input stream
863 or else (ch
= EOF
and then File
.Is_Regular_File
)
867 Item
:= Wide_Character'Val (0);
869 -- If the character is in the range 16#0000# to 16#007F# it stands
870 -- for itself and occupies a single byte, so we can unget it with
873 elsif ch
<= 16#
0080#
then
874 End_Of_Line
:= False;
876 Item
:= Wide_Character'Val (ch
);
878 -- For a character above this range, we read the character, using
879 -- the Get_Wide_Char routine. It may well occupy more than one byte
880 -- so we can't put it back with ungetc. Instead we save it in the
881 -- control block, setting a flag that everyone interested in reading
882 -- characters must test before reading the stream.
885 Item
:= Get_Wide_Char
(Character'Val (ch
), File
);
886 End_Of_Line
:= False;
887 File
.Saved_Wide_Character
:= Item
;
888 File
.Before_Wide_Character
:= True;
894 (Item
: out Wide_Character;
895 End_Of_Line
: out Boolean)
898 Look_Ahead
(Current_In
, Item
, End_Of_Line
);
905 function Mode
(File
: in File_Type
) return File_Mode
is
907 return To_TIO
(FIO
.Mode
(AP
(File
)));
914 function Name
(File
: in File_Type
) return String is
916 return FIO
.Name
(AP
(File
));
924 (File
: in File_Type
;
925 Spacing
: in Positive_Count
:= 1)
928 -- Raise Constraint_Error if out of range value. The reason for this
929 -- explicit test is that we don't want junk values around, even if
930 -- checks are off in the caller.
932 if Spacing
not in Positive_Count
then
933 raise Constraint_Error
;
936 FIO
.Check_Write_Status
(AP
(File
));
938 for K
in 1 .. Spacing
loop
940 File
.Line
:= File
.Line
+ 1;
942 if File
.Page_Length
/= 0
943 and then File
.Line
> File
.Page_Length
947 File
.Page
:= File
.Page
+ 1;
954 procedure New_Line
(Spacing
: in Positive_Count
:= 1) is
956 New_Line
(Current_Out
, Spacing
);
963 procedure New_Page
(File
: in File_Type
) is
965 FIO
.Check_Write_Status
(AP
(File
));
967 if File
.Col
/= 1 or else File
.Line
= 1 then
972 File
.Page
:= File
.Page
+ 1;
977 procedure New_Page
is
979 New_Page
(Current_Out
);
986 function Nextc
(File
: File_Type
) return int
is
990 ch
:= fgetc
(File
.Stream
);
993 if ferror
(File
.Stream
) /= 0 then
998 if ungetc
(ch
, File
.Stream
) = EOF
then
1011 (File
: in out File_Type
;
1012 Mode
: in File_Mode
;
1014 Form
: in String := "")
1016 Dummy_File_Control_Block
: Wide_Text_AFCB
;
1017 pragma Warnings
(Off
, Dummy_File_Control_Block
);
1018 -- Yes, we know this is never assigned a value, only the tag
1019 -- is used for dispatching purposes, so that's expected.
1022 FIO
.Open
(File_Ptr
=> AP
(File
),
1023 Dummy_FCB
=> Dummy_File_Control_Block
,
1024 Mode
=> To_FCB
(Mode
),
1037 -- Note: we assume that it is impossible in practice for the page
1038 -- to exceed the value of Count'Last, i.e. no check is required for
1039 -- overflow raising layout error.
1041 function Page
(File
: in File_Type
) return Positive_Count
is
1043 FIO
.Check_File_Open
(AP
(File
));
1047 function Page
return Positive_Count
is
1049 return Page
(Current_Out
);
1056 function Page_Length
(File
: in File_Type
) return Count
is
1058 FIO
.Check_Write_Status
(AP
(File
));
1059 return File
.Page_Length
;
1062 function Page_Length
return Count
is
1064 return Page_Length
(Current_Out
);
1072 (File
: in File_Type
;
1073 Item
: in Wide_Character)
1075 procedure Out_Char
(C
: Character);
1076 -- Procedure to output one character of a wide character sequence
1078 procedure Out_Char
(C
: Character) is
1080 Putc
(Character'Pos (C
), File
);
1083 procedure WC_Out
is new Wide_Char_To_Char_Sequence
(Out_Char
);
1086 WC_Out
(Item
, File
.WC_Method
);
1087 File
.Col
:= File
.Col
+ 1;
1090 procedure Put
(Item
: in Wide_Character) is
1092 Put
(Current_Out
, Item
);
1100 (File
: in File_Type
;
1101 Item
: in Wide_String)
1104 for J
in Item
'Range loop
1105 Put
(File
, Item
(J
));
1109 procedure Put
(Item
: in Wide_String) is
1111 Put
(Current_Out
, Item
);
1119 (File
: in File_Type
;
1120 Item
: in Wide_String)
1127 procedure Put_Line
(Item
: in Wide_String) is
1129 Put
(Current_Out
, Item
);
1130 New_Line
(Current_Out
);
1137 procedure Putc
(ch
: int
; File
: File_Type
) is
1139 if fputc
(ch
, File
.Stream
) = EOF
then
1148 -- This is the primitive Stream Read routine, used when a Text_IO file
1149 -- is treated directly as a stream using Text_IO.Streams.Stream.
1152 (File
: in out Wide_Text_AFCB
;
1153 Item
: out Stream_Element_Array
;
1154 Last
: out Stream_Element_Offset
)
1157 pragma Unreferenced
(Discard_ch
);
1160 -- Need to deal with Before_Wide_Character ???
1162 if File
.Mode
/= FCB
.In_File
then
1166 -- Deal with case where our logical and physical position do not match
1167 -- because of being after an LM or LM-PM sequence when in fact we are
1168 -- logically positioned before it.
1170 if File
.Before_LM
then
1172 -- If we are before a PM, then it is possible for a stream read
1173 -- to leave us after the LM and before the PM, which is a bit
1174 -- odd. The easiest way to deal with this is to unget the PM,
1175 -- so we are indeed positioned between the characters. This way
1176 -- further stream read operations will work correctly, and the
1177 -- effect on text processing is a little weird, but what can
1178 -- be expected if stream and text input are mixed this way?
1180 if File
.Before_LM_PM
then
1181 Discard_ch
:= ungetc
(PM
, File
.Stream
);
1182 File
.Before_LM_PM
:= False;
1185 File
.Before_LM
:= False;
1187 Item
(Item
'First) := Stream_Element
(Character'Pos (ASCII
.LF
));
1189 if Item
'Length = 1 then
1195 Stream_Element_Offset
1196 (fread
(buffer
=> Item
'Address,
1197 index
=> size_t
(Item
'First + 1),
1199 count
=> Item
'Length - 1,
1200 stream
=> File
.Stream
));
1206 -- Now we do the read. Since this is a text file, it is normally in
1207 -- text mode, but stream data must be read in binary mode, so we
1208 -- temporarily set binary mode for the read, resetting it after.
1209 -- These calls have no effect in a system (like Unix) where there is
1210 -- no distinction between text and binary files.
1212 set_binary_mode
(fileno
(File
.Stream
));
1216 Stream_Element_Offset
1217 (fread
(Item
'Address, 1, Item
'Length, File
.Stream
)) - 1;
1219 if Last
< Item
'Last then
1220 if ferror
(File
.Stream
) /= 0 then
1225 set_text_mode
(fileno
(File
.Stream
));
1233 (File
: in out File_Type
;
1234 Mode
: in File_Mode
)
1237 -- Don't allow change of mode for current file (RM A.10.2(5))
1239 if (File
= Current_In
or else
1240 File
= Current_Out
or else
1241 File
= Current_Error
)
1242 and then To_FCB
(Mode
) /= File
.Mode
1247 Terminate_Line
(File
);
1248 FIO
.Reset
(AP
(File
), To_FCB
(Mode
));
1252 File
.Line_Length
:= 0;
1253 File
.Page_Length
:= 0;
1254 File
.Before_LM
:= False;
1255 File
.Before_LM_PM
:= False;
1258 procedure Reset
(File
: in out File_Type
) is
1260 Terminate_Line
(File
);
1261 FIO
.Reset
(AP
(File
));
1265 File
.Line_Length
:= 0;
1266 File
.Page_Length
:= 0;
1267 File
.Before_LM
:= False;
1268 File
.Before_LM_PM
:= False;
1276 (File
: in File_Type
;
1277 To
: in Positive_Count
)
1282 -- Raise Constraint_Error if out of range value. The reason for this
1283 -- explicit test is that we don't want junk values around, even if
1284 -- checks are off in the caller.
1286 if To
not in Positive_Count
then
1287 raise Constraint_Error
;
1290 FIO
.Check_File_Open
(AP
(File
));
1292 if To
= File
.Col
then
1296 if Mode
(File
) >= Out_File
then
1297 if File
.Line_Length
/= 0 and then To
> File
.Line_Length
then
1301 if To
< File
.Col
then
1305 while File
.Col
< To
loop
1317 File
.Line
:= File
.Line
+ 1;
1320 elsif ch
= PM
and then File
.Is_Regular_File
then
1321 File
.Page
:= File
.Page
+ 1;
1325 elsif To
= File
.Col
then
1330 File
.Col
:= File
.Col
+ 1;
1336 procedure Set_Col
(To
: in Positive_Count
) is
1338 Set_Col
(Current_Out
, To
);
1345 procedure Set_Error
(File
: in File_Type
) is
1347 FIO
.Check_Write_Status
(AP
(File
));
1348 Current_Err
:= File
;
1355 procedure Set_Input
(File
: in File_Type
) is
1357 FIO
.Check_Read_Status
(AP
(File
));
1366 (File
: in File_Type
;
1367 To
: in Positive_Count
)
1370 -- Raise Constraint_Error if out of range value. The reason for this
1371 -- explicit test is that we don't want junk values around, even if
1372 -- checks are off in the caller.
1374 if To
not in Positive_Count
then
1375 raise Constraint_Error
;
1378 FIO
.Check_File_Open
(AP
(File
));
1380 if To
= File
.Line
then
1384 if Mode
(File
) >= Out_File
then
1385 if File
.Page_Length
/= 0 and then To
> File
.Page_Length
then
1389 if To
< File
.Line
then
1393 while File
.Line
< To
loop
1398 while To
/= File
.Line
loop
1404 procedure Set_Line
(To
: in Positive_Count
) is
1406 Set_Line
(Current_Out
, To
);
1409 ---------------------
1410 -- Set_Line_Length --
1411 ---------------------
1413 procedure Set_Line_Length
(File
: in File_Type
; To
: in Count
) is
1415 -- Raise Constraint_Error if out of range value. The reason for this
1416 -- explicit test is that we don't want junk values around, even if
1417 -- checks are off in the caller.
1419 if To
not in Count
then
1420 raise Constraint_Error
;
1423 FIO
.Check_Write_Status
(AP
(File
));
1424 File
.Line_Length
:= To
;
1425 end Set_Line_Length
;
1427 procedure Set_Line_Length
(To
: in Count
) is
1429 Set_Line_Length
(Current_Out
, To
);
1430 end Set_Line_Length
;
1436 procedure Set_Output
(File
: in File_Type
) is
1438 FIO
.Check_Write_Status
(AP
(File
));
1439 Current_Out
:= File
;
1442 ---------------------
1443 -- Set_Page_Length --
1444 ---------------------
1446 procedure Set_Page_Length
(File
: in File_Type
; To
: in Count
) is
1448 -- Raise Constraint_Error if out of range value. The reason for this
1449 -- explicit test is that we don't want junk values around, even if
1450 -- checks are off in the caller.
1452 if To
not in Count
then
1453 raise Constraint_Error
;
1456 FIO
.Check_Write_Status
(AP
(File
));
1457 File
.Page_Length
:= To
;
1458 end Set_Page_Length
;
1460 procedure Set_Page_Length
(To
: in Count
) is
1462 Set_Page_Length
(Current_Out
, To
);
1463 end Set_Page_Length
;
1469 procedure Set_WCEM
(File
: in out File_Type
) is
1474 File
.WC_Method
:= WCEM_Brackets
;
1475 FIO
.Form_Parameter
(File
.Form
.all, "wcem", Start
, Stop
);
1478 File
.WC_Method
:= WCEM_Brackets
;
1480 elsif Start
/= 0 then
1481 if Stop
= Start
then
1482 for J
in WC_Encoding_Letters
'Range loop
1483 if File
.Form
(Start
) = WC_Encoding_Letters
(J
) then
1484 File
.WC_Method
:= J
;
1491 Raise_Exception
(Use_Error
'Identity, "invalid WCEM form parameter");
1500 (File
: in File_Type
;
1501 Spacing
: in Positive_Count
:= 1)
1506 -- Raise Constraint_Error if out of range value. The reason for this
1507 -- explicit test is that we don't want junk values around, even if
1508 -- checks are off in the caller.
1510 if Spacing
not in Positive_Count
then
1511 raise Constraint_Error
;
1514 FIO
.Check_Read_Status
(AP
(File
));
1516 for L
in 1 .. Spacing
loop
1517 if File
.Before_LM
then
1518 File
.Before_LM
:= False;
1519 File
.Before_LM_PM
:= False;
1524 -- If at end of file now, then immediately raise End_Error. Note
1525 -- that we can never be positioned between a line mark and a page
1526 -- mark, so if we are at the end of file, we cannot logically be
1527 -- before the implicit page mark that is at the end of the file.
1529 -- For the same reason, we do not need an explicit check for a
1530 -- page mark. If there is a FF in the middle of a line, the file
1531 -- is not in canonical format and we do not care about the page
1532 -- numbers for files other than ones in canonical format.
1538 -- If not at end of file, then loop till we get to an LM or EOF.
1539 -- The latter case happens only in non-canonical files where the
1540 -- last line is not terminated by LM, but we don't want to blow
1541 -- up for such files, so we assume an implicit LM in this case.
1544 exit when ch
= LM
or ch
= EOF
;
1549 -- We have got past a line mark, now, for a regular file only,
1550 -- see if a page mark immediately follows this line mark and
1551 -- if so, skip past the page mark as well. We do not do this
1552 -- for non-regular files, since it would cause an undesirable
1553 -- wait for an additional character.
1556 File
.Line
:= File
.Line
+ 1;
1558 if File
.Before_LM_PM
then
1559 File
.Page
:= File
.Page
+ 1;
1561 File
.Before_LM_PM
:= False;
1563 elsif File
.Is_Regular_File
then
1566 -- Page mark can be explicit, or implied at the end of the file
1568 if (ch
= PM
or else ch
= EOF
)
1569 and then File
.Is_Regular_File
1571 File
.Page
:= File
.Page
+ 1;
1580 File
.Before_Wide_Character
:= False;
1583 procedure Skip_Line
(Spacing
: in Positive_Count
:= 1) is
1585 Skip_Line
(Current_In
, Spacing
);
1592 procedure Skip_Page
(File
: in File_Type
) is
1596 FIO
.Check_Read_Status
(AP
(File
));
1598 -- If at page mark already, just skip it
1600 if File
.Before_LM_PM
then
1601 File
.Before_LM
:= False;
1602 File
.Before_LM_PM
:= False;
1603 File
.Page
:= File
.Page
+ 1;
1609 -- This is a bit tricky, if we are logically before an LM then
1610 -- it is not an error if we are at an end of file now, since we
1611 -- are not really at it.
1613 if File
.Before_LM
then
1614 File
.Before_LM
:= False;
1615 File
.Before_LM_PM
:= False;
1618 -- Otherwise we do raise End_Error if we are at the end of file now
1628 -- Now we can just rumble along to the next page mark, or to the
1629 -- end of file, if that comes first. The latter case happens when
1630 -- the page mark is implied at the end of file.
1634 or else (ch
= PM
and then File
.Is_Regular_File
);
1638 File
.Page
:= File
.Page
+ 1;
1641 File
.Before_Wide_Character
:= False;
1644 procedure Skip_Page
is
1646 Skip_Page
(Current_In
);
1649 --------------------
1650 -- Standard_Error --
1651 --------------------
1653 function Standard_Error
return File_Type
is
1655 return Standard_Err
;
1658 function Standard_Error
return File_Access
is
1660 return Standard_Err
'Access;
1663 --------------------
1664 -- Standard_Input --
1665 --------------------
1667 function Standard_Input
return File_Type
is
1672 function Standard_Input
return File_Access
is
1674 return Standard_In
'Access;
1677 ---------------------
1678 -- Standard_Output --
1679 ---------------------
1681 function Standard_Output
return File_Type
is
1683 return Standard_Out
;
1684 end Standard_Output
;
1686 function Standard_Output
return File_Access
is
1688 return Standard_Out
'Access;
1689 end Standard_Output
;
1691 --------------------
1692 -- Terminate_Line --
1693 --------------------
1695 procedure Terminate_Line
(File
: File_Type
) is
1697 FIO
.Check_File_Open
(AP
(File
));
1699 -- For file other than In_File, test for needing to terminate last line
1701 if Mode
(File
) /= In_File
then
1703 -- If not at start of line definition need new line
1705 if File
.Col
/= 1 then
1708 -- For files other than standard error and standard output, we
1709 -- make sure that an empty file has a single line feed, so that
1710 -- it is properly formatted. We avoid this for the standard files
1711 -- because it is too much of a nuisance to have these odd line
1712 -- feeds when nothing has been written to the file.
1714 elsif (File
/= Standard_Err
and then File
/= Standard_Out
)
1715 and then (File
.Line
= 1 and then File
.Page
= 1)
1726 procedure Ungetc
(ch
: int
; File
: File_Type
) is
1729 if ungetc
(ch
, File
.Stream
) = EOF
then
1739 -- This is the primitive Stream Write routine, used when a Text_IO file
1740 -- is treated directly as a stream using Text_IO.Streams.Stream.
1743 (File
: in out Wide_Text_AFCB
;
1744 Item
: in Stream_Element_Array
)
1746 Siz
: constant size_t
:= Item
'Length;
1749 if File
.Mode
= FCB
.In_File
then
1753 -- Now we do the write. Since this is a text file, it is normally in
1754 -- text mode, but stream data must be written in binary mode, so we
1755 -- temporarily set binary mode for the write, resetting it after.
1756 -- These calls have no effect in a system (like Unix) where there is
1757 -- no distinction between text and binary files.
1759 set_binary_mode
(fileno
(File
.Stream
));
1761 if fwrite
(Item
'Address, 1, Siz
, File
.Stream
) /= Siz
then
1765 set_text_mode
(fileno
(File
.Stream
));
1768 -- Use "preallocated" strings to avoid calling "new" during the
1769 -- elaboration of the run time. This is needed in the tasking case to
1770 -- avoid calling Task_Lock too early. A filename is expected to end with
1771 -- a null character in the runtime, here the null characters are added
1772 -- just to have a correct filename length.
1774 Err_Name
: aliased String := "*stderr" & ASCII
.Nul
;
1775 In_Name
: aliased String := "*stdin" & ASCII
.Nul
;
1776 Out_Name
: aliased String := "*stdout" & ASCII
.Nul
;
1779 -------------------------------
1780 -- Initialize Standard Files --
1781 -------------------------------
1783 for J
in WC_Encoding_Method
loop
1784 if WC_Encoding
= WC_Encoding_Letters
(J
) then
1789 -- Note: the names in these files are bogus, and probably it would be
1790 -- better for these files to have no names, but the ACVC test insist!
1791 -- We use names that are bound to fail in open etc.
1793 Standard_Err
.Stream
:= stderr
;
1794 Standard_Err
.Name
:= Err_Name
'Access;
1795 Standard_Err
.Form
:= Null_Str
'Unrestricted_Access;
1796 Standard_Err
.Mode
:= FCB
.Out_File
;
1797 Standard_Err
.Is_Regular_File
:= is_regular_file
(fileno
(stderr
)) /= 0;
1798 Standard_Err
.Is_Temporary_File
:= False;
1799 Standard_Err
.Is_System_File
:= True;
1800 Standard_Err
.Is_Text_File
:= True;
1801 Standard_Err
.Access_Method
:= 'T';
1802 Standard_Err
.WC_Method
:= Default_WCEM
;
1804 Standard_In
.Stream
:= stdin
;
1805 Standard_In
.Name
:= In_Name
'Access;
1806 Standard_In
.Form
:= Null_Str
'Unrestricted_Access;
1807 Standard_In
.Mode
:= FCB
.In_File
;
1808 Standard_In
.Is_Regular_File
:= is_regular_file
(fileno
(stdin
)) /= 0;
1809 Standard_In
.Is_Temporary_File
:= False;
1810 Standard_In
.Is_System_File
:= True;
1811 Standard_In
.Is_Text_File
:= True;
1812 Standard_In
.Access_Method
:= 'T';
1813 Standard_In
.WC_Method
:= Default_WCEM
;
1815 Standard_Out
.Stream
:= stdout
;
1816 Standard_Out
.Name
:= Out_Name
'Access;
1817 Standard_Out
.Form
:= Null_Str
'Unrestricted_Access;
1818 Standard_Out
.Mode
:= FCB
.Out_File
;
1819 Standard_Out
.Is_Regular_File
:= is_regular_file
(fileno
(stdout
)) /= 0;
1820 Standard_Out
.Is_Temporary_File
:= False;
1821 Standard_Out
.Is_System_File
:= True;
1822 Standard_Out
.Is_Text_File
:= True;
1823 Standard_Out
.Access_Method
:= 'T';
1824 Standard_Out
.WC_Method
:= Default_WCEM
;
1826 FIO
.Chain_File
(AP
(Standard_In
));
1827 FIO
.Chain_File
(AP
(Standard_Out
));
1828 FIO
.Chain_File
(AP
(Standard_Err
));
1830 FIO
.Make_Unbuffered
(AP
(Standard_Out
));
1831 FIO
.Make_Unbuffered
(AP
(Standard_Err
));
1833 end Ada
.Wide_Text_IO
;