1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . W I D E _ T E X T _ I O --
9 -- Copyright (C) 1992-2006, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Ada
.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
;
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
: 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
74 File
: File_Type
) return Wide_Character;
75 -- This routine is identical to Get_Wide_Char, except that the reads are
76 -- done in Get_Immediate mode (i.e. without waiting for a line return).
78 procedure Set_WCEM
(File
: in out File_Type
);
79 -- Called by Open and Create to set the wide character encoding method
80 -- for the file, processing a WCEM form parameter if one is present.
81 -- File is IN OUT because it may be closed in case of an error.
87 function AFCB_Allocate
88 (Control_Block
: Wide_Text_AFCB
) return FCB
.AFCB_Ptr
90 pragma Unreferenced
(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
: 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
: File_Mode
:= Out_File
;
169 Dummy_File_Control_Block
: Wide_Text_AFCB
;
170 pragma Warnings
(Off
, Dummy_File_Control_Block
);
171 -- Yes, we know this is never assigned a value, only the tag
172 -- is used for dispatching purposes, so that's expected.
175 FIO
.Open
(File_Ptr
=> AP
(File
),
176 Dummy_FCB
=> Dummy_File_Control_Block
,
177 Mode
=> To_FCB
(Mode
),
190 function Current_Error
return File_Type
is
195 function Current_Error
return File_Access
is
197 return Current_Err
'Access;
204 function Current_Input
return File_Type
is
209 function Current_Input
return File_Access
is
211 return Current_In
'Access;
218 function Current_Output
return File_Type
is
223 function Current_Output
return File_Access
is
225 return Current_Out
'Access;
232 procedure Delete
(File
: in out File_Type
) is
234 FIO
.Delete
(AP
(File
));
241 function End_Of_File
(File
: File_Type
) return Boolean is
245 FIO
.Check_Read_Status
(AP
(File
));
247 if File
.Before_Wide_Character
then
250 elsif File
.Before_LM
then
252 if File
.Before_LM_PM
then
253 return Nextc
(File
) = EOF
;
267 File
.Before_LM
:= True;
271 -- Here we are just past the line mark with Before_LM set so that we
272 -- do not have to try to back up past the LM, thus avoiding the need
273 -- to back up more than one character.
280 elsif ch
= PM
and then File
.Is_Regular_File
then
281 File
.Before_LM_PM
:= True;
282 return Nextc
(File
) = EOF
;
284 -- Here if neither EOF nor PM followed end of line
293 function End_Of_File
return Boolean is
295 return End_Of_File
(Current_In
);
302 function End_Of_Line
(File
: File_Type
) return Boolean is
306 FIO
.Check_Read_Status
(AP
(File
));
308 if File
.Before_Wide_Character
then
311 elsif File
.Before_LM
then
327 function End_Of_Line
return Boolean is
329 return End_Of_Line
(Current_In
);
336 function End_Of_Page
(File
: File_Type
) return Boolean is
340 FIO
.Check_Read_Status
(AP
(File
));
342 if not File
.Is_Regular_File
then
345 elsif File
.Before_Wide_Character
then
348 elsif File
.Before_LM
then
349 if File
.Before_LM_PM
then
364 File
.Before_LM
:= True;
368 -- Here we are just past the line mark with Before_LM set so that we
369 -- do not have to try to back up past the LM, thus avoiding the need
370 -- to back up more than one character.
374 return ch
= PM
or else ch
= EOF
;
377 function End_Of_Page
return Boolean is
379 return End_Of_Page
(Current_In
);
386 procedure Flush
(File
: File_Type
) is
388 FIO
.Flush
(AP
(File
));
400 function Form
(File
: File_Type
) return String is
402 return FIO
.Form
(AP
(File
));
411 Item
: out Wide_Character)
416 FIO
.Check_Read_Status
(AP
(File
));
418 if File
.Before_Wide_Character
then
419 File
.Before_Wide_Character
:= False;
420 Item
:= File
.Saved_Wide_Character
;
423 Get_Character
(File
, C
);
424 Item
:= Get_Wide_Char
(C
, File
);
428 procedure Get
(Item
: out Wide_Character) is
430 Get
(Current_In
, Item
);
435 Item
: out Wide_String)
438 for J
in Item
'Range loop
439 Get
(File
, Item
(J
));
443 procedure Get
(Item
: out Wide_String) is
445 Get
(Current_In
, Item
);
452 procedure Get_Character
454 Item
: out Character)
459 if File
.Before_LM
then
460 File
.Before_LM
:= False;
461 File
.Before_LM_PM
:= False;
464 if File
.Before_LM_PM
then
466 File
.Page
:= File
.Page
+ 1;
467 File
.Before_LM_PM
:= False;
470 File
.Line
:= File
.Line
+ 1;
481 File
.Line
:= File
.Line
+ 1;
484 elsif ch
= PM
and then File
.Is_Regular_File
then
485 File
.Page
:= File
.Page
+ 1;
489 Item
:= Character'Val (ch
);
490 File
.Col
:= File
.Col
+ 1;
500 procedure Get_Immediate
502 Item
: out Wide_Character)
507 FIO
.Check_Read_Status
(AP
(File
));
509 if File
.Before_Wide_Character
then
510 File
.Before_Wide_Character
:= False;
511 Item
:= File
.Saved_Wide_Character
;
513 elsif File
.Before_LM
then
514 File
.Before_LM
:= False;
515 File
.Before_LM_PM
:= False;
516 Item
:= Wide_Character'Val (LM
);
519 ch
:= Getc_Immed
(File
);
524 Item
:= Get_Wide_Char_Immed
(Character'Val (ch
), File
);
529 procedure Get_Immediate
530 (Item
: out Wide_Character)
533 Get_Immediate
(Current_In
, Item
);
536 procedure Get_Immediate
538 Item
: out Wide_Character;
539 Available
: out Boolean)
544 FIO
.Check_Read_Status
(AP
(File
));
547 if File
.Before_Wide_Character
then
548 File
.Before_Wide_Character
:= False;
549 Item
:= File
.Saved_Wide_Character
;
551 elsif File
.Before_LM
then
552 File
.Before_LM
:= False;
553 File
.Before_LM_PM
:= False;
554 Item
:= Wide_Character'Val (LM
);
557 ch
:= Getc_Immed
(File
);
562 Item
:= Get_Wide_Char_Immed
(Character'Val (ch
), File
);
567 procedure Get_Immediate
568 (Item
: out Wide_Character;
569 Available
: out Boolean)
572 Get_Immediate
(Current_In
, Item
, Available
);
581 Item
: out Wide_String;
585 FIO
.Check_Read_Status
(AP
(File
));
586 Last
:= Item
'First - 1;
588 -- Immediate exit for null string, this is a case in which we do not
589 -- need to test for end of file and we do not skip a line mark under
590 -- any circumstances.
592 if Last
>= Item
'Last then
596 -- Here we have at least one character, if we are immediately before
597 -- a line mark, then we will just skip past it storing no characters.
599 if File
.Before_LM
then
600 File
.Before_LM
:= False;
601 File
.Before_LM_PM
:= False;
603 -- Otherwise we need to read some characters
606 -- If we are at the end of file now, it means we are trying to
607 -- skip a file terminator and we raise End_Error (RM A.10.7(20))
609 if Nextc
(File
) = EOF
then
613 -- Loop through characters in string
616 -- Exit the loop if read is terminated by encountering line mark
617 -- Note that the use of Skip_Line here ensures we properly deal
618 -- with setting the page and line numbers.
620 if End_Of_Line
(File
) then
625 -- Otherwise store the character, note that we know that ch is
626 -- something other than LM or EOF. It could possibly be a page
627 -- mark if there is a stray page mark in the middle of a line,
628 -- but this is not an official page mark in any case, since
629 -- official page marks can only follow a line mark. The whole
630 -- page business is pretty much nonsense anyway, so we do not
631 -- want to waste time trying to make sense out of non-standard
632 -- page marks in the file! This means that the behavior of
633 -- Get_Line is different from repeated Get of a character, but
634 -- that's too bad. We only promise that page numbers etc make
635 -- sense if the file is formatted in a standard manner.
637 -- Note: we do not adjust the column number because it is quicker
638 -- to adjust it once at the end of the operation than incrementing
639 -- it each time around the loop.
642 Get
(File
, Item
(Last
));
644 -- All done if the string is full, this is the case in which
645 -- we do not skip the following line mark. We need to adjust
646 -- the column number in this case.
648 if Last
= Item
'Last then
649 File
.Col
:= File
.Col
+ Count
(Item
'Length);
653 -- Exit from the loop if we are at the end of file. This happens
654 -- if we have a last line that is not terminated with a line mark.
655 -- In this case we consider that there is an implied line mark;
656 -- this is a non-standard file, but we will treat it nicely.
658 exit when Nextc
(File
) = EOF
;
664 (Item
: out Wide_String;
668 Get_Line
(Current_In
, Item
, Last
);
671 function Get_Line
(File
: File_Type
) return Wide_String is
672 Buffer
: Wide_String (1 .. 500);
675 function Get_Rest
(S
: Wide_String) return Wide_String;
676 -- This is a recursive function that reads the rest of the line and
677 -- returns it. S is the part read so far.
683 function Get_Rest
(S
: Wide_String) return Wide_String is
685 -- Each time we allocate a buffer the same size as what we have
686 -- read so far. This limits us to a logarithmic number of calls
687 -- to Get_Rest and also ensures only a linear use of stack space.
689 Buffer
: Wide_String (1 .. S
'Length);
693 Get_Line
(File
, Buffer
, Last
);
696 R
: constant Wide_String := S
& Buffer
(1 .. Last
);
698 if Last
< Buffer
'Last then
706 -- Start of processing for Get_Line
709 Get_Line
(File
, Buffer
, Last
);
711 if Last
< Buffer
'Last then
712 return Buffer
(1 .. Last
);
714 return Get_Rest
(Buffer
(1 .. Last
));
718 function Get_Line
return Wide_String is
720 return Get_Line
(Current_In
);
727 function Get_Wide_Char
729 File
: File_Type
) return Wide_Character
731 function In_Char
return Character;
732 -- Function used to obtain additional characters it the wide character
733 -- sequence is more than one character long.
735 function WC_In
is new Char_Sequence_To_Wide_Char
(In_Char
);
741 function In_Char
return Character is
742 ch
: constant Integer := Getc
(File
);
747 return Character'Val (ch
);
751 -- Start of processing for In_Char
754 return WC_In
(C
, File
.WC_Method
);
757 -------------------------
758 -- Get_Wide_Char_Immed --
759 -------------------------
761 function Get_Wide_Char_Immed
763 File
: File_Type
) return Wide_Character
765 function In_Char
return Character;
766 -- Function used to obtain additional characters it the wide character
767 -- sequence is more than one character long.
769 function WC_In
is new Char_Sequence_To_Wide_Char
(In_Char
);
775 function In_Char
return Character is
776 ch
: constant Integer := Getc_Immed
(File
);
781 return Character'Val (ch
);
785 -- Start of processing for Get_Wide_Char_Immed
788 return WC_In
(C
, File
.WC_Method
);
789 end Get_Wide_Char_Immed
;
795 function Getc
(File
: File_Type
) return int
is
799 ch
:= fgetc
(File
.Stream
);
801 if ch
= EOF
and then ferror
(File
.Stream
) /= 0 then
812 function Getc_Immed
(File
: File_Type
) return int
is
816 procedure getc_immediate
817 (stream
: FILEs
; ch
: out int
; end_of_file
: out int
);
818 pragma Import
(C
, getc_immediate
, "getc_immediate");
821 FIO
.Check_Read_Status
(AP
(File
));
823 if File
.Before_LM
then
824 File
.Before_LM
:= False;
825 File
.Before_LM_PM
:= False;
829 getc_immediate
(File
.Stream
, ch
, end_of_file
);
831 if ferror
(File
.Stream
) /= 0 then
833 elsif end_of_file
/= 0 then
845 function Is_Open
(File
: File_Type
) return Boolean is
847 return FIO
.Is_Open
(AP
(File
));
854 -- Note: we assume that it is impossible in practice for the line
855 -- to exceed the value of Count'Last, i.e. no check is required for
856 -- overflow raising layout error.
858 function Line
(File
: File_Type
) return Positive_Count
is
860 FIO
.Check_File_Open
(AP
(File
));
864 function Line
return Positive_Count
is
866 return Line
(Current_Out
);
873 function Line_Length
(File
: File_Type
) return Count
is
875 FIO
.Check_Write_Status
(AP
(File
));
876 return File
.Line_Length
;
879 function Line_Length
return Count
is
881 return Line_Length
(Current_Out
);
890 Item
: out Wide_Character;
891 End_Of_Line
: out Boolean)
895 -- Start of processing for Look_Ahead
898 FIO
.Check_Read_Status
(AP
(File
));
900 -- If we are logically before a line mark, we can return immediately
902 if File
.Before_LM
then
904 Item
:= Wide_Character'Val (0);
906 -- If we are before a wide character, just return it (this happens
907 -- if there are two calls to Look_Ahead in a row).
909 elsif File
.Before_Wide_Character
then
910 End_Of_Line
:= False;
911 Item
:= File
.Saved_Wide_Character
;
913 -- otherwise we must read a character from the input stream
920 or else (ch
= EOF
and then File
.Is_Regular_File
)
924 Item
:= Wide_Character'Val (0);
926 -- If the character is in the range 16#0000# to 16#007F# it stands
927 -- for itself and occupies a single byte, so we can unget it with
930 elsif ch
<= 16#
0080#
then
931 End_Of_Line
:= False;
933 Item
:= Wide_Character'Val (ch
);
935 -- For a character above this range, we read the character, using
936 -- the Get_Wide_Char routine. It may well occupy more than one byte
937 -- so we can't put it back with ungetc. Instead we save it in the
938 -- control block, setting a flag that everyone interested in reading
939 -- characters must test before reading the stream.
942 Item
:= Get_Wide_Char
(Character'Val (ch
), File
);
943 End_Of_Line
:= False;
944 File
.Saved_Wide_Character
:= Item
;
945 File
.Before_Wide_Character
:= True;
951 (Item
: out Wide_Character;
952 End_Of_Line
: out Boolean)
955 Look_Ahead
(Current_In
, Item
, End_Of_Line
);
962 function Mode
(File
: File_Type
) return File_Mode
is
964 return To_TIO
(FIO
.Mode
(AP
(File
)));
971 function Name
(File
: File_Type
) return String is
973 return FIO
.Name
(AP
(File
));
982 Spacing
: Positive_Count
:= 1)
985 -- Raise Constraint_Error if out of range value. The reason for this
986 -- explicit test is that we don't want junk values around, even if
987 -- checks are off in the caller.
989 if not Spacing
'Valid then
990 raise Constraint_Error
;
993 FIO
.Check_Write_Status
(AP
(File
));
995 for K
in 1 .. Spacing
loop
997 File
.Line
:= File
.Line
+ 1;
999 if File
.Page_Length
/= 0
1000 and then File
.Line
> File
.Page_Length
1004 File
.Page
:= File
.Page
+ 1;
1011 procedure New_Line
(Spacing
: Positive_Count
:= 1) is
1013 New_Line
(Current_Out
, Spacing
);
1020 procedure New_Page
(File
: File_Type
) is
1022 FIO
.Check_Write_Status
(AP
(File
));
1024 if File
.Col
/= 1 or else File
.Line
= 1 then
1029 File
.Page
:= File
.Page
+ 1;
1034 procedure New_Page
is
1036 New_Page
(Current_Out
);
1043 function Nextc
(File
: File_Type
) return int
is
1047 ch
:= fgetc
(File
.Stream
);
1050 if ferror
(File
.Stream
) /= 0 then
1055 if ungetc
(ch
, File
.Stream
) = EOF
then
1068 (File
: in out File_Type
;
1071 Form
: String := "")
1073 Dummy_File_Control_Block
: Wide_Text_AFCB
;
1074 pragma Warnings
(Off
, Dummy_File_Control_Block
);
1075 -- Yes, we know this is never assigned a value, only the tag
1076 -- is used for dispatching purposes, so that's expected.
1079 FIO
.Open
(File_Ptr
=> AP
(File
),
1080 Dummy_FCB
=> Dummy_File_Control_Block
,
1081 Mode
=> To_FCB
(Mode
),
1094 -- Note: we assume that it is impossible in practice for the page
1095 -- to exceed the value of Count'Last, i.e. no check is required for
1096 -- overflow raising layout error.
1098 function Page
(File
: File_Type
) return Positive_Count
is
1100 FIO
.Check_File_Open
(AP
(File
));
1104 function Page
return Positive_Count
is
1106 return Page
(Current_Out
);
1113 function Page_Length
(File
: File_Type
) return Count
is
1115 FIO
.Check_Write_Status
(AP
(File
));
1116 return File
.Page_Length
;
1119 function Page_Length
return Count
is
1121 return Page_Length
(Current_Out
);
1130 Item
: Wide_Character)
1132 procedure Out_Char
(C
: Character);
1133 -- Procedure to output one character of a wide character sequence
1135 procedure WC_Out
is new Wide_Char_To_Char_Sequence
(Out_Char
);
1140 procedure Out_Char
(C
: Character) is
1142 Putc
(Character'Pos (C
), File
);
1145 -- Start of processing for Put
1148 WC_Out
(Item
, File
.WC_Method
);
1149 File
.Col
:= File
.Col
+ 1;
1152 procedure Put
(Item
: Wide_Character) is
1154 Put
(Current_Out
, Item
);
1166 for J
in Item
'Range loop
1167 Put
(File
, Item
(J
));
1171 procedure Put
(Item
: Wide_String) is
1173 Put
(Current_Out
, Item
);
1189 procedure Put_Line
(Item
: Wide_String) is
1191 Put
(Current_Out
, Item
);
1192 New_Line
(Current_Out
);
1199 procedure Putc
(ch
: int
; File
: File_Type
) is
1201 if fputc
(ch
, File
.Stream
) = EOF
then
1210 -- This is the primitive Stream Read routine, used when a Text_IO file
1211 -- is treated directly as a stream using Text_IO.Streams.Stream.
1214 (File
: in out Wide_Text_AFCB
;
1215 Item
: out Stream_Element_Array
;
1216 Last
: out Stream_Element_Offset
)
1219 pragma Unreferenced
(Discard_ch
);
1222 -- Need to deal with Before_Wide_Character ???
1224 if File
.Mode
/= FCB
.In_File
then
1228 -- Deal with case where our logical and physical position do not match
1229 -- because of being after an LM or LM-PM sequence when in fact we are
1230 -- logically positioned before it.
1232 if File
.Before_LM
then
1234 -- If we are before a PM, then it is possible for a stream read
1235 -- to leave us after the LM and before the PM, which is a bit
1236 -- odd. The easiest way to deal with this is to unget the PM,
1237 -- so we are indeed positioned between the characters. This way
1238 -- further stream read operations will work correctly, and the
1239 -- effect on text processing is a little weird, but what can
1240 -- be expected if stream and text input are mixed this way?
1242 if File
.Before_LM_PM
then
1243 Discard_ch
:= ungetc
(PM
, File
.Stream
);
1244 File
.Before_LM_PM
:= False;
1247 File
.Before_LM
:= False;
1249 Item
(Item
'First) := Stream_Element
(Character'Pos (ASCII
.LF
));
1251 if Item
'Length = 1 then
1257 Stream_Element_Offset
1258 (fread
(buffer
=> Item
'Address,
1259 index
=> size_t
(Item
'First + 1),
1261 count
=> Item
'Length - 1,
1262 stream
=> File
.Stream
));
1268 -- Now we do the read. Since this is a text file, it is normally in
1269 -- text mode, but stream data must be read in binary mode, so we
1270 -- temporarily set binary mode for the read, resetting it after.
1271 -- These calls have no effect in a system (like Unix) where there is
1272 -- no distinction between text and binary files.
1274 set_binary_mode
(fileno
(File
.Stream
));
1278 Stream_Element_Offset
1279 (fread
(Item
'Address, 1, Item
'Length, File
.Stream
)) - 1;
1281 if Last
< Item
'Last then
1282 if ferror
(File
.Stream
) /= 0 then
1287 set_text_mode
(fileno
(File
.Stream
));
1295 (File
: in out File_Type
;
1299 -- Don't allow change of mode for current file (RM A.10.2(5))
1301 if (File
= Current_In
or else
1302 File
= Current_Out
or else
1303 File
= Current_Error
)
1304 and then To_FCB
(Mode
) /= File
.Mode
1309 Terminate_Line
(File
);
1310 FIO
.Reset
(AP
(File
), To_FCB
(Mode
));
1314 File
.Line_Length
:= 0;
1315 File
.Page_Length
:= 0;
1316 File
.Before_LM
:= False;
1317 File
.Before_LM_PM
:= False;
1320 procedure Reset
(File
: in out File_Type
) is
1322 Terminate_Line
(File
);
1323 FIO
.Reset
(AP
(File
));
1327 File
.Line_Length
:= 0;
1328 File
.Page_Length
:= 0;
1329 File
.Before_LM
:= False;
1330 File
.Before_LM_PM
:= False;
1339 To
: Positive_Count
)
1344 -- Raise Constraint_Error if out of range value. The reason for this
1345 -- explicit test is that we don't want junk values around, even if
1346 -- checks are off in the caller.
1348 if not To
'Valid then
1349 raise Constraint_Error
;
1352 FIO
.Check_File_Open
(AP
(File
));
1354 if To
= File
.Col
then
1358 if Mode
(File
) >= Out_File
then
1359 if File
.Line_Length
/= 0 and then To
> File
.Line_Length
then
1363 if To
< File
.Col
then
1367 while File
.Col
< To
loop
1379 File
.Line
:= File
.Line
+ 1;
1382 elsif ch
= PM
and then File
.Is_Regular_File
then
1383 File
.Page
:= File
.Page
+ 1;
1387 elsif To
= File
.Col
then
1392 File
.Col
:= File
.Col
+ 1;
1398 procedure Set_Col
(To
: Positive_Count
) is
1400 Set_Col
(Current_Out
, To
);
1407 procedure Set_Error
(File
: File_Type
) is
1409 FIO
.Check_Write_Status
(AP
(File
));
1410 Current_Err
:= File
;
1417 procedure Set_Input
(File
: File_Type
) is
1419 FIO
.Check_Read_Status
(AP
(File
));
1429 To
: Positive_Count
)
1432 -- Raise Constraint_Error if out of range value. The reason for this
1433 -- explicit test is that we don't want junk values around, even if
1434 -- checks are off in the caller.
1436 if not To
'Valid then
1437 raise Constraint_Error
;
1440 FIO
.Check_File_Open
(AP
(File
));
1442 if To
= File
.Line
then
1446 if Mode
(File
) >= Out_File
then
1447 if File
.Page_Length
/= 0 and then To
> File
.Page_Length
then
1451 if To
< File
.Line
then
1455 while File
.Line
< To
loop
1460 while To
/= File
.Line
loop
1466 procedure Set_Line
(To
: Positive_Count
) is
1468 Set_Line
(Current_Out
, To
);
1471 ---------------------
1472 -- Set_Line_Length --
1473 ---------------------
1475 procedure Set_Line_Length
(File
: File_Type
; To
: Count
) is
1477 -- Raise Constraint_Error if out of range value. The reason for this
1478 -- explicit test is that we don't want junk values around, even if
1479 -- checks are off in the caller.
1481 if not To
'Valid then
1482 raise Constraint_Error
;
1485 FIO
.Check_Write_Status
(AP
(File
));
1486 File
.Line_Length
:= To
;
1487 end Set_Line_Length
;
1489 procedure Set_Line_Length
(To
: Count
) is
1491 Set_Line_Length
(Current_Out
, To
);
1492 end Set_Line_Length
;
1498 procedure Set_Output
(File
: File_Type
) is
1500 FIO
.Check_Write_Status
(AP
(File
));
1501 Current_Out
:= File
;
1504 ---------------------
1505 -- Set_Page_Length --
1506 ---------------------
1508 procedure Set_Page_Length
(File
: File_Type
; To
: Count
) is
1510 -- Raise Constraint_Error if out of range value. The reason for this
1511 -- explicit test is that we don't want junk values around, even if
1512 -- checks are off in the caller.
1514 if not To
'Valid then
1515 raise Constraint_Error
;
1518 FIO
.Check_Write_Status
(AP
(File
));
1519 File
.Page_Length
:= To
;
1520 end Set_Page_Length
;
1522 procedure Set_Page_Length
(To
: Count
) is
1524 Set_Page_Length
(Current_Out
, To
);
1525 end Set_Page_Length
;
1531 procedure Set_WCEM
(File
: in out File_Type
) is
1536 File
.WC_Method
:= WCEM_Brackets
;
1537 FIO
.Form_Parameter
(File
.Form
.all, "wcem", Start
, Stop
);
1540 File
.WC_Method
:= WCEM_Brackets
;
1542 elsif Start
/= 0 then
1543 if Stop
= Start
then
1544 for J
in WC_Encoding_Letters
'Range loop
1545 if File
.Form
(Start
) = WC_Encoding_Letters
(J
) then
1546 File
.WC_Method
:= J
;
1553 Raise_Exception
(Use_Error
'Identity, "invalid WCEM form parameter");
1563 Spacing
: Positive_Count
:= 1)
1568 -- Raise Constraint_Error if out of range value. The reason for this
1569 -- explicit test is that we don't want junk values around, even if
1570 -- checks are off in the caller.
1572 if not Spacing
'Valid then
1573 raise Constraint_Error
;
1576 FIO
.Check_Read_Status
(AP
(File
));
1578 for L
in 1 .. Spacing
loop
1579 if File
.Before_LM
then
1580 File
.Before_LM
:= False;
1581 File
.Before_LM_PM
:= False;
1586 -- If at end of file now, then immediately raise End_Error. Note
1587 -- that we can never be positioned between a line mark and a page
1588 -- mark, so if we are at the end of file, we cannot logically be
1589 -- before the implicit page mark that is at the end of the file.
1591 -- For the same reason, we do not need an explicit check for a
1592 -- page mark. If there is a FF in the middle of a line, the file
1593 -- is not in canonical format and we do not care about the page
1594 -- numbers for files other than ones in canonical format.
1600 -- If not at end of file, then loop till we get to an LM or EOF.
1601 -- The latter case happens only in non-canonical files where the
1602 -- last line is not terminated by LM, but we don't want to blow
1603 -- up for such files, so we assume an implicit LM in this case.
1606 exit when ch
= LM
or ch
= EOF
;
1611 -- We have got past a line mark, now, for a regular file only,
1612 -- see if a page mark immediately follows this line mark and
1613 -- if so, skip past the page mark as well. We do not do this
1614 -- for non-regular files, since it would cause an undesirable
1615 -- wait for an additional character.
1618 File
.Line
:= File
.Line
+ 1;
1620 if File
.Before_LM_PM
then
1621 File
.Page
:= File
.Page
+ 1;
1623 File
.Before_LM_PM
:= False;
1625 elsif File
.Is_Regular_File
then
1628 -- Page mark can be explicit, or implied at the end of the file
1630 if (ch
= PM
or else ch
= EOF
)
1631 and then File
.Is_Regular_File
1633 File
.Page
:= File
.Page
+ 1;
1642 File
.Before_Wide_Character
:= False;
1645 procedure Skip_Line
(Spacing
: Positive_Count
:= 1) is
1647 Skip_Line
(Current_In
, Spacing
);
1654 procedure Skip_Page
(File
: File_Type
) is
1658 FIO
.Check_Read_Status
(AP
(File
));
1660 -- If at page mark already, just skip it
1662 if File
.Before_LM_PM
then
1663 File
.Before_LM
:= False;
1664 File
.Before_LM_PM
:= False;
1665 File
.Page
:= File
.Page
+ 1;
1671 -- This is a bit tricky, if we are logically before an LM then
1672 -- it is not an error if we are at an end of file now, since we
1673 -- are not really at it.
1675 if File
.Before_LM
then
1676 File
.Before_LM
:= False;
1677 File
.Before_LM_PM
:= False;
1680 -- Otherwise we do raise End_Error if we are at the end of file now
1690 -- Now we can just rumble along to the next page mark, or to the
1691 -- end of file, if that comes first. The latter case happens when
1692 -- the page mark is implied at the end of file.
1696 or else (ch
= PM
and then File
.Is_Regular_File
);
1700 File
.Page
:= File
.Page
+ 1;
1703 File
.Before_Wide_Character
:= False;
1706 procedure Skip_Page
is
1708 Skip_Page
(Current_In
);
1711 --------------------
1712 -- Standard_Error --
1713 --------------------
1715 function Standard_Error
return File_Type
is
1717 return Standard_Err
;
1720 function Standard_Error
return File_Access
is
1722 return Standard_Err
'Access;
1725 --------------------
1726 -- Standard_Input --
1727 --------------------
1729 function Standard_Input
return File_Type
is
1734 function Standard_Input
return File_Access
is
1736 return Standard_In
'Access;
1739 ---------------------
1740 -- Standard_Output --
1741 ---------------------
1743 function Standard_Output
return File_Type
is
1745 return Standard_Out
;
1746 end Standard_Output
;
1748 function Standard_Output
return File_Access
is
1750 return Standard_Out
'Access;
1751 end Standard_Output
;
1753 --------------------
1754 -- Terminate_Line --
1755 --------------------
1757 procedure Terminate_Line
(File
: File_Type
) is
1759 FIO
.Check_File_Open
(AP
(File
));
1761 -- For file other than In_File, test for needing to terminate last line
1763 if Mode
(File
) /= In_File
then
1765 -- If not at start of line definition need new line
1767 if File
.Col
/= 1 then
1770 -- For files other than standard error and standard output, we
1771 -- make sure that an empty file has a single line feed, so that
1772 -- it is properly formatted. We avoid this for the standard files
1773 -- because it is too much of a nuisance to have these odd line
1774 -- feeds when nothing has been written to the file.
1776 elsif (File
/= Standard_Err
and then File
/= Standard_Out
)
1777 and then (File
.Line
= 1 and then File
.Page
= 1)
1788 procedure Ungetc
(ch
: int
; File
: File_Type
) is
1791 if ungetc
(ch
, File
.Stream
) = EOF
then
1801 -- This is the primitive Stream Write routine, used when a Text_IO file
1802 -- is treated directly as a stream using Text_IO.Streams.Stream.
1805 (File
: in out Wide_Text_AFCB
;
1806 Item
: Stream_Element_Array
)
1808 Siz
: constant size_t
:= Item
'Length;
1811 if File
.Mode
= FCB
.In_File
then
1815 -- Now we do the write. Since this is a text file, it is normally in
1816 -- text mode, but stream data must be written in binary mode, so we
1817 -- temporarily set binary mode for the write, resetting it after.
1818 -- These calls have no effect in a system (like Unix) where there is
1819 -- no distinction between text and binary files.
1821 set_binary_mode
(fileno
(File
.Stream
));
1823 if fwrite
(Item
'Address, 1, Siz
, File
.Stream
) /= Siz
then
1827 set_text_mode
(fileno
(File
.Stream
));
1830 -- Use "preallocated" strings to avoid calling "new" during the
1831 -- elaboration of the run time. This is needed in the tasking case to
1832 -- avoid calling Task_Lock too early. A filename is expected to end with
1833 -- a null character in the runtime, here the null characters are added
1834 -- just to have a correct filename length.
1836 Err_Name
: aliased String := "*stderr" & ASCII
.Nul
;
1837 In_Name
: aliased String := "*stdin" & ASCII
.Nul
;
1838 Out_Name
: aliased String := "*stdout" & ASCII
.Nul
;
1841 -------------------------------
1842 -- Initialize Standard Files --
1843 -------------------------------
1845 for J
in WC_Encoding_Method
loop
1846 if WC_Encoding
= WC_Encoding_Letters
(J
) then
1851 -- Note: the names in these files are bogus, and probably it would be
1852 -- better for these files to have no names, but the ACVC test insist!
1853 -- We use names that are bound to fail in open etc.
1855 Standard_Err
.Stream
:= stderr
;
1856 Standard_Err
.Name
:= Err_Name
'Access;
1857 Standard_Err
.Form
:= Null_Str
'Unrestricted_Access;
1858 Standard_Err
.Mode
:= FCB
.Out_File
;
1859 Standard_Err
.Is_Regular_File
:= is_regular_file
(fileno
(stderr
)) /= 0;
1860 Standard_Err
.Is_Temporary_File
:= False;
1861 Standard_Err
.Is_System_File
:= True;
1862 Standard_Err
.Is_Text_File
:= True;
1863 Standard_Err
.Access_Method
:= 'T';
1864 Standard_Err
.WC_Method
:= Default_WCEM
;
1866 Standard_In
.Stream
:= stdin
;
1867 Standard_In
.Name
:= In_Name
'Access;
1868 Standard_In
.Form
:= Null_Str
'Unrestricted_Access;
1869 Standard_In
.Mode
:= FCB
.In_File
;
1870 Standard_In
.Is_Regular_File
:= is_regular_file
(fileno
(stdin
)) /= 0;
1871 Standard_In
.Is_Temporary_File
:= False;
1872 Standard_In
.Is_System_File
:= True;
1873 Standard_In
.Is_Text_File
:= True;
1874 Standard_In
.Access_Method
:= 'T';
1875 Standard_In
.WC_Method
:= Default_WCEM
;
1877 Standard_Out
.Stream
:= stdout
;
1878 Standard_Out
.Name
:= Out_Name
'Access;
1879 Standard_Out
.Form
:= Null_Str
'Unrestricted_Access;
1880 Standard_Out
.Mode
:= FCB
.Out_File
;
1881 Standard_Out
.Is_Regular_File
:= is_regular_file
(fileno
(stdout
)) /= 0;
1882 Standard_Out
.Is_Temporary_File
:= False;
1883 Standard_Out
.Is_System_File
:= True;
1884 Standard_Out
.Is_Text_File
:= True;
1885 Standard_Out
.Access_Method
:= 'T';
1886 Standard_Out
.WC_Method
:= Default_WCEM
;
1888 FIO
.Chain_File
(AP
(Standard_In
));
1889 FIO
.Chain_File
(AP
(Standard_Out
));
1890 FIO
.Chain_File
(AP
(Standard_Err
));
1892 FIO
.Make_Unbuffered
(AP
(Standard_Out
));
1893 FIO
.Make_Unbuffered
(AP
(Standard_Err
));
1895 end Ada
.Wide_Text_IO
;