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.Exceptions; use Ada.Exceptions;
35 with Ada.Streams; use Ada.Streams;
36 with Interfaces.C_Streams; use Interfaces.C_Streams;
40 with System.WCh_Cnv; use System.WCh_Cnv;
41 with System.WCh_Con; use System.WCh_Con;
43 with Ada.Unchecked_Conversion;
44 with Ada.Unchecked_Deallocation;
46 pragma Elaborate_All (System.File_IO);
47 -- Needed because of calls to Chain_File in package body elaboration
49 package body Ada.Wide_Text_IO is
51 package FIO renames System.File_IO;
53 subtype AP is FCB.AFCB_Ptr;
55 function To_FCB is new Ada.Unchecked_Conversion (File_Mode, FCB.File_Mode);
56 function To_TIO is new Ada.Unchecked_Conversion (FCB.File_Mode, File_Mode);
57 use type FCB.File_Mode;
59 use type System.CRTL.size_t;
61 WC_Encoding : Character;
62 pragma Import (C, WC_Encoding, "__gl_wc_encoding");
64 -----------------------
65 -- Local Subprograms --
66 -----------------------
68 function Getc_Immed (File : File_Type) return int;
69 -- This routine is identical to Getc, except that the read is done in
70 -- Get_Immediate mode (i.e. without waiting for a line return).
72 function Get_Wide_Char_Immed
74 File : File_Type) return Wide_Character;
75 -- This routine is identical to Get_Wide_Char, except that the reads are
76 -- done in Get_Immediate mode (i.e. without waiting for a line return).
78 procedure Set_WCEM (File : in out File_Type);
79 -- Called by Open and Create to set the wide character encoding method
80 -- for the file, processing a WCEM form parameter if one is present.
81 -- File is IN OUT because it may be closed in case of an error.
87 function AFCB_Allocate
88 (Control_Block : Wide_Text_AFCB) return FCB.AFCB_Ptr
90 pragma Unreferenced (Control_Block);
92 return new Wide_Text_AFCB;
99 procedure AFCB_Close (File : not null access Wide_Text_AFCB) is
101 -- If the file being closed is one of the current files, then close
102 -- the corresponding current file. It is not clear that this action
103 -- is required (RM A.10.3(23)) but it seems reasonable, and besides
104 -- ACVC test CE3208A expects this behavior.
106 if File_Type (File) = Current_In then
108 elsif File_Type (File) = Current_Out then
110 elsif File_Type (File) = Current_Err then
114 Terminate_Line (File_Type (File));
121 procedure AFCB_Free (File : not null access Wide_Text_AFCB) is
122 type FCB_Ptr is access all Wide_Text_AFCB;
123 FT : FCB_Ptr := FCB_Ptr (File);
126 new Ada.Unchecked_Deallocation (Wide_Text_AFCB, FCB_Ptr);
136 procedure Close (File : in out File_Type) is
138 FIO.Close (AP (File));
145 -- Note: we assume that it is impossible in practice for the column
146 -- to exceed the value of Count'Last, i.e. no check is required for
147 -- overflow raising layout error.
149 function Col (File : File_Type) return Positive_Count is
151 FIO.Check_File_Open (AP (File));
155 function Col return Positive_Count is
157 return Col (Current_Out);
165 (File : in out File_Type;
166 Mode : File_Mode := Out_File;
170 Dummy_File_Control_Block : Wide_Text_AFCB;
171 pragma Warnings (Off, Dummy_File_Control_Block);
172 -- Yes, we know this is never assigned a value, only the tag
173 -- is used for dispatching purposes, so that's expected.
176 FIO.Open (File_Ptr => AP (File),
177 Dummy_FCB => Dummy_File_Control_Block,
178 Mode => To_FCB (Mode),
191 function Current_Error return File_Type is
196 function Current_Error return File_Access is
198 return Current_Err'Access;
205 function Current_Input return File_Type is
210 function Current_Input return File_Access is
212 return Current_In'Access;
219 function Current_Output return File_Type is
224 function Current_Output return File_Access is
226 return Current_Out'Access;
233 procedure Delete (File : in out File_Type) is
235 FIO.Delete (AP (File));
242 function End_Of_File (File : File_Type) return Boolean is
246 FIO.Check_Read_Status (AP (File));
248 if File.Before_Wide_Character then
251 elsif File.Before_LM then
253 if File.Before_LM_PM then
254 return Nextc (File) = EOF;
268 File.Before_LM := True;
272 -- Here we are just past the line mark with Before_LM set so that we
273 -- do not have to try to back up past the LM, thus avoiding the need
274 -- to back up more than one character.
281 elsif ch = PM and then File.Is_Regular_File then
282 File.Before_LM_PM := True;
283 return Nextc (File) = EOF;
285 -- Here if neither EOF nor PM followed end of line
294 function End_Of_File return Boolean is
296 return End_Of_File (Current_In);
303 function End_Of_Line (File : File_Type) return Boolean is
307 FIO.Check_Read_Status (AP (File));
309 if File.Before_Wide_Character then
312 elsif File.Before_LM then
328 function End_Of_Line return Boolean is
330 return End_Of_Line (Current_In);
337 function End_Of_Page (File : File_Type) return Boolean is
341 FIO.Check_Read_Status (AP (File));
343 if not File.Is_Regular_File then
346 elsif File.Before_Wide_Character then
349 elsif File.Before_LM then
350 if File.Before_LM_PM then
365 File.Before_LM := True;
369 -- Here we are just past the line mark with Before_LM set so that we
370 -- do not have to try to back up past the LM, thus avoiding the need
371 -- to back up more than one character.
375 return ch = PM or else ch = EOF;
378 function End_Of_Page return Boolean is
380 return End_Of_Page (Current_In);
387 procedure Flush (File : File_Type) is
389 FIO.Flush (AP (File));
401 function Form (File : File_Type) return String is
403 return FIO.Form (AP (File));
412 Item : out Wide_Character)
417 FIO.Check_Read_Status (AP (File));
419 if File.Before_Wide_Character then
420 File.Before_Wide_Character := False;
421 Item := File.Saved_Wide_Character;
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 ch := Getc_Immed (File);
563 Item := Get_Wide_Char_Immed (Character'Val (ch), File);
568 procedure Get_Immediate
569 (Item : out Wide_Character;
570 Available : out Boolean)
573 Get_Immediate (Current_In, Item, Available);
582 Item : out Wide_String;
586 FIO.Check_Read_Status (AP (File));
587 Last := Item'First - 1;
589 -- Immediate exit for null string, this is a case in which we do not
590 -- need to test for end of file and we do not skip a line mark under
591 -- any circumstances.
593 if Last >= Item'Last then
597 -- Here we have at least one character, if we are immediately before
598 -- a line mark, then we will just skip past it storing no characters.
600 if File.Before_LM then
601 File.Before_LM := False;
602 File.Before_LM_PM := False;
604 -- Otherwise we need to read some characters
607 -- If we are at the end of file now, it means we are trying to
608 -- skip a file terminator and we raise End_Error (RM A.10.7(20))
610 if Nextc (File) = EOF then
614 -- Loop through characters in string
617 -- Exit the loop if read is terminated by encountering line mark
618 -- Note that the use of Skip_Line here ensures we properly deal
619 -- with setting the page and line numbers.
621 if End_Of_Line (File) then
626 -- Otherwise store the character, note that we know that ch is
627 -- something other than LM or EOF. It could possibly be a page
628 -- mark if there is a stray page mark in the middle of a line,
629 -- but this is not an official page mark in any case, since
630 -- official page marks can only follow a line mark. The whole
631 -- page business is pretty much nonsense anyway, so we do not
632 -- want to waste time trying to make sense out of non-standard
633 -- page marks in the file! This means that the behavior of
634 -- Get_Line is different from repeated Get of a character, but
635 -- that's too bad. We only promise that page numbers etc make
636 -- sense if the file is formatted in a standard manner.
638 -- Note: we do not adjust the column number because it is quicker
639 -- to adjust it once at the end of the operation than incrementing
640 -- it each time around the loop.
643 Get (File, Item (Last));
645 -- All done if the string is full, this is the case in which
646 -- we do not skip the following line mark. We need to adjust
647 -- the column number in this case.
649 if Last = Item'Last then
650 File.Col := File.Col + Count (Item'Length);
654 -- Exit from the loop if we are at the end of file. This happens
655 -- if we have a last line that is not terminated with a line mark.
656 -- In this case we consider that there is an implied line mark;
657 -- this is a non-standard file, but we will treat it nicely.
659 exit when Nextc (File) = EOF;
665 (Item : out Wide_String;
669 Get_Line (Current_In, Item, Last);
672 function Get_Line (File : File_Type) return Wide_String is
673 Buffer : Wide_String (1 .. 500);
676 function Get_Rest (S : Wide_String) return Wide_String;
677 -- This is a recursive function that reads the rest of the line and
678 -- returns it. S is the part read so far.
684 function Get_Rest (S : Wide_String) return Wide_String is
686 -- Each time we allocate a buffer the same size as what we have
687 -- read so far. This limits us to a logarithmic number of calls
688 -- to Get_Rest and also ensures only a linear use of stack space.
690 Buffer : Wide_String (1 .. S'Length);
694 Get_Line (File, Buffer, Last);
697 R : constant Wide_String := S & Buffer (1 .. Last);
699 if Last < Buffer'Last then
707 -- Start of processing for Get_Line
710 Get_Line (File, Buffer, Last);
712 if Last < Buffer'Last then
713 return Buffer (1 .. Last);
715 return Get_Rest (Buffer (1 .. Last));
719 function Get_Line return Wide_String is
721 return Get_Line (Current_In);
728 function Get_Wide_Char
730 File : File_Type) return Wide_Character
732 function In_Char return Character;
733 -- Function used to obtain additional characters it the wide character
734 -- sequence is more than one character long.
736 function WC_In is new Char_Sequence_To_Wide_Char (In_Char);
742 function In_Char return Character is
743 ch : constant Integer := Getc (File);
748 return Character'Val (ch);
752 -- Start of processing for In_Char
755 return WC_In (C, File.WC_Method);
758 -------------------------
759 -- Get_Wide_Char_Immed --
760 -------------------------
762 function Get_Wide_Char_Immed
764 File : File_Type) return Wide_Character
766 function In_Char return Character;
767 -- Function used to obtain additional characters it the wide character
768 -- sequence is more than one character long.
770 function WC_In is new Char_Sequence_To_Wide_Char (In_Char);
776 function In_Char return Character is
777 ch : constant Integer := Getc_Immed (File);
782 return Character'Val (ch);
786 -- Start of processing for Get_Wide_Char_Immed
789 return WC_In (C, File.WC_Method);
790 end Get_Wide_Char_Immed;
796 function Getc (File : File_Type) return int is
800 ch := fgetc (File.Stream);
802 if ch = EOF and then ferror (File.Stream) /= 0 then
813 function Getc_Immed (File : File_Type) return int is
817 procedure getc_immediate
818 (stream : FILEs; ch : out int; end_of_file : out int);
819 pragma Import (C, getc_immediate, "getc_immediate");
822 FIO.Check_Read_Status (AP (File));
824 if File.Before_LM then
825 File.Before_LM := False;
826 File.Before_LM_PM := False;
830 getc_immediate (File.Stream, ch, end_of_file);
832 if ferror (File.Stream) /= 0 then
834 elsif end_of_file /= 0 then
846 function Is_Open (File : File_Type) return Boolean is
848 return FIO.Is_Open (AP (File));
855 -- Note: we assume that it is impossible in practice for the line
856 -- to exceed the value of Count'Last, i.e. no check is required for
857 -- overflow raising layout error.
859 function Line (File : File_Type) return Positive_Count is
861 FIO.Check_File_Open (AP (File));
865 function Line return Positive_Count is
867 return Line (Current_Out);
874 function Line_Length (File : File_Type) return Count is
876 FIO.Check_Write_Status (AP (File));
877 return File.Line_Length;
880 function Line_Length return Count is
882 return Line_Length (Current_Out);
891 Item : out Wide_Character;
892 End_Of_Line : out Boolean)
896 -- Start of processing for Look_Ahead
899 FIO.Check_Read_Status (AP (File));
901 -- If we are logically before a line mark, we can return immediately
903 if File.Before_LM then
905 Item := Wide_Character'Val (0);
907 -- If we are before a wide character, just return it (this happens
908 -- if there are two calls to Look_Ahead in a row).
910 elsif File.Before_Wide_Character then
911 End_Of_Line := False;
912 Item := File.Saved_Wide_Character;
914 -- otherwise we must read a character from the input stream
921 or else (ch = EOF and then File.Is_Regular_File)
925 Item := Wide_Character'Val (0);
927 -- If the character is in the range 16#0000# to 16#007F# it stands
928 -- for itself and occupies a single byte, so we can unget it with
931 elsif ch <= 16#0080# then
932 End_Of_Line := False;
934 Item := Wide_Character'Val (ch);
936 -- For a character above this range, we read the character, using
937 -- the Get_Wide_Char routine. It may well occupy more than one byte
938 -- so we can't put it back with ungetc. Instead we save it in the
939 -- control block, setting a flag that everyone interested in reading
940 -- characters must test before reading the stream.
943 Item := Get_Wide_Char (Character'Val (ch), File);
944 End_Of_Line := False;
945 File.Saved_Wide_Character := Item;
946 File.Before_Wide_Character := True;
952 (Item : out Wide_Character;
953 End_Of_Line : out Boolean)
956 Look_Ahead (Current_In, Item, End_Of_Line);
963 function Mode (File : File_Type) return File_Mode is
965 return To_TIO (FIO.Mode (AP (File)));
972 function Name (File : File_Type) return String is
974 return FIO.Name (AP (File));
983 Spacing : Positive_Count := 1)
986 -- Raise Constraint_Error if out of range value. The reason for this
987 -- explicit test is that we don't want junk values around, even if
988 -- checks are off in the caller.
990 if not Spacing'Valid then
991 raise Constraint_Error;
994 FIO.Check_Write_Status (AP (File));
996 for K in 1 .. Spacing loop
998 File.Line := File.Line + 1;
1000 if File.Page_Length /= 0
1001 and then File.Line > File.Page_Length
1005 File.Page := File.Page + 1;
1012 procedure New_Line (Spacing : Positive_Count := 1) is
1014 New_Line (Current_Out, Spacing);
1021 procedure New_Page (File : File_Type) is
1023 FIO.Check_Write_Status (AP (File));
1025 if File.Col /= 1 or else File.Line = 1 then
1030 File.Page := File.Page + 1;
1035 procedure New_Page is
1037 New_Page (Current_Out);
1044 function Nextc (File : File_Type) return int is
1048 ch := fgetc (File.Stream);
1051 if ferror (File.Stream) /= 0 then
1056 if ungetc (ch, File.Stream) = EOF then
1069 (File : in out File_Type;
1072 Form : String := "")
1074 Dummy_File_Control_Block : Wide_Text_AFCB;
1075 pragma Warnings (Off, Dummy_File_Control_Block);
1076 -- Yes, we know this is never assigned a value, only the tag
1077 -- is used for dispatching purposes, so that's expected.
1080 FIO.Open (File_Ptr => AP (File),
1081 Dummy_FCB => Dummy_File_Control_Block,
1082 Mode => To_FCB (Mode),
1095 -- Note: we assume that it is impossible in practice for the page
1096 -- to exceed the value of Count'Last, i.e. no check is required for
1097 -- overflow raising layout error.
1099 function Page (File : File_Type) return Positive_Count is
1101 FIO.Check_File_Open (AP (File));
1105 function Page return Positive_Count is
1107 return Page (Current_Out);
1114 function Page_Length (File : File_Type) return Count is
1116 FIO.Check_Write_Status (AP (File));
1117 return File.Page_Length;
1120 function Page_Length return Count is
1122 return Page_Length (Current_Out);
1131 Item : Wide_Character)
1133 procedure Out_Char (C : Character);
1134 -- Procedure to output one character of a wide character sequence
1136 procedure WC_Out is new Wide_Char_To_Char_Sequence (Out_Char);
1141 procedure Out_Char (C : Character) is
1143 Putc (Character'Pos (C), File);
1146 -- Start of processing for Put
1149 WC_Out (Item, File.WC_Method);
1150 File.Col := File.Col + 1;
1153 procedure Put (Item : Wide_Character) is
1155 Put (Current_Out, Item);
1167 for J in Item'Range loop
1168 Put (File, Item (J));
1172 procedure Put (Item : Wide_String) is
1174 Put (Current_Out, Item);
1190 procedure Put_Line (Item : Wide_String) is
1192 Put (Current_Out, Item);
1193 New_Line (Current_Out);
1200 procedure Putc (ch : int; File : File_Type) is
1202 if fputc (ch, File.Stream) = EOF then
1211 -- This is the primitive Stream Read routine, used when a Text_IO file
1212 -- is treated directly as a stream using Text_IO.Streams.Stream.
1215 (File : in out Wide_Text_AFCB;
1216 Item : out Stream_Element_Array;
1217 Last : out Stream_Element_Offset)
1220 pragma Unreferenced (Discard_ch);
1223 -- Need to deal with Before_Wide_Character ???
1225 if File.Mode /= FCB.In_File then
1229 -- Deal with case where our logical and physical position do not match
1230 -- because of being after an LM or LM-PM sequence when in fact we are
1231 -- logically positioned before it.
1233 if File.Before_LM then
1235 -- If we are before a PM, then it is possible for a stream read
1236 -- to leave us after the LM and before the PM, which is a bit
1237 -- odd. The easiest way to deal with this is to unget the PM,
1238 -- so we are indeed positioned between the characters. This way
1239 -- further stream read operations will work correctly, and the
1240 -- effect on text processing is a little weird, but what can
1241 -- be expected if stream and text input are mixed this way?
1243 if File.Before_LM_PM then
1244 Discard_ch := ungetc (PM, File.Stream);
1245 File.Before_LM_PM := False;
1248 File.Before_LM := False;
1250 Item (Item'First) := Stream_Element (Character'Pos (ASCII.LF));
1252 if Item'Length = 1 then
1258 Stream_Element_Offset
1259 (fread (buffer => Item'Address,
1260 index => size_t (Item'First + 1),
1262 count => Item'Length - 1,
1263 stream => File.Stream));
1269 -- Now we do the read. Since this is a text file, it is normally in
1270 -- text mode, but stream data must be read in binary mode, so we
1271 -- temporarily set binary mode for the read, resetting it after.
1272 -- These calls have no effect in a system (like Unix) where there is
1273 -- no distinction between text and binary files.
1275 set_binary_mode (fileno (File.Stream));
1279 Stream_Element_Offset
1280 (fread (Item'Address, 1, Item'Length, File.Stream)) - 1;
1282 if Last < Item'Last then
1283 if ferror (File.Stream) /= 0 then
1288 set_text_mode (fileno (File.Stream));
1296 (File : in out File_Type;
1300 -- Don't allow change of mode for current file (RM A.10.2(5))
1302 if (File = Current_In or else
1303 File = Current_Out or else
1304 File = Current_Error)
1305 and then To_FCB (Mode) /= File.Mode
1310 Terminate_Line (File);
1311 FIO.Reset (AP (File), To_FCB (Mode));
1315 File.Line_Length := 0;
1316 File.Page_Length := 0;
1317 File.Before_LM := False;
1318 File.Before_LM_PM := False;
1321 procedure Reset (File : in out File_Type) is
1323 Terminate_Line (File);
1324 FIO.Reset (AP (File));
1328 File.Line_Length := 0;
1329 File.Page_Length := 0;
1330 File.Before_LM := False;
1331 File.Before_LM_PM := False;
1340 To : Positive_Count)
1345 -- Raise Constraint_Error if out of range value. The reason for this
1346 -- explicit test is that we don't want junk values around, even if
1347 -- checks are off in the caller.
1349 if not To'Valid then
1350 raise Constraint_Error;
1353 FIO.Check_File_Open (AP (File));
1355 if To = File.Col then
1359 if Mode (File) >= Out_File then
1360 if File.Line_Length /= 0 and then To > File.Line_Length then
1364 if To < File.Col then
1368 while File.Col < To loop
1380 File.Line := File.Line + 1;
1383 elsif ch = PM and then File.Is_Regular_File then
1384 File.Page := File.Page + 1;
1388 elsif To = File.Col then
1393 File.Col := File.Col + 1;
1399 procedure Set_Col (To : Positive_Count) is
1401 Set_Col (Current_Out, To);
1408 procedure Set_Error (File : File_Type) is
1410 FIO.Check_Write_Status (AP (File));
1411 Current_Err := File;
1418 procedure Set_Input (File : File_Type) is
1420 FIO.Check_Read_Status (AP (File));
1430 To : Positive_Count)
1433 -- Raise Constraint_Error if out of range value. The reason for this
1434 -- explicit test is that we don't want junk values around, even if
1435 -- checks are off in the caller.
1437 if not To'Valid then
1438 raise Constraint_Error;
1441 FIO.Check_File_Open (AP (File));
1443 if To = File.Line then
1447 if Mode (File) >= Out_File then
1448 if File.Page_Length /= 0 and then To > File.Page_Length then
1452 if To < File.Line then
1456 while File.Line < To loop
1461 while To /= File.Line loop
1467 procedure Set_Line (To : Positive_Count) is
1469 Set_Line (Current_Out, To);
1472 ---------------------
1473 -- Set_Line_Length --
1474 ---------------------
1476 procedure Set_Line_Length (File : File_Type; To : Count) is
1478 -- Raise Constraint_Error if out of range value. The reason for this
1479 -- explicit test is that we don't want junk values around, even if
1480 -- checks are off in the caller.
1482 if not To'Valid then
1483 raise Constraint_Error;
1486 FIO.Check_Write_Status (AP (File));
1487 File.Line_Length := To;
1488 end Set_Line_Length;
1490 procedure Set_Line_Length (To : Count) is
1492 Set_Line_Length (Current_Out, To);
1493 end Set_Line_Length;
1499 procedure Set_Output (File : File_Type) is
1501 FIO.Check_Write_Status (AP (File));
1502 Current_Out := File;
1505 ---------------------
1506 -- Set_Page_Length --
1507 ---------------------
1509 procedure Set_Page_Length (File : File_Type; To : Count) is
1511 -- Raise Constraint_Error if out of range value. The reason for this
1512 -- explicit test is that we don't want junk values around, even if
1513 -- checks are off in the caller.
1515 if not To'Valid then
1516 raise Constraint_Error;
1519 FIO.Check_Write_Status (AP (File));
1520 File.Page_Length := To;
1521 end Set_Page_Length;
1523 procedure Set_Page_Length (To : Count) is
1525 Set_Page_Length (Current_Out, To);
1526 end Set_Page_Length;
1532 procedure Set_WCEM (File : in out File_Type) is
1537 File.WC_Method := WCEM_Brackets;
1538 FIO.Form_Parameter (File.Form.all, "wcem", Start, Stop);
1541 File.WC_Method := WCEM_Brackets;
1543 elsif Start /= 0 then
1544 if Stop = Start then
1545 for J in WC_Encoding_Letters'Range loop
1546 if File.Form (Start) = WC_Encoding_Letters (J) then
1547 File.WC_Method := J;
1554 Raise_Exception (Use_Error'Identity, "invalid WCEM form parameter");
1564 Spacing : Positive_Count := 1)
1569 -- Raise Constraint_Error if out of range value. The reason for this
1570 -- explicit test is that we don't want junk values around, even if
1571 -- checks are off in the caller.
1573 if not Spacing'Valid then
1574 raise Constraint_Error;
1577 FIO.Check_Read_Status (AP (File));
1579 for L in 1 .. Spacing loop
1580 if File.Before_LM then
1581 File.Before_LM := False;
1582 File.Before_LM_PM := False;
1587 -- If at end of file now, then immediately raise End_Error. Note
1588 -- that we can never be positioned between a line mark and a page
1589 -- mark, so if we are at the end of file, we cannot logically be
1590 -- before the implicit page mark that is at the end of the file.
1592 -- For the same reason, we do not need an explicit check for a
1593 -- page mark. If there is a FF in the middle of a line, the file
1594 -- is not in canonical format and we do not care about the page
1595 -- numbers for files other than ones in canonical format.
1601 -- If not at end of file, then loop till we get to an LM or EOF.
1602 -- The latter case happens only in non-canonical files where the
1603 -- last line is not terminated by LM, but we don't want to blow
1604 -- up for such files, so we assume an implicit LM in this case.
1607 exit when ch = LM or ch = EOF;
1612 -- We have got past a line mark, now, for a regular file only,
1613 -- see if a page mark immediately follows this line mark and
1614 -- if so, skip past the page mark as well. We do not do this
1615 -- for non-regular files, since it would cause an undesirable
1616 -- wait for an additional character.
1619 File.Line := File.Line + 1;
1621 if File.Before_LM_PM then
1622 File.Page := File.Page + 1;
1624 File.Before_LM_PM := False;
1626 elsif File.Is_Regular_File then
1629 -- Page mark can be explicit, or implied at the end of the file
1631 if (ch = PM or else ch = EOF)
1632 and then File.Is_Regular_File
1634 File.Page := File.Page + 1;
1643 File.Before_Wide_Character := False;
1646 procedure Skip_Line (Spacing : Positive_Count := 1) is
1648 Skip_Line (Current_In, Spacing);
1655 procedure Skip_Page (File : File_Type) is
1659 FIO.Check_Read_Status (AP (File));
1661 -- If at page mark already, just skip it
1663 if File.Before_LM_PM then
1664 File.Before_LM := False;
1665 File.Before_LM_PM := False;
1666 File.Page := File.Page + 1;
1672 -- This is a bit tricky, if we are logically before an LM then
1673 -- it is not an error if we are at an end of file now, since we
1674 -- are not really at it.
1676 if File.Before_LM then
1677 File.Before_LM := False;
1678 File.Before_LM_PM := False;
1681 -- Otherwise we do raise End_Error if we are at the end of file now
1691 -- Now we can just rumble along to the next page mark, or to the
1692 -- end of file, if that comes first. The latter case happens when
1693 -- the page mark is implied at the end of file.
1697 or else (ch = PM and then File.Is_Regular_File);
1701 File.Page := File.Page + 1;
1704 File.Before_Wide_Character := False;
1707 procedure Skip_Page is
1709 Skip_Page (Current_In);
1712 --------------------
1713 -- Standard_Error --
1714 --------------------
1716 function Standard_Error return File_Type is
1718 return Standard_Err;
1721 function Standard_Error return File_Access is
1723 return Standard_Err'Access;
1726 --------------------
1727 -- Standard_Input --
1728 --------------------
1730 function Standard_Input return File_Type is
1735 function Standard_Input return File_Access is
1737 return Standard_In'Access;
1740 ---------------------
1741 -- Standard_Output --
1742 ---------------------
1744 function Standard_Output return File_Type is
1746 return Standard_Out;
1747 end Standard_Output;
1749 function Standard_Output return File_Access is
1751 return Standard_Out'Access;
1752 end Standard_Output;
1754 --------------------
1755 -- Terminate_Line --
1756 --------------------
1758 procedure Terminate_Line (File : File_Type) is
1760 FIO.Check_File_Open (AP (File));
1762 -- For file other than In_File, test for needing to terminate last line
1764 if Mode (File) /= In_File then
1766 -- If not at start of line definition need new line
1768 if File.Col /= 1 then
1771 -- For files other than standard error and standard output, we
1772 -- make sure that an empty file has a single line feed, so that
1773 -- it is properly formatted. We avoid this for the standard files
1774 -- because it is too much of a nuisance to have these odd line
1775 -- feeds when nothing has been written to the file.
1777 elsif (File /= Standard_Err and then File /= Standard_Out)
1778 and then (File.Line = 1 and then File.Page = 1)
1789 procedure Ungetc (ch : int; File : File_Type) is
1792 if ungetc (ch, File.Stream) = EOF then
1802 -- This is the primitive Stream Write routine, used when a Text_IO file
1803 -- is treated directly as a stream using Text_IO.Streams.Stream.
1806 (File : in out Wide_Text_AFCB;
1807 Item : Stream_Element_Array)
1809 pragma Warnings (Off, File);
1810 -- Because in this implementation we don't need IN OUT, we only read
1812 Siz : constant size_t := Item'Length;
1815 if File.Mode = FCB.In_File then
1819 -- Now we do the write. Since this is a text file, it is normally in
1820 -- text mode, but stream data must be written in binary mode, so we
1821 -- temporarily set binary mode for the write, resetting it after.
1822 -- These calls have no effect in a system (like Unix) where there is
1823 -- no distinction between text and binary files.
1825 set_binary_mode (fileno (File.Stream));
1827 if fwrite (Item'Address, 1, Siz, File.Stream) /= Siz then
1831 set_text_mode (fileno (File.Stream));
1834 -- Use "preallocated" strings to avoid calling "new" during the
1835 -- elaboration of the run time. This is needed in the tasking case to
1836 -- avoid calling Task_Lock too early. A filename is expected to end with
1837 -- a null character in the runtime, here the null characters are added
1838 -- just to have a correct filename length.
1840 Err_Name : aliased String := "*stderr" & ASCII.Nul;
1841 In_Name : aliased String := "*stdin" & ASCII.Nul;
1842 Out_Name : aliased String := "*stdout" & ASCII.Nul;
1845 -------------------------------
1846 -- Initialize Standard Files --
1847 -------------------------------
1849 for J in WC_Encoding_Method loop
1850 if WC_Encoding = WC_Encoding_Letters (J) then
1855 -- Note: the names in these files are bogus, and probably it would be
1856 -- better for these files to have no names, but the ACVC test insist!
1857 -- We use names that are bound to fail in open etc.
1859 Standard_Err.Stream := stderr;
1860 Standard_Err.Name := Err_Name'Access;
1861 Standard_Err.Form := Null_Str'Unrestricted_Access;
1862 Standard_Err.Mode := FCB.Out_File;
1863 Standard_Err.Is_Regular_File := is_regular_file (fileno (stderr)) /= 0;
1864 Standard_Err.Is_Temporary_File := False;
1865 Standard_Err.Is_System_File := True;
1866 Standard_Err.Is_Text_File := True;
1867 Standard_Err.Access_Method := 'T';
1868 Standard_Err.WC_Method := Default_WCEM;
1870 Standard_In.Stream := stdin;
1871 Standard_In.Name := In_Name'Access;
1872 Standard_In.Form := Null_Str'Unrestricted_Access;
1873 Standard_In.Mode := FCB.In_File;
1874 Standard_In.Is_Regular_File := is_regular_file (fileno (stdin)) /= 0;
1875 Standard_In.Is_Temporary_File := False;
1876 Standard_In.Is_System_File := True;
1877 Standard_In.Is_Text_File := True;
1878 Standard_In.Access_Method := 'T';
1879 Standard_In.WC_Method := Default_WCEM;
1881 Standard_Out.Stream := stdout;
1882 Standard_Out.Name := Out_Name'Access;
1883 Standard_Out.Form := Null_Str'Unrestricted_Access;
1884 Standard_Out.Mode := FCB.Out_File;
1885 Standard_Out.Is_Regular_File := is_regular_file (fileno (stdout)) /= 0;
1886 Standard_Out.Is_Temporary_File := False;
1887 Standard_Out.Is_System_File := True;
1888 Standard_Out.Is_Text_File := True;
1889 Standard_Out.Access_Method := 'T';
1890 Standard_Out.WC_Method := Default_WCEM;
1892 FIO.Chain_File (AP (Standard_In));
1893 FIO.Chain_File (AP (Standard_Out));
1894 FIO.Chain_File (AP (Standard_Err));
1896 FIO.Make_Unbuffered (AP (Standard_Out));
1897 FIO.Make_Unbuffered (AP (Standard_Err));
1899 end Ada.Wide_Text_IO;