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-2005 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;
41 with System.WCh_Cnv; use System.WCh_Cnv;
42 with System.WCh_Con; use System.WCh_Con;
44 with Unchecked_Conversion;
45 with Unchecked_Deallocation;
47 pragma Elaborate_All (System.File_IO);
48 -- Needed because of calls to Chain_File in package body elaboration
50 package body Ada.Wide_Wide_Text_IO is
52 package FIO renames System.File_IO;
54 subtype AP is FCB.AFCB_Ptr;
56 function To_FCB is new Unchecked_Conversion (File_Mode, FCB.File_Mode);
57 function To_TIO is new Unchecked_Conversion (FCB.File_Mode, File_Mode);
58 use type FCB.File_Mode;
60 use type System.CRTL.size_t;
62 WC_Encoding : Character;
63 pragma Import (C, WC_Encoding, "__gl_wc_encoding");
65 -----------------------
66 -- Local Subprograms --
67 -----------------------
69 function Getc_Immed (File : File_Type) return int;
70 -- This routine is identical to Getc, except that the read is done in
71 -- Get_Immediate mode (i.e. without waiting for a line return).
73 function Get_Wide_Wide_Char_Immed
75 File : File_Type) return Wide_Wide_Character;
76 -- This routine is identical to Get_Wide_Wide_Char, except that the reads
77 -- are 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_Wide_Text_AFCB) return FCB.AFCB_Ptr
91 pragma Unreferenced (Control_Block);
93 return new Wide_Wide_Text_AFCB;
100 procedure AFCB_Close (File : access Wide_Wide_Text_AFCB) is
102 -- If the file being closed is one of the current files, then close
103 -- the corresponding current file. It is not clear that this action
104 -- is required (RM A.10.3(23)) but it seems reasonable, and besides
105 -- ACVC test CE3208A expects this behavior.
107 if File_Type (File) = Current_In then
109 elsif File_Type (File) = Current_Out then
111 elsif File_Type (File) = Current_Err then
115 Terminate_Line (File_Type (File));
122 procedure AFCB_Free (File : access Wide_Wide_Text_AFCB) is
123 type FCB_Ptr is access all Wide_Wide_Text_AFCB;
124 FT : FCB_Ptr := FCB_Ptr (File);
126 procedure Free is new
127 Unchecked_Deallocation (Wide_Wide_Text_AFCB, FCB_Ptr);
137 procedure Close (File : in out File_Type) is
139 FIO.Close (AP (File));
146 -- Note: we assume that it is impossible in practice for the column
147 -- to exceed the value of Count'Last, i.e. no check is required for
148 -- overflow raising layout error.
150 function Col (File : File_Type) return Positive_Count is
152 FIO.Check_File_Open (AP (File));
156 function Col return Positive_Count is
158 return Col (Current_Out);
166 (File : in out File_Type;
167 Mode : File_Mode := Out_File;
171 Dummy_File_Control_Block : Wide_Wide_Text_AFCB;
172 pragma Warnings (Off, Dummy_File_Control_Block);
173 -- Yes, we know this is never assigned a value, only the tag
174 -- is used for dispatching purposes, so that's expected.
177 FIO.Open (File_Ptr => AP (File),
178 Dummy_FCB => Dummy_File_Control_Block,
179 Mode => To_FCB (Mode),
192 function Current_Error return File_Type is
197 function Current_Error return File_Access is
199 return Current_Err'Access;
206 function Current_Input return File_Type is
211 function Current_Input return File_Access is
213 return Current_In'Access;
220 function Current_Output return File_Type is
225 function Current_Output return File_Access is
227 return Current_Out'Access;
234 procedure Delete (File : in out File_Type) is
236 FIO.Delete (AP (File));
243 function End_Of_File (File : File_Type) return Boolean is
247 FIO.Check_Read_Status (AP (File));
249 if File.Before_Wide_Wide_Character then
252 elsif File.Before_LM then
254 if File.Before_LM_PM then
255 return Nextc (File) = EOF;
269 File.Before_LM := True;
273 -- Here we are just past the line mark with Before_LM set so that we
274 -- do not have to try to back up past the LM, thus avoiding the need
275 -- to back up more than one character.
282 elsif ch = PM and then File.Is_Regular_File then
283 File.Before_LM_PM := True;
284 return Nextc (File) = EOF;
286 -- Here if neither EOF nor PM followed end of line
295 function End_Of_File return Boolean is
297 return End_Of_File (Current_In);
304 function End_Of_Line (File : File_Type) return Boolean is
308 FIO.Check_Read_Status (AP (File));
310 if File.Before_Wide_Wide_Character then
313 elsif File.Before_LM then
329 function End_Of_Line return Boolean is
331 return End_Of_Line (Current_In);
338 function End_Of_Page (File : File_Type) return Boolean is
342 FIO.Check_Read_Status (AP (File));
344 if not File.Is_Regular_File then
347 elsif File.Before_Wide_Wide_Character then
350 elsif File.Before_LM then
351 if File.Before_LM_PM then
366 File.Before_LM := True;
370 -- Here we are just past the line mark with Before_LM set so that we
371 -- do not have to try to back up past the LM, thus avoiding the need
372 -- to back up more than one character.
376 return ch = PM or else ch = EOF;
379 function End_Of_Page return Boolean is
381 return End_Of_Page (Current_In);
388 procedure Flush (File : File_Type) is
390 FIO.Flush (AP (File));
402 function Form (File : File_Type) return String is
404 return FIO.Form (AP (File));
413 Item : out Wide_Wide_Character)
418 FIO.Check_Read_Status (AP (File));
420 if File.Before_Wide_Wide_Character then
421 File.Before_Wide_Wide_Character := False;
422 Item := File.Saved_Wide_Wide_Character;
425 Get_Character (File, C);
426 Item := Get_Wide_Wide_Char (C, File);
430 procedure Get (Item : out Wide_Wide_Character) is
432 Get (Current_In, Item);
437 Item : out Wide_Wide_String)
440 for J in Item'Range loop
441 Get (File, Item (J));
445 procedure Get (Item : out Wide_Wide_String) is
447 Get (Current_In, Item);
454 procedure Get_Character
456 Item : out Character)
461 if File.Before_LM then
462 File.Before_LM := False;
463 File.Before_LM_PM := False;
466 if File.Before_LM_PM then
468 File.Page := File.Page + 1;
469 File.Before_LM_PM := False;
472 File.Line := File.Line + 1;
483 File.Line := File.Line + 1;
486 elsif ch = PM and then File.Is_Regular_File then
487 File.Page := File.Page + 1;
491 Item := Character'Val (ch);
492 File.Col := File.Col + 1;
502 procedure Get_Immediate
504 Item : out Wide_Wide_Character)
509 FIO.Check_Read_Status (AP (File));
511 if File.Before_Wide_Wide_Character then
512 File.Before_Wide_Wide_Character := False;
513 Item := File.Saved_Wide_Wide_Character;
515 elsif File.Before_LM then
516 File.Before_LM := False;
517 File.Before_LM_PM := False;
518 Item := Wide_Wide_Character'Val (LM);
521 ch := Getc_Immed (File);
526 Item := Get_Wide_Wide_Char_Immed (Character'Val (ch), File);
531 procedure Get_Immediate
532 (Item : out Wide_Wide_Character)
535 Get_Immediate (Current_In, Item);
538 procedure Get_Immediate
540 Item : out Wide_Wide_Character;
541 Available : out Boolean)
546 FIO.Check_Read_Status (AP (File));
549 if File.Before_Wide_Wide_Character then
550 File.Before_Wide_Wide_Character := False;
551 Item := File.Saved_Wide_Wide_Character;
553 elsif File.Before_LM then
554 File.Before_LM := False;
555 File.Before_LM_PM := False;
556 Item := Wide_Wide_Character'Val (LM);
559 ch := Getc_Immed (File);
564 Item := Get_Wide_Wide_Char_Immed (Character'Val (ch), File);
569 procedure Get_Immediate
570 (Item : out Wide_Wide_Character;
571 Available : out Boolean)
574 Get_Immediate (Current_In, Item, Available);
583 Item : out Wide_Wide_String;
587 FIO.Check_Read_Status (AP (File));
588 Last := Item'First - 1;
590 -- Immediate exit for null string, this is a case in which we do not
591 -- need to test for end of file and we do not skip a line mark under
592 -- any circumstances.
594 if Last >= Item'Last then
598 -- Here we have at least one character, if we are immediately before
599 -- a line mark, then we will just skip past it storing no characters.
601 if File.Before_LM then
602 File.Before_LM := False;
603 File.Before_LM_PM := False;
605 -- Otherwise we need to read some characters
608 -- If we are at the end of file now, it means we are trying to
609 -- skip a file terminator and we raise End_Error (RM A.10.7(20))
611 if Nextc (File) = EOF then
615 -- Loop through characters in string
618 -- Exit the loop if read is terminated by encountering line mark
619 -- Note that the use of Skip_Line here ensures we properly deal
620 -- with setting the page and line numbers.
622 if End_Of_Line (File) then
627 -- Otherwise store the character, note that we know that ch is
628 -- something other than LM or EOF. It could possibly be a page
629 -- mark if there is a stray page mark in the middle of a line,
630 -- but this is not an official page mark in any case, since
631 -- official page marks can only follow a line mark. The whole
632 -- page business is pretty much nonsense anyway, so we do not
633 -- want to waste time trying to make sense out of non-standard
634 -- page marks in the file! This means that the behavior of
635 -- Get_Line is different from repeated Get of a character, but
636 -- that's too bad. We only promise that page numbers etc make
637 -- sense if the file is formatted in a standard manner.
639 -- Note: we do not adjust the column number because it is quicker
640 -- to adjust it once at the end of the operation than incrementing
641 -- it each time around the loop.
644 Get (File, Item (Last));
646 -- All done if the string is full, this is the case in which
647 -- we do not skip the following line mark. We need to adjust
648 -- the column number in this case.
650 if Last = Item'Last then
651 File.Col := File.Col + Count (Item'Length);
655 -- Exit from the loop if we are at the end of file. This happens
656 -- if we have a last line that is not terminated with a line mark.
657 -- In this case we consider that there is an implied line mark;
658 -- this is a non-standard file, but we will treat it nicely.
660 exit when Nextc (File) = EOF;
666 (Item : out Wide_Wide_String;
670 Get_Line (Current_In, Item, Last);
673 function Get_Line (File : File_Type) return Wide_Wide_String is
674 Buffer : Wide_Wide_String (1 .. 500);
677 function Get_Rest (S : Wide_Wide_String) return Wide_Wide_String;
678 -- This is a recursive function that reads the rest of the line and
679 -- returns it. S is the part read so far.
685 function Get_Rest (S : Wide_Wide_String) return Wide_Wide_String is
687 -- Each time we allocate a buffer the same size as what we have
688 -- read so far. This limits us to a logarithmic number of calls
689 -- to Get_Rest and also ensures only a linear use of stack space.
691 Buffer : Wide_Wide_String (1 .. S'Length);
695 Get_Line (File, Buffer, Last);
698 R : constant Wide_Wide_String := S & Buffer (1 .. Last);
700 if Last < Buffer'Last then
708 -- Start of processing for Get_Line
711 Get_Line (File, Buffer, Last);
713 if Last < Buffer'Last then
714 return Buffer (1 .. Last);
716 return Get_Rest (Buffer (1 .. Last));
720 function Get_Line return Wide_Wide_String is
722 return Get_Line (Current_In);
725 ------------------------
726 -- Get_Wide_Wide_Char --
727 ------------------------
729 function Get_Wide_Wide_Char
731 File : File_Type) return Wide_Wide_Character
733 function In_Char return Character;
734 -- Function used to obtain additional characters it the wide character
735 -- sequence is more than one character long.
737 function WC_In is new Char_Sequence_To_UTF_32 (In_Char);
743 function In_Char return Character is
744 ch : constant Integer := Getc (File);
749 return Character'Val (ch);
753 -- Start of processing for Get_Wide_Wide_Char
756 return Wide_Wide_Character'Val (WC_In (C, File.WC_Method));
757 end Get_Wide_Wide_Char;
759 ------------------------------
760 -- Get_Wide_Wide_Char_Immed --
761 ------------------------------
763 function Get_Wide_Wide_Char_Immed
765 File : File_Type) return Wide_Wide_Character
767 function In_Char return Character;
768 -- Function used to obtain additional characters it the wide character
769 -- sequence is more than one character long.
771 function WC_In is new Char_Sequence_To_UTF_32 (In_Char);
777 function In_Char return Character is
778 ch : constant Integer := Getc_Immed (File);
783 return Character'Val (ch);
787 -- Start of processing for Get_Wide_Wide_Char_Immed
790 return Wide_Wide_Character'Val (WC_In (C, File.WC_Method));
791 end Get_Wide_Wide_Char_Immed;
797 function Getc (File : File_Type) return int is
801 ch := fgetc (File.Stream);
803 if ch = EOF and then ferror (File.Stream) /= 0 then
814 function Getc_Immed (File : File_Type) return int is
818 procedure getc_immediate
819 (stream : FILEs; ch : out int; end_of_file : out int);
820 pragma Import (C, getc_immediate, "getc_immediate");
823 FIO.Check_Read_Status (AP (File));
825 if File.Before_LM then
826 File.Before_LM := False;
827 File.Before_LM_PM := False;
831 getc_immediate (File.Stream, ch, end_of_file);
833 if ferror (File.Stream) /= 0 then
835 elsif end_of_file /= 0 then
847 function Is_Open (File : File_Type) return Boolean is
849 return FIO.Is_Open (AP (File));
856 -- Note: we assume that it is impossible in practice for the line
857 -- to exceed the value of Count'Last, i.e. no check is required for
858 -- overflow raising layout error.
860 function Line (File : File_Type) return Positive_Count is
862 FIO.Check_File_Open (AP (File));
866 function Line return Positive_Count is
868 return Line (Current_Out);
875 function Line_Length (File : File_Type) return Count is
877 FIO.Check_Write_Status (AP (File));
878 return File.Line_Length;
881 function Line_Length return Count is
883 return Line_Length (Current_Out);
892 Item : out Wide_Wide_Character;
893 End_Of_Line : out Boolean)
897 -- Start of processing for Look_Ahead
900 FIO.Check_Read_Status (AP (File));
902 -- If we are logically before a line mark, we can return immediately
904 if File.Before_LM then
906 Item := Wide_Wide_Character'Val (0);
908 -- If we are before a wide character, just return it (this happens
909 -- if there are two calls to Look_Ahead in a row).
911 elsif File.Before_Wide_Wide_Character then
912 End_Of_Line := False;
913 Item := File.Saved_Wide_Wide_Character;
915 -- otherwise we must read a character from the input stream
922 or else (ch = EOF and then File.Is_Regular_File)
926 Item := Wide_Wide_Character'Val (0);
928 -- If the character is in the range 16#0000# to 16#007F# it stands
929 -- for itself and occupies a single byte, so we can unget it with
932 elsif ch <= 16#0080# then
933 End_Of_Line := False;
935 Item := Wide_Wide_Character'Val (ch);
937 -- For a character above this range, we read the character, using
938 -- the Get_Wide_Wide_Char routine. It may well occupy more than one
939 -- byte so we can't put it back with ungetc. Instead we save it in
940 -- the control block, setting a flag that everyone interested in
941 -- reading characters must test before reading the stream.
944 Item := Get_Wide_Wide_Char (Character'Val (ch), File);
945 End_Of_Line := False;
946 File.Saved_Wide_Wide_Character := Item;
947 File.Before_Wide_Wide_Character := True;
953 (Item : out Wide_Wide_Character;
954 End_Of_Line : out Boolean)
957 Look_Ahead (Current_In, Item, End_Of_Line);
964 function Mode (File : File_Type) return File_Mode is
966 return To_TIO (FIO.Mode (AP (File)));
973 function Name (File : File_Type) return String is
975 return FIO.Name (AP (File));
984 Spacing : Positive_Count := 1)
987 -- Raise Constraint_Error if out of range value. The reason for this
988 -- explicit test is that we don't want junk values around, even if
989 -- checks are off in the caller.
991 if Spacing not in Positive_Count then
992 raise Constraint_Error;
995 FIO.Check_Write_Status (AP (File));
997 for K in 1 .. Spacing loop
999 File.Line := File.Line + 1;
1001 if File.Page_Length /= 0
1002 and then File.Line > File.Page_Length
1006 File.Page := File.Page + 1;
1013 procedure New_Line (Spacing : Positive_Count := 1) is
1015 New_Line (Current_Out, Spacing);
1022 procedure New_Page (File : File_Type) is
1024 FIO.Check_Write_Status (AP (File));
1026 if File.Col /= 1 or else File.Line = 1 then
1031 File.Page := File.Page + 1;
1036 procedure New_Page is
1038 New_Page (Current_Out);
1045 function Nextc (File : File_Type) return int is
1049 ch := fgetc (File.Stream);
1052 if ferror (File.Stream) /= 0 then
1057 if ungetc (ch, File.Stream) = EOF then
1070 (File : in out File_Type;
1073 Form : String := "")
1075 Dummy_File_Control_Block : Wide_Wide_Text_AFCB;
1076 pragma Warnings (Off, Dummy_File_Control_Block);
1077 -- Yes, we know this is never assigned a value, only the tag
1078 -- is used for dispatching purposes, so that's expected.
1081 FIO.Open (File_Ptr => AP (File),
1082 Dummy_FCB => Dummy_File_Control_Block,
1083 Mode => To_FCB (Mode),
1096 -- Note: we assume that it is impossible in practice for the page
1097 -- to exceed the value of Count'Last, i.e. no check is required for
1098 -- overflow raising layout error.
1100 function Page (File : File_Type) return Positive_Count is
1102 FIO.Check_File_Open (AP (File));
1106 function Page return Positive_Count is
1108 return Page (Current_Out);
1115 function Page_Length (File : File_Type) return Count is
1117 FIO.Check_Write_Status (AP (File));
1118 return File.Page_Length;
1121 function Page_Length return Count is
1123 return Page_Length (Current_Out);
1132 Item : Wide_Wide_Character)
1134 procedure Out_Char (C : Character);
1135 -- Procedure to output one character of a wide character sequence
1137 procedure WC_Out is new UTF_32_To_Char_Sequence (Out_Char);
1143 procedure Out_Char (C : Character) is
1145 Putc (Character'Pos (C), File);
1148 -- Start of processing for Put
1151 WC_Out (Wide_Wide_Character'Pos (Item), File.WC_Method);
1152 File.Col := File.Col + 1;
1155 procedure Put (Item : Wide_Wide_Character) is
1157 Put (Current_Out, Item);
1166 Item : Wide_Wide_String)
1169 for J in Item'Range loop
1170 Put (File, Item (J));
1174 procedure Put (Item : Wide_Wide_String) is
1176 Put (Current_Out, Item);
1185 Item : Wide_Wide_String)
1192 procedure Put_Line (Item : Wide_Wide_String) is
1194 Put (Current_Out, Item);
1195 New_Line (Current_Out);
1202 procedure Putc (ch : int; File : File_Type) is
1204 if fputc (ch, File.Stream) = EOF then
1213 -- This is the primitive Stream Read routine, used when a Text_IO file
1214 -- is treated directly as a stream using Text_IO.Streams.Stream.
1217 (File : in out Wide_Wide_Text_AFCB;
1218 Item : out Stream_Element_Array;
1219 Last : out Stream_Element_Offset)
1222 pragma Unreferenced (Discard_ch);
1225 -- Need to deal with Before_Wide_Wide_Character ???
1227 if File.Mode /= FCB.In_File then
1231 -- Deal with case where our logical and physical position do not match
1232 -- because of being after an LM or LM-PM sequence when in fact we are
1233 -- logically positioned before it.
1235 if File.Before_LM then
1237 -- If we are before a PM, then it is possible for a stream read
1238 -- to leave us after the LM and before the PM, which is a bit
1239 -- odd. The easiest way to deal with this is to unget the PM,
1240 -- so we are indeed positioned between the characters. This way
1241 -- further stream read operations will work correctly, and the
1242 -- effect on text processing is a little weird, but what can
1243 -- be expected if stream and text input are mixed this way?
1245 if File.Before_LM_PM then
1246 Discard_ch := ungetc (PM, File.Stream);
1247 File.Before_LM_PM := False;
1250 File.Before_LM := False;
1252 Item (Item'First) := Stream_Element (Character'Pos (ASCII.LF));
1254 if Item'Length = 1 then
1260 Stream_Element_Offset
1261 (fread (buffer => Item'Address,
1262 index => size_t (Item'First + 1),
1264 count => Item'Length - 1,
1265 stream => File.Stream));
1271 -- Now we do the read. Since this is a text file, it is normally in
1272 -- text mode, but stream data must be read in binary mode, so we
1273 -- temporarily set binary mode for the read, resetting it after.
1274 -- These calls have no effect in a system (like Unix) where there is
1275 -- no distinction between text and binary files.
1277 set_binary_mode (fileno (File.Stream));
1281 Stream_Element_Offset
1282 (fread (Item'Address, 1, Item'Length, File.Stream)) - 1;
1284 if Last < Item'Last then
1285 if ferror (File.Stream) /= 0 then
1290 set_text_mode (fileno (File.Stream));
1298 (File : in out File_Type;
1302 -- Don't allow change of mode for current file (RM A.10.2(5))
1304 if (File = Current_In or else
1305 File = Current_Out or else
1306 File = Current_Error)
1307 and then To_FCB (Mode) /= File.Mode
1312 Terminate_Line (File);
1313 FIO.Reset (AP (File), To_FCB (Mode));
1317 File.Line_Length := 0;
1318 File.Page_Length := 0;
1319 File.Before_LM := False;
1320 File.Before_LM_PM := False;
1323 procedure Reset (File : in out File_Type) is
1325 Terminate_Line (File);
1326 FIO.Reset (AP (File));
1330 File.Line_Length := 0;
1331 File.Page_Length := 0;
1332 File.Before_LM := False;
1333 File.Before_LM_PM := False;
1342 To : Positive_Count)
1347 -- Raise Constraint_Error if out of range value. The reason for this
1348 -- explicit test is that we don't want junk values around, even if
1349 -- checks are off in the caller.
1351 if To not in Positive_Count then
1352 raise Constraint_Error;
1355 FIO.Check_File_Open (AP (File));
1357 if To = File.Col then
1361 if Mode (File) >= Out_File then
1362 if File.Line_Length /= 0 and then To > File.Line_Length then
1366 if To < File.Col then
1370 while File.Col < To loop
1382 File.Line := File.Line + 1;
1385 elsif ch = PM and then File.Is_Regular_File then
1386 File.Page := File.Page + 1;
1390 elsif To = File.Col then
1395 File.Col := File.Col + 1;
1401 procedure Set_Col (To : Positive_Count) is
1403 Set_Col (Current_Out, To);
1410 procedure Set_Error (File : File_Type) is
1412 FIO.Check_Write_Status (AP (File));
1413 Current_Err := File;
1420 procedure Set_Input (File : File_Type) is
1422 FIO.Check_Read_Status (AP (File));
1432 To : Positive_Count)
1435 -- Raise Constraint_Error if out of range value. The reason for this
1436 -- explicit test is that we don't want junk values around, even if
1437 -- checks are off in the caller.
1439 if To not in Positive_Count then
1440 raise Constraint_Error;
1443 FIO.Check_File_Open (AP (File));
1445 if To = File.Line then
1449 if Mode (File) >= Out_File then
1450 if File.Page_Length /= 0 and then To > File.Page_Length then
1454 if To < File.Line then
1458 while File.Line < To loop
1463 while To /= File.Line loop
1469 procedure Set_Line (To : Positive_Count) is
1471 Set_Line (Current_Out, To);
1474 ---------------------
1475 -- Set_Line_Length --
1476 ---------------------
1478 procedure Set_Line_Length (File : File_Type; To : Count) is
1480 -- Raise Constraint_Error if out of range value. The reason for this
1481 -- explicit test is that we don't want junk values around, even if
1482 -- checks are off in the caller.
1484 if To not in Count then
1485 raise Constraint_Error;
1488 FIO.Check_Write_Status (AP (File));
1489 File.Line_Length := To;
1490 end Set_Line_Length;
1492 procedure Set_Line_Length (To : Count) is
1494 Set_Line_Length (Current_Out, To);
1495 end Set_Line_Length;
1501 procedure Set_Output (File : File_Type) is
1503 FIO.Check_Write_Status (AP (File));
1504 Current_Out := File;
1507 ---------------------
1508 -- Set_Page_Length --
1509 ---------------------
1511 procedure Set_Page_Length (File : File_Type; To : Count) is
1513 -- Raise Constraint_Error if out of range value. The reason for this
1514 -- explicit test is that we don't want junk values around, even if
1515 -- checks are off in the caller.
1517 if To not in Count then
1518 raise Constraint_Error;
1521 FIO.Check_Write_Status (AP (File));
1522 File.Page_Length := To;
1523 end Set_Page_Length;
1525 procedure Set_Page_Length (To : Count) is
1527 Set_Page_Length (Current_Out, To);
1528 end Set_Page_Length;
1534 procedure Set_WCEM (File : in out File_Type) is
1539 File.WC_Method := WCEM_Brackets;
1540 FIO.Form_Parameter (File.Form.all, "wcem", Start, Stop);
1543 File.WC_Method := WCEM_Brackets;
1545 elsif Start /= 0 then
1546 if Stop = Start then
1547 for J in WC_Encoding_Letters'Range loop
1548 if File.Form (Start) = WC_Encoding_Letters (J) then
1549 File.WC_Method := J;
1556 Raise_Exception (Use_Error'Identity, "invalid WCEM form parameter");
1566 Spacing : Positive_Count := 1)
1571 -- Raise Constraint_Error if out of range value. The reason for this
1572 -- explicit test is that we don't want junk values around, even if
1573 -- checks are off in the caller.
1575 if Spacing not in Positive_Count then
1576 raise Constraint_Error;
1579 FIO.Check_Read_Status (AP (File));
1581 for L in 1 .. Spacing loop
1582 if File.Before_LM then
1583 File.Before_LM := False;
1584 File.Before_LM_PM := False;
1589 -- If at end of file now, then immediately raise End_Error. Note
1590 -- that we can never be positioned between a line mark and a page
1591 -- mark, so if we are at the end of file, we cannot logically be
1592 -- before the implicit page mark that is at the end of the file.
1594 -- For the same reason, we do not need an explicit check for a
1595 -- page mark. If there is a FF in the middle of a line, the file
1596 -- is not in canonical format and we do not care about the page
1597 -- numbers for files other than ones in canonical format.
1603 -- If not at end of file, then loop till we get to an LM or EOF.
1604 -- The latter case happens only in non-canonical files where the
1605 -- last line is not terminated by LM, but we don't want to blow
1606 -- up for such files, so we assume an implicit LM in this case.
1609 exit when ch = LM or ch = EOF;
1614 -- We have got past a line mark, now, for a regular file only,
1615 -- see if a page mark immediately follows this line mark and
1616 -- if so, skip past the page mark as well. We do not do this
1617 -- for non-regular files, since it would cause an undesirable
1618 -- wait for an additional character.
1621 File.Line := File.Line + 1;
1623 if File.Before_LM_PM then
1624 File.Page := File.Page + 1;
1626 File.Before_LM_PM := False;
1628 elsif File.Is_Regular_File then
1631 -- Page mark can be explicit, or implied at the end of the file
1633 if (ch = PM or else ch = EOF)
1634 and then File.Is_Regular_File
1636 File.Page := File.Page + 1;
1645 File.Before_Wide_Wide_Character := False;
1648 procedure Skip_Line (Spacing : Positive_Count := 1) is
1650 Skip_Line (Current_In, Spacing);
1657 procedure Skip_Page (File : File_Type) is
1661 FIO.Check_Read_Status (AP (File));
1663 -- If at page mark already, just skip it
1665 if File.Before_LM_PM then
1666 File.Before_LM := False;
1667 File.Before_LM_PM := False;
1668 File.Page := File.Page + 1;
1674 -- This is a bit tricky, if we are logically before an LM then
1675 -- it is not an error if we are at an end of file now, since we
1676 -- are not really at it.
1678 if File.Before_LM then
1679 File.Before_LM := False;
1680 File.Before_LM_PM := False;
1683 -- Otherwise we do raise End_Error if we are at the end of file now
1693 -- Now we can just rumble along to the next page mark, or to the
1694 -- end of file, if that comes first. The latter case happens when
1695 -- the page mark is implied at the end of file.
1699 or else (ch = PM and then File.Is_Regular_File);
1703 File.Page := File.Page + 1;
1706 File.Before_Wide_Wide_Character := False;
1709 procedure Skip_Page is
1711 Skip_Page (Current_In);
1714 --------------------
1715 -- Standard_Error --
1716 --------------------
1718 function Standard_Error return File_Type is
1720 return Standard_Err;
1723 function Standard_Error return File_Access is
1725 return Standard_Err'Access;
1728 --------------------
1729 -- Standard_Input --
1730 --------------------
1732 function Standard_Input return File_Type is
1737 function Standard_Input return File_Access is
1739 return Standard_In'Access;
1742 ---------------------
1743 -- Standard_Output --
1744 ---------------------
1746 function Standard_Output return File_Type is
1748 return Standard_Out;
1749 end Standard_Output;
1751 function Standard_Output return File_Access is
1753 return Standard_Out'Access;
1754 end Standard_Output;
1756 --------------------
1757 -- Terminate_Line --
1758 --------------------
1760 procedure Terminate_Line (File : File_Type) is
1762 FIO.Check_File_Open (AP (File));
1764 -- For file other than In_File, test for needing to terminate last line
1766 if Mode (File) /= In_File then
1768 -- If not at start of line definition need new line
1770 if File.Col /= 1 then
1773 -- For files other than standard error and standard output, we
1774 -- make sure that an empty file has a single line feed, so that
1775 -- it is properly formatted. We avoid this for the standard files
1776 -- because it is too much of a nuisance to have these odd line
1777 -- feeds when nothing has been written to the file.
1779 elsif (File /= Standard_Err and then File /= Standard_Out)
1780 and then (File.Line = 1 and then File.Page = 1)
1791 procedure Ungetc (ch : int; File : File_Type) is
1794 if ungetc (ch, File.Stream) = EOF then
1804 -- This is the primitive Stream Write routine, used when a Text_IO file
1805 -- is treated directly as a stream using Text_IO.Streams.Stream.
1808 (File : in out Wide_Wide_Text_AFCB;
1809 Item : Stream_Element_Array)
1811 Siz : constant size_t := Item'Length;
1814 if File.Mode = FCB.In_File then
1818 -- Now we do the write. Since this is a text file, it is normally in
1819 -- text mode, but stream data must be written in binary mode, so we
1820 -- temporarily set binary mode for the write, resetting it after.
1821 -- These calls have no effect in a system (like Unix) where there is
1822 -- no distinction between text and binary files.
1824 set_binary_mode (fileno (File.Stream));
1826 if fwrite (Item'Address, 1, Siz, File.Stream) /= Siz then
1830 set_text_mode (fileno (File.Stream));
1833 -- Use "preallocated" strings to avoid calling "new" during the
1834 -- elaboration of the run time. This is needed in the tasking case to
1835 -- avoid calling Task_Lock too early. A filename is expected to end with
1836 -- a null character in the runtime, here the null characters are added
1837 -- just to have a correct filename length.
1839 Err_Name : aliased String := "*stderr" & ASCII.Nul;
1840 In_Name : aliased String := "*stdin" & ASCII.Nul;
1841 Out_Name : aliased String := "*stdout" & ASCII.Nul;
1844 -------------------------------
1845 -- Initialize Standard Files --
1846 -------------------------------
1848 for J in WC_Encoding_Method loop
1849 if WC_Encoding = WC_Encoding_Letters (J) then
1854 -- Note: the names in these files are bogus, and probably it would be
1855 -- better for these files to have no names, but the ACVC test insist!
1856 -- We use names that are bound to fail in open etc.
1858 Standard_Err.Stream := stderr;
1859 Standard_Err.Name := Err_Name'Access;
1860 Standard_Err.Form := Null_Str'Unrestricted_Access;
1861 Standard_Err.Mode := FCB.Out_File;
1862 Standard_Err.Is_Regular_File := is_regular_file (fileno (stderr)) /= 0;
1863 Standard_Err.Is_Temporary_File := False;
1864 Standard_Err.Is_System_File := True;
1865 Standard_Err.Is_Text_File := True;
1866 Standard_Err.Access_Method := 'T';
1867 Standard_Err.WC_Method := Default_WCEM;
1869 Standard_In.Stream := stdin;
1870 Standard_In.Name := In_Name'Access;
1871 Standard_In.Form := Null_Str'Unrestricted_Access;
1872 Standard_In.Mode := FCB.In_File;
1873 Standard_In.Is_Regular_File := is_regular_file (fileno (stdin)) /= 0;
1874 Standard_In.Is_Temporary_File := False;
1875 Standard_In.Is_System_File := True;
1876 Standard_In.Is_Text_File := True;
1877 Standard_In.Access_Method := 'T';
1878 Standard_In.WC_Method := Default_WCEM;
1880 Standard_Out.Stream := stdout;
1881 Standard_Out.Name := Out_Name'Access;
1882 Standard_Out.Form := Null_Str'Unrestricted_Access;
1883 Standard_Out.Mode := FCB.Out_File;
1884 Standard_Out.Is_Regular_File := is_regular_file (fileno (stdout)) /= 0;
1885 Standard_Out.Is_Temporary_File := False;
1886 Standard_Out.Is_System_File := True;
1887 Standard_Out.Is_Text_File := True;
1888 Standard_Out.Access_Method := 'T';
1889 Standard_Out.WC_Method := Default_WCEM;
1891 FIO.Chain_File (AP (Standard_In));
1892 FIO.Chain_File (AP (Standard_Out));
1893 FIO.Chain_File (AP (Standard_Err));
1895 FIO.Make_Unbuffered (AP (Standard_Out));
1896 FIO.Make_Unbuffered (AP (Standard_Err));
1898 end Ada.Wide_Wide_Text_IO;