1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . W I D E _ T E X T _ I O --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
33 ------------------------------------------------------------------------------
35 with Ada.Exceptions; use Ada.Exceptions;
36 with Ada.Streams; use Ada.Streams;
37 with Interfaces.C_Streams; use Interfaces.C_Streams;
41 with System.WCh_Cnv; use System.WCh_Cnv;
42 with System.WCh_Con; use System.WCh_Con;
43 with Unchecked_Conversion;
44 with Unchecked_Deallocation;
46 pragma Elaborate_All (System.File_IO);
47 -- Needed because of calls to Chain_File in package body elaboration
49 package body Ada.Wide_Text_IO is
51 package FIO renames System.File_IO;
53 subtype AP is FCB.AFCB_Ptr;
55 function To_FCB is new Unchecked_Conversion (File_Mode, FCB.File_Mode);
56 function To_TIO is new Unchecked_Conversion (FCB.File_Mode, File_Mode);
57 use type FCB.File_Mode;
59 WC_Encoding : Character;
60 pragma Import (C, WC_Encoding, "__gl_wc_encoding");
62 -----------------------
63 -- Local Subprograms --
64 -----------------------
66 function Getc_Immed (File : in File_Type) return int;
67 -- This routine is identical to Getc, except that the read is done in
68 -- Get_Immediate mode (i.e. without waiting for a line return).
70 function Get_Wide_Char_Immed
73 return Wide_Character;
74 -- This routine is identical to Get_Wide_Char, except that the reads are
75 -- done in Get_Immediate mode (i.e. without waiting for a line return).
77 procedure Set_WCEM (File : in out File_Type);
78 -- Called by Open and Create to set the wide character encoding method
79 -- for the file, processing a WCEM form parameter if one is present.
80 -- File is IN OUT because it may be closed in case of an error.
86 function AFCB_Allocate
87 (Control_Block : Wide_Text_AFCB)
90 pragma Warnings (Off, Control_Block);
93 return new Wide_Text_AFCB;
100 procedure AFCB_Close (File : access Wide_Text_AFCB) is
102 -- If the file being closed is one of the current files, then close
103 -- the corresponding current file. It is not clear that this action
104 -- is required (RM A.10.3(23)) but it seems reasonable, and besides
105 -- ACVC test CE3208A expects this behavior.
107 if File_Type (File) = Current_In then
109 elsif File_Type (File) = Current_Out then
111 elsif File_Type (File) = Current_Err then
115 Terminate_Line (File_Type (File));
122 procedure AFCB_Free (File : access Wide_Text_AFCB) is
123 type FCB_Ptr is access all Wide_Text_AFCB;
124 FT : FCB_Ptr := FCB_Ptr (File);
126 procedure Free is new Unchecked_Deallocation (Wide_Text_AFCB, FCB_Ptr);
136 procedure Close (File : in out File_Type) is
138 FIO.Close (AP (File));
145 -- Note: we assume that it is impossible in practice for the column
146 -- to exceed the value of Count'Last, i.e. no check is required for
147 -- overflow raising layout error.
149 function Col (File : in File_Type) return Positive_Count is
151 FIO.Check_File_Open (AP (File));
155 function Col return Positive_Count is
157 return Col (Current_Out);
165 (File : in out File_Type;
166 Mode : in File_Mode := Out_File;
167 Name : in String := "";
168 Form : in String := "")
170 File_Control_Block : Wide_Text_AFCB;
173 FIO.Open (File_Ptr => AP (File),
174 Dummy_FCB => File_Control_Block,
175 Mode => To_FCB (Mode),
188 function Current_Error return File_Type is
193 function Current_Error return File_Access is
195 return Current_Err'Access;
202 function Current_Input return File_Type is
207 function Current_Input return File_Access is
209 return Current_In'Access;
216 function Current_Output return File_Type is
221 function Current_Output return File_Access is
223 return Current_Out'Access;
230 procedure Delete (File : in out File_Type) is
232 FIO.Delete (AP (File));
239 function End_Of_File (File : in File_Type) return Boolean is
243 FIO.Check_Read_Status (AP (File));
245 if File.Before_Wide_Character then
248 elsif File.Before_LM then
250 if File.Before_LM_PM then
251 return Nextc (File) = EOF;
265 File.Before_LM := True;
269 -- Here we are just past the line mark with Before_LM set so that we
270 -- do not have to try to back up past the LM, thus avoiding the need
271 -- to back up more than one character.
278 elsif ch = PM and then File.Is_Regular_File then
279 File.Before_LM_PM := True;
280 return Nextc (File) = EOF;
282 -- Here if neither EOF nor PM followed end of line
291 function End_Of_File return Boolean is
293 return End_Of_File (Current_In);
300 function End_Of_Line (File : in File_Type) return Boolean is
304 FIO.Check_Read_Status (AP (File));
306 if File.Before_Wide_Character then
309 elsif File.Before_LM then
325 function End_Of_Line return Boolean is
327 return End_Of_Line (Current_In);
334 function End_Of_Page (File : in File_Type) return Boolean is
338 FIO.Check_Read_Status (AP (File));
340 if not File.Is_Regular_File then
343 elsif File.Before_Wide_Character then
346 elsif File.Before_LM then
347 if File.Before_LM_PM then
362 File.Before_LM := True;
366 -- Here we are just past the line mark with Before_LM set so that we
367 -- do not have to try to back up past the LM, thus avoiding the need
368 -- to back up more than one character.
372 return ch = PM or else ch = EOF;
375 function End_Of_Page return Boolean is
377 return End_Of_Page (Current_In);
384 procedure Flush (File : in File_Type) is
386 FIO.Flush (AP (File));
398 function Form (File : in File_Type) return String is
400 return FIO.Form (AP (File));
408 (File : in File_Type;
409 Item : out Wide_Character)
414 FIO.Check_Read_Status (AP (File));
416 if File.Before_Wide_Character then
417 File.Before_Wide_Character := False;
418 Item := File.Saved_Wide_Character;
421 Get_Character (File, C);
422 Item := Get_Wide_Char (C, File);
426 procedure Get (Item : out Wide_Character) is
428 Get (Current_In, Item);
432 (File : in File_Type;
433 Item : out Wide_String)
436 for J in Item'Range loop
437 Get (File, Item (J));
441 procedure Get (Item : out Wide_String) is
443 Get (Current_In, Item);
450 procedure Get_Character
451 (File : in File_Type;
452 Item : out Character)
457 if File.Before_LM then
458 File.Before_LM := False;
459 File.Before_LM_PM := False;
462 if File.Before_LM_PM then
464 File.Page := File.Page + 1;
465 File.Before_LM_PM := False;
468 File.Line := File.Line + 1;
479 File.Line := File.Line + 1;
482 elsif ch = PM and then File.Is_Regular_File then
483 File.Page := File.Page + 1;
487 Item := Character'Val (ch);
488 File.Col := File.Col + 1;
498 procedure Get_Immediate
499 (File : in File_Type;
500 Item : out Wide_Character)
505 FIO.Check_Read_Status (AP (File));
507 if File.Before_Wide_Character then
508 File.Before_Wide_Character := False;
509 Item := File.Saved_Wide_Character;
511 elsif File.Before_LM then
512 File.Before_LM := False;
513 File.Before_LM_PM := False;
514 Item := Wide_Character'Val (LM);
517 ch := Getc_Immed (File);
522 Item := Get_Wide_Char_Immed (Character'Val (ch), File);
527 procedure Get_Immediate
528 (Item : out Wide_Character)
531 Get_Immediate (Current_In, Item);
534 procedure Get_Immediate
535 (File : in File_Type;
536 Item : out Wide_Character;
537 Available : out Boolean)
542 FIO.Check_Read_Status (AP (File));
545 if File.Before_Wide_Character then
546 File.Before_Wide_Character := False;
547 Item := File.Saved_Wide_Character;
549 elsif File.Before_LM then
550 File.Before_LM := False;
551 File.Before_LM_PM := False;
552 Item := Wide_Character'Val (LM);
555 ch := Getc_Immed (File);
560 Item := Get_Wide_Char_Immed (Character'Val (ch), File);
565 procedure Get_Immediate
566 (Item : out Wide_Character;
567 Available : out Boolean)
570 Get_Immediate (Current_In, Item, Available);
578 (File : in File_Type;
579 Item : out Wide_String;
583 FIO.Check_Read_Status (AP (File));
584 Last := Item'First - 1;
586 -- Immediate exit for null string, this is a case in which we do not
587 -- need to test for end of file and we do not skip a line mark under
588 -- any circumstances.
590 if Last >= Item'Last then
594 -- Here we have at least one character, if we are immediately before
595 -- a line mark, then we will just skip past it storing no characters.
597 if File.Before_LM then
598 File.Before_LM := False;
599 File.Before_LM_PM := False;
601 -- Otherwise we need to read some characters
604 -- If we are at the end of file now, it means we are trying to
605 -- skip a file terminator and we raise End_Error (RM A.10.7(20))
607 if Nextc (File) = EOF then
611 -- Loop through characters in string
614 -- Exit the loop if read is terminated by encountering line mark
615 -- Note that the use of Skip_Line here ensures we properly deal
616 -- with setting the page and line numbers.
618 if End_Of_Line (File) then
623 -- Otherwise store the character, note that we know that ch is
624 -- something other than LM or EOF. It could possibly be a page
625 -- mark if there is a stray page mark in the middle of a line,
626 -- but this is not an official page mark in any case, since
627 -- official page marks can only follow a line mark. The whole
628 -- page business is pretty much nonsense anyway, so we do not
629 -- want to waste time trying to make sense out of non-standard
630 -- page marks in the file! This means that the behavior of
631 -- Get_Line is different from repeated Get of a character, but
632 -- that's too bad. We only promise that page numbers etc make
633 -- sense if the file is formatted in a standard manner.
635 -- Note: we do not adjust the column number because it is quicker
636 -- to adjust it once at the end of the operation than incrementing
637 -- it each time around the loop.
640 Get (File, Item (Last));
642 -- All done if the string is full, this is the case in which
643 -- we do not skip the following line mark. We need to adjust
644 -- the column number in this case.
646 if Last = Item'Last then
647 File.Col := File.Col + Count (Item'Length);
651 -- Exit from the loop if we are at the end of file. This happens
652 -- if we have a last line that is not terminated with a line mark.
653 -- In this case we consider that there is an implied line mark;
654 -- this is a non-standard file, but we will treat it nicely.
656 exit when Nextc (File) = EOF;
662 (Item : out Wide_String;
666 Get_Line (Current_In, Item, Last);
673 function Get_Wide_Char
676 return Wide_Character
678 function In_Char return Character;
679 -- Function used to obtain additional characters it the wide character
680 -- sequence is more than one character long.
682 function In_Char return Character is
683 ch : constant Integer := Getc (File);
689 return Character'Val (ch);
693 function WC_In is new Char_Sequence_To_Wide_Char (In_Char);
696 return WC_In (C, File.WC_Method);
699 -------------------------
700 -- Get_Wide_Char_Immed --
701 -------------------------
703 function Get_Wide_Char_Immed
706 return Wide_Character
708 function In_Char return Character;
709 -- Function used to obtain additional characters it the wide character
710 -- sequence is more than one character long.
712 function In_Char return Character is
713 ch : constant Integer := Getc_Immed (File);
719 return Character'Val (ch);
723 function WC_In is new Char_Sequence_To_Wide_Char (In_Char);
726 return WC_In (C, File.WC_Method);
727 end Get_Wide_Char_Immed;
733 function Getc (File : File_Type) return int is
737 ch := fgetc (File.Stream);
739 if ch = EOF and then ferror (File.Stream) /= 0 then
750 function Getc_Immed (File : in File_Type) return int is
754 procedure getc_immediate
755 (stream : FILEs; ch : out int; end_of_file : out int);
756 pragma Import (C, getc_immediate, "getc_immediate");
759 FIO.Check_Read_Status (AP (File));
761 if File.Before_LM then
762 File.Before_LM := False;
763 File.Before_LM_PM := False;
767 getc_immediate (File.Stream, ch, end_of_file);
769 if ferror (File.Stream) /= 0 then
771 elsif end_of_file /= 0 then
783 function Is_Open (File : in File_Type) return Boolean is
785 return FIO.Is_Open (AP (File));
792 -- Note: we assume that it is impossible in practice for the line
793 -- to exceed the value of Count'Last, i.e. no check is required for
794 -- overflow raising layout error.
796 function Line (File : in File_Type) return Positive_Count is
798 FIO.Check_File_Open (AP (File));
802 function Line return Positive_Count is
804 return Line (Current_Out);
811 function Line_Length (File : in File_Type) return Count is
813 FIO.Check_Write_Status (AP (File));
814 return File.Line_Length;
817 function Line_Length return Count is
819 return Line_Length (Current_Out);
827 (File : in File_Type;
828 Item : out Wide_Character;
829 End_Of_Line : out Boolean)
833 -- Start of processing for Look_Ahead
836 FIO.Check_Read_Status (AP (File));
838 -- If we are logically before a line mark, we can return immediately
840 if File.Before_LM then
842 Item := Wide_Character'Val (0);
844 -- If we are before a wide character, just return it (this happens
845 -- if there are two calls to Look_Ahead in a row).
847 elsif File.Before_Wide_Character then
848 End_Of_Line := False;
849 Item := File.Saved_Wide_Character;
851 -- otherwise we must read a character from the input stream
858 or else (ch = EOF and then File.Is_Regular_File)
862 Item := Wide_Character'Val (0);
864 -- If the character is in the range 16#0000# to 16#007F# it stands
865 -- for itself and occupies a single byte, so we can unget it with
868 elsif ch <= 16#0080# then
869 End_Of_Line := False;
871 Item := Wide_Character'Val (ch);
873 -- For a character above this range, we read the character, using
874 -- the Get_Wide_Char routine. It may well occupy more than one byte
875 -- so we can't put it back with ungetc. Instead we save it in the
876 -- control block, setting a flag that everyone interested in reading
877 -- characters must test before reading the stream.
880 Item := Get_Wide_Char (Character'Val (ch), File);
881 End_Of_Line := False;
882 File.Saved_Wide_Character := Item;
883 File.Before_Wide_Character := True;
889 (Item : out Wide_Character;
890 End_Of_Line : out Boolean)
893 Look_Ahead (Current_In, Item, End_Of_Line);
900 function Mode (File : in File_Type) return File_Mode is
902 return To_TIO (FIO.Mode (AP (File)));
909 function Name (File : in File_Type) return String is
911 return FIO.Name (AP (File));
919 (File : in File_Type;
920 Spacing : in Positive_Count := 1)
923 -- Raise Constraint_Error if out of range value. The reason for this
924 -- explicit test is that we don't want junk values around, even if
925 -- checks are off in the caller.
927 if Spacing not in Positive_Count then
928 raise Constraint_Error;
931 FIO.Check_Write_Status (AP (File));
933 for K in 1 .. Spacing loop
935 File.Line := File.Line + 1;
937 if File.Page_Length /= 0
938 and then File.Line > File.Page_Length
942 File.Page := File.Page + 1;
949 procedure New_Line (Spacing : in Positive_Count := 1) is
951 New_Line (Current_Out, Spacing);
958 procedure New_Page (File : in File_Type) is
960 FIO.Check_Write_Status (AP (File));
962 if File.Col /= 1 or else File.Line = 1 then
967 File.Page := File.Page + 1;
972 procedure New_Page is
974 New_Page (Current_Out);
981 function Nextc (File : File_Type) return int is
985 ch := fgetc (File.Stream);
988 if ferror (File.Stream) /= 0 then
993 if ungetc (ch, File.Stream) = EOF then
1006 (File : in out File_Type;
1007 Mode : in File_Mode;
1009 Form : in String := "")
1011 File_Control_Block : Wide_Text_AFCB;
1014 FIO.Open (File_Ptr => AP (File),
1015 Dummy_FCB => File_Control_Block,
1016 Mode => To_FCB (Mode),
1029 -- Note: we assume that it is impossible in practice for the page
1030 -- to exceed the value of Count'Last, i.e. no check is required for
1031 -- overflow raising layout error.
1033 function Page (File : in File_Type) return Positive_Count is
1035 FIO.Check_File_Open (AP (File));
1039 function Page return Positive_Count is
1041 return Page (Current_Out);
1048 function Page_Length (File : in File_Type) return Count is
1050 FIO.Check_Write_Status (AP (File));
1051 return File.Page_Length;
1054 function Page_Length return Count is
1056 return Page_Length (Current_Out);
1064 (File : in File_Type;
1065 Item : in Wide_Character)
1067 procedure Out_Char (C : Character);
1068 -- Procedure to output one character of a wide character sequence
1070 procedure Out_Char (C : Character) is
1072 Putc (Character'Pos (C), File);
1075 procedure WC_Out is new Wide_Char_To_Char_Sequence (Out_Char);
1078 WC_Out (Item, File.WC_Method);
1079 File.Col := File.Col + 1;
1082 procedure Put (Item : in Wide_Character) is
1084 Put (Current_Out, Item);
1092 (File : in File_Type;
1093 Item : in Wide_String)
1096 for J in Item'Range loop
1097 Put (File, Item (J));
1101 procedure Put (Item : in Wide_String) is
1103 Put (Current_Out, Item);
1111 (File : in File_Type;
1112 Item : in Wide_String)
1119 procedure Put_Line (Item : in Wide_String) is
1121 Put (Current_Out, Item);
1122 New_Line (Current_Out);
1129 procedure Putc (ch : int; File : File_Type) is
1131 if fputc (ch, File.Stream) = EOF then
1140 -- This is the primitive Stream Read routine, used when a Text_IO file
1141 -- is treated directly as a stream using Text_IO.Streams.Stream.
1144 (File : in out Wide_Text_AFCB;
1145 Item : out Stream_Element_Array;
1146 Last : out Stream_Element_Offset)
1151 -- Need to deal with Before_Wide_Character ???
1153 if File.Mode /= FCB.In_File then
1157 -- Deal with case where our logical and physical position do not match
1158 -- because of being after an LM or LM-PM sequence when in fact we are
1159 -- logically positioned before it.
1161 if File.Before_LM then
1163 -- If we are before a PM, then it is possible for a stream read
1164 -- to leave us after the LM and before the PM, which is a bit
1165 -- odd. The easiest way to deal with this is to unget the PM,
1166 -- so we are indeed positioned between the characters. This way
1167 -- further stream read operations will work correctly, and the
1168 -- effect on text processing is a little weird, but what can
1169 -- be expected if stream and text input are mixed this way?
1171 if File.Before_LM_PM then
1172 ch := ungetc (PM, File.Stream);
1173 File.Before_LM_PM := False;
1176 File.Before_LM := False;
1178 Item (Item'First) := Stream_Element (Character'Pos (ASCII.LF));
1180 if Item'Length = 1 then
1186 Stream_Element_Offset
1187 (fread (buffer => Item'Address,
1188 index => size_t (Item'First + 1),
1190 count => Item'Length - 1,
1191 stream => File.Stream));
1197 -- Now we do the read. Since this is a text file, it is normally in
1198 -- text mode, but stream data must be read in binary mode, so we
1199 -- temporarily set binary mode for the read, resetting it after.
1200 -- These calls have no effect in a system (like Unix) where there is
1201 -- no distinction between text and binary files.
1203 set_binary_mode (fileno (File.Stream));
1207 Stream_Element_Offset
1208 (fread (Item'Address, 1, Item'Length, File.Stream)) - 1;
1210 if Last < Item'Last then
1211 if ferror (File.Stream) /= 0 then
1216 set_text_mode (fileno (File.Stream));
1224 (File : in out File_Type;
1225 Mode : in File_Mode)
1228 -- Don't allow change of mode for current file (RM A.10.2(5))
1230 if (File = Current_In or else
1231 File = Current_Out or else
1232 File = Current_Error)
1233 and then To_FCB (Mode) /= File.Mode
1238 Terminate_Line (File);
1239 FIO.Reset (AP (File), To_FCB (Mode));
1243 File.Line_Length := 0;
1244 File.Page_Length := 0;
1245 File.Before_LM := False;
1246 File.Before_LM_PM := False;
1249 procedure Reset (File : in out File_Type) is
1251 Terminate_Line (File);
1252 FIO.Reset (AP (File));
1256 File.Line_Length := 0;
1257 File.Page_Length := 0;
1258 File.Before_LM := False;
1259 File.Before_LM_PM := False;
1267 (File : in File_Type;
1268 To : in Positive_Count)
1273 -- Raise Constraint_Error if out of range value. The reason for this
1274 -- explicit test is that we don't want junk values around, even if
1275 -- checks are off in the caller.
1277 if To not in Positive_Count then
1278 raise Constraint_Error;
1281 FIO.Check_File_Open (AP (File));
1283 if To = File.Col then
1287 if Mode (File) >= Out_File then
1288 if File.Line_Length /= 0 and then To > File.Line_Length then
1292 if To < File.Col then
1296 while File.Col < To loop
1308 File.Line := File.Line + 1;
1311 elsif ch = PM and then File.Is_Regular_File then
1312 File.Page := File.Page + 1;
1316 elsif To = File.Col then
1321 File.Col := File.Col + 1;
1327 procedure Set_Col (To : in Positive_Count) is
1329 Set_Col (Current_Out, To);
1336 procedure Set_Error (File : in File_Type) is
1338 FIO.Check_Write_Status (AP (File));
1339 Current_Err := File;
1346 procedure Set_Input (File : in File_Type) is
1348 FIO.Check_Read_Status (AP (File));
1357 (File : in File_Type;
1358 To : in Positive_Count)
1361 -- Raise Constraint_Error if out of range value. The reason for this
1362 -- explicit test is that we don't want junk values around, even if
1363 -- checks are off in the caller.
1365 if To not in Positive_Count then
1366 raise Constraint_Error;
1369 FIO.Check_File_Open (AP (File));
1371 if To = File.Line then
1375 if Mode (File) >= Out_File then
1376 if File.Page_Length /= 0 and then To > File.Page_Length then
1380 if To < File.Line then
1384 while File.Line < To loop
1389 while To /= File.Line loop
1395 procedure Set_Line (To : in Positive_Count) is
1397 Set_Line (Current_Out, To);
1400 ---------------------
1401 -- Set_Line_Length --
1402 ---------------------
1404 procedure Set_Line_Length (File : in File_Type; To : in Count) is
1406 -- Raise Constraint_Error if out of range value. The reason for this
1407 -- explicit test is that we don't want junk values around, even if
1408 -- checks are off in the caller.
1410 if To not in Count then
1411 raise Constraint_Error;
1414 FIO.Check_Write_Status (AP (File));
1415 File.Line_Length := To;
1416 end Set_Line_Length;
1418 procedure Set_Line_Length (To : in Count) is
1420 Set_Line_Length (Current_Out, To);
1421 end Set_Line_Length;
1427 procedure Set_Output (File : in File_Type) is
1429 FIO.Check_Write_Status (AP (File));
1430 Current_Out := File;
1433 ---------------------
1434 -- Set_Page_Length --
1435 ---------------------
1437 procedure Set_Page_Length (File : in File_Type; To : in Count) is
1439 -- Raise Constraint_Error if out of range value. The reason for this
1440 -- explicit test is that we don't want junk values around, even if
1441 -- checks are off in the caller.
1443 if To not in Count then
1444 raise Constraint_Error;
1447 FIO.Check_Write_Status (AP (File));
1448 File.Page_Length := To;
1449 end Set_Page_Length;
1451 procedure Set_Page_Length (To : in Count) is
1453 Set_Page_Length (Current_Out, To);
1454 end Set_Page_Length;
1460 procedure Set_WCEM (File : in out File_Type) is
1465 File.WC_Method := WCEM_Brackets;
1466 FIO.Form_Parameter (File.Form.all, "wcem", Start, Stop);
1469 File.WC_Method := WCEM_Brackets;
1471 elsif Start /= 0 then
1472 if Stop = Start then
1473 for J in WC_Encoding_Letters'Range loop
1474 if File.Form (Start) = WC_Encoding_Letters (J) then
1475 File.WC_Method := J;
1482 Raise_Exception (Use_Error'Identity, "invalid WCEM form parameter");
1491 (File : in File_Type;
1492 Spacing : in Positive_Count := 1)
1497 -- Raise Constraint_Error if out of range value. The reason for this
1498 -- explicit test is that we don't want junk values around, even if
1499 -- checks are off in the caller.
1501 if Spacing not in Positive_Count then
1502 raise Constraint_Error;
1505 FIO.Check_Read_Status (AP (File));
1507 for L in 1 .. Spacing loop
1508 if File.Before_LM then
1509 File.Before_LM := False;
1510 File.Before_LM_PM := False;
1515 -- If at end of file now, then immediately raise End_Error. Note
1516 -- that we can never be positioned between a line mark and a page
1517 -- mark, so if we are at the end of file, we cannot logically be
1518 -- before the implicit page mark that is at the end of the file.
1520 -- For the same reason, we do not need an explicit check for a
1521 -- page mark. If there is a FF in the middle of a line, the file
1522 -- is not in canonical format and we do not care about the page
1523 -- numbers for files other than ones in canonical format.
1529 -- If not at end of file, then loop till we get to an LM or EOF.
1530 -- The latter case happens only in non-canonical files where the
1531 -- last line is not terminated by LM, but we don't want to blow
1532 -- up for such files, so we assume an implicit LM in this case.
1535 exit when ch = LM or ch = EOF;
1540 -- We have got past a line mark, now, for a regular file only,
1541 -- see if a page mark immediately follows this line mark and
1542 -- if so, skip past the page mark as well. We do not do this
1543 -- for non-regular files, since it would cause an undesirable
1544 -- wait for an additional character.
1547 File.Line := File.Line + 1;
1549 if File.Before_LM_PM then
1550 File.Page := File.Page + 1;
1552 File.Before_LM_PM := False;
1554 elsif File.Is_Regular_File then
1557 -- Page mark can be explicit, or implied at the end of the file
1559 if (ch = PM or else ch = EOF)
1560 and then File.Is_Regular_File
1562 File.Page := File.Page + 1;
1571 File.Before_Wide_Character := False;
1574 procedure Skip_Line (Spacing : in Positive_Count := 1) is
1576 Skip_Line (Current_In, Spacing);
1583 procedure Skip_Page (File : in File_Type) is
1587 FIO.Check_Read_Status (AP (File));
1589 -- If at page mark already, just skip it
1591 if File.Before_LM_PM then
1592 File.Before_LM := False;
1593 File.Before_LM_PM := False;
1594 File.Page := File.Page + 1;
1600 -- This is a bit tricky, if we are logically before an LM then
1601 -- it is not an error if we are at an end of file now, since we
1602 -- are not really at it.
1604 if File.Before_LM then
1605 File.Before_LM := False;
1606 File.Before_LM_PM := False;
1609 -- Otherwise we do raise End_Error if we are at the end of file now
1619 -- Now we can just rumble along to the next page mark, or to the
1620 -- end of file, if that comes first. The latter case happens when
1621 -- the page mark is implied at the end of file.
1625 or else (ch = PM and then File.Is_Regular_File);
1629 File.Page := File.Page + 1;
1632 File.Before_Wide_Character := False;
1635 procedure Skip_Page is
1637 Skip_Page (Current_In);
1640 --------------------
1641 -- Standard_Error --
1642 --------------------
1644 function Standard_Error return File_Type is
1646 return Standard_Err;
1649 function Standard_Error return File_Access is
1651 return Standard_Err'Access;
1654 --------------------
1655 -- Standard_Input --
1656 --------------------
1658 function Standard_Input return File_Type is
1663 function Standard_Input return File_Access is
1665 return Standard_In'Access;
1668 ---------------------
1669 -- Standard_Output --
1670 ---------------------
1672 function Standard_Output return File_Type is
1674 return Standard_Out;
1675 end Standard_Output;
1677 function Standard_Output return File_Access is
1679 return Standard_Out'Access;
1680 end Standard_Output;
1682 --------------------
1683 -- Terminate_Line --
1684 --------------------
1686 procedure Terminate_Line (File : File_Type) is
1688 FIO.Check_File_Open (AP (File));
1690 -- For file other than In_File, test for needing to terminate last line
1692 if Mode (File) /= In_File then
1694 -- If not at start of line definition need new line
1696 if File.Col /= 1 then
1699 -- For files other than standard error and standard output, we
1700 -- make sure that an empty file has a single line feed, so that
1701 -- it is properly formatted. We avoid this for the standard files
1702 -- because it is too much of a nuisance to have these odd line
1703 -- feeds when nothing has been written to the file.
1705 elsif (File /= Standard_Err and then File /= Standard_Out)
1706 and then (File.Line = 1 and then File.Page = 1)
1717 procedure Ungetc (ch : int; File : File_Type) is
1720 if ungetc (ch, File.Stream) = EOF then
1730 -- This is the primitive Stream Write routine, used when a Text_IO file
1731 -- is treated directly as a stream using Text_IO.Streams.Stream.
1734 (File : in out Wide_Text_AFCB;
1735 Item : in Stream_Element_Array)
1737 Siz : constant size_t := Item'Length;
1740 if File.Mode = FCB.In_File then
1744 -- Now we do the write. Since this is a text file, it is normally in
1745 -- text mode, but stream data must be written in binary mode, so we
1746 -- temporarily set binary mode for the write, resetting it after.
1747 -- These calls have no effect in a system (like Unix) where there is
1748 -- no distinction between text and binary files.
1750 set_binary_mode (fileno (File.Stream));
1752 if fwrite (Item'Address, 1, Siz, File.Stream) /= Siz then
1756 set_text_mode (fileno (File.Stream));
1759 -- Use "preallocated" strings to avoid calling "new" during the
1760 -- elaboration of the run time. This is needed in the tasking case to
1761 -- avoid calling Task_Lock too early. A filename is expected to end with
1762 -- a null character in the runtime, here the null characters are added
1763 -- just to have a correct filename length.
1765 Err_Name : aliased String := "*stderr" & ASCII.Nul;
1766 In_Name : aliased String := "*stdin" & ASCII.Nul;
1767 Out_Name : aliased String := "*stdout" & ASCII.Nul;
1770 -------------------------------
1771 -- Initialize Standard Files --
1772 -------------------------------
1774 for J in WC_Encoding_Method loop
1775 if WC_Encoding = WC_Encoding_Letters (J) then
1780 -- Note: the names in these files are bogus, and probably it would be
1781 -- better for these files to have no names, but the ACVC test insist!
1782 -- We use names that are bound to fail in open etc.
1784 Standard_Err.Stream := stderr;
1785 Standard_Err.Name := Err_Name'Access;
1786 Standard_Err.Form := Null_Str'Unrestricted_Access;
1787 Standard_Err.Mode := FCB.Out_File;
1788 Standard_Err.Is_Regular_File := is_regular_file (fileno (stderr)) /= 0;
1789 Standard_Err.Is_Temporary_File := False;
1790 Standard_Err.Is_System_File := True;
1791 Standard_Err.Is_Text_File := True;
1792 Standard_Err.Access_Method := 'T';
1793 Standard_Err.WC_Method := Default_WCEM;
1795 Standard_In.Stream := stdin;
1796 Standard_In.Name := In_Name'Access;
1797 Standard_In.Form := Null_Str'Unrestricted_Access;
1798 Standard_In.Mode := FCB.In_File;
1799 Standard_In.Is_Regular_File := is_regular_file (fileno (stdin)) /= 0;
1800 Standard_In.Is_Temporary_File := False;
1801 Standard_In.Is_System_File := True;
1802 Standard_In.Is_Text_File := True;
1803 Standard_In.Access_Method := 'T';
1804 Standard_In.WC_Method := Default_WCEM;
1806 Standard_Out.Stream := stdout;
1807 Standard_Out.Name := Out_Name'Access;
1808 Standard_Out.Form := Null_Str'Unrestricted_Access;
1809 Standard_Out.Mode := FCB.Out_File;
1810 Standard_Out.Is_Regular_File := is_regular_file (fileno (stdout)) /= 0;
1811 Standard_Out.Is_Temporary_File := False;
1812 Standard_Out.Is_System_File := True;
1813 Standard_Out.Is_Text_File := True;
1814 Standard_Out.Access_Method := 'T';
1815 Standard_Out.WC_Method := Default_WCEM;
1817 FIO.Chain_File (AP (Standard_In));
1818 FIO.Chain_File (AP (Standard_Out));
1819 FIO.Chain_File (AP (Standard_Err));
1821 FIO.Make_Unbuffered (AP (Standard_Out));
1822 FIO.Make_Unbuffered (AP (Standard_Err));
1824 end Ada.Wide_Text_IO;