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-2008, 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.Streams; use Ada.Streams;
35 with Interfaces.C_Streams; use Interfaces.C_Streams;
39 with System.WCh_Cnv; use System.WCh_Cnv;
40 with System.WCh_Con; use System.WCh_Con;
42 with Ada.Unchecked_Conversion;
43 with Ada.Unchecked_Deallocation;
45 pragma Elaborate_All (System.File_IO);
46 -- Needed because of calls to Chain_File in package body elaboration
48 package body Ada.Wide_Wide_Text_IO is
50 package FIO renames System.File_IO;
52 subtype AP is FCB.AFCB_Ptr;
54 function To_FCB is new Ada.Unchecked_Conversion (File_Mode, FCB.File_Mode);
55 function To_TIO is new Ada.Unchecked_Conversion (FCB.File_Mode, File_Mode);
56 use type FCB.File_Mode;
58 use type System.CRTL.size_t;
60 WC_Encoding : Character;
61 pragma Import (C, WC_Encoding, "__gl_wc_encoding");
63 -----------------------
64 -- Local Subprograms --
65 -----------------------
67 function Getc_Immed (File : File_Type) return int;
68 -- This routine is identical to Getc, except that the read is done in
69 -- Get_Immediate mode (i.e. without waiting for a line return).
71 function Get_Wide_Wide_Char_Immed
73 File : File_Type) return Wide_Wide_Character;
74 -- This routine is identical to Get_Wide_Wide_Char, except that the reads
75 -- are done in Get_Immediate mode (i.e. without waiting for a line return).
77 procedure Set_WCEM (File : in out File_Type);
78 -- Called by Open and Create to set the wide character encoding method for
79 -- the file, processing a WCEM form parameter if one is present. File is
80 -- IN OUT because it may be closed in case of an error.
86 function AFCB_Allocate
87 (Control_Block : Wide_Wide_Text_AFCB) return FCB.AFCB_Ptr
89 pragma Unreferenced (Control_Block);
91 return new Wide_Wide_Text_AFCB;
98 procedure AFCB_Close (File : not null access Wide_Wide_Text_AFCB) is
100 -- If the file being closed is one of the current files, then close
101 -- the corresponding current file. It is not clear that this action
102 -- is required (RM A.10.3(23)) but it seems reasonable, and besides
103 -- ACVC test CE3208A expects this behavior.
105 if File_Type (File) = Current_In then
107 elsif File_Type (File) = Current_Out then
109 elsif File_Type (File) = Current_Err then
113 Terminate_Line (File_Type (File));
120 procedure AFCB_Free (File : not null access Wide_Wide_Text_AFCB) is
121 type FCB_Ptr is access all Wide_Wide_Text_AFCB;
122 FT : FCB_Ptr := FCB_Ptr (File);
124 procedure Free is new
125 Ada.Unchecked_Deallocation (Wide_Wide_Text_AFCB, FCB_Ptr);
135 procedure Close (File : in out File_Type) is
137 for AFCB'Address use File'Address;
138 pragma Import (Ada, AFCB);
140 FIO.Close (AFCB'Access);
147 -- Note: we assume that it is impossible in practice for the column
148 -- to exceed the value of Count'Last, i.e. no check is required for
149 -- overflow raising layout error.
151 function Col (File : File_Type) return Positive_Count is
153 FIO.Check_File_Open (AP (File));
157 function Col return Positive_Count is
159 return Col (Current_Out);
167 (File : in out File_Type;
168 Mode : File_Mode := Out_File;
172 Dummy_File_Control_Block : Wide_Wide_Text_AFCB;
173 pragma Warnings (Off, Dummy_File_Control_Block);
174 -- Yes, we know this is never assigned a value, only the tag
175 -- is used for dispatching purposes, so that's expected.
178 FIO.Open (File_Ptr => AP (File),
179 Dummy_FCB => Dummy_File_Control_Block,
180 Mode => To_FCB (Mode),
195 function Current_Error return File_Type is
200 function Current_Error return File_Access is
202 return Current_Err.Self'Access;
209 function Current_Input return File_Type is
214 function Current_Input return File_Access is
216 return Current_In.Self'Access;
223 function Current_Output return File_Type is
228 function Current_Output return File_Access is
230 return Current_Out.Self'Access;
237 procedure Delete (File : in out File_Type) is
239 for AFCB'Address use File'Address;
240 pragma Import (Ada, AFCB);
242 FIO.Delete (AFCB'Access);
249 function End_Of_File (File : File_Type) return Boolean is
253 FIO.Check_Read_Status (AP (File));
255 if File.Before_Wide_Wide_Character then
258 elsif File.Before_LM then
259 if File.Before_LM_PM then
260 return Nextc (File) = EOF;
274 File.Before_LM := True;
278 -- Here we are just past the line mark with Before_LM set so that we
279 -- do not have to try to back up past the LM, thus avoiding the need
280 -- to back up more than one character.
287 elsif ch = PM and then File.Is_Regular_File then
288 File.Before_LM_PM := True;
289 return Nextc (File) = EOF;
291 -- Here if neither EOF nor PM followed end of line
300 function End_Of_File return Boolean is
302 return End_Of_File (Current_In);
309 function End_Of_Line (File : File_Type) return Boolean is
313 FIO.Check_Read_Status (AP (File));
315 if File.Before_Wide_Wide_Character then
318 elsif File.Before_LM then
334 function End_Of_Line return Boolean is
336 return End_Of_Line (Current_In);
343 function End_Of_Page (File : File_Type) return Boolean is
347 FIO.Check_Read_Status (AP (File));
349 if not File.Is_Regular_File then
352 elsif File.Before_Wide_Wide_Character then
355 elsif File.Before_LM then
356 if File.Before_LM_PM then
371 File.Before_LM := True;
375 -- Here we are just past the line mark with Before_LM set so that we
376 -- do not have to try to back up past the LM, thus avoiding the need
377 -- to back up more than one character.
381 return ch = PM or else ch = EOF;
384 function End_Of_Page return Boolean is
386 return End_Of_Page (Current_In);
393 procedure Flush (File : File_Type) is
395 FIO.Flush (AP (File));
407 function Form (File : File_Type) return String is
409 return FIO.Form (AP (File));
418 Item : out Wide_Wide_Character)
423 FIO.Check_Read_Status (AP (File));
425 if File.Before_Wide_Wide_Character then
426 File.Before_Wide_Wide_Character := False;
427 Item := File.Saved_Wide_Wide_Character;
429 -- Ada.Text_IO checks Before_LM_PM here, shouldn't we do the same???
432 Get_Character (File, C);
433 Item := Get_Wide_Wide_Char (C, File);
437 procedure Get (Item : out Wide_Wide_Character) is
439 Get (Current_In, Item);
444 Item : out Wide_Wide_String)
447 for J in Item'Range loop
448 Get (File, Item (J));
452 procedure Get (Item : out Wide_Wide_String) is
454 Get (Current_In, Item);
461 procedure Get_Character
463 Item : out Character)
468 if File.Before_LM then
469 File.Before_LM := False;
470 File.Before_LM_PM := False;
473 if File.Before_LM_PM then
475 File.Page := File.Page + 1;
476 File.Before_LM_PM := False;
479 File.Line := File.Line + 1;
490 File.Line := File.Line + 1;
493 elsif ch = PM and then File.Is_Regular_File then
494 File.Page := File.Page + 1;
498 Item := Character'Val (ch);
499 File.Col := File.Col + 1;
509 procedure Get_Immediate
511 Item : out Wide_Wide_Character)
516 FIO.Check_Read_Status (AP (File));
518 if File.Before_Wide_Wide_Character then
519 File.Before_Wide_Wide_Character := False;
520 Item := File.Saved_Wide_Wide_Character;
522 elsif File.Before_LM then
523 File.Before_LM := False;
524 File.Before_LM_PM := False;
525 Item := Wide_Wide_Character'Val (LM);
528 ch := Getc_Immed (File);
533 Item := Get_Wide_Wide_Char_Immed (Character'Val (ch), File);
538 procedure Get_Immediate
539 (Item : out Wide_Wide_Character)
542 Get_Immediate (Current_In, Item);
545 procedure Get_Immediate
547 Item : out Wide_Wide_Character;
548 Available : out Boolean)
553 FIO.Check_Read_Status (AP (File));
556 if File.Before_Wide_Wide_Character then
557 File.Before_Wide_Wide_Character := False;
558 Item := File.Saved_Wide_Wide_Character;
560 elsif File.Before_LM then
561 File.Before_LM := False;
562 File.Before_LM_PM := False;
563 Item := Wide_Wide_Character'Val (LM);
566 -- Shouldn't we use getc_immediate_nowait here, like Text_IO???
568 ch := Getc_Immed (File);
573 Item := Get_Wide_Wide_Char_Immed (Character'Val (ch), File);
578 procedure Get_Immediate
579 (Item : out Wide_Wide_Character;
580 Available : out Boolean)
583 Get_Immediate (Current_In, Item, Available);
592 Item : out Wide_Wide_String;
596 FIO.Check_Read_Status (AP (File));
597 Last := Item'First - 1;
599 -- Immediate exit for null string, this is a case in which we do not
600 -- need to test for end of file and we do not skip a line mark under
601 -- any circumstances.
603 if Last >= Item'Last then
607 -- Here we have at least one character, if we are immediately before
608 -- a line mark, then we will just skip past it storing no characters.
610 if File.Before_LM then
611 File.Before_LM := False;
612 File.Before_LM_PM := False;
614 -- Otherwise we need to read some characters
617 -- If we are at the end of file now, it means we are trying to
618 -- skip a file terminator and we raise End_Error (RM A.10.7(20))
620 if Nextc (File) = EOF then
624 -- Loop through characters in string
627 -- Exit the loop if read is terminated by encountering line mark
628 -- Note that the use of Skip_Line here ensures we properly deal
629 -- with setting the page and line numbers.
631 if End_Of_Line (File) then
636 -- Otherwise store the character, note that we know that ch is
637 -- something other than LM or EOF. It could possibly be a page
638 -- mark if there is a stray page mark in the middle of a line,
639 -- but this is not an official page mark in any case, since
640 -- official page marks can only follow a line mark. The whole
641 -- page business is pretty much nonsense anyway, so we do not
642 -- want to waste time trying to make sense out of non-standard
643 -- page marks in the file! This means that the behavior of
644 -- Get_Line is different from repeated Get of a character, but
645 -- that's too bad. We only promise that page numbers etc make
646 -- sense if the file is formatted in a standard manner.
648 -- Note: we do not adjust the column number because it is quicker
649 -- to adjust it once at the end of the operation than incrementing
650 -- it each time around the loop.
653 Get (File, Item (Last));
655 -- All done if the string is full, this is the case in which
656 -- we do not skip the following line mark. We need to adjust
657 -- the column number in this case.
659 if Last = Item'Last then
660 File.Col := File.Col + Count (Item'Length);
664 -- Exit from the loop if we are at the end of file. This happens
665 -- if we have a last line that is not terminated with a line mark.
666 -- In this case we consider that there is an implied line mark;
667 -- this is a non-standard file, but we will treat it nicely.
669 exit when Nextc (File) = EOF;
675 (Item : out Wide_Wide_String;
679 Get_Line (Current_In, Item, Last);
682 function Get_Line (File : File_Type) return Wide_Wide_String is
683 Buffer : Wide_Wide_String (1 .. 500);
686 function Get_Rest (S : Wide_Wide_String) return Wide_Wide_String;
687 -- This is a recursive function that reads the rest of the line and
688 -- returns it. S is the part read so far.
694 function Get_Rest (S : Wide_Wide_String) return Wide_Wide_String is
696 -- Each time we allocate a buffer the same size as what we have
697 -- read so far. This limits us to a logarithmic number of calls
698 -- to Get_Rest and also ensures only a linear use of stack space.
700 Buffer : Wide_Wide_String (1 .. S'Length);
704 Get_Line (File, Buffer, Last);
707 R : constant Wide_Wide_String := S & Buffer (1 .. Last);
709 if Last < Buffer'Last then
717 -- Start of processing for Get_Line
720 Get_Line (File, Buffer, Last);
722 if Last < Buffer'Last then
723 return Buffer (1 .. Last);
725 return Get_Rest (Buffer (1 .. Last));
729 function Get_Line return Wide_Wide_String is
731 return Get_Line (Current_In);
734 ------------------------
735 -- Get_Wide_Wide_Char --
736 ------------------------
738 function Get_Wide_Wide_Char
740 File : File_Type) return Wide_Wide_Character
742 function In_Char return Character;
743 -- Function used to obtain additional characters it the wide character
744 -- sequence is more than one character long.
746 function WC_In is new Char_Sequence_To_UTF_32 (In_Char);
752 function In_Char return Character is
753 ch : constant Integer := Getc (File);
758 return Character'Val (ch);
762 -- Start of processing for Get_Wide_Wide_Char
765 FIO.Check_Read_Status (AP (File));
766 return Wide_Wide_Character'Val (WC_In (C, File.WC_Method));
767 end Get_Wide_Wide_Char;
769 ------------------------------
770 -- Get_Wide_Wide_Char_Immed --
771 ------------------------------
773 function Get_Wide_Wide_Char_Immed
775 File : File_Type) return Wide_Wide_Character
777 function In_Char return Character;
778 -- Function used to obtain additional characters it the wide character
779 -- sequence is more than one character long.
781 function WC_In is new Char_Sequence_To_UTF_32 (In_Char);
787 function In_Char return Character is
788 ch : constant Integer := Getc_Immed (File);
793 return Character'Val (ch);
797 -- Start of processing for Get_Wide_Wide_Char_Immed
800 FIO.Check_Read_Status (AP (File));
801 return Wide_Wide_Character'Val (WC_In (C, File.WC_Method));
802 end Get_Wide_Wide_Char_Immed;
808 function Getc (File : File_Type) return int is
812 ch := fgetc (File.Stream);
814 if ch = EOF and then ferror (File.Stream) /= 0 then
825 function Getc_Immed (File : File_Type) return int is
829 procedure getc_immediate
830 (stream : FILEs; ch : out int; end_of_file : out int);
831 pragma Import (C, getc_immediate, "getc_immediate");
834 FIO.Check_Read_Status (AP (File));
836 if File.Before_LM then
837 File.Before_LM := False;
838 File.Before_LM_PM := False;
842 getc_immediate (File.Stream, ch, end_of_file);
844 if ferror (File.Stream) /= 0 then
846 elsif end_of_file /= 0 then
858 function Is_Open (File : File_Type) return Boolean is
860 return FIO.Is_Open (AP (File));
867 -- Note: we assume that it is impossible in practice for the line
868 -- to exceed the value of Count'Last, i.e. no check is required for
869 -- overflow raising layout error.
871 function Line (File : File_Type) return Positive_Count is
873 FIO.Check_File_Open (AP (File));
877 function Line return Positive_Count is
879 return Line (Current_Out);
886 function Line_Length (File : File_Type) return Count is
888 FIO.Check_Write_Status (AP (File));
889 return File.Line_Length;
892 function Line_Length return Count is
894 return Line_Length (Current_Out);
903 Item : out Wide_Wide_Character;
904 End_Of_Line : out Boolean)
908 -- Start of processing for Look_Ahead
911 FIO.Check_Read_Status (AP (File));
913 -- If we are logically before a line mark, we can return immediately
915 if File.Before_LM then
917 Item := Wide_Wide_Character'Val (0);
919 -- If we are before a wide character, just return it (this can happen
920 -- if there are two calls to Look_Ahead in a row).
922 elsif File.Before_Wide_Wide_Character then
923 End_Of_Line := False;
924 Item := File.Saved_Wide_Wide_Character;
926 -- otherwise we must read a character from the input stream
933 or else (ch = EOF and then File.Is_Regular_File)
937 Item := Wide_Wide_Character'Val (0);
939 -- Case where character obtained does not represent the start of an
940 -- encoded sequence so it stands for itself and we can unget it with
943 elsif not Is_Start_Of_Encoding
944 (Character'Val (ch), File.WC_Method)
946 End_Of_Line := False;
948 Item := Wide_Wide_Character'Val (ch);
950 -- For the start of an encoding, we read the character using the
951 -- Get_Wide_Wide_Char routine. It will occupy more than one byte so
952 -- we can't put it back with ungetc. Instead we save it in the
953 -- control block, setting a flag that everyone interested in reading
954 -- characters must test before reading the stream.
957 Item := Get_Wide_Wide_Char (Character'Val (ch), File);
958 End_Of_Line := False;
959 File.Saved_Wide_Wide_Character := Item;
960 File.Before_Wide_Wide_Character := True;
966 (Item : out Wide_Wide_Character;
967 End_Of_Line : out Boolean)
970 Look_Ahead (Current_In, Item, End_Of_Line);
977 function Mode (File : File_Type) return File_Mode is
979 return To_TIO (FIO.Mode (AP (File)));
986 function Name (File : File_Type) return String is
988 return FIO.Name (AP (File));
997 Spacing : Positive_Count := 1)
1000 -- Raise Constraint_Error if out of range value. The reason for this
1001 -- explicit test is that we don't want junk values around, even if
1002 -- checks are off in the caller.
1004 if not Spacing'Valid then
1005 raise Constraint_Error;
1008 FIO.Check_Write_Status (AP (File));
1010 for K in 1 .. Spacing loop
1012 File.Line := File.Line + 1;
1014 if File.Page_Length /= 0
1015 and then File.Line > File.Page_Length
1019 File.Page := File.Page + 1;
1026 procedure New_Line (Spacing : Positive_Count := 1) is
1028 New_Line (Current_Out, Spacing);
1035 procedure New_Page (File : File_Type) is
1037 FIO.Check_Write_Status (AP (File));
1039 if File.Col /= 1 or else File.Line = 1 then
1044 File.Page := File.Page + 1;
1049 procedure New_Page is
1051 New_Page (Current_Out);
1058 function Nextc (File : File_Type) return int is
1062 ch := fgetc (File.Stream);
1065 if ferror (File.Stream) /= 0 then
1070 if ungetc (ch, File.Stream) = EOF then
1083 (File : in out File_Type;
1086 Form : String := "")
1088 Dummy_File_Control_Block : Wide_Wide_Text_AFCB;
1089 pragma Warnings (Off, Dummy_File_Control_Block);
1090 -- Yes, we know this is never assigned a value, only the tag
1091 -- is used for dispatching purposes, so that's expected.
1094 FIO.Open (File_Ptr => AP (File),
1095 Dummy_FCB => Dummy_File_Control_Block,
1096 Mode => To_FCB (Mode),
1111 -- Note: we assume that it is impossible in practice for the page
1112 -- to exceed the value of Count'Last, i.e. no check is required for
1113 -- overflow raising layout error.
1115 function Page (File : File_Type) return Positive_Count is
1117 FIO.Check_File_Open (AP (File));
1121 function Page return Positive_Count is
1123 return Page (Current_Out);
1130 function Page_Length (File : File_Type) return Count is
1132 FIO.Check_Write_Status (AP (File));
1133 return File.Page_Length;
1136 function Page_Length return Count is
1138 return Page_Length (Current_Out);
1147 Item : Wide_Wide_Character)
1149 procedure Out_Char (C : Character);
1150 -- Procedure to output one character of a wide character sequence
1152 procedure WC_Out is new UTF_32_To_Char_Sequence (Out_Char);
1158 procedure Out_Char (C : Character) is
1160 Putc (Character'Pos (C), File);
1163 -- Start of processing for Put
1166 FIO.Check_Write_Status (AP (File));
1167 WC_Out (Wide_Wide_Character'Pos (Item), File.WC_Method);
1168 File.Col := File.Col + 1;
1171 procedure Put (Item : Wide_Wide_Character) is
1173 Put (Current_Out, Item);
1182 Item : Wide_Wide_String)
1185 for J in Item'Range loop
1186 Put (File, Item (J));
1190 procedure Put (Item : Wide_Wide_String) is
1192 Put (Current_Out, Item);
1201 Item : Wide_Wide_String)
1208 procedure Put_Line (Item : Wide_Wide_String) is
1210 Put (Current_Out, Item);
1211 New_Line (Current_Out);
1218 procedure Putc (ch : int; File : File_Type) is
1220 if fputc (ch, File.Stream) = EOF then
1229 -- This is the primitive Stream Read routine, used when a Text_IO file
1230 -- is treated directly as a stream using Text_IO.Streams.Stream.
1233 (File : in out Wide_Wide_Text_AFCB;
1234 Item : out Stream_Element_Array;
1235 Last : out Stream_Element_Offset)
1238 pragma Unreferenced (Discard_ch);
1241 -- Need to deal with Before_Wide_Wide_Character ???
1243 if File.Mode /= FCB.In_File then
1247 -- Deal with case where our logical and physical position do not match
1248 -- because of being after an LM or LM-PM sequence when in fact we are
1249 -- logically positioned before it.
1251 if File.Before_LM then
1253 -- If we are before a PM, then it is possible for a stream read
1254 -- to leave us after the LM and before the PM, which is a bit
1255 -- odd. The easiest way to deal with this is to unget the PM,
1256 -- so we are indeed positioned between the characters. This way
1257 -- further stream read operations will work correctly, and the
1258 -- effect on text processing is a little weird, but what can
1259 -- be expected if stream and text input are mixed this way?
1261 if File.Before_LM_PM then
1262 Discard_ch := ungetc (PM, File.Stream);
1263 File.Before_LM_PM := False;
1266 File.Before_LM := False;
1268 Item (Item'First) := Stream_Element (Character'Pos (ASCII.LF));
1270 if Item'Length = 1 then
1276 Stream_Element_Offset
1277 (fread (buffer => Item'Address,
1278 index => size_t (Item'First + 1),
1280 count => Item'Length - 1,
1281 stream => File.Stream));
1287 -- Now we do the read. Since this is a text file, it is normally in
1288 -- text mode, but stream data must be read in binary mode, so we
1289 -- temporarily set binary mode for the read, resetting it after.
1290 -- These calls have no effect in a system (like Unix) where there is
1291 -- no distinction between text and binary files.
1293 set_binary_mode (fileno (File.Stream));
1297 Stream_Element_Offset
1298 (fread (Item'Address, 1, Item'Length, File.Stream)) - 1;
1300 if Last < Item'Last then
1301 if ferror (File.Stream) /= 0 then
1306 set_text_mode (fileno (File.Stream));
1314 (File : in out File_Type;
1318 for AFCB'Address use File'Address;
1319 pragma Import (Ada, AFCB);
1321 -- Don't allow change of mode for current file (RM A.10.2(5))
1323 if (File = Current_In or else
1324 File = Current_Out or else
1325 File = Current_Error)
1326 and then To_FCB (Mode) /= File.Mode
1331 Terminate_Line (File);
1332 FIO.Reset (AFCB'Access, To_FCB (Mode));
1336 File.Line_Length := 0;
1337 File.Page_Length := 0;
1338 File.Before_LM := False;
1339 File.Before_LM_PM := False;
1342 procedure Reset (File : in out File_Type) is
1344 for AFCB'Address use File'Address;
1345 pragma Import (Ada, AFCB);
1347 Terminate_Line (File);
1348 FIO.Reset (AFCB'Access);
1352 File.Line_Length := 0;
1353 File.Page_Length := 0;
1354 File.Before_LM := False;
1355 File.Before_LM_PM := False;
1364 To : Positive_Count)
1369 -- Raise Constraint_Error if out of range value. The reason for this
1370 -- explicit test is that we don't want junk values around, even if
1371 -- checks are off in the caller.
1373 if not To'Valid then
1374 raise Constraint_Error;
1377 FIO.Check_File_Open (AP (File));
1379 if To = File.Col then
1383 if Mode (File) >= Out_File then
1384 if File.Line_Length /= 0 and then To > File.Line_Length then
1388 if To < File.Col then
1392 while File.Col < To loop
1404 File.Line := File.Line + 1;
1407 elsif ch = PM and then File.Is_Regular_File then
1408 File.Page := File.Page + 1;
1412 elsif To = File.Col then
1417 File.Col := File.Col + 1;
1423 procedure Set_Col (To : Positive_Count) is
1425 Set_Col (Current_Out, To);
1432 procedure Set_Error (File : File_Type) is
1434 FIO.Check_Write_Status (AP (File));
1435 Current_Err := File;
1442 procedure Set_Input (File : File_Type) is
1444 FIO.Check_Read_Status (AP (File));
1454 To : Positive_Count)
1457 -- Raise Constraint_Error if out of range value. The reason for this
1458 -- explicit test is that we don't want junk values around, even if
1459 -- checks are off in the caller.
1461 if not To'Valid then
1462 raise Constraint_Error;
1465 FIO.Check_File_Open (AP (File));
1467 if To = File.Line then
1471 if Mode (File) >= Out_File then
1472 if File.Page_Length /= 0 and then To > File.Page_Length then
1476 if To < File.Line then
1480 while File.Line < To loop
1485 while To /= File.Line loop
1491 procedure Set_Line (To : Positive_Count) is
1493 Set_Line (Current_Out, To);
1496 ---------------------
1497 -- Set_Line_Length --
1498 ---------------------
1500 procedure Set_Line_Length (File : File_Type; To : Count) is
1502 -- Raise Constraint_Error if out of range value. The reason for this
1503 -- explicit test is that we don't want junk values around, even if
1504 -- checks are off in the caller.
1506 if not To'Valid then
1507 raise Constraint_Error;
1510 FIO.Check_Write_Status (AP (File));
1511 File.Line_Length := To;
1512 end Set_Line_Length;
1514 procedure Set_Line_Length (To : Count) is
1516 Set_Line_Length (Current_Out, To);
1517 end Set_Line_Length;
1523 procedure Set_Output (File : File_Type) is
1525 FIO.Check_Write_Status (AP (File));
1526 Current_Out := File;
1529 ---------------------
1530 -- Set_Page_Length --
1531 ---------------------
1533 procedure Set_Page_Length (File : File_Type; To : Count) is
1535 -- Raise Constraint_Error if out of range value. The reason for this
1536 -- explicit test is that we don't want junk values around, even if
1537 -- checks are off in the caller.
1539 if not To'Valid then
1540 raise Constraint_Error;
1543 FIO.Check_Write_Status (AP (File));
1544 File.Page_Length := To;
1545 end Set_Page_Length;
1547 procedure Set_Page_Length (To : Count) is
1549 Set_Page_Length (Current_Out, To);
1550 end Set_Page_Length;
1556 procedure Set_WCEM (File : in out File_Type) is
1561 File.WC_Method := WCEM_Brackets;
1562 FIO.Form_Parameter (File.Form.all, "wcem", Start, Stop);
1565 File.WC_Method := WCEM_Brackets;
1567 elsif Start /= 0 then
1568 if Stop = Start then
1569 for J in WC_Encoding_Letters'Range loop
1570 if File.Form (Start) = WC_Encoding_Letters (J) then
1571 File.WC_Method := J;
1578 raise Use_Error with "invalid WCEM form parameter";
1588 Spacing : Positive_Count := 1)
1593 -- Raise Constraint_Error if out of range value. The reason for this
1594 -- explicit test is that we don't want junk values around, even if
1595 -- checks are off in the caller.
1597 if not Spacing'Valid then
1598 raise Constraint_Error;
1601 FIO.Check_Read_Status (AP (File));
1603 for L in 1 .. Spacing loop
1604 if File.Before_LM then
1605 File.Before_LM := False;
1606 File.Before_LM_PM := False;
1611 -- If at end of file now, then immediately raise End_Error. Note
1612 -- that we can never be positioned between a line mark and a page
1613 -- mark, so if we are at the end of file, we cannot logically be
1614 -- before the implicit page mark that is at the end of the file.
1616 -- For the same reason, we do not need an explicit check for a
1617 -- page mark. If there is a FF in the middle of a line, the file
1618 -- is not in canonical format and we do not care about the page
1619 -- numbers for files other than ones in canonical format.
1625 -- If not at end of file, then loop till we get to an LM or EOF.
1626 -- The latter case happens only in non-canonical files where the
1627 -- last line is not terminated by LM, but we don't want to blow
1628 -- up for such files, so we assume an implicit LM in this case.
1631 exit when ch = LM or ch = EOF;
1636 -- We have got past a line mark, now, for a regular file only,
1637 -- see if a page mark immediately follows this line mark and
1638 -- if so, skip past the page mark as well. We do not do this
1639 -- for non-regular files, since it would cause an undesirable
1640 -- wait for an additional character.
1643 File.Line := File.Line + 1;
1645 if File.Before_LM_PM then
1646 File.Page := File.Page + 1;
1648 File.Before_LM_PM := False;
1650 elsif File.Is_Regular_File then
1653 -- Page mark can be explicit, or implied at the end of the file
1655 if (ch = PM or else ch = EOF)
1656 and then File.Is_Regular_File
1658 File.Page := File.Page + 1;
1666 File.Before_Wide_Wide_Character := False;
1669 procedure Skip_Line (Spacing : Positive_Count := 1) is
1671 Skip_Line (Current_In, Spacing);
1678 procedure Skip_Page (File : File_Type) is
1682 FIO.Check_Read_Status (AP (File));
1684 -- If at page mark already, just skip it
1686 if File.Before_LM_PM then
1687 File.Before_LM := False;
1688 File.Before_LM_PM := False;
1689 File.Page := File.Page + 1;
1695 -- This is a bit tricky, if we are logically before an LM then
1696 -- it is not an error if we are at an end of file now, since we
1697 -- are not really at it.
1699 if File.Before_LM then
1700 File.Before_LM := False;
1701 File.Before_LM_PM := False;
1704 -- Otherwise we do raise End_Error if we are at the end of file now
1714 -- Now we can just rumble along to the next page mark, or to the
1715 -- end of file, if that comes first. The latter case happens when
1716 -- the page mark is implied at the end of file.
1720 or else (ch = PM and then File.Is_Regular_File);
1724 File.Page := File.Page + 1;
1727 File.Before_Wide_Wide_Character := False;
1730 procedure Skip_Page is
1732 Skip_Page (Current_In);
1735 --------------------
1736 -- Standard_Error --
1737 --------------------
1739 function Standard_Error return File_Type is
1741 return Standard_Err;
1744 function Standard_Error return File_Access is
1746 return Standard_Err'Access;
1749 --------------------
1750 -- Standard_Input --
1751 --------------------
1753 function Standard_Input return File_Type is
1758 function Standard_Input return File_Access is
1760 return Standard_In'Access;
1763 ---------------------
1764 -- Standard_Output --
1765 ---------------------
1767 function Standard_Output return File_Type is
1769 return Standard_Out;
1770 end Standard_Output;
1772 function Standard_Output return File_Access is
1774 return Standard_Out'Access;
1775 end Standard_Output;
1777 --------------------
1778 -- Terminate_Line --
1779 --------------------
1781 procedure Terminate_Line (File : File_Type) is
1783 FIO.Check_File_Open (AP (File));
1785 -- For file other than In_File, test for needing to terminate last line
1787 if Mode (File) /= In_File then
1789 -- If not at start of line definition need new line
1791 if File.Col /= 1 then
1794 -- For files other than standard error and standard output, we
1795 -- make sure that an empty file has a single line feed, so that
1796 -- it is properly formatted. We avoid this for the standard files
1797 -- because it is too much of a nuisance to have these odd line
1798 -- feeds when nothing has been written to the file.
1800 elsif (File /= Standard_Err and then File /= Standard_Out)
1801 and then (File.Line = 1 and then File.Page = 1)
1812 procedure Ungetc (ch : int; File : File_Type) is
1815 if ungetc (ch, File.Stream) = EOF then
1825 -- This is the primitive Stream Write routine, used when a Text_IO file
1826 -- is treated directly as a stream using Text_IO.Streams.Stream.
1829 (File : in out Wide_Wide_Text_AFCB;
1830 Item : Stream_Element_Array)
1832 pragma Warnings (Off, File);
1833 -- Because in this implementation we don't need IN OUT, we only read
1835 Siz : constant size_t := Item'Length;
1838 if File.Mode = FCB.In_File then
1842 -- Now we do the write. Since this is a text file, it is normally in
1843 -- text mode, but stream data must be written in binary mode, so we
1844 -- temporarily set binary mode for the write, resetting it after.
1845 -- These calls have no effect in a system (like Unix) where there is
1846 -- no distinction between text and binary files.
1848 set_binary_mode (fileno (File.Stream));
1850 if fwrite (Item'Address, 1, Siz, File.Stream) /= Siz then
1854 set_text_mode (fileno (File.Stream));
1857 -- Use "preallocated" strings to avoid calling "new" during the
1858 -- elaboration of the run time. This is needed in the tasking case to
1859 -- avoid calling Task_Lock too early. A filename is expected to end with
1860 -- a null character in the runtime, here the null characters are added
1861 -- just to have a correct filename length.
1863 Err_Name : aliased String := "*stderr" & ASCII.NUL;
1864 In_Name : aliased String := "*stdin" & ASCII.NUL;
1865 Out_Name : aliased String := "*stdout" & ASCII.NUL;
1868 -------------------------------
1869 -- Initialize Standard Files --
1870 -------------------------------
1872 for J in WC_Encoding_Method loop
1873 if WC_Encoding = WC_Encoding_Letters (J) then
1878 -- Note: the names in these files are bogus, and probably it would be
1879 -- better for these files to have no names, but the ACVC test insist!
1880 -- We use names that are bound to fail in open etc.
1882 Standard_Err.Stream := stderr;
1883 Standard_Err.Name := Err_Name'Access;
1884 Standard_Err.Form := Null_Str'Unrestricted_Access;
1885 Standard_Err.Mode := FCB.Out_File;
1886 Standard_Err.Is_Regular_File := is_regular_file (fileno (stderr)) /= 0;
1887 Standard_Err.Is_Temporary_File := False;
1888 Standard_Err.Is_System_File := True;
1889 Standard_Err.Is_Text_File := True;
1890 Standard_Err.Access_Method := 'T';
1891 Standard_Err.Self := Standard_Err;
1892 Standard_Err.WC_Method := Default_WCEM;
1894 Standard_In.Stream := stdin;
1895 Standard_In.Name := In_Name'Access;
1896 Standard_In.Form := Null_Str'Unrestricted_Access;
1897 Standard_In.Mode := FCB.In_File;
1898 Standard_In.Is_Regular_File := is_regular_file (fileno (stdin)) /= 0;
1899 Standard_In.Is_Temporary_File := False;
1900 Standard_In.Is_System_File := True;
1901 Standard_In.Is_Text_File := True;
1902 Standard_In.Access_Method := 'T';
1903 Standard_In.Self := Standard_In;
1904 Standard_In.WC_Method := Default_WCEM;
1906 Standard_Out.Stream := stdout;
1907 Standard_Out.Name := Out_Name'Access;
1908 Standard_Out.Form := Null_Str'Unrestricted_Access;
1909 Standard_Out.Mode := FCB.Out_File;
1910 Standard_Out.Is_Regular_File := is_regular_file (fileno (stdout)) /= 0;
1911 Standard_Out.Is_Temporary_File := False;
1912 Standard_Out.Is_System_File := True;
1913 Standard_Out.Is_Text_File := True;
1914 Standard_Out.Access_Method := 'T';
1915 Standard_Out.Self := Standard_Out;
1916 Standard_Out.WC_Method := Default_WCEM;
1918 FIO.Chain_File (AP (Standard_In));
1919 FIO.Chain_File (AP (Standard_Out));
1920 FIO.Chain_File (AP (Standard_Err));
1922 FIO.Make_Unbuffered (AP (Standard_Out));
1923 FIO.Make_Unbuffered (AP (Standard_Err));
1925 end Ada.Wide_Wide_Text_IO;