1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . T E X T _ I O --
9 -- Copyright (C) 1992-2009, 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 3, 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. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with Ada.Streams; use Ada.Streams;
33 with Interfaces.C_Streams; use Interfaces.C_Streams;
37 with System.WCh_Cnv; use System.WCh_Cnv;
38 with System.WCh_Con; use System.WCh_Con;
40 with Ada.Unchecked_Conversion;
41 with Ada.Unchecked_Deallocation;
43 pragma Elaborate_All (System.File_IO);
44 -- Needed because of calls to Chain_File in package body elaboration
46 package body Ada.Text_IO is
48 package FIO renames System.File_IO;
50 subtype AP is FCB.AFCB_Ptr;
52 function To_FCB is new Ada.Unchecked_Conversion (File_Mode, FCB.File_Mode);
53 function To_TIO is new Ada.Unchecked_Conversion (FCB.File_Mode, File_Mode);
54 use type FCB.File_Mode;
56 use type System.CRTL.size_t;
58 WC_Encoding : Character;
59 pragma Import (C, WC_Encoding, "__gl_wc_encoding");
60 -- Default wide character encoding
62 Err_Name : aliased String := "*stderr" & ASCII.NUL;
63 In_Name : aliased String := "*stdin" & ASCII.NUL;
64 Out_Name : aliased String := "*stdout" & ASCII.NUL;
65 -- Names of standard files
67 -- Use "preallocated" strings to avoid calling "new" during the elaboration
68 -- of the run time. This is needed in the tasking case to avoid calling
69 -- Task_Lock too early. A filename is expected to end with a null character
70 -- in the runtime, here the null characters are added just to have a
71 -- correct filename length.
73 -- Note: the names for these files are bogus, and probably it would be
74 -- better for these files to have no names, but the ACVC tests insist!
75 -- We use names that are bound to fail in open etc.
77 Null_Str : aliased constant String := "";
78 -- Used as form string for standard files
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
84 function Get_Upper_Half_Char
86 File : File_Type) return Character;
87 -- This function is shared by Get and Get_Immediate to extract an encoded
88 -- upper half character value from the given File. The first byte has
89 -- already been read and is passed in C. The character value is returned as
90 -- the result, and the file pointer is bumped past the character.
91 -- Constraint_Error is raised if the encoded value is outside the bounds of
94 function Get_Upper_Half_Char_Immed
96 File : File_Type) return Character;
97 -- This routine is identical to Get_Upper_Half_Char, except that the reads
98 -- are done in Get_Immediate mode (i.e. without waiting for a line return).
100 function Getc (File : File_Type) return int;
101 -- Gets next character from file, which has already been checked for being
102 -- in read status, and returns the character read if no error occurs. The
103 -- result is EOF if the end of file was read.
105 function Getc_Immed (File : File_Type) return int;
106 -- This routine is identical to Getc, except that the read is done in
107 -- Get_Immediate mode (i.e. without waiting for a line return).
109 function Has_Upper_Half_Character (Item : String) return Boolean;
110 -- Returns True if any of the characters is in the range 16#80#-16#FF#
112 function Nextc (File : File_Type) return int;
113 -- Returns next character from file without skipping past it (i.e. it is a
114 -- combination of Getc followed by an Ungetc).
116 procedure Put_Encoded (File : File_Type; Char : Character);
117 -- Called to output a character Char to the given File, when the encoding
118 -- method for the file is other than brackets, and Char is upper half.
120 procedure Putc (ch : int; File : File_Type);
121 -- Outputs the given character to the file, which has already been checked
122 -- for being in output status. Device_Error is raised if the character
123 -- cannot be written.
125 procedure Set_WCEM (File : in out File_Type);
126 -- Called by Open and Create to set the wide character encoding method for
127 -- the file, processing a WCEM form parameter if one is present. File is
128 -- IN OUT because it may be closed in case of an error.
130 procedure Terminate_Line (File : File_Type);
131 -- If the file is in Write_File or Append_File mode, and the current line
132 -- is not terminated, then a line terminator is written using New_Line.
133 -- Note that there is no Terminate_Page routine, because the page mark at
134 -- the end of the file is implied if necessary.
136 procedure Ungetc (ch : int; File : File_Type);
137 -- Pushes back character into stream, using ungetc. The caller has checked
138 -- that the file is in read status. Device_Error is raised if the character
139 -- cannot be pushed back. An attempt to push back and end of file character
146 function AFCB_Allocate (Control_Block : Text_AFCB) return FCB.AFCB_Ptr is
147 pragma Unreferenced (Control_Block);
149 return new Text_AFCB;
156 procedure AFCB_Close (File : not null access Text_AFCB) is
158 -- If the file being closed is one of the current files, then close
159 -- the corresponding current file. It is not clear that this action
160 -- is required (RM A.10.3(23)) but it seems reasonable, and besides
161 -- ACVC test CE3208A expects this behavior.
163 if File_Type (File) = Current_In then
165 elsif File_Type (File) = Current_Out then
167 elsif File_Type (File) = Current_Err then
171 Terminate_Line (File_Type (File));
178 procedure AFCB_Free (File : not null access Text_AFCB) is
179 type FCB_Ptr is access all Text_AFCB;
180 FT : FCB_Ptr := FCB_Ptr (File);
182 procedure Free is new Ada.Unchecked_Deallocation (Text_AFCB, FCB_Ptr);
192 procedure Close (File : in out File_Type) is
194 FIO.Close (AP (File)'Unrestricted_Access);
201 -- Note: we assume that it is impossible in practice for the column
202 -- to exceed the value of Count'Last, i.e. no check is required for
203 -- overflow raising layout error.
205 function Col (File : File_Type) return Positive_Count is
207 FIO.Check_File_Open (AP (File));
211 function Col return Positive_Count is
213 return Col (Current_Out);
221 (File : in out File_Type;
222 Mode : File_Mode := Out_File;
226 Dummy_File_Control_Block : Text_AFCB;
227 pragma Warnings (Off, Dummy_File_Control_Block);
228 -- Yes, we know this is never assigned a value, only the tag
229 -- is used for dispatching purposes, so that's expected.
232 FIO.Open (File_Ptr => AP (File),
233 Dummy_FCB => Dummy_File_Control_Block,
234 Mode => To_FCB (Mode),
249 function Current_Error return File_Type is
254 function Current_Error return File_Access is
256 return Current_Err.Self'Access;
263 function Current_Input return File_Type is
268 function Current_Input return File_Access is
270 return Current_In.Self'Access;
277 function Current_Output return File_Type is
282 function Current_Output return File_Access is
284 return Current_Out.Self'Access;
291 procedure Delete (File : in out File_Type) is
293 FIO.Delete (AP (File)'Unrestricted_Access);
300 function End_Of_File (File : File_Type) return Boolean is
304 FIO.Check_Read_Status (AP (File));
306 if File.Before_Upper_Half_Character then
309 elsif File.Before_LM then
310 if File.Before_LM_PM then
311 return Nextc (File) = EOF;
325 File.Before_LM := True;
329 -- Here we are just past the line mark with Before_LM set so that we
330 -- do not have to try to back up past the LM, thus avoiding the need
331 -- to back up more than one character.
338 elsif ch = PM and then File.Is_Regular_File then
339 File.Before_LM_PM := True;
340 return Nextc (File) = EOF;
342 -- Here if neither EOF nor PM followed end of line
351 function End_Of_File return Boolean is
353 return End_Of_File (Current_In);
360 function End_Of_Line (File : File_Type) return Boolean is
364 FIO.Check_Read_Status (AP (File));
366 if File.Before_Upper_Half_Character then
369 elsif File.Before_LM then
385 function End_Of_Line return Boolean is
387 return End_Of_Line (Current_In);
394 function End_Of_Page (File : File_Type) return Boolean is
398 FIO.Check_Read_Status (AP (File));
400 if not File.Is_Regular_File then
403 elsif File.Before_Upper_Half_Character then
406 elsif File.Before_LM then
407 if File.Before_LM_PM then
422 File.Before_LM := True;
426 -- Here we are just past the line mark with Before_LM set so that we
427 -- do not have to try to back up past the LM, thus avoiding the need
428 -- to back up more than one character.
432 return ch = PM or else ch = EOF;
435 function End_Of_Page return Boolean is
437 return End_Of_Page (Current_In);
444 procedure Flush (File : File_Type) is
446 FIO.Flush (AP (File));
458 function Form (File : File_Type) return String is
460 return FIO.Form (AP (File));
469 Item : out Character)
474 FIO.Check_Read_Status (AP (File));
476 if File.Before_Upper_Half_Character then
477 File.Before_Upper_Half_Character := False;
478 Item := File.Saved_Upper_Half_Character;
480 elsif File.Before_LM then
481 File.Before_LM := False;
484 if File.Before_LM_PM then
486 File.Page := File.Page + 1;
487 File.Before_LM_PM := False;
489 File.Line := File.Line + 1;
500 File.Line := File.Line + 1;
503 elsif ch = PM and then File.Is_Regular_File then
504 File.Page := File.Page + 1;
508 Item := Character'Val (ch);
509 File.Col := File.Col + 1;
515 procedure Get (Item : out Character) is
517 Get (Current_In, Item);
528 FIO.Check_Read_Status (AP (File));
530 if File.Before_LM then
531 File.Before_LM := False;
532 File.Before_LM_PM := False;
535 if File.Before_LM_PM then
537 File.Page := File.Page + 1;
538 File.Before_LM_PM := False;
541 File.Line := File.Line + 1;
546 while J <= Item'Last loop
553 File.Line := File.Line + 1;
556 elsif ch = PM and then File.Is_Regular_File then
557 File.Page := File.Page + 1;
561 Item (J) := Character'Val (ch);
563 File.Col := File.Col + 1;
568 procedure Get (Item : out String) is
570 Get (Current_In, Item);
577 procedure Get_Immediate
579 Item : out Character)
584 FIO.Check_Read_Status (AP (File));
586 if File.Before_Upper_Half_Character then
587 File.Before_Upper_Half_Character := False;
588 Item := File.Saved_Upper_Half_Character;
590 elsif File.Before_LM then
591 File.Before_LM := False;
592 File.Before_LM_PM := False;
593 Item := Character'Val (LM);
596 ch := Getc_Immed (File);
602 (if not Is_Start_Of_Encoding (Character'Val (ch), File.WC_Method)
603 then Character'Val (ch)
604 else Get_Upper_Half_Char_Immed (Character'Val (ch), File));
609 procedure Get_Immediate
610 (Item : out Character)
613 Get_Immediate (Current_In, Item);
616 procedure Get_Immediate
618 Item : out Character;
619 Available : out Boolean)
625 procedure getc_immediate_nowait
628 end_of_file : out int;
630 pragma Import (C, getc_immediate_nowait, "getc_immediate_nowait");
633 FIO.Check_Read_Status (AP (File));
636 if File.Before_Upper_Half_Character then
637 File.Before_Upper_Half_Character := False;
638 Item := File.Saved_Upper_Half_Character;
640 elsif File.Before_LM then
641 File.Before_LM := False;
642 File.Before_LM_PM := False;
643 Item := Character'Val (LM);
646 getc_immediate_nowait (File.Stream, ch, end_of_file, avail);
648 if ferror (File.Stream) /= 0 then
651 elsif end_of_file /= 0 then
662 (if Is_Start_Of_Encoding (Character'Val (ch), File.WC_Method)
663 then Character'Val (ch)
664 else Get_Upper_Half_Char_Immed (Character'Val (ch), File));
670 procedure Get_Immediate
671 (Item : out Character;
672 Available : out Boolean)
675 Get_Immediate (Current_In, Item, Available);
690 FIO.Check_Read_Status (AP (File));
691 Last := Item'First - 1;
693 -- Immediate exit for null string, this is a case in which we do not
694 -- need to test for end of file and we do not skip a line mark under
695 -- any circumstances.
697 if Last >= Item'Last then
701 -- Here we have at least one character, if we are immediately before
702 -- a line mark, then we will just skip past it storing no characters.
704 if File.Before_LM then
705 File.Before_LM := False;
706 File.Before_LM_PM := False;
708 -- Otherwise we need to read some characters
713 -- If we are at the end of file now, it means we are trying to
714 -- skip a file terminator and we raise End_Error (RM A.10.7(20))
720 -- Loop through characters. Don't bother if we hit a page mark,
721 -- since in normal files, page marks can only follow line marks
722 -- in any case and we only promise to treat the page nonsense
723 -- correctly in the absense of such rogue page marks.
726 -- Exit the loop if read is terminated by encountering line mark
730 -- Otherwise store the character, note that we know that ch is
731 -- something other than LM or EOF. It could possibly be a page
732 -- mark if there is a stray page mark in the middle of a line,
733 -- but this is not an official page mark in any case, since
734 -- official page marks can only follow a line mark. The whole
735 -- page business is pretty much nonsense anyway, so we do not
736 -- want to waste time trying to make sense out of non-standard
737 -- page marks in the file! This means that the behavior of
738 -- Get_Line is different from repeated Get of a character, but
739 -- that's too bad. We only promise that page numbers etc make
740 -- sense if the file is formatted in a standard manner.
742 -- Note: we do not adjust the column number because it is quicker
743 -- to adjust it once at the end of the operation than incrementing
744 -- it each time around the loop.
747 Item (Last) := Character'Val (ch);
749 -- All done if the string is full, this is the case in which
750 -- we do not skip the following line mark. We need to adjust
751 -- the column number in this case.
753 if Last = Item'Last then
754 File.Col := File.Col + Count (Item'Length);
758 -- Otherwise read next character. We also exit from the loop if
759 -- we read an end of file. This is the case where the last line
760 -- is not terminated with a line mark, and we consider that there
761 -- is an implied line mark in this case (this is a non-standard
762 -- file, but it is nice to treat it reasonably).
769 -- We have skipped past, but not stored, a line mark. Skip following
770 -- page mark if one follows, but do not do this for a non-regular
771 -- file (since otherwise we get annoying wait for an extra character)
773 File.Line := File.Line + 1;
776 if File.Before_LM_PM then
778 File.Before_LM_PM := False;
779 File.Page := File.Page + 1;
781 elsif File.Is_Regular_File then
784 if ch = PM and then File.Is_Regular_File then
786 File.Page := File.Page + 1;
798 Get_Line (Current_In, Item, Last);
801 function Get_Line (File : File_Type) return String is
802 Buffer : String (1 .. 500);
805 function Get_Rest (S : String) return String;
806 -- This is a recursive function that reads the rest of the line and
807 -- returns it. S is the part read so far.
813 function Get_Rest (S : String) return String is
815 -- Each time we allocate a buffer the same size as what we have
816 -- read so far. This limits us to a logarithmic number of calls
817 -- to Get_Rest and also ensures only a linear use of stack space.
819 Buffer : String (1 .. S'Length);
823 Get_Line (File, Buffer, Last);
826 R : constant String := S & Buffer (1 .. Last);
828 if Last < Buffer'Last then
836 -- Start of processing for Get_Line
839 Get_Line (File, Buffer, Last);
841 if Last < Buffer'Last then
842 return Buffer (1 .. Last);
844 return Get_Rest (Buffer (1 .. Last));
848 function Get_Line return String is
850 return Get_Line (Current_In);
853 -------------------------
854 -- Get_Upper_Half_Char --
855 -------------------------
857 function Get_Upper_Half_Char
859 File : File_Type) return Character
861 Result : Wide_Character;
863 function In_Char return Character;
864 -- Function used to obtain additional characters it the wide character
865 -- sequence is more than one character long.
867 function WC_In is new Char_Sequence_To_Wide_Char (In_Char);
873 function In_Char return Character is
874 ch : constant Integer := Getc (File);
879 return Character'Val (ch);
883 -- Start of processing for Get_Upper_Half_Char
886 Result := WC_In (C, File.WC_Method);
888 if Wide_Character'Pos (Result) > 16#FF# then
889 raise Constraint_Error with
890 "invalid wide character in Text_'I'O input";
892 return Character'Val (Wide_Character'Pos (Result));
894 end Get_Upper_Half_Char;
896 -------------------------------
897 -- Get_Upper_Half_Char_Immed --
898 -------------------------------
900 function Get_Upper_Half_Char_Immed
902 File : File_Type) return Character
904 Result : Wide_Character;
906 function In_Char return Character;
907 -- Function used to obtain additional characters it the wide character
908 -- sequence is more than one character long.
910 function WC_In is new Char_Sequence_To_Wide_Char (In_Char);
916 function In_Char return Character is
917 ch : constant Integer := Getc_Immed (File);
922 return Character'Val (ch);
926 -- Start of processing for Get_Upper_Half_Char_Immed
929 Result := WC_In (C, File.WC_Method);
931 if Wide_Character'Pos (Result) > 16#FF# then
932 raise Constraint_Error with
933 "invalid wide character in Text_'I'O input";
935 return Character'Val (Wide_Character'Pos (Result));
937 end Get_Upper_Half_Char_Immed;
943 function Getc (File : File_Type) return int is
947 ch := fgetc (File.Stream);
949 if ch = EOF and then ferror (File.Stream) /= 0 then
960 function Getc_Immed (File : File_Type) return int is
964 procedure getc_immediate
965 (stream : FILEs; ch : out int; end_of_file : out int);
966 pragma Import (C, getc_immediate, "getc_immediate");
969 FIO.Check_Read_Status (AP (File));
971 if File.Before_LM then
972 File.Before_LM := False;
973 File.Before_LM_PM := False;
977 getc_immediate (File.Stream, ch, end_of_file);
979 if ferror (File.Stream) /= 0 then
981 elsif end_of_file /= 0 then
989 ------------------------------
990 -- Has_Upper_Half_Character --
991 ------------------------------
993 function Has_Upper_Half_Character (Item : String) return Boolean is
995 for J in Item'Range loop
996 if Character'Pos (Item (J)) >= 16#80# then
1002 end Has_Upper_Half_Character;
1004 -------------------------------
1005 -- Initialize_Standard_Files --
1006 -------------------------------
1008 procedure Initialize_Standard_Files is
1010 Standard_Err.Stream := stderr;
1011 Standard_Err.Name := Err_Name'Access;
1012 Standard_Err.Form := Null_Str'Unrestricted_Access;
1013 Standard_Err.Mode := FCB.Out_File;
1014 Standard_Err.Is_Regular_File := is_regular_file (fileno (stderr)) /= 0;
1015 Standard_Err.Is_Temporary_File := False;
1016 Standard_Err.Is_System_File := True;
1017 Standard_Err.Is_Text_File := True;
1018 Standard_Err.Access_Method := 'T';
1019 Standard_Err.Self := Standard_Err;
1020 Standard_Err.WC_Method := Default_WCEM;
1022 Standard_In.Stream := stdin;
1023 Standard_In.Name := In_Name'Access;
1024 Standard_In.Form := Null_Str'Unrestricted_Access;
1025 Standard_In.Mode := FCB.In_File;
1026 Standard_In.Is_Regular_File := is_regular_file (fileno (stdin)) /= 0;
1027 Standard_In.Is_Temporary_File := False;
1028 Standard_In.Is_System_File := True;
1029 Standard_In.Is_Text_File := True;
1030 Standard_In.Access_Method := 'T';
1031 Standard_In.Self := Standard_In;
1032 Standard_In.WC_Method := Default_WCEM;
1034 Standard_Out.Stream := stdout;
1035 Standard_Out.Name := Out_Name'Access;
1036 Standard_Out.Form := Null_Str'Unrestricted_Access;
1037 Standard_Out.Mode := FCB.Out_File;
1038 Standard_Out.Is_Regular_File := is_regular_file (fileno (stdout)) /= 0;
1039 Standard_Out.Is_Temporary_File := False;
1040 Standard_Out.Is_System_File := True;
1041 Standard_Out.Is_Text_File := True;
1042 Standard_Out.Access_Method := 'T';
1043 Standard_Out.Self := Standard_Out;
1044 Standard_Out.WC_Method := Default_WCEM;
1046 FIO.Make_Unbuffered (AP (Standard_Out));
1047 FIO.Make_Unbuffered (AP (Standard_Err));
1048 end Initialize_Standard_Files;
1054 function Is_Open (File : File_Type) return Boolean is
1056 return FIO.Is_Open (AP (File));
1063 -- Note: we assume that it is impossible in practice for the line
1064 -- to exceed the value of Count'Last, i.e. no check is required for
1065 -- overflow raising layout error.
1067 function Line (File : File_Type) return Positive_Count is
1069 FIO.Check_File_Open (AP (File));
1073 function Line return Positive_Count is
1075 return Line (Current_Out);
1082 function Line_Length (File : File_Type) return Count is
1084 FIO.Check_Write_Status (AP (File));
1085 return File.Line_Length;
1088 function Line_Length return Count is
1090 return Line_Length (Current_Out);
1097 procedure Look_Ahead
1099 Item : out Character;
1100 End_Of_Line : out Boolean)
1105 FIO.Check_Read_Status (AP (File));
1107 -- If we are logically before a line mark, we can return immediately
1109 if File.Before_LM then
1110 End_Of_Line := True;
1113 -- If we are before an upper half character just return it (this can
1114 -- happen if there are two calls to Look_Ahead in a row).
1116 elsif File.Before_Upper_Half_Character then
1117 End_Of_Line := False;
1118 Item := File.Saved_Upper_Half_Character;
1120 -- Otherwise we must read a character from the input stream
1127 or else (ch = PM and then File.Is_Regular_File)
1129 End_Of_Line := True;
1133 -- Case where character obtained does not represent the start of an
1134 -- encoded sequence so it stands for itself and we can unget it with
1137 elsif not Is_Start_Of_Encoding
1138 (Character'Val (ch), File.WC_Method)
1140 End_Of_Line := False;
1142 Item := Character'Val (ch);
1144 -- For the start of an encoding, we read the character using the
1145 -- Get_Upper_Half_Char routine. It will occupy more than one byte
1146 -- so we can't put it back with ungetc. Instead we save it in the
1147 -- control block, setting a flag that everyone interested in reading
1148 -- characters must test before reading the stream.
1151 Item := Get_Upper_Half_Char (Character'Val (ch), File);
1152 End_Of_Line := False;
1153 File.Saved_Upper_Half_Character := Item;
1154 File.Before_Upper_Half_Character := True;
1159 procedure Look_Ahead
1160 (Item : out Character;
1161 End_Of_Line : out Boolean)
1164 Look_Ahead (Current_In, Item, End_Of_Line);
1171 function Mode (File : File_Type) return File_Mode is
1173 return To_TIO (FIO.Mode (AP (File)));
1180 function Name (File : File_Type) return String is
1182 return FIO.Name (AP (File));
1191 Spacing : Positive_Count := 1)
1194 -- Raise Constraint_Error if out of range value. The reason for this
1195 -- explicit test is that we don't want junk values around, even if
1196 -- checks are off in the caller.
1198 if not Spacing'Valid then
1199 raise Constraint_Error;
1202 FIO.Check_Write_Status (AP (File));
1204 for K in 1 .. Spacing loop
1206 File.Line := File.Line + 1;
1208 if File.Page_Length /= 0
1209 and then File.Line > File.Page_Length
1213 File.Page := File.Page + 1;
1220 procedure New_Line (Spacing : Positive_Count := 1) is
1222 New_Line (Current_Out, Spacing);
1229 procedure New_Page (File : File_Type) is
1231 FIO.Check_Write_Status (AP (File));
1233 if File.Col /= 1 or else File.Line = 1 then
1238 File.Page := File.Page + 1;
1243 procedure New_Page is
1245 New_Page (Current_Out);
1252 function Nextc (File : File_Type) return int is
1256 ch := fgetc (File.Stream);
1259 if ferror (File.Stream) /= 0 then
1264 if ungetc (ch, File.Stream) = EOF then
1277 (File : in out File_Type;
1280 Form : String := "")
1282 Dummy_File_Control_Block : Text_AFCB;
1283 pragma Warnings (Off, Dummy_File_Control_Block);
1284 -- Yes, we know this is never assigned a value, only the tag
1285 -- is used for dispatching purposes, so that's expected.
1288 FIO.Open (File_Ptr => AP (File),
1289 Dummy_FCB => Dummy_File_Control_Block,
1290 Mode => To_FCB (Mode),
1305 -- Note: we assume that it is impossible in practice for the page
1306 -- to exceed the value of Count'Last, i.e. no check is required for
1307 -- overflow raising layout error.
1309 function Page (File : File_Type) return Positive_Count is
1311 FIO.Check_File_Open (AP (File));
1315 function Page return Positive_Count is
1317 return Page (Current_Out);
1324 function Page_Length (File : File_Type) return Count is
1326 FIO.Check_Write_Status (AP (File));
1327 return File.Page_Length;
1330 function Page_Length return Count is
1332 return Page_Length (Current_Out);
1344 FIO.Check_Write_Status (AP (File));
1346 if File.Line_Length /= 0 and then File.Col > File.Line_Length then
1350 -- If lower half character, or brackets encoding, output directly
1352 if Character'Pos (Item) < 16#80#
1353 or else File.WC_Method = WCEM_Brackets
1355 if fputc (Character'Pos (Item), File.Stream) = EOF then
1359 -- Case of upper half character with non-brackets encoding
1362 Put_Encoded (File, Item);
1365 File.Col := File.Col + 1;
1368 procedure Put (Item : Character) is
1370 FIO.Check_Write_Status (AP (Current_Out));
1372 if Current_Out.Line_Length /= 0
1373 and then Current_Out.Col > Current_Out.Line_Length
1375 New_Line (Current_Out);
1378 -- If lower half character, or brackets encoding, output directly
1380 if Character'Pos (Item) < 16#80#
1381 or else Default_WCEM = WCEM_Brackets
1383 if fputc (Character'Pos (Item), Current_Out.Stream) = EOF then
1387 -- Case of upper half character with non-brackets encoding
1390 Put_Encoded (Current_Out, Item);
1393 Current_Out.Col := Current_Out.Col + 1;
1405 FIO.Check_Write_Status (AP (File));
1407 -- Only have something to do if string is non-null
1409 if Item'Length > 0 then
1411 -- If we have bounded lines, or if the file encoding is other than
1412 -- Brackets and the string has at least one upper half character,
1413 -- then output the string character by character.
1415 if File.Line_Length /= 0
1416 or else (File.WC_Method /= WCEM_Brackets
1417 and then Has_Upper_Half_Character (Item))
1419 for J in Item'Range loop
1420 Put (File, Item (J));
1423 -- Otherwise we can output the entire string at once. Note that if
1424 -- there are LF or FF characters in the string, we do not bother to
1425 -- count them as line or page terminators.
1428 FIO.Write_Buf (AP (File), Item'Address, Item'Length);
1429 File.Col := File.Col + Item'Length;
1434 procedure Put (Item : String) is
1436 Put (Current_Out, Item);
1443 procedure Put_Encoded (File : File_Type; Char : Character) is
1444 procedure Out_Char (C : Character);
1445 -- Procedure to output one character of an upper half encoded sequence
1447 procedure WC_Out is new Wide_Char_To_Char_Sequence (Out_Char);
1453 procedure Out_Char (C : Character) is
1455 Putc (Character'Pos (C), File);
1458 -- Start of processing for Put_Encoded
1461 WC_Out (Wide_Character'Val (Character'Pos (Char)), File.WC_Method);
1472 Ilen : Natural := Item'Length;
1473 Istart : Natural := Item'First;
1476 FIO.Check_Write_Status (AP (File));
1478 -- If we have bounded lines, or if the file encoding is other than
1479 -- Brackets and the string has at least one upper half character, then
1480 -- output the string character by character.
1482 if File.Line_Length /= 0
1483 or else (File.WC_Method /= WCEM_Brackets
1484 and then Has_Upper_Half_Character (Item))
1486 for J in Item'Range loop
1487 Put (File, Item (J));
1494 -- Normal case where we do not need to output character by character
1496 -- We setup a single string that has the necessary terminators and
1497 -- then write it with a single call. The reason for doing this is
1498 -- that it gives better behavior for the use of Put_Line in multi-
1499 -- tasking programs, since often the OS will treat the entire put
1500 -- operation as an atomic operation.
1502 -- We only do this if the message is 512 characters or less in length,
1503 -- since otherwise Put_Line would use an unbounded amount of stack
1504 -- space and could cause undetected stack overflow. If we have a
1505 -- longer string, then output the first part separately to avoid this.
1508 FIO.Write_Buf (AP (File), Item'Address, size_t (Ilen - 512));
1509 Istart := Istart + Ilen - 512;
1513 -- Now prepare the string with its terminator
1516 Buffer : String (1 .. Ilen + 2);
1520 Buffer (1 .. Ilen) := Item (Istart .. Item'Last);
1521 Buffer (Ilen + 1) := Character'Val (LM);
1523 if File.Page_Length /= 0
1524 and then File.Line > File.Page_Length
1526 Buffer (Ilen + 2) := Character'Val (PM);
1527 Plen := size_t (Ilen) + 2;
1529 File.Page := File.Page + 1;
1532 Plen := size_t (Ilen) + 1;
1533 File.Line := File.Line + 1;
1536 FIO.Write_Buf (AP (File), Buffer'Address, Plen);
1542 procedure Put_Line (Item : String) is
1544 Put_Line (Current_Out, Item);
1551 procedure Putc (ch : int; File : File_Type) is
1553 if fputc (ch, File.Stream) = EOF then
1562 -- This is the primitive Stream Read routine, used when a Text_IO file
1563 -- is treated directly as a stream using Text_IO.Streams.Stream.
1566 (File : in out Text_AFCB;
1567 Item : out Stream_Element_Array;
1568 Last : out Stream_Element_Offset)
1571 pragma Warnings (Off, Discard_ch);
1574 -- Need to deal with Before_Upper_Half_Character ???
1576 if File.Mode /= FCB.In_File then
1580 -- Deal with case where our logical and physical position do not match
1581 -- because of being after an LM or LM-PM sequence when in fact we are
1582 -- logically positioned before it.
1584 if File.Before_LM then
1586 -- If we are before a PM, then it is possible for a stream read
1587 -- to leave us after the LM and before the PM, which is a bit
1588 -- odd. The easiest way to deal with this is to unget the PM,
1589 -- so we are indeed positioned between the characters. This way
1590 -- further stream read operations will work correctly, and the
1591 -- effect on text processing is a little weird, but what can
1592 -- be expected if stream and text input are mixed this way?
1594 if File.Before_LM_PM then
1595 Discard_ch := ungetc (PM, File.Stream);
1596 File.Before_LM_PM := False;
1599 File.Before_LM := False;
1601 Item (Item'First) := Stream_Element (Character'Pos (ASCII.LF));
1603 if Item'Length = 1 then
1609 Stream_Element_Offset
1610 (fread (buffer => Item'Address,
1611 index => size_t (Item'First + 1),
1613 count => Item'Length - 1,
1614 stream => File.Stream));
1620 -- Now we do the read. Since this is a text file, it is normally in
1621 -- text mode, but stream data must be read in binary mode, so we
1622 -- temporarily set binary mode for the read, resetting it after.
1623 -- These calls have no effect in a system (like Unix) where there is
1624 -- no distinction between text and binary files.
1626 set_binary_mode (fileno (File.Stream));
1630 Stream_Element_Offset
1631 (fread (Item'Address, 1, Item'Length, File.Stream)) - 1;
1633 if Last < Item'Last then
1634 if ferror (File.Stream) /= 0 then
1639 set_text_mode (fileno (File.Stream));
1647 (File : in out File_Type;
1651 -- Don't allow change of mode for current file (RM A.10.2(5))
1653 if (File = Current_In or else
1654 File = Current_Out or else
1655 File = Current_Error)
1656 and then To_FCB (Mode) /= File.Mode
1661 Terminate_Line (File);
1662 FIO.Reset (AP (File)'Unrestricted_Access, To_FCB (Mode));
1666 File.Line_Length := 0;
1667 File.Page_Length := 0;
1668 File.Before_LM := False;
1669 File.Before_LM_PM := False;
1672 procedure Reset (File : in out File_Type) is
1674 Terminate_Line (File);
1675 FIO.Reset (AP (File)'Unrestricted_Access);
1679 File.Line_Length := 0;
1680 File.Page_Length := 0;
1681 File.Before_LM := False;
1682 File.Before_LM_PM := False;
1691 To : Positive_Count)
1696 -- Raise Constraint_Error if out of range value. The reason for this
1697 -- explicit test is that we don't want junk values around, even if
1698 -- checks are off in the caller.
1700 if not To'Valid then
1701 raise Constraint_Error;
1704 FIO.Check_File_Open (AP (File));
1708 if Mode (File) >= Out_File then
1710 -- Error if we attempt to set Col to a value greater than the
1711 -- maximum permissible line length.
1713 if File.Line_Length /= 0 and then To > File.Line_Length then
1717 -- If we are behind current position, then go to start of new line
1719 if To < File.Col then
1723 -- Loop to output blanks till we are at the required column
1725 while File.Col < To loop
1732 -- If we are logically before a LM, but physically after it, the
1733 -- file position still reflects the position before the LM, so eat
1734 -- it now and adjust the file position appropriately.
1736 if File.Before_LM then
1737 File.Before_LM := False;
1738 File.Before_LM_PM := False;
1739 File.Line := File.Line + 1;
1743 -- Loop reading characters till we get one at the required Col value
1746 -- Read next character. The reason we have to read ahead is to
1747 -- skip formatting characters, the effect of Set_Col is to set
1748 -- us to a real character with the right Col value, and format
1749 -- characters don't count.
1753 -- Error if we hit an end of file
1758 -- If line mark, eat it and adjust file position
1761 File.Line := File.Line + 1;
1764 -- If recognized page mark, eat it, and adjust file position
1766 elsif ch = PM and then File.Is_Regular_File then
1767 File.Page := File.Page + 1;
1771 -- Otherwise this is the character we are looking for, so put it
1772 -- back in the input stream (we have not adjusted the file
1773 -- position yet, so everything is set right after this ungetc).
1775 elsif To = File.Col then
1779 -- Keep skipping characters if we are not there yet, updating the
1780 -- file position past the skipped character.
1783 File.Col := File.Col + 1;
1789 procedure Set_Col (To : Positive_Count) is
1791 Set_Col (Current_Out, To);
1798 procedure Set_Error (File : File_Type) is
1800 FIO.Check_Write_Status (AP (File));
1801 Current_Err := File;
1808 procedure Set_Input (File : File_Type) is
1810 FIO.Check_Read_Status (AP (File));
1820 To : Positive_Count)
1823 -- Raise Constraint_Error if out of range value. The reason for this
1824 -- explicit test is that we don't want junk values around, even if
1825 -- checks are off in the caller.
1827 if not To'Valid then
1828 raise Constraint_Error;
1831 FIO.Check_File_Open (AP (File));
1833 if To = File.Line then
1837 if Mode (File) >= Out_File then
1838 if File.Page_Length /= 0 and then To > File.Page_Length then
1842 if To < File.Line then
1846 while File.Line < To loop
1851 while To /= File.Line loop
1857 procedure Set_Line (To : Positive_Count) is
1859 Set_Line (Current_Out, To);
1862 ---------------------
1863 -- Set_Line_Length --
1864 ---------------------
1866 procedure Set_Line_Length (File : File_Type; To : Count) is
1868 -- Raise Constraint_Error if out of range value. The reason for this
1869 -- explicit test is that we don't want junk values around, even if
1870 -- checks are off in the caller.
1872 if not To'Valid then
1873 raise Constraint_Error;
1876 FIO.Check_Write_Status (AP (File));
1877 File.Line_Length := To;
1878 end Set_Line_Length;
1880 procedure Set_Line_Length (To : Count) is
1882 Set_Line_Length (Current_Out, To);
1883 end Set_Line_Length;
1889 procedure Set_Output (File : File_Type) is
1891 FIO.Check_Write_Status (AP (File));
1892 Current_Out := File;
1895 ---------------------
1896 -- Set_Page_Length --
1897 ---------------------
1899 procedure Set_Page_Length (File : File_Type; To : Count) is
1901 -- Raise Constraint_Error if out of range value. The reason for this
1902 -- explicit test is that we don't want junk values around, even if
1903 -- checks are off in the caller.
1905 if not To'Valid then
1906 raise Constraint_Error;
1909 FIO.Check_Write_Status (AP (File));
1910 File.Page_Length := To;
1911 end Set_Page_Length;
1913 procedure Set_Page_Length (To : Count) is
1915 Set_Page_Length (Current_Out, To);
1916 end Set_Page_Length;
1922 procedure Set_WCEM (File : in out File_Type) is
1927 File.WC_Method := WCEM_Brackets;
1928 FIO.Form_Parameter (File.Form.all, "wcem", Start, Stop);
1931 File.WC_Method := WCEM_Brackets;
1934 if Stop = Start then
1935 for J in WC_Encoding_Letters'Range loop
1936 if File.Form (Start) = WC_Encoding_Letters (J) then
1937 File.WC_Method := J;
1944 raise Use_Error with "invalid WCEM form parameter";
1954 Spacing : Positive_Count := 1)
1959 -- Raise Constraint_Error if out of range value. The reason for this
1960 -- explicit test is that we don't want junk values around, even if
1961 -- checks are off in the caller.
1963 if not Spacing'Valid then
1964 raise Constraint_Error;
1967 FIO.Check_Read_Status (AP (File));
1969 for L in 1 .. Spacing loop
1970 if File.Before_LM then
1971 File.Before_LM := False;
1973 -- Note that if File.Before_LM_PM is currently set, we also have
1974 -- to reset it (because it makes sense for Before_LM_PM to be set
1975 -- only when Before_LM is also set). This is done later on in this
1976 -- subprogram, as soon as Before_LM_PM has been taken into account
1977 -- for the purpose of page and line counts.
1982 -- If at end of file now, then immediately raise End_Error. Note
1983 -- that we can never be positioned between a line mark and a page
1984 -- mark, so if we are at the end of file, we cannot logically be
1985 -- before the implicit page mark that is at the end of the file.
1987 -- For the same reason, we do not need an explicit check for a
1988 -- page mark. If there is a FF in the middle of a line, the file
1989 -- is not in canonical format and we do not care about the page
1990 -- numbers for files other than ones in canonical format.
1996 -- If not at end of file, then loop till we get to an LM or EOF.
1997 -- The latter case happens only in non-canonical files where the
1998 -- last line is not terminated by LM, but we don't want to blow
1999 -- up for such files, so we assume an implicit LM in this case.
2002 exit when ch = LM or else ch = EOF;
2007 -- We have got past a line mark, now, for a regular file only,
2008 -- see if a page mark immediately follows this line mark and
2009 -- if so, skip past the page mark as well. We do not do this
2010 -- for non-regular files, since it would cause an undesirable
2011 -- wait for an additional character.
2014 File.Line := File.Line + 1;
2016 if File.Before_LM_PM then
2017 File.Page := File.Page + 1;
2019 File.Before_LM_PM := False;
2021 elsif File.Is_Regular_File then
2024 -- Page mark can be explicit, or implied at the end of the file
2026 if (ch = PM or else ch = EOF)
2027 and then File.Is_Regular_File
2029 File.Page := File.Page + 1;
2037 File.Before_Upper_Half_Character := False;
2040 procedure Skip_Line (Spacing : Positive_Count := 1) is
2042 Skip_Line (Current_In, Spacing);
2049 procedure Skip_Page (File : File_Type) is
2053 FIO.Check_Read_Status (AP (File));
2055 -- If at page mark already, just skip it
2057 if File.Before_LM_PM then
2058 File.Before_LM := False;
2059 File.Before_LM_PM := False;
2060 File.Page := File.Page + 1;
2066 -- This is a bit tricky, if we are logically before an LM then
2067 -- it is not an error if we are at an end of file now, since we
2068 -- are not really at it.
2070 if File.Before_LM then
2071 File.Before_LM := False;
2072 File.Before_LM_PM := False;
2075 -- Otherwise we do raise End_Error if we are at the end of file now
2085 -- Now we can just rumble along to the next page mark, or to the
2086 -- end of file, if that comes first. The latter case happens when
2087 -- the page mark is implied at the end of file.
2091 or else (ch = PM and then File.Is_Regular_File);
2095 File.Page := File.Page + 1;
2098 File.Before_Upper_Half_Character := False;
2101 procedure Skip_Page is
2103 Skip_Page (Current_In);
2106 --------------------
2107 -- Standard_Error --
2108 --------------------
2110 function Standard_Error return File_Type is
2112 return Standard_Err;
2115 function Standard_Error return File_Access is
2117 return Standard_Err'Access;
2120 --------------------
2121 -- Standard_Input --
2122 --------------------
2124 function Standard_Input return File_Type is
2129 function Standard_Input return File_Access is
2131 return Standard_In'Access;
2134 ---------------------
2135 -- Standard_Output --
2136 ---------------------
2138 function Standard_Output return File_Type is
2140 return Standard_Out;
2141 end Standard_Output;
2143 function Standard_Output return File_Access is
2145 return Standard_Out'Access;
2146 end Standard_Output;
2148 --------------------
2149 -- Terminate_Line --
2150 --------------------
2152 procedure Terminate_Line (File : File_Type) is
2154 FIO.Check_File_Open (AP (File));
2156 -- For file other than In_File, test for needing to terminate last line
2158 if Mode (File) /= In_File then
2160 -- If not at start of line definition need new line
2162 if File.Col /= 1 then
2165 -- For files other than standard error and standard output, we
2166 -- make sure that an empty file has a single line feed, so that
2167 -- it is properly formatted. We avoid this for the standard files
2168 -- because it is too much of a nuisance to have these odd line
2169 -- feeds when nothing has been written to the file.
2171 -- We also avoid this for files opened in append mode, in
2172 -- accordance with (RM A.8.2(10))
2174 elsif (File /= Standard_Err and then File /= Standard_Out)
2175 and then (File.Line = 1 and then File.Page = 1)
2176 and then Mode (File) = Out_File
2187 procedure Ungetc (ch : int; File : File_Type) is
2190 if ungetc (ch, File.Stream) = EOF then
2200 -- This is the primitive Stream Write routine, used when a Text_IO file
2201 -- is treated directly as a stream using Text_IO.Streams.Stream.
2204 (File : in out Text_AFCB;
2205 Item : Stream_Element_Array)
2207 pragma Warnings (Off, File);
2208 -- Because in this implementation we don't need IN OUT, we only read
2210 function Has_Translated_Characters return Boolean;
2211 -- return True if Item array contains a character which will be
2212 -- translated under the text file mode. There is only one such
2213 -- character under DOS based systems which is character 10.
2215 text_translation_required : Boolean;
2216 for text_translation_required'Size use Character'Size;
2217 pragma Import (C, text_translation_required,
2218 "__gnat_text_translation_required");
2220 Siz : constant size_t := Item'Length;
2222 -------------------------------
2223 -- Has_Translated_Characters --
2224 -------------------------------
2226 function Has_Translated_Characters return Boolean is
2228 for K in Item'Range loop
2229 if Item (K) = 10 then
2234 end Has_Translated_Characters;
2236 Needs_Binary_Write : constant Boolean :=
2237 text_translation_required
2238 and then Has_Translated_Characters;
2240 -- Start of processing for Write
2243 if File.Mode = FCB.In_File then
2247 -- Now we do the write. Since this is a text file, it is normally in
2248 -- text mode, but stream data must be written in binary mode, so we
2249 -- temporarily set binary mode for the write, resetting it after. This
2250 -- is done only if needed (i.e. there is some characters in Item which
2251 -- needs to be written using the binary mode).
2252 -- These calls have no effect in a system (like Unix) where there is
2253 -- no distinction between text and binary files.
2255 -- Since the character translation is done at the time the buffer is
2256 -- written (this is true under Windows) we first flush current buffer
2257 -- with text mode if needed.
2259 if Needs_Binary_Write then
2260 if fflush (File.Stream) = -1 then
2264 set_binary_mode (fileno (File.Stream));
2267 if fwrite (Item'Address, 1, Siz, File.Stream) /= Siz then
2271 -- At this point we need to flush the buffer using the binary mode then
2272 -- we reset to text mode.
2274 if Needs_Binary_Write then
2275 if fflush (File.Stream) = -1 then
2279 set_text_mode (fileno (File.Stream));
2284 -- Initialize Standard Files
2286 for J in WC_Encoding_Method loop
2287 if WC_Encoding = WC_Encoding_Letters (J) then
2292 Initialize_Standard_Files;
2294 FIO.Chain_File (AP (Standard_In));
2295 FIO.Chain_File (AP (Standard_Out));
2296 FIO.Chain_File (AP (Standard_Err));