1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . W I D E _ 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_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_Wide_Char_Immed
74 File
: File_Type
) return Wide_Wide_Character
;
75 -- This routine is identical to Get_Wide_Wide_Char, except that the reads
76 -- are 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_Wide_Text_AFCB
) return FCB
.AFCB_Ptr
90 pragma Unreferenced
(Control_Block
);
92 return new Wide_Wide_Text_AFCB
;
99 procedure AFCB_Close
(File
: access Wide_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_Wide_Text_AFCB
) is
122 type FCB_Ptr
is access all Wide_Wide_Text_AFCB
;
123 FT
: FCB_Ptr
:= FCB_Ptr
(File
);
125 procedure Free
is new
126 Unchecked_Deallocation
(Wide_Wide_Text_AFCB
, FCB_Ptr
);
136 procedure Close
(File
: in out File_Type
) is
138 FIO
.Close
(AP
(File
));
145 -- Note: we assume that it is impossible in practice for the column
146 -- to exceed the value of Count'Last, i.e. no check is required for
147 -- overflow raising layout error.
149 function Col
(File
: File_Type
) return Positive_Count
is
151 FIO
.Check_File_Open
(AP
(File
));
155 function Col
return Positive_Count
is
157 return Col
(Current_Out
);
165 (File
: in out File_Type
;
166 Mode
: File_Mode
:= Out_File
;
170 Dummy_File_Control_Block
: Wide_Wide_Text_AFCB
;
171 pragma Warnings
(Off
, Dummy_File_Control_Block
);
172 -- Yes, we know this is never assigned a value, only the tag
173 -- is used for dispatching purposes, so that's expected.
176 FIO
.Open
(File_Ptr
=> AP
(File
),
177 Dummy_FCB
=> Dummy_File_Control_Block
,
178 Mode
=> To_FCB
(Mode
),
191 function Current_Error
return File_Type
is
196 function Current_Error
return File_Access
is
198 return Current_Err
'Access;
205 function Current_Input
return File_Type
is
210 function Current_Input
return File_Access
is
212 return Current_In
'Access;
219 function Current_Output
return File_Type
is
224 function Current_Output
return File_Access
is
226 return Current_Out
'Access;
233 procedure Delete
(File
: in out File_Type
) is
235 FIO
.Delete
(AP
(File
));
242 function End_Of_File
(File
: File_Type
) return Boolean is
246 FIO
.Check_Read_Status
(AP
(File
));
248 if File
.Before_Wide_Wide_Character
then
251 elsif File
.Before_LM
then
253 if File
.Before_LM_PM
then
254 return Nextc
(File
) = EOF
;
268 File
.Before_LM
:= True;
272 -- Here we are just past the line mark with Before_LM set so that we
273 -- do not have to try to back up past the LM, thus avoiding the need
274 -- to back up more than one character.
281 elsif ch
= PM
and then File
.Is_Regular_File
then
282 File
.Before_LM_PM
:= True;
283 return Nextc
(File
) = EOF
;
285 -- Here if neither EOF nor PM followed end of line
294 function End_Of_File
return Boolean is
296 return End_Of_File
(Current_In
);
303 function End_Of_Line
(File
: File_Type
) return Boolean is
307 FIO
.Check_Read_Status
(AP
(File
));
309 if File
.Before_Wide_Wide_Character
then
312 elsif File
.Before_LM
then
328 function End_Of_Line
return Boolean is
330 return End_Of_Line
(Current_In
);
337 function End_Of_Page
(File
: File_Type
) return Boolean is
341 FIO
.Check_Read_Status
(AP
(File
));
343 if not File
.Is_Regular_File
then
346 elsif File
.Before_Wide_Wide_Character
then
349 elsif File
.Before_LM
then
350 if File
.Before_LM_PM
then
365 File
.Before_LM
:= True;
369 -- Here we are just past the line mark with Before_LM set so that we
370 -- do not have to try to back up past the LM, thus avoiding the need
371 -- to back up more than one character.
375 return ch
= PM
or else ch
= EOF
;
378 function End_Of_Page
return Boolean is
380 return End_Of_Page
(Current_In
);
387 procedure Flush
(File
: File_Type
) is
389 FIO
.Flush
(AP
(File
));
401 function Form
(File
: File_Type
) return String is
403 return FIO
.Form
(AP
(File
));
412 Item
: out Wide_Wide_Character
)
417 FIO
.Check_Read_Status
(AP
(File
));
419 if File
.Before_Wide_Wide_Character
then
420 File
.Before_Wide_Wide_Character
:= False;
421 Item
:= File
.Saved_Wide_Wide_Character
;
424 Get_Character
(File
, C
);
425 Item
:= Get_Wide_Wide_Char
(C
, File
);
429 procedure Get
(Item
: out Wide_Wide_Character
) is
431 Get
(Current_In
, Item
);
436 Item
: out Wide_Wide_String
)
439 for J
in Item
'Range loop
440 Get
(File
, Item
(J
));
444 procedure Get
(Item
: out Wide_Wide_String
) is
446 Get
(Current_In
, Item
);
453 procedure Get_Character
455 Item
: out Character)
460 if File
.Before_LM
then
461 File
.Before_LM
:= False;
462 File
.Before_LM_PM
:= False;
465 if File
.Before_LM_PM
then
467 File
.Page
:= File
.Page
+ 1;
468 File
.Before_LM_PM
:= False;
471 File
.Line
:= File
.Line
+ 1;
482 File
.Line
:= File
.Line
+ 1;
485 elsif ch
= PM
and then File
.Is_Regular_File
then
486 File
.Page
:= File
.Page
+ 1;
490 Item
:= Character'Val (ch
);
491 File
.Col
:= File
.Col
+ 1;
501 procedure Get_Immediate
503 Item
: out Wide_Wide_Character
)
508 FIO
.Check_Read_Status
(AP
(File
));
510 if File
.Before_Wide_Wide_Character
then
511 File
.Before_Wide_Wide_Character
:= False;
512 Item
:= File
.Saved_Wide_Wide_Character
;
514 elsif File
.Before_LM
then
515 File
.Before_LM
:= False;
516 File
.Before_LM_PM
:= False;
517 Item
:= Wide_Wide_Character
'Val (LM
);
520 ch
:= Getc_Immed
(File
);
525 Item
:= Get_Wide_Wide_Char_Immed
(Character'Val (ch
), File
);
530 procedure Get_Immediate
531 (Item
: out Wide_Wide_Character
)
534 Get_Immediate
(Current_In
, Item
);
537 procedure Get_Immediate
539 Item
: out Wide_Wide_Character
;
540 Available
: out Boolean)
545 FIO
.Check_Read_Status
(AP
(File
));
548 if File
.Before_Wide_Wide_Character
then
549 File
.Before_Wide_Wide_Character
:= False;
550 Item
:= File
.Saved_Wide_Wide_Character
;
552 elsif File
.Before_LM
then
553 File
.Before_LM
:= False;
554 File
.Before_LM_PM
:= False;
555 Item
:= Wide_Wide_Character
'Val (LM
);
558 ch
:= Getc_Immed
(File
);
563 Item
:= Get_Wide_Wide_Char_Immed
(Character'Val (ch
), File
);
568 procedure Get_Immediate
569 (Item
: out Wide_Wide_Character
;
570 Available
: out Boolean)
573 Get_Immediate
(Current_In
, Item
, Available
);
582 Item
: out Wide_Wide_String
;
586 FIO
.Check_Read_Status
(AP
(File
));
587 Last
:= Item
'First - 1;
589 -- Immediate exit for null string, this is a case in which we do not
590 -- need to test for end of file and we do not skip a line mark under
591 -- any circumstances.
593 if Last
>= Item
'Last then
597 -- Here we have at least one character, if we are immediately before
598 -- a line mark, then we will just skip past it storing no characters.
600 if File
.Before_LM
then
601 File
.Before_LM
:= False;
602 File
.Before_LM_PM
:= False;
604 -- Otherwise we need to read some characters
607 -- If we are at the end of file now, it means we are trying to
608 -- skip a file terminator and we raise End_Error (RM A.10.7(20))
610 if Nextc
(File
) = EOF
then
614 -- Loop through characters in string
617 -- Exit the loop if read is terminated by encountering line mark
618 -- Note that the use of Skip_Line here ensures we properly deal
619 -- with setting the page and line numbers.
621 if End_Of_Line
(File
) then
626 -- Otherwise store the character, note that we know that ch is
627 -- something other than LM or EOF. It could possibly be a page
628 -- mark if there is a stray page mark in the middle of a line,
629 -- but this is not an official page mark in any case, since
630 -- official page marks can only follow a line mark. The whole
631 -- page business is pretty much nonsense anyway, so we do not
632 -- want to waste time trying to make sense out of non-standard
633 -- page marks in the file! This means that the behavior of
634 -- Get_Line is different from repeated Get of a character, but
635 -- that's too bad. We only promise that page numbers etc make
636 -- sense if the file is formatted in a standard manner.
638 -- Note: we do not adjust the column number because it is quicker
639 -- to adjust it once at the end of the operation than incrementing
640 -- it each time around the loop.
643 Get
(File
, Item
(Last
));
645 -- All done if the string is full, this is the case in which
646 -- we do not skip the following line mark. We need to adjust
647 -- the column number in this case.
649 if Last
= Item
'Last then
650 File
.Col
:= File
.Col
+ Count
(Item
'Length);
654 -- Exit from the loop if we are at the end of file. This happens
655 -- if we have a last line that is not terminated with a line mark.
656 -- In this case we consider that there is an implied line mark;
657 -- this is a non-standard file, but we will treat it nicely.
659 exit when Nextc
(File
) = EOF
;
665 (Item
: out Wide_Wide_String
;
669 Get_Line
(Current_In
, Item
, Last
);
672 function Get_Line
(File
: File_Type
) return Wide_Wide_String
is
673 Buffer
: Wide_Wide_String
(1 .. 500);
676 function Get_Rest
(S
: Wide_Wide_String
) return Wide_Wide_String
;
677 -- This is a recursive function that reads the rest of the line and
678 -- returns it. S is the part read so far.
684 function Get_Rest
(S
: Wide_Wide_String
) return Wide_Wide_String
is
686 -- Each time we allocate a buffer the same size as what we have
687 -- read so far. This limits us to a logarithmic number of calls
688 -- to Get_Rest and also ensures only a linear use of stack space.
690 Buffer
: Wide_Wide_String
(1 .. S
'Length);
694 Get_Line
(File
, Buffer
, Last
);
697 R
: constant Wide_Wide_String
:= S
& Buffer
(1 .. Last
);
699 if Last
< Buffer
'Last then
707 -- Start of processing for Get_Line
710 Get_Line
(File
, Buffer
, Last
);
712 if Last
< Buffer
'Last then
713 return Buffer
(1 .. Last
);
715 return Get_Rest
(Buffer
(1 .. Last
));
719 function Get_Line
return Wide_Wide_String
is
721 return Get_Line
(Current_In
);
724 ------------------------
725 -- Get_Wide_Wide_Char --
726 ------------------------
728 function Get_Wide_Wide_Char
730 File
: File_Type
) return Wide_Wide_Character
732 function In_Char
return Character;
733 -- Function used to obtain additional characters it the wide character
734 -- sequence is more than one character long.
736 function WC_In
is new Char_Sequence_To_UTF_32
(In_Char
);
742 function In_Char
return Character is
743 ch
: constant Integer := Getc
(File
);
748 return Character'Val (ch
);
752 -- Start of processing for Get_Wide_Wide_Char
755 return Wide_Wide_Character
'Val (WC_In
(C
, File
.WC_Method
));
756 end Get_Wide_Wide_Char
;
758 ------------------------------
759 -- Get_Wide_Wide_Char_Immed --
760 ------------------------------
762 function Get_Wide_Wide_Char_Immed
764 File
: File_Type
) return Wide_Wide_Character
766 function In_Char
return Character;
767 -- Function used to obtain additional characters it the wide character
768 -- sequence is more than one character long.
770 function WC_In
is new Char_Sequence_To_UTF_32
(In_Char
);
776 function In_Char
return Character is
777 ch
: constant Integer := Getc_Immed
(File
);
782 return Character'Val (ch
);
786 -- Start of processing for Get_Wide_Wide_Char_Immed
789 return Wide_Wide_Character
'Val (WC_In
(C
, File
.WC_Method
));
790 end Get_Wide_Wide_Char_Immed
;
796 function Getc
(File
: File_Type
) return int
is
800 ch
:= fgetc
(File
.Stream
);
802 if ch
= EOF
and then ferror
(File
.Stream
) /= 0 then
813 function Getc_Immed
(File
: File_Type
) return int
is
817 procedure getc_immediate
818 (stream
: FILEs
; ch
: out int
; end_of_file
: out int
);
819 pragma Import
(C
, getc_immediate
, "getc_immediate");
822 FIO
.Check_Read_Status
(AP
(File
));
824 if File
.Before_LM
then
825 File
.Before_LM
:= False;
826 File
.Before_LM_PM
:= False;
830 getc_immediate
(File
.Stream
, ch
, end_of_file
);
832 if ferror
(File
.Stream
) /= 0 then
834 elsif end_of_file
/= 0 then
846 function Is_Open
(File
: File_Type
) return Boolean is
848 return FIO
.Is_Open
(AP
(File
));
855 -- Note: we assume that it is impossible in practice for the line
856 -- to exceed the value of Count'Last, i.e. no check is required for
857 -- overflow raising layout error.
859 function Line
(File
: File_Type
) return Positive_Count
is
861 FIO
.Check_File_Open
(AP
(File
));
865 function Line
return Positive_Count
is
867 return Line
(Current_Out
);
874 function Line_Length
(File
: File_Type
) return Count
is
876 FIO
.Check_Write_Status
(AP
(File
));
877 return File
.Line_Length
;
880 function Line_Length
return Count
is
882 return Line_Length
(Current_Out
);
891 Item
: out Wide_Wide_Character
;
892 End_Of_Line
: out Boolean)
896 -- Start of processing for Look_Ahead
899 FIO
.Check_Read_Status
(AP
(File
));
901 -- If we are logically before a line mark, we can return immediately
903 if File
.Before_LM
then
905 Item
:= Wide_Wide_Character
'Val (0);
907 -- If we are before a wide character, just return it (this happens
908 -- if there are two calls to Look_Ahead in a row).
910 elsif File
.Before_Wide_Wide_Character
then
911 End_Of_Line
:= False;
912 Item
:= File
.Saved_Wide_Wide_Character
;
914 -- otherwise we must read a character from the input stream
921 or else (ch
= EOF
and then File
.Is_Regular_File
)
925 Item
:= Wide_Wide_Character
'Val (0);
927 -- If the character is in the range 16#0000# to 16#007F# it stands
928 -- for itself and occupies a single byte, so we can unget it with
931 elsif ch
<= 16#
0080#
then
932 End_Of_Line
:= False;
934 Item
:= Wide_Wide_Character
'Val (ch
);
936 -- For a character above this range, we read the character, using
937 -- the Get_Wide_Wide_Char routine. It may well occupy more than one
938 -- byte so we can't put it back with ungetc. Instead we save it in
939 -- the control block, setting a flag that everyone interested in
940 -- reading characters must test before reading the stream.
943 Item
:= Get_Wide_Wide_Char
(Character'Val (ch
), File
);
944 End_Of_Line
:= False;
945 File
.Saved_Wide_Wide_Character
:= Item
;
946 File
.Before_Wide_Wide_Character
:= True;
952 (Item
: out Wide_Wide_Character
;
953 End_Of_Line
: out Boolean)
956 Look_Ahead
(Current_In
, Item
, End_Of_Line
);
963 function Mode
(File
: File_Type
) return File_Mode
is
965 return To_TIO
(FIO
.Mode
(AP
(File
)));
972 function Name
(File
: File_Type
) return String is
974 return FIO
.Name
(AP
(File
));
983 Spacing
: Positive_Count
:= 1)
986 -- Raise Constraint_Error if out of range value. The reason for this
987 -- explicit test is that we don't want junk values around, even if
988 -- checks are off in the caller.
990 if not Spacing
'Valid then
991 raise Constraint_Error
;
994 FIO
.Check_Write_Status
(AP
(File
));
996 for K
in 1 .. Spacing
loop
998 File
.Line
:= File
.Line
+ 1;
1000 if File
.Page_Length
/= 0
1001 and then File
.Line
> File
.Page_Length
1005 File
.Page
:= File
.Page
+ 1;
1012 procedure New_Line
(Spacing
: Positive_Count
:= 1) is
1014 New_Line
(Current_Out
, Spacing
);
1021 procedure New_Page
(File
: File_Type
) is
1023 FIO
.Check_Write_Status
(AP
(File
));
1025 if File
.Col
/= 1 or else File
.Line
= 1 then
1030 File
.Page
:= File
.Page
+ 1;
1035 procedure New_Page
is
1037 New_Page
(Current_Out
);
1044 function Nextc
(File
: File_Type
) return int
is
1048 ch
:= fgetc
(File
.Stream
);
1051 if ferror
(File
.Stream
) /= 0 then
1056 if ungetc
(ch
, File
.Stream
) = EOF
then
1069 (File
: in out File_Type
;
1072 Form
: String := "")
1074 Dummy_File_Control_Block
: Wide_Wide_Text_AFCB
;
1075 pragma Warnings
(Off
, Dummy_File_Control_Block
);
1076 -- Yes, we know this is never assigned a value, only the tag
1077 -- is used for dispatching purposes, so that's expected.
1080 FIO
.Open
(File_Ptr
=> AP
(File
),
1081 Dummy_FCB
=> Dummy_File_Control_Block
,
1082 Mode
=> To_FCB
(Mode
),
1095 -- Note: we assume that it is impossible in practice for the page
1096 -- to exceed the value of Count'Last, i.e. no check is required for
1097 -- overflow raising layout error.
1099 function Page
(File
: File_Type
) return Positive_Count
is
1101 FIO
.Check_File_Open
(AP
(File
));
1105 function Page
return Positive_Count
is
1107 return Page
(Current_Out
);
1114 function Page_Length
(File
: File_Type
) return Count
is
1116 FIO
.Check_Write_Status
(AP
(File
));
1117 return File
.Page_Length
;
1120 function Page_Length
return Count
is
1122 return Page_Length
(Current_Out
);
1131 Item
: Wide_Wide_Character
)
1133 procedure Out_Char
(C
: Character);
1134 -- Procedure to output one character of a wide character sequence
1136 procedure WC_Out
is new UTF_32_To_Char_Sequence
(Out_Char
);
1142 procedure Out_Char
(C
: Character) is
1144 Putc
(Character'Pos (C
), File
);
1147 -- Start of processing for Put
1150 WC_Out
(Wide_Wide_Character
'Pos (Item
), File
.WC_Method
);
1151 File
.Col
:= File
.Col
+ 1;
1154 procedure Put
(Item
: Wide_Wide_Character
) is
1156 Put
(Current_Out
, Item
);
1165 Item
: Wide_Wide_String
)
1168 for J
in Item
'Range loop
1169 Put
(File
, Item
(J
));
1173 procedure Put
(Item
: Wide_Wide_String
) is
1175 Put
(Current_Out
, Item
);
1184 Item
: Wide_Wide_String
)
1191 procedure Put_Line
(Item
: Wide_Wide_String
) is
1193 Put
(Current_Out
, Item
);
1194 New_Line
(Current_Out
);
1201 procedure Putc
(ch
: int
; File
: File_Type
) is
1203 if fputc
(ch
, File
.Stream
) = EOF
then
1212 -- This is the primitive Stream Read routine, used when a Text_IO file
1213 -- is treated directly as a stream using Text_IO.Streams.Stream.
1216 (File
: in out Wide_Wide_Text_AFCB
;
1217 Item
: out Stream_Element_Array
;
1218 Last
: out Stream_Element_Offset
)
1221 pragma Unreferenced
(Discard_ch
);
1224 -- Need to deal with Before_Wide_Wide_Character ???
1226 if File
.Mode
/= FCB
.In_File
then
1230 -- Deal with case where our logical and physical position do not match
1231 -- because of being after an LM or LM-PM sequence when in fact we are
1232 -- logically positioned before it.
1234 if File
.Before_LM
then
1236 -- If we are before a PM, then it is possible for a stream read
1237 -- to leave us after the LM and before the PM, which is a bit
1238 -- odd. The easiest way to deal with this is to unget the PM,
1239 -- so we are indeed positioned between the characters. This way
1240 -- further stream read operations will work correctly, and the
1241 -- effect on text processing is a little weird, but what can
1242 -- be expected if stream and text input are mixed this way?
1244 if File
.Before_LM_PM
then
1245 Discard_ch
:= ungetc
(PM
, File
.Stream
);
1246 File
.Before_LM_PM
:= False;
1249 File
.Before_LM
:= False;
1251 Item
(Item
'First) := Stream_Element
(Character'Pos (ASCII
.LF
));
1253 if Item
'Length = 1 then
1259 Stream_Element_Offset
1260 (fread
(buffer
=> Item
'Address,
1261 index
=> size_t
(Item
'First + 1),
1263 count
=> Item
'Length - 1,
1264 stream
=> File
.Stream
));
1270 -- Now we do the read. Since this is a text file, it is normally in
1271 -- text mode, but stream data must be read in binary mode, so we
1272 -- temporarily set binary mode for the read, resetting it after.
1273 -- These calls have no effect in a system (like Unix) where there is
1274 -- no distinction between text and binary files.
1276 set_binary_mode
(fileno
(File
.Stream
));
1280 Stream_Element_Offset
1281 (fread
(Item
'Address, 1, Item
'Length, File
.Stream
)) - 1;
1283 if Last
< Item
'Last then
1284 if ferror
(File
.Stream
) /= 0 then
1289 set_text_mode
(fileno
(File
.Stream
));
1297 (File
: in out File_Type
;
1301 -- Don't allow change of mode for current file (RM A.10.2(5))
1303 if (File
= Current_In
or else
1304 File
= Current_Out
or else
1305 File
= Current_Error
)
1306 and then To_FCB
(Mode
) /= File
.Mode
1311 Terminate_Line
(File
);
1312 FIO
.Reset
(AP
(File
), To_FCB
(Mode
));
1316 File
.Line_Length
:= 0;
1317 File
.Page_Length
:= 0;
1318 File
.Before_LM
:= False;
1319 File
.Before_LM_PM
:= False;
1322 procedure Reset
(File
: in out File_Type
) is
1324 Terminate_Line
(File
);
1325 FIO
.Reset
(AP
(File
));
1329 File
.Line_Length
:= 0;
1330 File
.Page_Length
:= 0;
1331 File
.Before_LM
:= False;
1332 File
.Before_LM_PM
:= False;
1341 To
: Positive_Count
)
1346 -- Raise Constraint_Error if out of range value. The reason for this
1347 -- explicit test is that we don't want junk values around, even if
1348 -- checks are off in the caller.
1350 if not To
'Valid then
1351 raise Constraint_Error
;
1354 FIO
.Check_File_Open
(AP
(File
));
1356 if To
= File
.Col
then
1360 if Mode
(File
) >= Out_File
then
1361 if File
.Line_Length
/= 0 and then To
> File
.Line_Length
then
1365 if To
< File
.Col
then
1369 while File
.Col
< To
loop
1381 File
.Line
:= File
.Line
+ 1;
1384 elsif ch
= PM
and then File
.Is_Regular_File
then
1385 File
.Page
:= File
.Page
+ 1;
1389 elsif To
= File
.Col
then
1394 File
.Col
:= File
.Col
+ 1;
1400 procedure Set_Col
(To
: Positive_Count
) is
1402 Set_Col
(Current_Out
, To
);
1409 procedure Set_Error
(File
: File_Type
) is
1411 FIO
.Check_Write_Status
(AP
(File
));
1412 Current_Err
:= File
;
1419 procedure Set_Input
(File
: File_Type
) is
1421 FIO
.Check_Read_Status
(AP
(File
));
1431 To
: Positive_Count
)
1434 -- Raise Constraint_Error if out of range value. The reason for this
1435 -- explicit test is that we don't want junk values around, even if
1436 -- checks are off in the caller.
1438 if not To
'Valid then
1439 raise Constraint_Error
;
1442 FIO
.Check_File_Open
(AP
(File
));
1444 if To
= File
.Line
then
1448 if Mode
(File
) >= Out_File
then
1449 if File
.Page_Length
/= 0 and then To
> File
.Page_Length
then
1453 if To
< File
.Line
then
1457 while File
.Line
< To
loop
1462 while To
/= File
.Line
loop
1468 procedure Set_Line
(To
: Positive_Count
) is
1470 Set_Line
(Current_Out
, To
);
1473 ---------------------
1474 -- Set_Line_Length --
1475 ---------------------
1477 procedure Set_Line_Length
(File
: File_Type
; To
: Count
) is
1479 -- Raise Constraint_Error if out of range value. The reason for this
1480 -- explicit test is that we don't want junk values around, even if
1481 -- checks are off in the caller.
1483 if not To
'Valid then
1484 raise Constraint_Error
;
1487 FIO
.Check_Write_Status
(AP
(File
));
1488 File
.Line_Length
:= To
;
1489 end Set_Line_Length
;
1491 procedure Set_Line_Length
(To
: Count
) is
1493 Set_Line_Length
(Current_Out
, To
);
1494 end Set_Line_Length
;
1500 procedure Set_Output
(File
: File_Type
) is
1502 FIO
.Check_Write_Status
(AP
(File
));
1503 Current_Out
:= File
;
1506 ---------------------
1507 -- Set_Page_Length --
1508 ---------------------
1510 procedure Set_Page_Length
(File
: File_Type
; To
: Count
) is
1512 -- Raise Constraint_Error if out of range value. The reason for this
1513 -- explicit test is that we don't want junk values around, even if
1514 -- checks are off in the caller.
1516 if not To
'Valid then
1517 raise Constraint_Error
;
1520 FIO
.Check_Write_Status
(AP
(File
));
1521 File
.Page_Length
:= To
;
1522 end Set_Page_Length
;
1524 procedure Set_Page_Length
(To
: Count
) is
1526 Set_Page_Length
(Current_Out
, To
);
1527 end Set_Page_Length
;
1533 procedure Set_WCEM
(File
: in out File_Type
) is
1538 File
.WC_Method
:= WCEM_Brackets
;
1539 FIO
.Form_Parameter
(File
.Form
.all, "wcem", Start
, Stop
);
1542 File
.WC_Method
:= WCEM_Brackets
;
1544 elsif Start
/= 0 then
1545 if Stop
= Start
then
1546 for J
in WC_Encoding_Letters
'Range loop
1547 if File
.Form
(Start
) = WC_Encoding_Letters
(J
) then
1548 File
.WC_Method
:= J
;
1555 Raise_Exception
(Use_Error
'Identity, "invalid WCEM form parameter");
1565 Spacing
: Positive_Count
:= 1)
1570 -- Raise Constraint_Error if out of range value. The reason for this
1571 -- explicit test is that we don't want junk values around, even if
1572 -- checks are off in the caller.
1574 if not Spacing
'Valid then
1575 raise Constraint_Error
;
1578 FIO
.Check_Read_Status
(AP
(File
));
1580 for L
in 1 .. Spacing
loop
1581 if File
.Before_LM
then
1582 File
.Before_LM
:= False;
1583 File
.Before_LM_PM
:= False;
1588 -- If at end of file now, then immediately raise End_Error. Note
1589 -- that we can never be positioned between a line mark and a page
1590 -- mark, so if we are at the end of file, we cannot logically be
1591 -- before the implicit page mark that is at the end of the file.
1593 -- For the same reason, we do not need an explicit check for a
1594 -- page mark. If there is a FF in the middle of a line, the file
1595 -- is not in canonical format and we do not care about the page
1596 -- numbers for files other than ones in canonical format.
1602 -- If not at end of file, then loop till we get to an LM or EOF.
1603 -- The latter case happens only in non-canonical files where the
1604 -- last line is not terminated by LM, but we don't want to blow
1605 -- up for such files, so we assume an implicit LM in this case.
1608 exit when ch
= LM
or ch
= EOF
;
1613 -- We have got past a line mark, now, for a regular file only,
1614 -- see if a page mark immediately follows this line mark and
1615 -- if so, skip past the page mark as well. We do not do this
1616 -- for non-regular files, since it would cause an undesirable
1617 -- wait for an additional character.
1620 File
.Line
:= File
.Line
+ 1;
1622 if File
.Before_LM_PM
then
1623 File
.Page
:= File
.Page
+ 1;
1625 File
.Before_LM_PM
:= False;
1627 elsif File
.Is_Regular_File
then
1630 -- Page mark can be explicit, or implied at the end of the file
1632 if (ch
= PM
or else ch
= EOF
)
1633 and then File
.Is_Regular_File
1635 File
.Page
:= File
.Page
+ 1;
1644 File
.Before_Wide_Wide_Character
:= False;
1647 procedure Skip_Line
(Spacing
: Positive_Count
:= 1) is
1649 Skip_Line
(Current_In
, Spacing
);
1656 procedure Skip_Page
(File
: File_Type
) is
1660 FIO
.Check_Read_Status
(AP
(File
));
1662 -- If at page mark already, just skip it
1664 if File
.Before_LM_PM
then
1665 File
.Before_LM
:= False;
1666 File
.Before_LM_PM
:= False;
1667 File
.Page
:= File
.Page
+ 1;
1673 -- This is a bit tricky, if we are logically before an LM then
1674 -- it is not an error if we are at an end of file now, since we
1675 -- are not really at it.
1677 if File
.Before_LM
then
1678 File
.Before_LM
:= False;
1679 File
.Before_LM_PM
:= False;
1682 -- Otherwise we do raise End_Error if we are at the end of file now
1692 -- Now we can just rumble along to the next page mark, or to the
1693 -- end of file, if that comes first. The latter case happens when
1694 -- the page mark is implied at the end of file.
1698 or else (ch
= PM
and then File
.Is_Regular_File
);
1702 File
.Page
:= File
.Page
+ 1;
1705 File
.Before_Wide_Wide_Character
:= False;
1708 procedure Skip_Page
is
1710 Skip_Page
(Current_In
);
1713 --------------------
1714 -- Standard_Error --
1715 --------------------
1717 function Standard_Error
return File_Type
is
1719 return Standard_Err
;
1722 function Standard_Error
return File_Access
is
1724 return Standard_Err
'Access;
1727 --------------------
1728 -- Standard_Input --
1729 --------------------
1731 function Standard_Input
return File_Type
is
1736 function Standard_Input
return File_Access
is
1738 return Standard_In
'Access;
1741 ---------------------
1742 -- Standard_Output --
1743 ---------------------
1745 function Standard_Output
return File_Type
is
1747 return Standard_Out
;
1748 end Standard_Output
;
1750 function Standard_Output
return File_Access
is
1752 return Standard_Out
'Access;
1753 end Standard_Output
;
1755 --------------------
1756 -- Terminate_Line --
1757 --------------------
1759 procedure Terminate_Line
(File
: File_Type
) is
1761 FIO
.Check_File_Open
(AP
(File
));
1763 -- For file other than In_File, test for needing to terminate last line
1765 if Mode
(File
) /= In_File
then
1767 -- If not at start of line definition need new line
1769 if File
.Col
/= 1 then
1772 -- For files other than standard error and standard output, we
1773 -- make sure that an empty file has a single line feed, so that
1774 -- it is properly formatted. We avoid this for the standard files
1775 -- because it is too much of a nuisance to have these odd line
1776 -- feeds when nothing has been written to the file.
1778 elsif (File
/= Standard_Err
and then File
/= Standard_Out
)
1779 and then (File
.Line
= 1 and then File
.Page
= 1)
1790 procedure Ungetc
(ch
: int
; File
: File_Type
) is
1793 if ungetc
(ch
, File
.Stream
) = EOF
then
1803 -- This is the primitive Stream Write routine, used when a Text_IO file
1804 -- is treated directly as a stream using Text_IO.Streams.Stream.
1807 (File
: in out Wide_Wide_Text_AFCB
;
1808 Item
: Stream_Element_Array
)
1810 Siz
: constant size_t
:= Item
'Length;
1813 if File
.Mode
= FCB
.In_File
then
1817 -- Now we do the write. Since this is a text file, it is normally in
1818 -- text mode, but stream data must be written in binary mode, so we
1819 -- temporarily set binary mode for the write, resetting it after.
1820 -- These calls have no effect in a system (like Unix) where there is
1821 -- no distinction between text and binary files.
1823 set_binary_mode
(fileno
(File
.Stream
));
1825 if fwrite
(Item
'Address, 1, Siz
, File
.Stream
) /= Siz
then
1829 set_text_mode
(fileno
(File
.Stream
));
1832 -- Use "preallocated" strings to avoid calling "new" during the
1833 -- elaboration of the run time. This is needed in the tasking case to
1834 -- avoid calling Task_Lock too early. A filename is expected to end with
1835 -- a null character in the runtime, here the null characters are added
1836 -- just to have a correct filename length.
1838 Err_Name
: aliased String := "*stderr" & ASCII
.Nul
;
1839 In_Name
: aliased String := "*stdin" & ASCII
.Nul
;
1840 Out_Name
: aliased String := "*stdout" & ASCII
.Nul
;
1843 -------------------------------
1844 -- Initialize Standard Files --
1845 -------------------------------
1847 for J
in WC_Encoding_Method
loop
1848 if WC_Encoding
= WC_Encoding_Letters
(J
) then
1853 -- Note: the names in these files are bogus, and probably it would be
1854 -- better for these files to have no names, but the ACVC test insist!
1855 -- We use names that are bound to fail in open etc.
1857 Standard_Err
.Stream
:= stderr
;
1858 Standard_Err
.Name
:= Err_Name
'Access;
1859 Standard_Err
.Form
:= Null_Str
'Unrestricted_Access;
1860 Standard_Err
.Mode
:= FCB
.Out_File
;
1861 Standard_Err
.Is_Regular_File
:= is_regular_file
(fileno
(stderr
)) /= 0;
1862 Standard_Err
.Is_Temporary_File
:= False;
1863 Standard_Err
.Is_System_File
:= True;
1864 Standard_Err
.Is_Text_File
:= True;
1865 Standard_Err
.Access_Method
:= 'T';
1866 Standard_Err
.WC_Method
:= Default_WCEM
;
1868 Standard_In
.Stream
:= stdin
;
1869 Standard_In
.Name
:= In_Name
'Access;
1870 Standard_In
.Form
:= Null_Str
'Unrestricted_Access;
1871 Standard_In
.Mode
:= FCB
.In_File
;
1872 Standard_In
.Is_Regular_File
:= is_regular_file
(fileno
(stdin
)) /= 0;
1873 Standard_In
.Is_Temporary_File
:= False;
1874 Standard_In
.Is_System_File
:= True;
1875 Standard_In
.Is_Text_File
:= True;
1876 Standard_In
.Access_Method
:= 'T';
1877 Standard_In
.WC_Method
:= Default_WCEM
;
1879 Standard_Out
.Stream
:= stdout
;
1880 Standard_Out
.Name
:= Out_Name
'Access;
1881 Standard_Out
.Form
:= Null_Str
'Unrestricted_Access;
1882 Standard_Out
.Mode
:= FCB
.Out_File
;
1883 Standard_Out
.Is_Regular_File
:= is_regular_file
(fileno
(stdout
)) /= 0;
1884 Standard_Out
.Is_Temporary_File
:= False;
1885 Standard_Out
.Is_System_File
:= True;
1886 Standard_Out
.Is_Text_File
:= True;
1887 Standard_Out
.Access_Method
:= 'T';
1888 Standard_Out
.WC_Method
:= Default_WCEM
;
1890 FIO
.Chain_File
(AP
(Standard_In
));
1891 FIO
.Chain_File
(AP
(Standard_Out
));
1892 FIO
.Chain_File
(AP
(Standard_Err
));
1894 FIO
.Make_Unbuffered
(AP
(Standard_Out
));
1895 FIO
.Make_Unbuffered
(AP
(Standard_Err
));
1897 end Ada
.Wide_Wide_Text_IO
;