1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . W I D E _ T E X T _ I O --
9 -- Copyright (C) 1992-2014, 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.Wide_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_Wide_Char_Immed
86 File : File_Type) return Wide_Character;
87 -- This routine is identical to Get_Wide_Char, except that the reads are
88 -- done in Get_Immediate mode (i.e. without waiting for a line return).
90 function Getc_Immed (File : File_Type) return int;
91 -- This routine is identical to Getc, except that the read is done in
92 -- Get_Immediate mode (i.e. without waiting for a line return).
94 procedure Putc (ch : int; File : File_Type);
95 -- Outputs the given character to the file, which has already been checked
96 -- for being in output status. Device_Error is raised if the character
99 procedure Set_WCEM (File : in out File_Type);
100 -- Called by Open and Create to set the wide character encoding method for
101 -- the file, processing a WCEM form parameter if one is present. File is
102 -- IN OUT because it may be closed in case of an error.
104 procedure Terminate_Line (File : File_Type);
105 -- If the file is in Write_File or Append_File mode, and the current line
106 -- is not terminated, then a line terminator is written using New_Line.
107 -- Note that there is no Terminate_Page routine, because the page mark at
108 -- the end of the file is implied if necessary.
110 procedure Ungetc (ch : int; File : File_Type);
111 -- Pushes back character into stream, using ungetc. The caller has checked
112 -- that the file is in read status. Device_Error is raised if the character
113 -- cannot be pushed back. An attempt to push back and end of file character
120 function AFCB_Allocate
121 (Control_Block : Wide_Text_AFCB) return FCB.AFCB_Ptr
123 pragma Unreferenced (Control_Block);
125 return new Wide_Text_AFCB;
132 procedure AFCB_Close (File : not null access Wide_Text_AFCB) is
134 -- If the file being closed is one of the current files, then close
135 -- the corresponding current file. It is not clear that this action
136 -- is required (RM A.10.3(23)) but it seems reasonable, and besides
137 -- ACVC test CE3208A expects this behavior.
139 if File_Type (File) = Current_In then
141 elsif File_Type (File) = Current_Out then
143 elsif File_Type (File) = Current_Err then
147 Terminate_Line (File_Type (File));
154 procedure AFCB_Free (File : not null access Wide_Text_AFCB) is
155 type FCB_Ptr is access all Wide_Text_AFCB;
156 FT : FCB_Ptr := FCB_Ptr (File);
159 new Ada.Unchecked_Deallocation (Wide_Text_AFCB, FCB_Ptr);
169 procedure Close (File : in out File_Type) is
171 FIO.Close (AP (File)'Unrestricted_Access);
178 -- Note: we assume that it is impossible in practice for the column
179 -- to exceed the value of Count'Last, i.e. no check is required for
180 -- overflow raising layout error.
182 function Col (File : File_Type) return Positive_Count is
184 FIO.Check_File_Open (AP (File));
188 function Col return Positive_Count is
190 return Col (Current_Out);
198 (File : in out File_Type;
199 Mode : File_Mode := Out_File;
203 Dummy_File_Control_Block : Wide_Text_AFCB;
204 pragma Warnings (Off, Dummy_File_Control_Block);
205 -- Yes, we know this is never assigned a value, only the tag
206 -- is used for dispatching purposes, so that's expected.
209 FIO.Open (File_Ptr => AP (File),
210 Dummy_FCB => Dummy_File_Control_Block,
211 Mode => To_FCB (Mode),
226 function Current_Error return File_Type is
231 function Current_Error return File_Access is
233 return Current_Err.Self'Access;
240 function Current_Input return File_Type is
245 function Current_Input return File_Access is
247 return Current_In.Self'Access;
254 function Current_Output return File_Type is
259 function Current_Output return File_Access is
261 return Current_Out.Self'Access;
268 procedure Delete (File : in out File_Type) is
270 FIO.Delete (AP (File)'Unrestricted_Access);
277 function End_Of_File (File : File_Type) return Boolean is
281 FIO.Check_Read_Status (AP (File));
283 if File.Before_Wide_Character then
286 elsif File.Before_LM then
287 if File.Before_LM_PM then
288 return Nextc (File) = EOF;
302 File.Before_LM := True;
306 -- Here we are just past the line mark with Before_LM set so that we
307 -- do not have to try to back up past the LM, thus avoiding the need
308 -- to back up more than one character.
315 elsif ch = PM and then File.Is_Regular_File then
316 File.Before_LM_PM := True;
317 return Nextc (File) = EOF;
319 -- Here if neither EOF nor PM followed end of line
328 function End_Of_File return Boolean is
330 return End_Of_File (Current_In);
337 function End_Of_Line (File : File_Type) return Boolean is
341 FIO.Check_Read_Status (AP (File));
343 if File.Before_Wide_Character then
346 elsif File.Before_LM then
362 function End_Of_Line return Boolean is
364 return End_Of_Line (Current_In);
371 function End_Of_Page (File : File_Type) return Boolean is
375 FIO.Check_Read_Status (AP (File));
377 if not File.Is_Regular_File then
380 elsif File.Before_Wide_Character then
383 elsif File.Before_LM then
384 if File.Before_LM_PM then
399 File.Before_LM := True;
403 -- Here we are just past the line mark with Before_LM set so that we
404 -- do not have to try to back up past the LM, thus avoiding the need
405 -- to back up more than one character.
409 return ch = PM or else ch = EOF;
412 function End_Of_Page return Boolean is
414 return End_Of_Page (Current_In);
421 procedure Flush (File : File_Type) is
423 FIO.Flush (AP (File));
435 function Form (File : File_Type) return String is
437 return FIO.Form (AP (File));
446 Item : out Wide_Character)
451 FIO.Check_Read_Status (AP (File));
453 if File.Before_Wide_Character then
454 File.Before_Wide_Character := False;
455 Item := File.Saved_Wide_Character;
457 -- Ada.Text_IO checks Before_LM_PM here, shouldn't we do the same???
460 Get_Character (File, C);
461 Item := Get_Wide_Char (C, File);
465 procedure Get (Item : out Wide_Character) is
467 Get (Current_In, Item);
472 Item : out Wide_String)
475 for J in Item'Range loop
476 Get (File, Item (J));
480 procedure Get (Item : out Wide_String) is
482 Get (Current_In, Item);
489 procedure Get_Character
491 Item : out Character)
496 if File.Before_LM then
497 File.Before_LM := False;
498 File.Before_LM_PM := False;
501 if File.Before_LM_PM then
503 File.Page := File.Page + 1;
504 File.Before_LM_PM := False;
507 File.Line := File.Line + 1;
518 File.Line := File.Line + 1;
521 elsif ch = PM and then File.Is_Regular_File then
522 File.Page := File.Page + 1;
526 Item := Character'Val (ch);
527 File.Col := File.Col + 1;
537 procedure Get_Immediate
539 Item : out Wide_Character)
544 FIO.Check_Read_Status (AP (File));
546 if File.Before_Wide_Character then
547 File.Before_Wide_Character := False;
548 Item := File.Saved_Wide_Character;
550 elsif File.Before_LM then
551 File.Before_LM := False;
552 File.Before_LM_PM := False;
553 Item := Wide_Character'Val (LM);
556 ch := Getc_Immed (File);
561 Item := Get_Wide_Char_Immed (Character'Val (ch), File);
566 procedure Get_Immediate
567 (Item : out Wide_Character)
570 Get_Immediate (Current_In, Item);
573 procedure Get_Immediate
575 Item : out Wide_Character;
576 Available : out Boolean)
581 FIO.Check_Read_Status (AP (File));
584 if File.Before_Wide_Character then
585 File.Before_Wide_Character := False;
586 Item := File.Saved_Wide_Character;
588 elsif File.Before_LM then
589 File.Before_LM := False;
590 File.Before_LM_PM := False;
591 Item := Wide_Character'Val (LM);
594 -- Shouldn't we use getc_immediate_nowait here, like Text_IO???
596 ch := Getc_Immed (File);
601 Item := Get_Wide_Char_Immed (Character'Val (ch), File);
606 procedure Get_Immediate
607 (Item : out Wide_Character;
608 Available : out Boolean)
611 Get_Immediate (Current_In, Item, Available);
620 Item : out Wide_String;
624 FIO.Check_Read_Status (AP (File));
625 Last := Item'First - 1;
627 -- Immediate exit for null string, this is a case in which we do not
628 -- need to test for end of file and we do not skip a line mark under
629 -- any circumstances.
631 if Last >= Item'Last then
635 -- Here we have at least one character, if we are immediately before
636 -- a line mark, then we will just skip past it storing no characters.
638 if File.Before_LM then
639 File.Before_LM := False;
640 File.Before_LM_PM := False;
642 -- Otherwise we need to read some characters
645 -- If we are at the end of file now, it means we are trying to
646 -- skip a file terminator and we raise End_Error (RM A.10.7(20))
648 if Nextc (File) = EOF then
652 -- Loop through characters in string
655 -- Exit the loop if read is terminated by encountering line mark
656 -- Note that the use of Skip_Line here ensures we properly deal
657 -- with setting the page and line numbers.
659 if End_Of_Line (File) then
664 -- Otherwise store the character, note that we know that ch is
665 -- something other than LM or EOF. It could possibly be a page
666 -- mark if there is a stray page mark in the middle of a line, but
667 -- this is not an official page mark in any case, since official
668 -- page marks can only follow a line mark. The whole page business
669 -- is pretty much nonsense anyway, so we do not want to waste
670 -- time trying to make sense out of non-standard page marks in
671 -- the file. This means that the behavior of Get_Line is different
672 -- from repeated Get of a character, but that's too bad. We
673 -- only promise that page numbers etc make sense if the file
674 -- is formatted in a standard manner.
676 -- Note: we do not adjust the column number because it is quicker
677 -- to adjust it once at the end of the operation than incrementing
678 -- it each time around the loop.
681 Get (File, Item (Last));
683 -- All done if the string is full, this is the case in which
684 -- we do not skip the following line mark. We need to adjust
685 -- the column number in this case.
687 if Last = Item'Last then
688 File.Col := File.Col + Count (Item'Length);
692 -- Exit from the loop if we are at the end of file. This happens
693 -- if we have a last line that is not terminated with a line mark.
694 -- In this case we consider that there is an implied line mark;
695 -- this is a non-standard file, but we will treat it nicely.
697 exit when Nextc (File) = EOF;
703 (Item : out Wide_String;
707 Get_Line (Current_In, Item, Last);
710 function Get_Line (File : File_Type) return Wide_String is
711 Buffer : Wide_String (1 .. 500);
714 function Get_Rest (S : Wide_String) return Wide_String;
715 -- This is a recursive function that reads the rest of the line and
716 -- returns it. S is the part read so far.
722 function Get_Rest (S : Wide_String) return Wide_String is
724 -- Each time we allocate a buffer the same size as what we have
725 -- read so far. This limits us to a logarithmic number of calls
726 -- to Get_Rest and also ensures only a linear use of stack space.
728 Buffer : Wide_String (1 .. S'Length);
732 Get_Line (File, Buffer, Last);
735 R : constant Wide_String := S & Buffer (1 .. Last);
737 if Last < Buffer'Last then
745 -- Start of processing for Get_Line
748 Get_Line (File, Buffer, Last);
750 if Last < Buffer'Last then
751 return Buffer (1 .. Last);
753 return Get_Rest (Buffer (1 .. Last));
757 function Get_Line return Wide_String is
759 return Get_Line (Current_In);
766 function Get_Wide_Char
768 File : File_Type) return Wide_Character
770 function In_Char return Character;
771 -- Function used to obtain additional characters it the wide character
772 -- sequence is more than one character long.
774 function WC_In is new Char_Sequence_To_Wide_Char (In_Char);
780 function In_Char return Character is
781 ch : constant Integer := Getc (File);
786 return Character'Val (ch);
790 -- Start of processing for Get_Wide_Char
793 FIO.Check_Read_Status (AP (File));
794 return WC_In (C, File.WC_Method);
797 -------------------------
798 -- Get_Wide_Char_Immed --
799 -------------------------
801 function Get_Wide_Char_Immed
803 File : File_Type) return Wide_Character
805 function In_Char return Character;
806 -- Function used to obtain additional characters it the wide character
807 -- sequence is more than one character long.
809 function WC_In is new Char_Sequence_To_Wide_Char (In_Char);
815 function In_Char return Character is
816 ch : constant Integer := Getc_Immed (File);
821 return Character'Val (ch);
825 -- Start of processing for Get_Wide_Char_Immed
828 FIO.Check_Read_Status (AP (File));
829 return WC_In (C, File.WC_Method);
830 end Get_Wide_Char_Immed;
836 function Getc (File : File_Type) return int is
840 ch := fgetc (File.Stream);
842 if ch = EOF and then ferror (File.Stream) /= 0 then
853 function Getc_Immed (File : File_Type) return int is
857 procedure getc_immediate
858 (stream : FILEs; ch : out int; end_of_file : out int);
859 pragma Import (C, getc_immediate, "getc_immediate");
862 FIO.Check_Read_Status (AP (File));
864 if File.Before_LM then
865 File.Before_LM := False;
866 File.Before_LM_PM := False;
870 getc_immediate (File.Stream, ch, end_of_file);
872 if ferror (File.Stream) /= 0 then
874 elsif end_of_file /= 0 then
882 -------------------------------
883 -- Initialize_Standard_Files --
884 -------------------------------
886 procedure Initialize_Standard_Files is
888 Standard_Err.Stream := stderr;
889 Standard_Err.Name := Err_Name'Access;
890 Standard_Err.Form := Null_Str'Unrestricted_Access;
891 Standard_Err.Mode := FCB.Out_File;
892 Standard_Err.Is_Regular_File := is_regular_file (fileno (stderr)) /= 0;
893 Standard_Err.Is_Temporary_File := False;
894 Standard_Err.Is_System_File := True;
895 Standard_Err.Text_Encoding := Default_Text;
896 Standard_Err.Access_Method := 'T';
897 Standard_Err.Self := Standard_Err;
898 Standard_Err.WC_Method := Default_WCEM;
900 Standard_In.Stream := stdin;
901 Standard_In.Name := In_Name'Access;
902 Standard_In.Form := Null_Str'Unrestricted_Access;
903 Standard_In.Mode := FCB.In_File;
904 Standard_In.Is_Regular_File := is_regular_file (fileno (stdin)) /= 0;
905 Standard_In.Is_Temporary_File := False;
906 Standard_In.Is_System_File := True;
907 Standard_In.Text_Encoding := Default_Text;
908 Standard_In.Access_Method := 'T';
909 Standard_In.Self := Standard_In;
910 Standard_In.WC_Method := Default_WCEM;
912 Standard_Out.Stream := stdout;
913 Standard_Out.Name := Out_Name'Access;
914 Standard_Out.Form := Null_Str'Unrestricted_Access;
915 Standard_Out.Mode := FCB.Out_File;
916 Standard_Out.Is_Regular_File := is_regular_file (fileno (stdout)) /= 0;
917 Standard_Out.Is_Temporary_File := False;
918 Standard_Out.Is_System_File := True;
919 Standard_Out.Text_Encoding := Default_Text;
920 Standard_Out.Access_Method := 'T';
921 Standard_Out.Self := Standard_Out;
922 Standard_Out.WC_Method := Default_WCEM;
924 FIO.Make_Unbuffered (AP (Standard_Out));
925 FIO.Make_Unbuffered (AP (Standard_Err));
926 end Initialize_Standard_Files;
932 function Is_Open (File : File_Type) return Boolean is
934 return FIO.Is_Open (AP (File));
941 -- Note: we assume that it is impossible in practice for the line to exceed
942 -- the value of Count'Last, i.e. no check is required for overflow raising
945 function Line (File : File_Type) return Positive_Count is
947 FIO.Check_File_Open (AP (File));
951 function Line return Positive_Count is
953 return Line (Current_Out);
960 function Line_Length (File : File_Type) return Count is
962 FIO.Check_Write_Status (AP (File));
963 return File.Line_Length;
966 function Line_Length return Count is
968 return Line_Length (Current_Out);
977 Item : out Wide_Character;
978 End_Of_Line : out Boolean)
982 -- Start of processing for Look_Ahead
985 FIO.Check_Read_Status (AP (File));
987 -- If we are logically before a line mark, we can return immediately
989 if File.Before_LM then
991 Item := Wide_Character'Val (0);
993 -- If we are before a wide character, just return it (this can happen
994 -- if there are two calls to Look_Ahead in a row).
996 elsif File.Before_Wide_Character then
997 End_Of_Line := False;
998 Item := File.Saved_Wide_Character;
1000 -- otherwise we must read a character from the input stream
1007 or else (ch = EOF and then File.Is_Regular_File)
1009 End_Of_Line := True;
1011 Item := Wide_Character'Val (0);
1013 -- Case where character obtained does not represent the start of an
1014 -- encoded sequence so it stands for itself and we can unget it with
1017 elsif not Is_Start_Of_Encoding
1018 (Character'Val (ch), File.WC_Method)
1020 End_Of_Line := False;
1022 Item := Wide_Character'Val (ch);
1024 -- For the start of an encoding, we read the character using the
1025 -- Get_Wide_Char routine. It will occupy more than one byte so we
1026 -- can't put it back with ungetc. Instead we save it in the control
1027 -- block, setting a flag that everyone interested in reading
1028 -- characters must test before reading the stream.
1031 Item := Get_Wide_Char (Character'Val (ch), File);
1032 End_Of_Line := False;
1033 File.Saved_Wide_Character := Item;
1034 File.Before_Wide_Character := True;
1039 procedure Look_Ahead
1040 (Item : out Wide_Character;
1041 End_Of_Line : out Boolean)
1044 Look_Ahead (Current_In, Item, End_Of_Line);
1051 function Mode (File : File_Type) return File_Mode is
1053 return To_TIO (FIO.Mode (AP (File)));
1060 function Name (File : File_Type) return String is
1062 return FIO.Name (AP (File));
1071 Spacing : Positive_Count := 1)
1074 -- Raise Constraint_Error if out of range value. The reason for this
1075 -- explicit test is that we don't want junk values around, even if
1076 -- checks are off in the caller.
1078 if not Spacing'Valid then
1079 raise Constraint_Error;
1082 FIO.Check_Write_Status (AP (File));
1084 for K in 1 .. Spacing loop
1086 -- We use Put here (rather than Putc) so that we get the proper
1087 -- behavior on windows for output of Wide_String to the console.
1089 Put (File, Wide_Character'Val (LM));
1091 File.Line := File.Line + 1;
1093 if File.Page_Length /= 0 and then File.Line > File.Page_Length then
1095 -- Same situation as above, use Put instead of Putc
1097 Put (File, Wide_Character'Val (PM));
1100 File.Page := File.Page + 1;
1107 procedure New_Line (Spacing : Positive_Count := 1) is
1109 New_Line (Current_Out, Spacing);
1116 procedure New_Page (File : File_Type) is
1118 FIO.Check_Write_Status (AP (File));
1120 if File.Col /= 1 or else File.Line = 1 then
1125 File.Page := File.Page + 1;
1130 procedure New_Page is
1132 New_Page (Current_Out);
1139 function Nextc (File : File_Type) return int is
1143 ch := fgetc (File.Stream);
1146 if ferror (File.Stream) /= 0 then
1151 if ungetc (ch, File.Stream) = EOF then
1164 (File : in out File_Type;
1167 Form : String := "")
1169 Dummy_File_Control_Block : Wide_Text_AFCB;
1170 pragma Warnings (Off, Dummy_File_Control_Block);
1171 -- Yes, we know this is never assigned a value, only the tag
1172 -- is used for dispatching purposes, so that's expected.
1175 FIO.Open (File_Ptr => AP (File),
1176 Dummy_FCB => Dummy_File_Control_Block,
1177 Mode => To_FCB (Mode),
1192 -- Note: we assume that it is impossible in practice for the page
1193 -- to exceed the value of Count'Last, i.e. no check is required for
1194 -- overflow raising layout error.
1196 function Page (File : File_Type) return Positive_Count is
1198 FIO.Check_File_Open (AP (File));
1202 function Page return Positive_Count is
1204 return Page (Current_Out);
1211 function Page_Length (File : File_Type) return Count is
1213 FIO.Check_Write_Status (AP (File));
1214 return File.Page_Length;
1217 function Page_Length return Count is
1219 return Page_Length (Current_Out);
1228 Item : Wide_Character)
1230 wide_text_translation_required : Boolean;
1231 for wide_text_translation_required'Size use Character'Size;
1232 pragma Import (C, wide_text_translation_required,
1233 "__gnat_wide_text_translation_required");
1234 -- Text translation is required on Windows only. This means that the
1235 -- console is doing translation and we do not want to do any encoding
1236 -- here. If this boolean is set we just output the character as-is.
1238 procedure Out_Char (C : Character);
1239 -- Procedure to output one character of a wide character sequence
1241 procedure WC_Out is new Wide_Char_To_Char_Sequence (Out_Char);
1247 procedure Out_Char (C : Character) is
1249 Putc (Character'Pos (C), File);
1254 -- Start of processing for Put
1257 FIO.Check_Write_Status (AP (File));
1259 if wide_text_translation_required
1260 or else File.Text_Encoding /= Default_Text
1262 set_mode (fileno (File.Stream), File.Text_Encoding);
1263 Discard := fputwc (Wide_Character'Pos (Item), File.Stream);
1265 WC_Out (Item, File.WC_Method);
1268 File.Col := File.Col + 1;
1271 procedure Put (Item : Wide_Character) is
1273 Put (Current_Out, Item);
1285 for J in Item'Range loop
1286 Put (File, Item (J));
1290 procedure Put (Item : Wide_String) is
1292 Put (Current_Out, Item);
1308 procedure Put_Line (Item : Wide_String) is
1310 Put (Current_Out, Item);
1311 New_Line (Current_Out);
1318 procedure Putc (ch : int; File : File_Type) is
1320 if fputc (ch, File.Stream) = EOF then
1329 -- This is the primitive Stream Read routine, used when a Text_IO file
1330 -- is treated directly as a stream using Text_IO.Streams.Stream.
1333 (File : in out Wide_Text_AFCB;
1334 Item : out Stream_Element_Array;
1335 Last : out Stream_Element_Offset)
1338 pragma Unreferenced (Discard_ch);
1341 -- Need to deal with Before_Wide_Character ???
1343 if File.Mode /= FCB.In_File then
1347 -- Deal with case where our logical and physical position do not match
1348 -- because of being after an LM or LM-PM sequence when in fact we are
1349 -- logically positioned before it.
1351 if File.Before_LM then
1353 -- If we are before a PM, then it is possible for a stream read
1354 -- to leave us after the LM and before the PM, which is a bit
1355 -- odd. The easiest way to deal with this is to unget the PM,
1356 -- so we are indeed positioned between the characters. This way
1357 -- further stream read operations will work correctly, and the
1358 -- effect on text processing is a little weird, but what can
1359 -- be expected if stream and text input are mixed this way?
1361 if File.Before_LM_PM then
1362 Discard_ch := ungetc (PM, File.Stream);
1363 File.Before_LM_PM := False;
1366 File.Before_LM := False;
1368 Item (Item'First) := Stream_Element (Character'Pos (ASCII.LF));
1370 if Item'Length = 1 then
1376 Stream_Element_Offset
1377 (fread (buffer => Item'Address,
1378 index => size_t (Item'First + 1),
1380 count => Item'Length - 1,
1381 stream => File.Stream));
1387 -- Now we do the read. Since this is a text file, it is normally in
1388 -- text mode, but stream data must be read in binary mode, so we
1389 -- temporarily set binary mode for the read, resetting it after.
1390 -- These calls have no effect in a system (like Unix) where there is
1391 -- no distinction between text and binary files.
1393 set_binary_mode (fileno (File.Stream));
1397 Stream_Element_Offset
1398 (fread (Item'Address, 1, Item'Length, File.Stream)) - 1;
1400 if Last < Item'Last then
1401 if ferror (File.Stream) /= 0 then
1406 set_text_mode (fileno (File.Stream));
1414 (File : in out File_Type;
1418 -- Don't allow change of mode for current file (RM A.10.2(5))
1420 if (File = Current_In or else
1421 File = Current_Out or else
1422 File = Current_Error)
1423 and then To_FCB (Mode) /= File.Mode
1428 Terminate_Line (File);
1429 FIO.Reset (AP (File)'Unrestricted_Access, To_FCB (Mode));
1433 File.Line_Length := 0;
1434 File.Page_Length := 0;
1435 File.Before_LM := False;
1436 File.Before_LM_PM := False;
1439 procedure Reset (File : in out File_Type) is
1441 Terminate_Line (File);
1442 FIO.Reset (AP (File)'Unrestricted_Access);
1446 File.Line_Length := 0;
1447 File.Page_Length := 0;
1448 File.Before_LM := False;
1449 File.Before_LM_PM := False;
1458 To : Positive_Count)
1463 -- Raise Constraint_Error if out of range value. The reason for this
1464 -- explicit test is that we don't want junk values around, even if
1465 -- checks are off in the caller.
1467 if not To'Valid then
1468 raise Constraint_Error;
1471 FIO.Check_File_Open (AP (File));
1473 if To = File.Col then
1477 if Mode (File) >= Out_File then
1478 if File.Line_Length /= 0 and then To > File.Line_Length then
1482 if To < File.Col then
1486 while File.Col < To loop
1498 File.Line := File.Line + 1;
1501 elsif ch = PM and then File.Is_Regular_File then
1502 File.Page := File.Page + 1;
1506 elsif To = File.Col then
1511 File.Col := File.Col + 1;
1517 procedure Set_Col (To : Positive_Count) is
1519 Set_Col (Current_Out, To);
1526 procedure Set_Error (File : File_Type) is
1528 FIO.Check_Write_Status (AP (File));
1529 Current_Err := File;
1536 procedure Set_Input (File : File_Type) is
1538 FIO.Check_Read_Status (AP (File));
1548 To : Positive_Count)
1551 -- Raise Constraint_Error if out of range value. The reason for this
1552 -- explicit test is that we don't want junk values around, even if
1553 -- checks are off in the caller.
1555 if not To'Valid then
1556 raise Constraint_Error;
1559 FIO.Check_File_Open (AP (File));
1561 if To = File.Line then
1565 if Mode (File) >= Out_File then
1566 if File.Page_Length /= 0 and then To > File.Page_Length then
1570 if To < File.Line then
1574 while File.Line < To loop
1579 while To /= File.Line loop
1585 procedure Set_Line (To : Positive_Count) is
1587 Set_Line (Current_Out, To);
1590 ---------------------
1591 -- Set_Line_Length --
1592 ---------------------
1594 procedure Set_Line_Length (File : File_Type; To : Count) is
1596 -- Raise Constraint_Error if out of range value. The reason for this
1597 -- explicit test is that we don't want junk values around, even if
1598 -- checks are off in the caller.
1600 if not To'Valid then
1601 raise Constraint_Error;
1604 FIO.Check_Write_Status (AP (File));
1605 File.Line_Length := To;
1606 end Set_Line_Length;
1608 procedure Set_Line_Length (To : Count) is
1610 Set_Line_Length (Current_Out, To);
1611 end Set_Line_Length;
1617 procedure Set_Output (File : File_Type) is
1619 FIO.Check_Write_Status (AP (File));
1620 Current_Out := File;
1623 ---------------------
1624 -- Set_Page_Length --
1625 ---------------------
1627 procedure Set_Page_Length (File : File_Type; To : Count) is
1629 -- Raise Constraint_Error if out of range value. The reason for this
1630 -- explicit test is that we don't want junk values around, even if
1631 -- checks are off in the caller.
1633 if not To'Valid then
1634 raise Constraint_Error;
1637 FIO.Check_Write_Status (AP (File));
1638 File.Page_Length := To;
1639 end Set_Page_Length;
1641 procedure Set_Page_Length (To : Count) is
1643 Set_Page_Length (Current_Out, To);
1644 end Set_Page_Length;
1650 procedure Set_WCEM (File : in out File_Type) is
1655 File.WC_Method := WCEM_Brackets;
1656 FIO.Form_Parameter (File.Form.all, "wcem", Start, Stop);
1659 File.WC_Method := WCEM_Brackets;
1662 if Stop = Start then
1663 for J in WC_Encoding_Letters'Range loop
1664 if File.Form (Start) = WC_Encoding_Letters (J) then
1665 File.WC_Method := J;
1672 raise Use_Error with "invalid WCEM form parameter";
1682 Spacing : Positive_Count := 1)
1687 -- Raise Constraint_Error if out of range value. The reason for this
1688 -- explicit test is that we don't want junk values around, even if
1689 -- checks are off in the caller.
1691 if not Spacing'Valid then
1692 raise Constraint_Error;
1695 FIO.Check_Read_Status (AP (File));
1697 for L in 1 .. Spacing loop
1698 if File.Before_LM then
1699 File.Before_LM := False;
1700 File.Before_LM_PM := False;
1705 -- If at end of file now, then immediately raise End_Error. Note
1706 -- that we can never be positioned between a line mark and a page
1707 -- mark, so if we are at the end of file, we cannot logically be
1708 -- before the implicit page mark that is at the end of the file.
1710 -- For the same reason, we do not need an explicit check for a
1711 -- page mark. If there is a FF in the middle of a line, the file
1712 -- is not in canonical format and we do not care about the page
1713 -- numbers for files other than ones in canonical format.
1719 -- If not at end of file, then loop till we get to an LM or EOF.
1720 -- The latter case happens only in non-canonical files where the
1721 -- last line is not terminated by LM, but we don't want to blow
1722 -- up for such files, so we assume an implicit LM in this case.
1725 exit when ch = LM or else ch = EOF;
1730 -- We have got past a line mark, now, for a regular file only,
1731 -- see if a page mark immediately follows this line mark and
1732 -- if so, skip past the page mark as well. We do not do this
1733 -- for non-regular files, since it would cause an undesirable
1734 -- wait for an additional character.
1737 File.Line := File.Line + 1;
1739 if File.Before_LM_PM then
1740 File.Page := File.Page + 1;
1742 File.Before_LM_PM := False;
1744 elsif File.Is_Regular_File then
1747 -- Page mark can be explicit, or implied at the end of the file
1749 if (ch = PM or else ch = EOF)
1750 and then File.Is_Regular_File
1752 File.Page := File.Page + 1;
1760 File.Before_Wide_Character := False;
1763 procedure Skip_Line (Spacing : Positive_Count := 1) is
1765 Skip_Line (Current_In, Spacing);
1772 procedure Skip_Page (File : File_Type) is
1776 FIO.Check_Read_Status (AP (File));
1778 -- If at page mark already, just skip it
1780 if File.Before_LM_PM then
1781 File.Before_LM := False;
1782 File.Before_LM_PM := False;
1783 File.Page := File.Page + 1;
1789 -- This is a bit tricky, if we are logically before an LM then
1790 -- it is not an error if we are at an end of file now, since we
1791 -- are not really at it.
1793 if File.Before_LM then
1794 File.Before_LM := False;
1795 File.Before_LM_PM := False;
1798 -- Otherwise we do raise End_Error if we are at the end of file now
1808 -- Now we can just rumble along to the next page mark, or to the
1809 -- end of file, if that comes first. The latter case happens when
1810 -- the page mark is implied at the end of file.
1814 or else (ch = PM and then File.Is_Regular_File);
1818 File.Page := File.Page + 1;
1821 File.Before_Wide_Character := False;
1824 procedure Skip_Page is
1826 Skip_Page (Current_In);
1829 --------------------
1830 -- Standard_Error --
1831 --------------------
1833 function Standard_Error return File_Type is
1835 return Standard_Err;
1838 function Standard_Error return File_Access is
1840 return Standard_Err'Access;
1843 --------------------
1844 -- Standard_Input --
1845 --------------------
1847 function Standard_Input return File_Type is
1852 function Standard_Input return File_Access is
1854 return Standard_In'Access;
1857 ---------------------
1858 -- Standard_Output --
1859 ---------------------
1861 function Standard_Output return File_Type is
1863 return Standard_Out;
1864 end Standard_Output;
1866 function Standard_Output return File_Access is
1868 return Standard_Out'Access;
1869 end Standard_Output;
1871 --------------------
1872 -- Terminate_Line --
1873 --------------------
1875 procedure Terminate_Line (File : File_Type) is
1877 FIO.Check_File_Open (AP (File));
1879 -- For file other than In_File, test for needing to terminate last line
1881 if Mode (File) /= In_File then
1883 -- If not at start of line definition need new line
1885 if File.Col /= 1 then
1888 -- For files other than standard error and standard output, we
1889 -- make sure that an empty file has a single line feed, so that
1890 -- it is properly formatted. We avoid this for the standard files
1891 -- because it is too much of a nuisance to have these odd line
1892 -- feeds when nothing has been written to the file.
1894 elsif (File /= Standard_Err and then File /= Standard_Out)
1895 and then (File.Line = 1 and then File.Page = 1)
1906 procedure Ungetc (ch : int; File : File_Type) is
1909 if ungetc (ch, File.Stream) = EOF then
1919 -- This is the primitive Stream Write routine, used when a Text_IO file
1920 -- is treated directly as a stream using Text_IO.Streams.Stream.
1923 (File : in out Wide_Text_AFCB;
1924 Item : Stream_Element_Array)
1926 pragma Warnings (Off, File);
1927 -- Because in this implementation we don't need IN OUT, we only read
1929 Siz : constant size_t := Item'Length;
1932 if File.Mode = FCB.In_File then
1936 -- Now we do the write. Since this is a text file, it is normally in
1937 -- text mode, but stream data must be written in binary mode, so we
1938 -- temporarily set binary mode for the write, resetting it after.
1939 -- These calls have no effect in a system (like Unix) where there is
1940 -- no distinction between text and binary files.
1942 set_binary_mode (fileno (File.Stream));
1944 if fwrite (Item'Address, 1, Siz, File.Stream) /= Siz then
1948 set_text_mode (fileno (File.Stream));
1952 -- Initialize Standard Files
1954 for J in WC_Encoding_Method loop
1955 if WC_Encoding = WC_Encoding_Letters (J) then
1960 Initialize_Standard_Files;
1962 FIO.Chain_File (AP (Standard_In));
1963 FIO.Chain_File (AP (Standard_Out));
1964 FIO.Chain_File (AP (Standard_Err));
1966 end Ada.Wide_Text_IO;