1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . W I D E _ T E X T _ I O --
9 -- Copyright (C) 1992-2007, 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_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_Char_Immed
73 File : File_Type) return Wide_Character;
74 -- This routine is identical to Get_Wide_Char, except that the reads are
75 -- 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_Text_AFCB) return FCB.AFCB_Ptr
89 pragma Unreferenced (Control_Block);
91 return new Wide_Text_AFCB;
98 procedure AFCB_Close (File : not null access 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_Text_AFCB) is
121 type FCB_Ptr is access all Wide_Text_AFCB;
122 FT : FCB_Ptr := FCB_Ptr (File);
125 new Ada.Unchecked_Deallocation (Wide_Text_AFCB, FCB_Ptr);
135 procedure Close (File : in out File_Type) is
137 FIO.Close (AP (File));
144 -- Note: we assume that it is impossible in practice for the column
145 -- to exceed the value of Count'Last, i.e. no check is required for
146 -- overflow raising layout error.
148 function Col (File : File_Type) return Positive_Count is
150 FIO.Check_File_Open (AP (File));
154 function Col return Positive_Count is
156 return Col (Current_Out);
164 (File : in out File_Type;
165 Mode : File_Mode := Out_File;
169 Dummy_File_Control_Block : Wide_Text_AFCB;
170 pragma Warnings (Off, Dummy_File_Control_Block);
171 -- Yes, we know this is never assigned a value, only the tag
172 -- is used for dispatching purposes, so that's expected.
175 FIO.Open (File_Ptr => AP (File),
176 Dummy_FCB => Dummy_File_Control_Block,
177 Mode => To_FCB (Mode),
190 function Current_Error return File_Type is
195 function Current_Error return File_Access is
197 return Current_Err'Access;
204 function Current_Input return File_Type is
209 function Current_Input return File_Access is
211 return Current_In'Access;
218 function Current_Output return File_Type is
223 function Current_Output return File_Access is
225 return Current_Out'Access;
232 procedure Delete (File : in out File_Type) is
234 FIO.Delete (AP (File));
241 function End_Of_File (File : File_Type) return Boolean is
245 FIO.Check_Read_Status (AP (File));
247 if File.Before_Wide_Character then
250 elsif File.Before_LM then
251 if File.Before_LM_PM then
252 return Nextc (File) = EOF;
266 File.Before_LM := True;
270 -- Here we are just past the line mark with Before_LM set so that we
271 -- do not have to try to back up past the LM, thus avoiding the need
272 -- to back up more than one character.
279 elsif ch = PM and then File.Is_Regular_File then
280 File.Before_LM_PM := True;
281 return Nextc (File) = EOF;
283 -- Here if neither EOF nor PM followed end of line
292 function End_Of_File return Boolean is
294 return End_Of_File (Current_In);
301 function End_Of_Line (File : File_Type) return Boolean is
305 FIO.Check_Read_Status (AP (File));
307 if File.Before_Wide_Character then
310 elsif File.Before_LM then
326 function End_Of_Line return Boolean is
328 return End_Of_Line (Current_In);
335 function End_Of_Page (File : File_Type) return Boolean is
339 FIO.Check_Read_Status (AP (File));
341 if not File.Is_Regular_File then
344 elsif File.Before_Wide_Character then
347 elsif File.Before_LM then
348 if File.Before_LM_PM then
363 File.Before_LM := True;
367 -- Here we are just past the line mark with Before_LM set so that we
368 -- do not have to try to back up past the LM, thus avoiding the need
369 -- to back up more than one character.
373 return ch = PM or else ch = EOF;
376 function End_Of_Page return Boolean is
378 return End_Of_Page (Current_In);
385 procedure Flush (File : File_Type) is
387 FIO.Flush (AP (File));
399 function Form (File : File_Type) return String is
401 return FIO.Form (AP (File));
410 Item : out Wide_Character)
415 FIO.Check_Read_Status (AP (File));
417 if File.Before_Wide_Character then
418 File.Before_Wide_Character := False;
419 Item := File.Saved_Wide_Character;
421 -- Ada.Text_IO checks Before_LM_PM here, shouldn't we do the same???
424 Get_Character (File, C);
425 Item := Get_Wide_Char (C, File);
429 procedure Get (Item : out Wide_Character) is
431 Get (Current_In, Item);
436 Item : out Wide_String)
439 for J in Item'Range loop
440 Get (File, Item (J));
444 procedure Get (Item : out 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_Character)
508 FIO.Check_Read_Status (AP (File));
510 if File.Before_Wide_Character then
511 File.Before_Wide_Character := False;
512 Item := File.Saved_Wide_Character;
514 elsif File.Before_LM then
515 File.Before_LM := False;
516 File.Before_LM_PM := False;
517 Item := Wide_Character'Val (LM);
520 ch := Getc_Immed (File);
525 Item := Get_Wide_Char_Immed (Character'Val (ch), File);
530 procedure Get_Immediate
531 (Item : out Wide_Character)
534 Get_Immediate (Current_In, Item);
537 procedure Get_Immediate
539 Item : out Wide_Character;
540 Available : out Boolean)
545 FIO.Check_Read_Status (AP (File));
548 if File.Before_Wide_Character then
549 File.Before_Wide_Character := False;
550 Item := File.Saved_Wide_Character;
552 elsif File.Before_LM then
553 File.Before_LM := False;
554 File.Before_LM_PM := False;
555 Item := Wide_Character'Val (LM);
558 -- Shouldn't we use getc_immediate_nowait here, like Text_IO???
560 ch := Getc_Immed (File);
565 Item := Get_Wide_Char_Immed (Character'Val (ch), File);
570 procedure Get_Immediate
571 (Item : out Wide_Character;
572 Available : out Boolean)
575 Get_Immediate (Current_In, Item, Available);
584 Item : out Wide_String;
588 FIO.Check_Read_Status (AP (File));
589 Last := Item'First - 1;
591 -- Immediate exit for null string, this is a case in which we do not
592 -- need to test for end of file and we do not skip a line mark under
593 -- any circumstances.
595 if Last >= Item'Last then
599 -- Here we have at least one character, if we are immediately before
600 -- a line mark, then we will just skip past it storing no characters.
602 if File.Before_LM then
603 File.Before_LM := False;
604 File.Before_LM_PM := False;
606 -- Otherwise we need to read some characters
609 -- If we are at the end of file now, it means we are trying to
610 -- skip a file terminator and we raise End_Error (RM A.10.7(20))
612 if Nextc (File) = EOF then
616 -- Loop through characters in string
619 -- Exit the loop if read is terminated by encountering line mark
620 -- Note that the use of Skip_Line here ensures we properly deal
621 -- with setting the page and line numbers.
623 if End_Of_Line (File) then
628 -- Otherwise store the character, note that we know that ch is
629 -- something other than LM or EOF. It could possibly be a page
630 -- mark if there is a stray page mark in the middle of a line,
631 -- but this is not an official page mark in any case, since
632 -- official page marks can only follow a line mark. The whole
633 -- page business is pretty much nonsense anyway, so we do not
634 -- want to waste time trying to make sense out of non-standard
635 -- page marks in the file! This means that the behavior of
636 -- Get_Line is different from repeated Get of a character, but
637 -- that's too bad. We only promise that page numbers etc make
638 -- sense if the file is formatted in a standard manner.
640 -- Note: we do not adjust the column number because it is quicker
641 -- to adjust it once at the end of the operation than incrementing
642 -- it each time around the loop.
645 Get (File, Item (Last));
647 -- All done if the string is full, this is the case in which
648 -- we do not skip the following line mark. We need to adjust
649 -- the column number in this case.
651 if Last = Item'Last then
652 File.Col := File.Col + Count (Item'Length);
656 -- Exit from the loop if we are at the end of file. This happens
657 -- if we have a last line that is not terminated with a line mark.
658 -- In this case we consider that there is an implied line mark;
659 -- this is a non-standard file, but we will treat it nicely.
661 exit when Nextc (File) = EOF;
667 (Item : out Wide_String;
671 Get_Line (Current_In, Item, Last);
674 function Get_Line (File : File_Type) return Wide_String is
675 Buffer : Wide_String (1 .. 500);
678 function Get_Rest (S : Wide_String) return Wide_String;
679 -- This is a recursive function that reads the rest of the line and
680 -- returns it. S is the part read so far.
686 function Get_Rest (S : Wide_String) return Wide_String is
688 -- Each time we allocate a buffer the same size as what we have
689 -- read so far. This limits us to a logarithmic number of calls
690 -- to Get_Rest and also ensures only a linear use of stack space.
692 Buffer : Wide_String (1 .. S'Length);
696 Get_Line (File, Buffer, Last);
699 R : constant Wide_String := S & Buffer (1 .. Last);
701 if Last < Buffer'Last then
709 -- Start of processing for Get_Line
712 Get_Line (File, Buffer, Last);
714 if Last < Buffer'Last then
715 return Buffer (1 .. Last);
717 return Get_Rest (Buffer (1 .. Last));
721 function Get_Line return Wide_String is
723 return Get_Line (Current_In);
730 function Get_Wide_Char
732 File : File_Type) return Wide_Character
734 function In_Char return Character;
735 -- Function used to obtain additional characters it the wide character
736 -- sequence is more than one character long.
738 function WC_In is new Char_Sequence_To_Wide_Char (In_Char);
744 function In_Char return Character is
745 ch : constant Integer := Getc (File);
750 return Character'Val (ch);
754 -- Start of processing for Get_Wide_Char
757 return WC_In (C, File.WC_Method);
760 -------------------------
761 -- Get_Wide_Char_Immed --
762 -------------------------
764 function Get_Wide_Char_Immed
766 File : File_Type) return Wide_Character
768 function In_Char return Character;
769 -- Function used to obtain additional characters it the wide character
770 -- sequence is more than one character long.
772 function WC_In is new Char_Sequence_To_Wide_Char (In_Char);
778 function In_Char return Character is
779 ch : constant Integer := Getc_Immed (File);
784 return Character'Val (ch);
788 -- Start of processing for Get_Wide_Char_Immed
791 return WC_In (C, File.WC_Method);
792 end Get_Wide_Char_Immed;
798 function Getc (File : File_Type) return int is
802 ch := fgetc (File.Stream);
804 if ch = EOF and then ferror (File.Stream) /= 0 then
815 function Getc_Immed (File : File_Type) return int is
819 procedure getc_immediate
820 (stream : FILEs; ch : out int; end_of_file : out int);
821 pragma Import (C, getc_immediate, "getc_immediate");
824 FIO.Check_Read_Status (AP (File));
826 if File.Before_LM then
827 File.Before_LM := False;
828 File.Before_LM_PM := False;
832 getc_immediate (File.Stream, ch, end_of_file);
834 if ferror (File.Stream) /= 0 then
836 elsif end_of_file /= 0 then
848 function Is_Open (File : File_Type) return Boolean is
850 return FIO.Is_Open (AP (File));
857 -- Note: we assume that it is impossible in practice for the line
858 -- to exceed the value of Count'Last, i.e. no check is required for
859 -- overflow raising layout error.
861 function Line (File : File_Type) return Positive_Count is
863 FIO.Check_File_Open (AP (File));
867 function Line return Positive_Count is
869 return Line (Current_Out);
876 function Line_Length (File : File_Type) return Count is
878 FIO.Check_Write_Status (AP (File));
879 return File.Line_Length;
882 function Line_Length return Count is
884 return Line_Length (Current_Out);
893 Item : out Wide_Character;
894 End_Of_Line : out Boolean)
898 -- Start of processing for Look_Ahead
901 FIO.Check_Read_Status (AP (File));
903 -- If we are logically before a line mark, we can return immediately
905 if File.Before_LM then
907 Item := Wide_Character'Val (0);
909 -- If we are before a wide character, just return it (this can happen
910 -- if there are two calls to Look_Ahead in a row).
912 elsif File.Before_Wide_Character then
913 End_Of_Line := False;
914 Item := File.Saved_Wide_Character;
916 -- otherwise we must read a character from the input stream
923 or else (ch = EOF and then File.Is_Regular_File)
927 Item := Wide_Character'Val (0);
929 -- Case where character obtained does not represent the start of an
930 -- encoded sequence so it stands for itself and we can unget it with
933 elsif not Is_Start_Of_Encoding
934 (Character'Val (ch), File.WC_Method)
936 End_Of_Line := False;
938 Item := Wide_Character'Val (ch);
940 -- For the start of an encoding, we read the character using the
941 -- Get_Wide_Char routine. It will occupy more than one byte so we
942 -- can't put it back with ungetc. Instead we save it in the control
943 -- block, setting a flag that everyone interested in reading
944 -- characters must test before reading the stream.
947 Item := Get_Wide_Char (Character'Val (ch), File);
948 End_Of_Line := False;
949 File.Saved_Wide_Character := Item;
950 File.Before_Wide_Character := True;
956 (Item : out Wide_Character;
957 End_Of_Line : out Boolean)
960 Look_Ahead (Current_In, Item, End_Of_Line);
967 function Mode (File : File_Type) return File_Mode is
969 return To_TIO (FIO.Mode (AP (File)));
976 function Name (File : File_Type) return String is
978 return FIO.Name (AP (File));
987 Spacing : Positive_Count := 1)
990 -- Raise Constraint_Error if out of range value. The reason for this
991 -- explicit test is that we don't want junk values around, even if
992 -- checks are off in the caller.
994 if not Spacing'Valid then
995 raise Constraint_Error;
998 FIO.Check_Write_Status (AP (File));
1000 for K in 1 .. Spacing loop
1002 File.Line := File.Line + 1;
1004 if File.Page_Length /= 0
1005 and then File.Line > File.Page_Length
1009 File.Page := File.Page + 1;
1016 procedure New_Line (Spacing : Positive_Count := 1) is
1018 New_Line (Current_Out, Spacing);
1025 procedure New_Page (File : File_Type) is
1027 FIO.Check_Write_Status (AP (File));
1029 if File.Col /= 1 or else File.Line = 1 then
1034 File.Page := File.Page + 1;
1039 procedure New_Page is
1041 New_Page (Current_Out);
1048 function Nextc (File : File_Type) return int is
1052 ch := fgetc (File.Stream);
1055 if ferror (File.Stream) /= 0 then
1060 if ungetc (ch, File.Stream) = EOF then
1073 (File : in out File_Type;
1076 Form : String := "")
1078 Dummy_File_Control_Block : Wide_Text_AFCB;
1079 pragma Warnings (Off, Dummy_File_Control_Block);
1080 -- Yes, we know this is never assigned a value, only the tag
1081 -- is used for dispatching purposes, so that's expected.
1084 FIO.Open (File_Ptr => AP (File),
1085 Dummy_FCB => Dummy_File_Control_Block,
1086 Mode => To_FCB (Mode),
1099 -- Note: we assume that it is impossible in practice for the page
1100 -- to exceed the value of Count'Last, i.e. no check is required for
1101 -- overflow raising layout error.
1103 function Page (File : File_Type) return Positive_Count is
1105 FIO.Check_File_Open (AP (File));
1109 function Page return Positive_Count is
1111 return Page (Current_Out);
1118 function Page_Length (File : File_Type) return Count is
1120 FIO.Check_Write_Status (AP (File));
1121 return File.Page_Length;
1124 function Page_Length return Count is
1126 return Page_Length (Current_Out);
1135 Item : Wide_Character)
1137 procedure Out_Char (C : Character);
1138 -- Procedure to output one character of a wide character sequence
1140 procedure WC_Out is new Wide_Char_To_Char_Sequence (Out_Char);
1146 procedure Out_Char (C : Character) is
1148 Putc (Character'Pos (C), File);
1151 -- Start of processing for Put
1154 WC_Out (Item, File.WC_Method);
1155 File.Col := File.Col + 1;
1158 procedure Put (Item : Wide_Character) is
1160 Put (Current_Out, Item);
1172 for J in Item'Range loop
1173 Put (File, Item (J));
1177 procedure Put (Item : Wide_String) is
1179 Put (Current_Out, Item);
1195 procedure Put_Line (Item : Wide_String) is
1197 Put (Current_Out, Item);
1198 New_Line (Current_Out);
1205 procedure Putc (ch : int; File : File_Type) is
1207 if fputc (ch, File.Stream) = EOF then
1216 -- This is the primitive Stream Read routine, used when a Text_IO file
1217 -- is treated directly as a stream using Text_IO.Streams.Stream.
1220 (File : in out Wide_Text_AFCB;
1221 Item : out Stream_Element_Array;
1222 Last : out Stream_Element_Offset)
1225 pragma Unreferenced (Discard_ch);
1228 -- Need to deal with Before_Wide_Character ???
1230 if File.Mode /= FCB.In_File then
1234 -- Deal with case where our logical and physical position do not match
1235 -- because of being after an LM or LM-PM sequence when in fact we are
1236 -- logically positioned before it.
1238 if File.Before_LM then
1240 -- If we are before a PM, then it is possible for a stream read
1241 -- to leave us after the LM and before the PM, which is a bit
1242 -- odd. The easiest way to deal with this is to unget the PM,
1243 -- so we are indeed positioned between the characters. This way
1244 -- further stream read operations will work correctly, and the
1245 -- effect on text processing is a little weird, but what can
1246 -- be expected if stream and text input are mixed this way?
1248 if File.Before_LM_PM then
1249 Discard_ch := ungetc (PM, File.Stream);
1250 File.Before_LM_PM := False;
1253 File.Before_LM := False;
1255 Item (Item'First) := Stream_Element (Character'Pos (ASCII.LF));
1257 if Item'Length = 1 then
1263 Stream_Element_Offset
1264 (fread (buffer => Item'Address,
1265 index => size_t (Item'First + 1),
1267 count => Item'Length - 1,
1268 stream => File.Stream));
1274 -- Now we do the read. Since this is a text file, it is normally in
1275 -- text mode, but stream data must be read in binary mode, so we
1276 -- temporarily set binary mode for the read, resetting it after.
1277 -- These calls have no effect in a system (like Unix) where there is
1278 -- no distinction between text and binary files.
1280 set_binary_mode (fileno (File.Stream));
1284 Stream_Element_Offset
1285 (fread (Item'Address, 1, Item'Length, File.Stream)) - 1;
1287 if Last < Item'Last then
1288 if ferror (File.Stream) /= 0 then
1293 set_text_mode (fileno (File.Stream));
1301 (File : in out File_Type;
1305 -- Don't allow change of mode for current file (RM A.10.2(5))
1307 if (File = Current_In or else
1308 File = Current_Out or else
1309 File = Current_Error)
1310 and then To_FCB (Mode) /= File.Mode
1315 Terminate_Line (File);
1316 FIO.Reset (AP (File), To_FCB (Mode));
1320 File.Line_Length := 0;
1321 File.Page_Length := 0;
1322 File.Before_LM := False;
1323 File.Before_LM_PM := False;
1326 procedure Reset (File : in out File_Type) is
1328 Terminate_Line (File);
1329 FIO.Reset (AP (File));
1333 File.Line_Length := 0;
1334 File.Page_Length := 0;
1335 File.Before_LM := False;
1336 File.Before_LM_PM := False;
1345 To : Positive_Count)
1350 -- Raise Constraint_Error if out of range value. The reason for this
1351 -- explicit test is that we don't want junk values around, even if
1352 -- checks are off in the caller.
1354 if not To'Valid then
1355 raise Constraint_Error;
1358 FIO.Check_File_Open (AP (File));
1360 if To = File.Col then
1364 if Mode (File) >= Out_File then
1365 if File.Line_Length /= 0 and then To > File.Line_Length then
1369 if To < File.Col then
1373 while File.Col < To loop
1385 File.Line := File.Line + 1;
1388 elsif ch = PM and then File.Is_Regular_File then
1389 File.Page := File.Page + 1;
1393 elsif To = File.Col then
1398 File.Col := File.Col + 1;
1404 procedure Set_Col (To : Positive_Count) is
1406 Set_Col (Current_Out, To);
1413 procedure Set_Error (File : File_Type) is
1415 FIO.Check_Write_Status (AP (File));
1416 Current_Err := File;
1423 procedure Set_Input (File : File_Type) is
1425 FIO.Check_Read_Status (AP (File));
1435 To : Positive_Count)
1438 -- Raise Constraint_Error if out of range value. The reason for this
1439 -- explicit test is that we don't want junk values around, even if
1440 -- checks are off in the caller.
1442 if not To'Valid then
1443 raise Constraint_Error;
1446 FIO.Check_File_Open (AP (File));
1448 if To = File.Line then
1452 if Mode (File) >= Out_File then
1453 if File.Page_Length /= 0 and then To > File.Page_Length then
1457 if To < File.Line then
1461 while File.Line < To loop
1466 while To /= File.Line loop
1472 procedure Set_Line (To : Positive_Count) is
1474 Set_Line (Current_Out, To);
1477 ---------------------
1478 -- Set_Line_Length --
1479 ---------------------
1481 procedure Set_Line_Length (File : File_Type; To : Count) is
1483 -- Raise Constraint_Error if out of range value. The reason for this
1484 -- explicit test is that we don't want junk values around, even if
1485 -- checks are off in the caller.
1487 if not To'Valid then
1488 raise Constraint_Error;
1491 FIO.Check_Write_Status (AP (File));
1492 File.Line_Length := To;
1493 end Set_Line_Length;
1495 procedure Set_Line_Length (To : Count) is
1497 Set_Line_Length (Current_Out, To);
1498 end Set_Line_Length;
1504 procedure Set_Output (File : File_Type) is
1506 FIO.Check_Write_Status (AP (File));
1507 Current_Out := File;
1510 ---------------------
1511 -- Set_Page_Length --
1512 ---------------------
1514 procedure Set_Page_Length (File : File_Type; To : Count) is
1516 -- Raise Constraint_Error if out of range value. The reason for this
1517 -- explicit test is that we don't want junk values around, even if
1518 -- checks are off in the caller.
1520 if not To'Valid then
1521 raise Constraint_Error;
1524 FIO.Check_Write_Status (AP (File));
1525 File.Page_Length := To;
1526 end Set_Page_Length;
1528 procedure Set_Page_Length (To : Count) is
1530 Set_Page_Length (Current_Out, To);
1531 end Set_Page_Length;
1537 procedure Set_WCEM (File : in out File_Type) is
1542 File.WC_Method := WCEM_Brackets;
1543 FIO.Form_Parameter (File.Form.all, "wcem", Start, Stop);
1546 File.WC_Method := WCEM_Brackets;
1548 elsif Start /= 0 then
1549 if Stop = Start then
1550 for J in WC_Encoding_Letters'Range loop
1551 if File.Form (Start) = WC_Encoding_Letters (J) then
1552 File.WC_Method := J;
1559 raise Use_Error with "invalid WCEM form parameter";
1569 Spacing : Positive_Count := 1)
1574 -- Raise Constraint_Error if out of range value. The reason for this
1575 -- explicit test is that we don't want junk values around, even if
1576 -- checks are off in the caller.
1578 if not Spacing'Valid then
1579 raise Constraint_Error;
1582 FIO.Check_Read_Status (AP (File));
1584 for L in 1 .. Spacing loop
1585 if File.Before_LM then
1586 File.Before_LM := False;
1587 File.Before_LM_PM := False;
1592 -- If at end of file now, then immediately raise End_Error. Note
1593 -- that we can never be positioned between a line mark and a page
1594 -- mark, so if we are at the end of file, we cannot logically be
1595 -- before the implicit page mark that is at the end of the file.
1597 -- For the same reason, we do not need an explicit check for a
1598 -- page mark. If there is a FF in the middle of a line, the file
1599 -- is not in canonical format and we do not care about the page
1600 -- numbers for files other than ones in canonical format.
1606 -- If not at end of file, then loop till we get to an LM or EOF.
1607 -- The latter case happens only in non-canonical files where the
1608 -- last line is not terminated by LM, but we don't want to blow
1609 -- up for such files, so we assume an implicit LM in this case.
1612 exit when ch = LM or ch = EOF;
1617 -- We have got past a line mark, now, for a regular file only,
1618 -- see if a page mark immediately follows this line mark and
1619 -- if so, skip past the page mark as well. We do not do this
1620 -- for non-regular files, since it would cause an undesirable
1621 -- wait for an additional character.
1624 File.Line := File.Line + 1;
1626 if File.Before_LM_PM then
1627 File.Page := File.Page + 1;
1629 File.Before_LM_PM := False;
1631 elsif File.Is_Regular_File then
1634 -- Page mark can be explicit, or implied at the end of the file
1636 if (ch = PM or else ch = EOF)
1637 and then File.Is_Regular_File
1639 File.Page := File.Page + 1;
1647 File.Before_Wide_Character := False;
1650 procedure Skip_Line (Spacing : Positive_Count := 1) is
1652 Skip_Line (Current_In, Spacing);
1659 procedure Skip_Page (File : File_Type) is
1663 FIO.Check_Read_Status (AP (File));
1665 -- If at page mark already, just skip it
1667 if File.Before_LM_PM then
1668 File.Before_LM := False;
1669 File.Before_LM_PM := False;
1670 File.Page := File.Page + 1;
1676 -- This is a bit tricky, if we are logically before an LM then
1677 -- it is not an error if we are at an end of file now, since we
1678 -- are not really at it.
1680 if File.Before_LM then
1681 File.Before_LM := False;
1682 File.Before_LM_PM := False;
1685 -- Otherwise we do raise End_Error if we are at the end of file now
1695 -- Now we can just rumble along to the next page mark, or to the
1696 -- end of file, if that comes first. The latter case happens when
1697 -- the page mark is implied at the end of file.
1701 or else (ch = PM and then File.Is_Regular_File);
1705 File.Page := File.Page + 1;
1708 File.Before_Wide_Character := False;
1711 procedure Skip_Page is
1713 Skip_Page (Current_In);
1716 --------------------
1717 -- Standard_Error --
1718 --------------------
1720 function Standard_Error return File_Type is
1722 return Standard_Err;
1725 function Standard_Error return File_Access is
1727 return Standard_Err'Access;
1730 --------------------
1731 -- Standard_Input --
1732 --------------------
1734 function Standard_Input return File_Type is
1739 function Standard_Input return File_Access is
1741 return Standard_In'Access;
1744 ---------------------
1745 -- Standard_Output --
1746 ---------------------
1748 function Standard_Output return File_Type is
1750 return Standard_Out;
1751 end Standard_Output;
1753 function Standard_Output return File_Access is
1755 return Standard_Out'Access;
1756 end Standard_Output;
1758 --------------------
1759 -- Terminate_Line --
1760 --------------------
1762 procedure Terminate_Line (File : File_Type) is
1764 FIO.Check_File_Open (AP (File));
1766 -- For file other than In_File, test for needing to terminate last line
1768 if Mode (File) /= In_File then
1770 -- If not at start of line definition need new line
1772 if File.Col /= 1 then
1775 -- For files other than standard error and standard output, we
1776 -- make sure that an empty file has a single line feed, so that
1777 -- it is properly formatted. We avoid this for the standard files
1778 -- because it is too much of a nuisance to have these odd line
1779 -- feeds when nothing has been written to the file.
1781 elsif (File /= Standard_Err and then File /= Standard_Out)
1782 and then (File.Line = 1 and then File.Page = 1)
1793 procedure Ungetc (ch : int; File : File_Type) is
1796 if ungetc (ch, File.Stream) = EOF then
1806 -- This is the primitive Stream Write routine, used when a Text_IO file
1807 -- is treated directly as a stream using Text_IO.Streams.Stream.
1810 (File : in out Wide_Text_AFCB;
1811 Item : Stream_Element_Array)
1813 pragma Warnings (Off, File);
1814 -- Because in this implementation we don't need IN OUT, we only read
1816 Siz : constant size_t := Item'Length;
1819 if File.Mode = FCB.In_File then
1823 -- Now we do the write. Since this is a text file, it is normally in
1824 -- text mode, but stream data must be written in binary mode, so we
1825 -- temporarily set binary mode for the write, resetting it after.
1826 -- These calls have no effect in a system (like Unix) where there is
1827 -- no distinction between text and binary files.
1829 set_binary_mode (fileno (File.Stream));
1831 if fwrite (Item'Address, 1, Siz, File.Stream) /= Siz then
1835 set_text_mode (fileno (File.Stream));
1838 -- Use "preallocated" strings to avoid calling "new" during the
1839 -- elaboration of the run time. This is needed in the tasking case to
1840 -- avoid calling Task_Lock too early. A filename is expected to end with
1841 -- a null character in the runtime, here the null characters are added
1842 -- just to have a correct filename length.
1844 Err_Name : aliased String := "*stderr" & ASCII.Nul;
1845 In_Name : aliased String := "*stdin" & ASCII.Nul;
1846 Out_Name : aliased String := "*stdout" & ASCII.Nul;
1849 -------------------------------
1850 -- Initialize Standard Files --
1851 -------------------------------
1853 for J in WC_Encoding_Method loop
1854 if WC_Encoding = WC_Encoding_Letters (J) then
1859 -- Note: the names in these files are bogus, and probably it would be
1860 -- better for these files to have no names, but the ACVC test insist!
1861 -- We use names that are bound to fail in open etc.
1863 Standard_Err.Stream := stderr;
1864 Standard_Err.Name := Err_Name'Access;
1865 Standard_Err.Form := Null_Str'Unrestricted_Access;
1866 Standard_Err.Mode := FCB.Out_File;
1867 Standard_Err.Is_Regular_File := is_regular_file (fileno (stderr)) /= 0;
1868 Standard_Err.Is_Temporary_File := False;
1869 Standard_Err.Is_System_File := True;
1870 Standard_Err.Is_Text_File := True;
1871 Standard_Err.Access_Method := 'T';
1872 Standard_Err.WC_Method := Default_WCEM;
1874 Standard_In.Stream := stdin;
1875 Standard_In.Name := In_Name'Access;
1876 Standard_In.Form := Null_Str'Unrestricted_Access;
1877 Standard_In.Mode := FCB.In_File;
1878 Standard_In.Is_Regular_File := is_regular_file (fileno (stdin)) /= 0;
1879 Standard_In.Is_Temporary_File := False;
1880 Standard_In.Is_System_File := True;
1881 Standard_In.Is_Text_File := True;
1882 Standard_In.Access_Method := 'T';
1883 Standard_In.WC_Method := Default_WCEM;
1885 Standard_Out.Stream := stdout;
1886 Standard_Out.Name := Out_Name'Access;
1887 Standard_Out.Form := Null_Str'Unrestricted_Access;
1888 Standard_Out.Mode := FCB.Out_File;
1889 Standard_Out.Is_Regular_File := is_regular_file (fileno (stdout)) /= 0;
1890 Standard_Out.Is_Temporary_File := False;
1891 Standard_Out.Is_System_File := True;
1892 Standard_Out.Is_Text_File := True;
1893 Standard_Out.Access_Method := 'T';
1894 Standard_Out.WC_Method := Default_WCEM;
1896 FIO.Chain_File (AP (Standard_In));
1897 FIO.Chain_File (AP (Standard_Out));
1898 FIO.Chain_File (AP (Standard_Err));
1900 FIO.Make_Unbuffered (AP (Standard_Out));
1901 FIO.Make_Unbuffered (AP (Standard_Err));
1903 end Ada.Wide_Text_IO;