1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . W I D E _ W I D E _ T E X T _ I O --
9 -- Copyright (C) 1992-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.Wide_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");
61 -----------------------
62 -- Local Subprograms --
63 -----------------------
65 function Getc_Immed (File : File_Type) return int;
66 -- This routine is identical to Getc, except that the read is done in
67 -- Get_Immediate mode (i.e. without waiting for a line return).
69 function Get_Wide_Wide_Char_Immed
71 File : File_Type) return Wide_Wide_Character;
72 -- This routine is identical to Get_Wide_Wide_Char, except that the reads
73 -- are done in Get_Immediate mode (i.e. without waiting for a line return).
75 procedure Set_WCEM (File : in out File_Type);
76 -- Called by Open and Create to set the wide character encoding method for
77 -- the file, processing a WCEM form parameter if one is present. File is
78 -- IN OUT because it may be closed in case of an error.
84 function AFCB_Allocate
85 (Control_Block : Wide_Wide_Text_AFCB) return FCB.AFCB_Ptr
87 pragma Unreferenced (Control_Block);
89 return new Wide_Wide_Text_AFCB;
96 procedure AFCB_Close (File : not null access Wide_Wide_Text_AFCB) is
98 -- If the file being closed is one of the current files, then close
99 -- the corresponding current file. It is not clear that this action
100 -- is required (RM A.10.3(23)) but it seems reasonable, and besides
101 -- ACVC test CE3208A expects this behavior.
103 if File_Type (File) = Current_In then
105 elsif File_Type (File) = Current_Out then
107 elsif File_Type (File) = Current_Err then
111 Terminate_Line (File_Type (File));
118 procedure AFCB_Free (File : not null access Wide_Wide_Text_AFCB) is
119 type FCB_Ptr is access all Wide_Wide_Text_AFCB;
120 FT : FCB_Ptr := FCB_Ptr (File);
122 procedure Free is new
123 Ada.Unchecked_Deallocation (Wide_Wide_Text_AFCB, FCB_Ptr);
133 procedure Close (File : in out File_Type) is
135 FIO.Close (AP (File)'Unrestricted_Access);
142 -- Note: we assume that it is impossible in practice for the column
143 -- to exceed the value of Count'Last, i.e. no check is required for
144 -- overflow raising layout error.
146 function Col (File : File_Type) return Positive_Count is
148 FIO.Check_File_Open (AP (File));
152 function Col return Positive_Count is
154 return Col (Current_Out);
162 (File : in out File_Type;
163 Mode : File_Mode := Out_File;
167 Dummy_File_Control_Block : Wide_Wide_Text_AFCB;
168 pragma Warnings (Off, Dummy_File_Control_Block);
169 -- Yes, we know this is never assigned a value, only the tag
170 -- is used for dispatching purposes, so that's expected.
173 FIO.Open (File_Ptr => AP (File),
174 Dummy_FCB => Dummy_File_Control_Block,
175 Mode => To_FCB (Mode),
190 function Current_Error return File_Type is
195 function Current_Error return File_Access is
197 return Current_Err.Self'Access;
204 function Current_Input return File_Type is
209 function Current_Input return File_Access is
211 return Current_In.Self'Access;
218 function Current_Output return File_Type is
223 function Current_Output return File_Access is
225 return Current_Out.Self'Access;
232 procedure Delete (File : in out File_Type) is
234 FIO.Delete (AP (File)'Unrestricted_Access);
241 function End_Of_File (File : File_Type) return Boolean is
245 FIO.Check_Read_Status (AP (File));
247 if File.Before_Wide_Wide_Character then
250 elsif File.Before_LM then
251 if File.Before_LM_PM then
252 return Nextc (File) = EOF;
266 File.Before_LM := True;
270 -- Here we are just past the line mark with Before_LM set so that we
271 -- do not have to try to back up past the LM, thus avoiding the need
272 -- to back up more than one character.
279 elsif ch = PM and then File.Is_Regular_File then
280 File.Before_LM_PM := True;
281 return Nextc (File) = EOF;
283 -- Here if neither EOF nor PM followed end of line
292 function End_Of_File return Boolean is
294 return End_Of_File (Current_In);
301 function End_Of_Line (File : File_Type) return Boolean is
305 FIO.Check_Read_Status (AP (File));
307 if File.Before_Wide_Wide_Character then
310 elsif File.Before_LM then
326 function End_Of_Line return Boolean is
328 return End_Of_Line (Current_In);
335 function End_Of_Page (File : File_Type) return Boolean is
339 FIO.Check_Read_Status (AP (File));
341 if not File.Is_Regular_File then
344 elsif File.Before_Wide_Wide_Character then
347 elsif File.Before_LM then
348 if File.Before_LM_PM then
363 File.Before_LM := True;
367 -- Here we are just past the line mark with Before_LM set so that we
368 -- do not have to try to back up past the LM, thus avoiding the need
369 -- to back up more than one character.
373 return ch = PM or else ch = EOF;
376 function End_Of_Page return Boolean is
378 return End_Of_Page (Current_In);
385 procedure Flush (File : File_Type) is
387 FIO.Flush (AP (File));
399 function Form (File : File_Type) return String is
401 return FIO.Form (AP (File));
410 Item : out Wide_Wide_Character)
415 FIO.Check_Read_Status (AP (File));
417 if File.Before_Wide_Wide_Character then
418 File.Before_Wide_Wide_Character := False;
419 Item := File.Saved_Wide_Wide_Character;
421 -- Ada.Text_IO checks Before_LM_PM here, shouldn't we do the same???
424 Get_Character (File, C);
425 Item := Get_Wide_Wide_Char (C, File);
429 procedure Get (Item : out Wide_Wide_Character) is
431 Get (Current_In, Item);
436 Item : out Wide_Wide_String)
439 for J in Item'Range loop
440 Get (File, Item (J));
444 procedure Get (Item : out Wide_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_Wide_Character)
508 FIO.Check_Read_Status (AP (File));
510 if File.Before_Wide_Wide_Character then
511 File.Before_Wide_Wide_Character := False;
512 Item := File.Saved_Wide_Wide_Character;
514 elsif File.Before_LM then
515 File.Before_LM := False;
516 File.Before_LM_PM := False;
517 Item := Wide_Wide_Character'Val (LM);
520 ch := Getc_Immed (File);
525 Item := Get_Wide_Wide_Char_Immed (Character'Val (ch), File);
530 procedure Get_Immediate
531 (Item : out Wide_Wide_Character)
534 Get_Immediate (Current_In, Item);
537 procedure Get_Immediate
539 Item : out Wide_Wide_Character;
540 Available : out Boolean)
545 FIO.Check_Read_Status (AP (File));
548 if File.Before_Wide_Wide_Character then
549 File.Before_Wide_Wide_Character := False;
550 Item := File.Saved_Wide_Wide_Character;
552 elsif File.Before_LM then
553 File.Before_LM := False;
554 File.Before_LM_PM := False;
555 Item := Wide_Wide_Character'Val (LM);
558 -- Shouldn't we use getc_immediate_nowait here, like Text_IO???
560 ch := Getc_Immed (File);
565 Item := Get_Wide_Wide_Char_Immed (Character'Val (ch), File);
570 procedure Get_Immediate
571 (Item : out Wide_Wide_Character;
572 Available : out Boolean)
575 Get_Immediate (Current_In, Item, Available);
584 Item : out Wide_Wide_String;
588 FIO.Check_Read_Status (AP (File));
589 Last := Item'First - 1;
591 -- Immediate exit for null string, this is a case in which we do not
592 -- need to test for end of file and we do not skip a line mark under
593 -- any circumstances.
595 if Last >= Item'Last then
599 -- Here we have at least one character, if we are immediately before
600 -- a line mark, then we will just skip past it storing no characters.
602 if File.Before_LM then
603 File.Before_LM := False;
604 File.Before_LM_PM := False;
606 -- Otherwise we need to read some characters
609 -- If we are at the end of file now, it means we are trying to
610 -- skip a file terminator and we raise End_Error (RM A.10.7(20))
612 if Nextc (File) = EOF then
616 -- Loop through characters in string
619 -- Exit the loop if read is terminated by encountering line mark
620 -- Note that the use of Skip_Line here ensures we properly deal
621 -- with setting the page and line numbers.
623 if End_Of_Line (File) then
628 -- Otherwise store the character, note that we know that ch is
629 -- something other than LM or EOF. It could possibly be a page
630 -- mark if there is a stray page mark in the middle of a line,
631 -- but this is not an official page mark in any case, since
632 -- official page marks can only follow a line mark. The whole
633 -- page business is pretty much nonsense anyway, so we do not
634 -- want to waste time trying to make sense out of non-standard
635 -- page marks in the file! This means that the behavior of
636 -- Get_Line is different from repeated Get of a character, but
637 -- that's too bad. We only promise that page numbers etc make
638 -- sense if the file is formatted in a standard manner.
640 -- Note: we do not adjust the column number because it is quicker
641 -- to adjust it once at the end of the operation than incrementing
642 -- it each time around the loop.
645 Get (File, Item (Last));
647 -- All done if the string is full, this is the case in which
648 -- we do not skip the following line mark. We need to adjust
649 -- the column number in this case.
651 if Last = Item'Last then
652 File.Col := File.Col + Count (Item'Length);
656 -- Exit from the loop if we are at the end of file. This happens
657 -- if we have a last line that is not terminated with a line mark.
658 -- In this case we consider that there is an implied line mark;
659 -- this is a non-standard file, but we will treat it nicely.
661 exit when Nextc (File) = EOF;
667 (Item : out Wide_Wide_String;
671 Get_Line (Current_In, Item, Last);
674 function Get_Line (File : File_Type) return Wide_Wide_String is
675 Buffer : Wide_Wide_String (1 .. 500);
678 function Get_Rest (S : Wide_Wide_String) return Wide_Wide_String;
679 -- This is a recursive function that reads the rest of the line and
680 -- returns it. S is the part read so far.
686 function Get_Rest (S : Wide_Wide_String) return Wide_Wide_String is
688 -- Each time we allocate a buffer the same size as what we have
689 -- read so far. This limits us to a logarithmic number of calls
690 -- to Get_Rest and also ensures only a linear use of stack space.
692 Buffer : Wide_Wide_String (1 .. S'Length);
696 Get_Line (File, Buffer, Last);
699 R : constant Wide_Wide_String := S & Buffer (1 .. Last);
701 if Last < Buffer'Last then
709 -- Start of processing for Get_Line
712 Get_Line (File, Buffer, Last);
714 if Last < Buffer'Last then
715 return Buffer (1 .. Last);
717 return Get_Rest (Buffer (1 .. Last));
721 function Get_Line return Wide_Wide_String is
723 return Get_Line (Current_In);
726 ------------------------
727 -- Get_Wide_Wide_Char --
728 ------------------------
730 function Get_Wide_Wide_Char
732 File : File_Type) return Wide_Wide_Character
734 function In_Char return Character;
735 -- Function used to obtain additional characters it the wide character
736 -- sequence is more than one character long.
738 function WC_In is new Char_Sequence_To_UTF_32 (In_Char);
744 function In_Char return Character is
745 ch : constant Integer := Getc (File);
750 return Character'Val (ch);
754 -- Start of processing for Get_Wide_Wide_Char
757 FIO.Check_Read_Status (AP (File));
758 return Wide_Wide_Character'Val (WC_In (C, File.WC_Method));
759 end Get_Wide_Wide_Char;
761 ------------------------------
762 -- Get_Wide_Wide_Char_Immed --
763 ------------------------------
765 function Get_Wide_Wide_Char_Immed
767 File : File_Type) return Wide_Wide_Character
769 function In_Char return Character;
770 -- Function used to obtain additional characters it the wide character
771 -- sequence is more than one character long.
773 function WC_In is new Char_Sequence_To_UTF_32 (In_Char);
779 function In_Char return Character is
780 ch : constant Integer := Getc_Immed (File);
785 return Character'Val (ch);
789 -- Start of processing for Get_Wide_Wide_Char_Immed
792 FIO.Check_Read_Status (AP (File));
793 return Wide_Wide_Character'Val (WC_In (C, File.WC_Method));
794 end Get_Wide_Wide_Char_Immed;
800 function Getc (File : File_Type) return int is
804 ch := fgetc (File.Stream);
806 if ch = EOF and then ferror (File.Stream) /= 0 then
817 function Getc_Immed (File : File_Type) return int is
821 procedure getc_immediate
822 (stream : FILEs; ch : out int; end_of_file : out int);
823 pragma Import (C, getc_immediate, "getc_immediate");
826 FIO.Check_Read_Status (AP (File));
828 if File.Before_LM then
829 File.Before_LM := False;
830 File.Before_LM_PM := False;
834 getc_immediate (File.Stream, ch, end_of_file);
836 if ferror (File.Stream) /= 0 then
838 elsif end_of_file /= 0 then
850 function Is_Open (File : File_Type) return Boolean is
852 return FIO.Is_Open (AP (File));
859 -- Note: we assume that it is impossible in practice for the line
860 -- to exceed the value of Count'Last, i.e. no check is required for
861 -- overflow raising layout error.
863 function Line (File : File_Type) return Positive_Count is
865 FIO.Check_File_Open (AP (File));
869 function Line return Positive_Count is
871 return Line (Current_Out);
878 function Line_Length (File : File_Type) return Count is
880 FIO.Check_Write_Status (AP (File));
881 return File.Line_Length;
884 function Line_Length return Count is
886 return Line_Length (Current_Out);
895 Item : out Wide_Wide_Character;
896 End_Of_Line : out Boolean)
900 -- Start of processing for Look_Ahead
903 FIO.Check_Read_Status (AP (File));
905 -- If we are logically before a line mark, we can return immediately
907 if File.Before_LM then
909 Item := Wide_Wide_Character'Val (0);
911 -- If we are before a wide character, just return it (this can happen
912 -- if there are two calls to Look_Ahead in a row).
914 elsif File.Before_Wide_Wide_Character then
915 End_Of_Line := False;
916 Item := File.Saved_Wide_Wide_Character;
918 -- otherwise we must read a character from the input stream
925 or else (ch = EOF and then File.Is_Regular_File)
929 Item := Wide_Wide_Character'Val (0);
931 -- Case where character obtained does not represent the start of an
932 -- encoded sequence so it stands for itself and we can unget it with
935 elsif not Is_Start_Of_Encoding
936 (Character'Val (ch), File.WC_Method)
938 End_Of_Line := False;
940 Item := Wide_Wide_Character'Val (ch);
942 -- For the start of an encoding, we read the character using the
943 -- Get_Wide_Wide_Char routine. It will occupy more than one byte so
944 -- we can't put it back with ungetc. Instead we save it in the
945 -- control block, setting a flag that everyone interested in reading
946 -- characters must test before reading the stream.
949 Item := Get_Wide_Wide_Char (Character'Val (ch), File);
950 End_Of_Line := False;
951 File.Saved_Wide_Wide_Character := Item;
952 File.Before_Wide_Wide_Character := True;
958 (Item : out Wide_Wide_Character;
959 End_Of_Line : out Boolean)
962 Look_Ahead (Current_In, Item, End_Of_Line);
969 function Mode (File : File_Type) return File_Mode is
971 return To_TIO (FIO.Mode (AP (File)));
978 function Name (File : File_Type) return String is
980 return FIO.Name (AP (File));
989 Spacing : Positive_Count := 1)
992 -- Raise Constraint_Error if out of range value. The reason for this
993 -- explicit test is that we don't want junk values around, even if
994 -- checks are off in the caller.
996 if not Spacing'Valid then
997 raise Constraint_Error;
1000 FIO.Check_Write_Status (AP (File));
1002 for K in 1 .. Spacing loop
1004 File.Line := File.Line + 1;
1006 if File.Page_Length /= 0
1007 and then File.Line > File.Page_Length
1011 File.Page := File.Page + 1;
1018 procedure New_Line (Spacing : Positive_Count := 1) is
1020 New_Line (Current_Out, Spacing);
1027 procedure New_Page (File : File_Type) is
1029 FIO.Check_Write_Status (AP (File));
1031 if File.Col /= 1 or else File.Line = 1 then
1036 File.Page := File.Page + 1;
1041 procedure New_Page is
1043 New_Page (Current_Out);
1050 function Nextc (File : File_Type) return int is
1054 ch := fgetc (File.Stream);
1057 if ferror (File.Stream) /= 0 then
1062 if ungetc (ch, File.Stream) = EOF then
1075 (File : in out File_Type;
1078 Form : String := "")
1080 Dummy_File_Control_Block : Wide_Wide_Text_AFCB;
1081 pragma Warnings (Off, Dummy_File_Control_Block);
1082 -- Yes, we know this is never assigned a value, only the tag
1083 -- is used for dispatching purposes, so that's expected.
1086 FIO.Open (File_Ptr => AP (File),
1087 Dummy_FCB => Dummy_File_Control_Block,
1088 Mode => To_FCB (Mode),
1103 -- Note: we assume that it is impossible in practice for the page
1104 -- to exceed the value of Count'Last, i.e. no check is required for
1105 -- overflow raising layout error.
1107 function Page (File : File_Type) return Positive_Count is
1109 FIO.Check_File_Open (AP (File));
1113 function Page return Positive_Count is
1115 return Page (Current_Out);
1122 function Page_Length (File : File_Type) return Count is
1124 FIO.Check_Write_Status (AP (File));
1125 return File.Page_Length;
1128 function Page_Length return Count is
1130 return Page_Length (Current_Out);
1139 Item : Wide_Wide_Character)
1141 procedure Out_Char (C : Character);
1142 -- Procedure to output one character of a wide character sequence
1144 procedure WC_Out is new UTF_32_To_Char_Sequence (Out_Char);
1150 procedure Out_Char (C : Character) is
1152 Putc (Character'Pos (C), File);
1155 -- Start of processing for Put
1158 FIO.Check_Write_Status (AP (File));
1159 WC_Out (Wide_Wide_Character'Pos (Item), File.WC_Method);
1160 File.Col := File.Col + 1;
1163 procedure Put (Item : Wide_Wide_Character) is
1165 Put (Current_Out, Item);
1174 Item : Wide_Wide_String)
1177 for J in Item'Range loop
1178 Put (File, Item (J));
1182 procedure Put (Item : Wide_Wide_String) is
1184 Put (Current_Out, Item);
1193 Item : Wide_Wide_String)
1200 procedure Put_Line (Item : Wide_Wide_String) is
1202 Put (Current_Out, Item);
1203 New_Line (Current_Out);
1210 procedure Putc (ch : int; File : File_Type) is
1212 if fputc (ch, File.Stream) = EOF then
1221 -- This is the primitive Stream Read routine, used when a Text_IO file
1222 -- is treated directly as a stream using Text_IO.Streams.Stream.
1225 (File : in out Wide_Wide_Text_AFCB;
1226 Item : out Stream_Element_Array;
1227 Last : out Stream_Element_Offset)
1230 pragma Unreferenced (Discard_ch);
1233 -- Need to deal with Before_Wide_Wide_Character ???
1235 if File.Mode /= FCB.In_File then
1239 -- Deal with case where our logical and physical position do not match
1240 -- because of being after an LM or LM-PM sequence when in fact we are
1241 -- logically positioned before it.
1243 if File.Before_LM then
1245 -- If we are before a PM, then it is possible for a stream read
1246 -- to leave us after the LM and before the PM, which is a bit
1247 -- odd. The easiest way to deal with this is to unget the PM,
1248 -- so we are indeed positioned between the characters. This way
1249 -- further stream read operations will work correctly, and the
1250 -- effect on text processing is a little weird, but what can
1251 -- be expected if stream and text input are mixed this way?
1253 if File.Before_LM_PM then
1254 Discard_ch := ungetc (PM, File.Stream);
1255 File.Before_LM_PM := False;
1258 File.Before_LM := False;
1260 Item (Item'First) := Stream_Element (Character'Pos (ASCII.LF));
1262 if Item'Length = 1 then
1268 Stream_Element_Offset
1269 (fread (buffer => Item'Address,
1270 index => size_t (Item'First + 1),
1272 count => Item'Length - 1,
1273 stream => File.Stream));
1279 -- Now we do the read. Since this is a text file, it is normally in
1280 -- text mode, but stream data must be read in binary mode, so we
1281 -- temporarily set binary mode for the read, resetting it after.
1282 -- These calls have no effect in a system (like Unix) where there is
1283 -- no distinction between text and binary files.
1285 set_binary_mode (fileno (File.Stream));
1289 Stream_Element_Offset
1290 (fread (Item'Address, 1, Item'Length, File.Stream)) - 1;
1292 if Last < Item'Last then
1293 if ferror (File.Stream) /= 0 then
1298 set_text_mode (fileno (File.Stream));
1306 (File : in out File_Type;
1310 -- Don't allow change of mode for current file (RM A.10.2(5))
1312 if (File = Current_In or else
1313 File = Current_Out or else
1314 File = Current_Error)
1315 and then To_FCB (Mode) /= File.Mode
1320 Terminate_Line (File);
1321 FIO.Reset (AP (File)'Unrestricted_Access, To_FCB (Mode));
1325 File.Line_Length := 0;
1326 File.Page_Length := 0;
1327 File.Before_LM := False;
1328 File.Before_LM_PM := False;
1331 procedure Reset (File : in out File_Type) is
1333 Terminate_Line (File);
1334 FIO.Reset (AP (File)'Unrestricted_Access);
1338 File.Line_Length := 0;
1339 File.Page_Length := 0;
1340 File.Before_LM := False;
1341 File.Before_LM_PM := False;
1350 To : Positive_Count)
1355 -- Raise Constraint_Error if out of range value. The reason for this
1356 -- explicit test is that we don't want junk values around, even if
1357 -- checks are off in the caller.
1359 if not To'Valid then
1360 raise Constraint_Error;
1363 FIO.Check_File_Open (AP (File));
1365 if To = File.Col then
1369 if Mode (File) >= Out_File then
1370 if File.Line_Length /= 0 and then To > File.Line_Length then
1374 if To < File.Col then
1378 while File.Col < To loop
1390 File.Line := File.Line + 1;
1393 elsif ch = PM and then File.Is_Regular_File then
1394 File.Page := File.Page + 1;
1398 elsif To = File.Col then
1403 File.Col := File.Col + 1;
1409 procedure Set_Col (To : Positive_Count) is
1411 Set_Col (Current_Out, To);
1418 procedure Set_Error (File : File_Type) is
1420 FIO.Check_Write_Status (AP (File));
1421 Current_Err := File;
1428 procedure Set_Input (File : File_Type) is
1430 FIO.Check_Read_Status (AP (File));
1440 To : Positive_Count)
1443 -- Raise Constraint_Error if out of range value. The reason for this
1444 -- explicit test is that we don't want junk values around, even if
1445 -- checks are off in the caller.
1447 if not To'Valid then
1448 raise Constraint_Error;
1451 FIO.Check_File_Open (AP (File));
1453 if To = File.Line then
1457 if Mode (File) >= Out_File then
1458 if File.Page_Length /= 0 and then To > File.Page_Length then
1462 if To < File.Line then
1466 while File.Line < To loop
1471 while To /= File.Line loop
1477 procedure Set_Line (To : Positive_Count) is
1479 Set_Line (Current_Out, To);
1482 ---------------------
1483 -- Set_Line_Length --
1484 ---------------------
1486 procedure Set_Line_Length (File : File_Type; To : Count) is
1488 -- Raise Constraint_Error if out of range value. The reason for this
1489 -- explicit test is that we don't want junk values around, even if
1490 -- checks are off in the caller.
1492 if not To'Valid then
1493 raise Constraint_Error;
1496 FIO.Check_Write_Status (AP (File));
1497 File.Line_Length := To;
1498 end Set_Line_Length;
1500 procedure Set_Line_Length (To : Count) is
1502 Set_Line_Length (Current_Out, To);
1503 end Set_Line_Length;
1509 procedure Set_Output (File : File_Type) is
1511 FIO.Check_Write_Status (AP (File));
1512 Current_Out := File;
1515 ---------------------
1516 -- Set_Page_Length --
1517 ---------------------
1519 procedure Set_Page_Length (File : File_Type; To : Count) is
1521 -- Raise Constraint_Error if out of range value. The reason for this
1522 -- explicit test is that we don't want junk values around, even if
1523 -- checks are off in the caller.
1525 if not To'Valid then
1526 raise Constraint_Error;
1529 FIO.Check_Write_Status (AP (File));
1530 File.Page_Length := To;
1531 end Set_Page_Length;
1533 procedure Set_Page_Length (To : Count) is
1535 Set_Page_Length (Current_Out, To);
1536 end Set_Page_Length;
1542 procedure Set_WCEM (File : in out File_Type) is
1547 File.WC_Method := WCEM_Brackets;
1548 FIO.Form_Parameter (File.Form.all, "wcem", Start, Stop);
1551 File.WC_Method := WCEM_Brackets;
1554 if Stop = Start then
1555 for J in WC_Encoding_Letters'Range loop
1556 if File.Form (Start) = WC_Encoding_Letters (J) then
1557 File.WC_Method := J;
1564 raise Use_Error with "invalid WCEM form parameter";
1574 Spacing : Positive_Count := 1)
1579 -- Raise Constraint_Error if out of range value. The reason for this
1580 -- explicit test is that we don't want junk values around, even if
1581 -- checks are off in the caller.
1583 if not Spacing'Valid then
1584 raise Constraint_Error;
1587 FIO.Check_Read_Status (AP (File));
1589 for L in 1 .. Spacing loop
1590 if File.Before_LM then
1591 File.Before_LM := False;
1592 File.Before_LM_PM := False;
1597 -- If at end of file now, then immediately raise End_Error. Note
1598 -- that we can never be positioned between a line mark and a page
1599 -- mark, so if we are at the end of file, we cannot logically be
1600 -- before the implicit page mark that is at the end of the file.
1602 -- For the same reason, we do not need an explicit check for a
1603 -- page mark. If there is a FF in the middle of a line, the file
1604 -- is not in canonical format and we do not care about the page
1605 -- numbers for files other than ones in canonical format.
1611 -- If not at end of file, then loop till we get to an LM or EOF.
1612 -- The latter case happens only in non-canonical files where the
1613 -- last line is not terminated by LM, but we don't want to blow
1614 -- up for such files, so we assume an implicit LM in this case.
1617 exit when ch = LM or else ch = EOF;
1622 -- We have got past a line mark, now, for a regular file only,
1623 -- see if a page mark immediately follows this line mark and
1624 -- if so, skip past the page mark as well. We do not do this
1625 -- for non-regular files, since it would cause an undesirable
1626 -- wait for an additional character.
1629 File.Line := File.Line + 1;
1631 if File.Before_LM_PM then
1632 File.Page := File.Page + 1;
1634 File.Before_LM_PM := False;
1636 elsif File.Is_Regular_File then
1639 -- Page mark can be explicit, or implied at the end of the file
1641 if (ch = PM or else ch = EOF)
1642 and then File.Is_Regular_File
1644 File.Page := File.Page + 1;
1652 File.Before_Wide_Wide_Character := False;
1655 procedure Skip_Line (Spacing : Positive_Count := 1) is
1657 Skip_Line (Current_In, Spacing);
1664 procedure Skip_Page (File : File_Type) is
1668 FIO.Check_Read_Status (AP (File));
1670 -- If at page mark already, just skip it
1672 if File.Before_LM_PM then
1673 File.Before_LM := False;
1674 File.Before_LM_PM := False;
1675 File.Page := File.Page + 1;
1681 -- This is a bit tricky, if we are logically before an LM then
1682 -- it is not an error if we are at an end of file now, since we
1683 -- are not really at it.
1685 if File.Before_LM then
1686 File.Before_LM := False;
1687 File.Before_LM_PM := False;
1690 -- Otherwise we do raise End_Error if we are at the end of file now
1700 -- Now we can just rumble along to the next page mark, or to the
1701 -- end of file, if that comes first. The latter case happens when
1702 -- the page mark is implied at the end of file.
1706 or else (ch = PM and then File.Is_Regular_File);
1710 File.Page := File.Page + 1;
1713 File.Before_Wide_Wide_Character := False;
1716 procedure Skip_Page is
1718 Skip_Page (Current_In);
1721 --------------------
1722 -- Standard_Error --
1723 --------------------
1725 function Standard_Error return File_Type is
1727 return Standard_Err;
1730 function Standard_Error return File_Access is
1732 return Standard_Err'Access;
1735 --------------------
1736 -- Standard_Input --
1737 --------------------
1739 function Standard_Input return File_Type is
1744 function Standard_Input return File_Access is
1746 return Standard_In'Access;
1749 ---------------------
1750 -- Standard_Output --
1751 ---------------------
1753 function Standard_Output return File_Type is
1755 return Standard_Out;
1756 end Standard_Output;
1758 function Standard_Output return File_Access is
1760 return Standard_Out'Access;
1761 end Standard_Output;
1763 --------------------
1764 -- Terminate_Line --
1765 --------------------
1767 procedure Terminate_Line (File : File_Type) is
1769 FIO.Check_File_Open (AP (File));
1771 -- For file other than In_File, test for needing to terminate last line
1773 if Mode (File) /= In_File then
1775 -- If not at start of line definition need new line
1777 if File.Col /= 1 then
1780 -- For files other than standard error and standard output, we
1781 -- make sure that an empty file has a single line feed, so that
1782 -- it is properly formatted. We avoid this for the standard files
1783 -- because it is too much of a nuisance to have these odd line
1784 -- feeds when nothing has been written to the file.
1786 elsif (File /= Standard_Err and then File /= Standard_Out)
1787 and then (File.Line = 1 and then File.Page = 1)
1798 procedure Ungetc (ch : int; File : File_Type) is
1801 if ungetc (ch, File.Stream) = EOF then
1811 -- This is the primitive Stream Write routine, used when a Text_IO file
1812 -- is treated directly as a stream using Text_IO.Streams.Stream.
1815 (File : in out Wide_Wide_Text_AFCB;
1816 Item : Stream_Element_Array)
1818 pragma Warnings (Off, File);
1819 -- Because in this implementation we don't need IN OUT, we only read
1821 Siz : constant size_t := Item'Length;
1824 if File.Mode = FCB.In_File then
1828 -- Now we do the write. Since this is a text file, it is normally in
1829 -- text mode, but stream data must be written in binary mode, so we
1830 -- temporarily set binary mode for the write, resetting it after.
1831 -- These calls have no effect in a system (like Unix) where there is
1832 -- no distinction between text and binary files.
1834 set_binary_mode (fileno (File.Stream));
1836 if fwrite (Item'Address, 1, Siz, File.Stream) /= Siz then
1840 set_text_mode (fileno (File.Stream));
1843 -- Use "preallocated" strings to avoid calling "new" during the
1844 -- elaboration of the run time. This is needed in the tasking case to
1845 -- avoid calling Task_Lock too early. A filename is expected to end with
1846 -- a null character in the runtime, here the null characters are added
1847 -- just to have a correct filename length.
1849 Err_Name : aliased String := "*stderr" & ASCII.NUL;
1850 In_Name : aliased String := "*stdin" & ASCII.NUL;
1851 Out_Name : aliased String := "*stdout" & ASCII.NUL;
1854 -------------------------------
1855 -- Initialize Standard Files --
1856 -------------------------------
1858 for J in WC_Encoding_Method loop
1859 if WC_Encoding = WC_Encoding_Letters (J) then
1864 -- Note: the names in these files are bogus, and probably it would be
1865 -- better for these files to have no names, but the ACVC test insist!
1866 -- We use names that are bound to fail in open etc.
1868 Standard_Err.Stream := stderr;
1869 Standard_Err.Name := Err_Name'Access;
1870 Standard_Err.Form := Null_Str'Unrestricted_Access;
1871 Standard_Err.Mode := FCB.Out_File;
1872 Standard_Err.Is_Regular_File := is_regular_file (fileno (stderr)) /= 0;
1873 Standard_Err.Is_Temporary_File := False;
1874 Standard_Err.Is_System_File := True;
1875 Standard_Err.Is_Text_File := True;
1876 Standard_Err.Access_Method := 'T';
1877 Standard_Err.Self := Standard_Err;
1878 Standard_Err.WC_Method := Default_WCEM;
1880 Standard_In.Stream := stdin;
1881 Standard_In.Name := In_Name'Access;
1882 Standard_In.Form := Null_Str'Unrestricted_Access;
1883 Standard_In.Mode := FCB.In_File;
1884 Standard_In.Is_Regular_File := is_regular_file (fileno (stdin)) /= 0;
1885 Standard_In.Is_Temporary_File := False;
1886 Standard_In.Is_System_File := True;
1887 Standard_In.Is_Text_File := True;
1888 Standard_In.Access_Method := 'T';
1889 Standard_In.Self := Standard_In;
1890 Standard_In.WC_Method := Default_WCEM;
1892 Standard_Out.Stream := stdout;
1893 Standard_Out.Name := Out_Name'Access;
1894 Standard_Out.Form := Null_Str'Unrestricted_Access;
1895 Standard_Out.Mode := FCB.Out_File;
1896 Standard_Out.Is_Regular_File := is_regular_file (fileno (stdout)) /= 0;
1897 Standard_Out.Is_Temporary_File := False;
1898 Standard_Out.Is_System_File := True;
1899 Standard_Out.Is_Text_File := True;
1900 Standard_Out.Access_Method := 'T';
1901 Standard_Out.Self := Standard_Out;
1902 Standard_Out.WC_Method := Default_WCEM;
1904 FIO.Chain_File (AP (Standard_In));
1905 FIO.Chain_File (AP (Standard_Out));
1906 FIO.Chain_File (AP (Standard_Err));
1908 FIO.Make_Unbuffered (AP (Standard_Out));
1909 FIO.Make_Unbuffered (AP (Standard_Err));
1911 end Ada.Wide_Wide_Text_IO;