2009-07-17 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / ada / a-textio.adb
blob4ea003dcda8f55f6392fa10d7a110d1892abb40d
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- A D A . T E X T _ I O --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
10 -- --
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. --
17 -- --
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. --
21 -- --
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/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 with Ada.Streams; use Ada.Streams;
33 with Interfaces.C_Streams; use Interfaces.C_Streams;
35 with System.File_IO;
36 with System.CRTL;
37 with System.WCh_Cnv; use System.WCh_Cnv;
38 with System.WCh_Con; use System.WCh_Con;
40 with Ada.Unchecked_Conversion;
41 with Ada.Unchecked_Deallocation;
43 pragma Elaborate_All (System.File_IO);
44 -- Needed because of calls to Chain_File in package body elaboration
46 package body Ada.Text_IO is
48 package FIO renames System.File_IO;
50 subtype AP is FCB.AFCB_Ptr;
52 function To_FCB is new Ada.Unchecked_Conversion (File_Mode, FCB.File_Mode);
53 function To_TIO is new Ada.Unchecked_Conversion (FCB.File_Mode, File_Mode);
54 use type FCB.File_Mode;
56 use type System.CRTL.size_t;
58 WC_Encoding : Character;
59 pragma Import (C, WC_Encoding, "__gl_wc_encoding");
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_Upper_Half_Char
70 (C : Character;
71 File : File_Type) return Character;
72 -- This function is shared by Get and Get_Immediate to extract an encoded
73 -- upper half character value from the given File. The first byte has
74 -- already been read and is passed in C. The character value is returned as
75 -- the result, and the file pointer is bumped past the character.
76 -- Constraint_Error is raised if the encoded value is outside the bounds of
77 -- type Character.
79 function Get_Upper_Half_Char_Immed
80 (C : Character;
81 File : File_Type) return Character;
82 -- This routine is identical to Get_Upper_Half_Char, except that the reads
83 -- are done in Get_Immediate mode (i.e. without waiting for a line return).
85 function Has_Upper_Half_Character (Item : String) return Boolean;
86 -- Returns True if any of the characters is in the range 16#80#-16#FF#
88 procedure Put_Encoded (File : File_Type; Char : Character);
89 -- Called to output a character Char to the given File, when the encoding
90 -- method for the file is other than brackets, and Char is upper half.
92 procedure Set_WCEM (File : in out File_Type);
93 -- Called by Open and Create to set the wide character encoding method for
94 -- the file, processing a WCEM form parameter if one is present. File is
95 -- IN OUT because it may be closed in case of an error.
97 -------------------
98 -- AFCB_Allocate --
99 -------------------
101 function AFCB_Allocate (Control_Block : Text_AFCB) return FCB.AFCB_Ptr is
102 pragma Unreferenced (Control_Block);
103 begin
104 return new Text_AFCB;
105 end AFCB_Allocate;
107 ----------------
108 -- AFCB_Close --
109 ----------------
111 procedure AFCB_Close (File : not null access Text_AFCB) is
112 begin
113 -- If the file being closed is one of the current files, then close
114 -- the corresponding current file. It is not clear that this action
115 -- is required (RM A.10.3(23)) but it seems reasonable, and besides
116 -- ACVC test CE3208A expects this behavior.
118 if File_Type (File) = Current_In then
119 Current_In := null;
120 elsif File_Type (File) = Current_Out then
121 Current_Out := null;
122 elsif File_Type (File) = Current_Err then
123 Current_Err := null;
124 end if;
126 Terminate_Line (File_Type (File));
127 end AFCB_Close;
129 ---------------
130 -- AFCB_Free --
131 ---------------
133 procedure AFCB_Free (File : not null access Text_AFCB) is
134 type FCB_Ptr is access all Text_AFCB;
135 FT : FCB_Ptr := FCB_Ptr (File);
137 procedure Free is new Ada.Unchecked_Deallocation (Text_AFCB, FCB_Ptr);
139 begin
140 Free (FT);
141 end AFCB_Free;
143 -----------
144 -- Close --
145 -----------
147 procedure Close (File : in out File_Type) is
148 begin
149 FIO.Close (AP (File)'Unrestricted_Access);
150 end Close;
152 ---------
153 -- Col --
154 ---------
156 -- Note: we assume that it is impossible in practice for the column
157 -- to exceed the value of Count'Last, i.e. no check is required for
158 -- overflow raising layout error.
160 function Col (File : File_Type) return Positive_Count is
161 begin
162 FIO.Check_File_Open (AP (File));
163 return File.Col;
164 end Col;
166 function Col return Positive_Count is
167 begin
168 return Col (Current_Out);
169 end Col;
171 ------------
172 -- Create --
173 ------------
175 procedure Create
176 (File : in out File_Type;
177 Mode : File_Mode := Out_File;
178 Name : String := "";
179 Form : String := "")
181 Dummy_File_Control_Block : Text_AFCB;
182 pragma Warnings (Off, Dummy_File_Control_Block);
183 -- Yes, we know this is never assigned a value, only the tag
184 -- is used for dispatching purposes, so that's expected.
186 begin
187 FIO.Open (File_Ptr => AP (File),
188 Dummy_FCB => Dummy_File_Control_Block,
189 Mode => To_FCB (Mode),
190 Name => Name,
191 Form => Form,
192 Amethod => 'T',
193 Creat => True,
194 Text => True);
196 File.Self := File;
197 Set_WCEM (File);
198 end Create;
200 -------------------
201 -- Current_Error --
202 -------------------
204 function Current_Error return File_Type is
205 begin
206 return Current_Err;
207 end Current_Error;
209 function Current_Error return File_Access is
210 begin
211 return Current_Err.Self'Access;
212 end Current_Error;
214 -------------------
215 -- Current_Input --
216 -------------------
218 function Current_Input return File_Type is
219 begin
220 return Current_In;
221 end Current_Input;
223 function Current_Input return File_Access is
224 begin
225 return Current_In.Self'Access;
226 end Current_Input;
228 --------------------
229 -- Current_Output --
230 --------------------
232 function Current_Output return File_Type is
233 begin
234 return Current_Out;
235 end Current_Output;
237 function Current_Output return File_Access is
238 begin
239 return Current_Out.Self'Access;
240 end Current_Output;
242 ------------
243 -- Delete --
244 ------------
246 procedure Delete (File : in out File_Type) is
247 begin
248 FIO.Delete (AP (File)'Unrestricted_Access);
249 end Delete;
251 -----------------
252 -- End_Of_File --
253 -----------------
255 function End_Of_File (File : File_Type) return Boolean is
256 ch : int;
258 begin
259 FIO.Check_Read_Status (AP (File));
261 if File.Before_Upper_Half_Character then
262 return False;
264 elsif File.Before_LM then
265 if File.Before_LM_PM then
266 return Nextc (File) = EOF;
267 end if;
269 else
270 ch := Getc (File);
272 if ch = EOF then
273 return True;
275 elsif ch /= LM then
276 Ungetc (ch, File);
277 return False;
279 else -- ch = LM
280 File.Before_LM := True;
281 end if;
282 end if;
284 -- Here we are just past the line mark with Before_LM set so that we
285 -- do not have to try to back up past the LM, thus avoiding the need
286 -- to back up more than one character.
288 ch := Getc (File);
290 if ch = EOF then
291 return True;
293 elsif ch = PM and then File.Is_Regular_File then
294 File.Before_LM_PM := True;
295 return Nextc (File) = EOF;
297 -- Here if neither EOF nor PM followed end of line
299 else
300 Ungetc (ch, File);
301 return False;
302 end if;
304 end End_Of_File;
306 function End_Of_File return Boolean is
307 begin
308 return End_Of_File (Current_In);
309 end End_Of_File;
311 -----------------
312 -- End_Of_Line --
313 -----------------
315 function End_Of_Line (File : File_Type) return Boolean is
316 ch : int;
318 begin
319 FIO.Check_Read_Status (AP (File));
321 if File.Before_Upper_Half_Character then
322 return False;
324 elsif File.Before_LM then
325 return True;
327 else
328 ch := Getc (File);
330 if ch = EOF then
331 return True;
333 else
334 Ungetc (ch, File);
335 return (ch = LM);
336 end if;
337 end if;
338 end End_Of_Line;
340 function End_Of_Line return Boolean is
341 begin
342 return End_Of_Line (Current_In);
343 end End_Of_Line;
345 -----------------
346 -- End_Of_Page --
347 -----------------
349 function End_Of_Page (File : File_Type) return Boolean is
350 ch : int;
352 begin
353 FIO.Check_Read_Status (AP (File));
355 if not File.Is_Regular_File then
356 return False;
358 elsif File.Before_Upper_Half_Character then
359 return False;
361 elsif File.Before_LM then
362 if File.Before_LM_PM then
363 return True;
364 end if;
366 else
367 ch := Getc (File);
369 if ch = EOF then
370 return True;
372 elsif ch /= LM then
373 Ungetc (ch, File);
374 return False;
376 else -- ch = LM
377 File.Before_LM := True;
378 end if;
379 end if;
381 -- Here we are just past the line mark with Before_LM set so that we
382 -- do not have to try to back up past the LM, thus avoiding the need
383 -- to back up more than one character.
385 ch := Nextc (File);
387 return ch = PM or else ch = EOF;
388 end End_Of_Page;
390 function End_Of_Page return Boolean is
391 begin
392 return End_Of_Page (Current_In);
393 end End_Of_Page;
395 --------------
396 -- EOF_Char --
397 --------------
399 function EOF_Char return Integer is
400 begin
401 return EOF;
402 end EOF_Char;
404 -----------
405 -- Flush --
406 -----------
408 procedure Flush (File : File_Type) is
409 begin
410 FIO.Flush (AP (File));
411 end Flush;
413 procedure Flush is
414 begin
415 Flush (Current_Out);
416 end Flush;
418 ----------
419 -- Form --
420 ----------
422 function Form (File : File_Type) return String is
423 begin
424 return FIO.Form (AP (File));
425 end Form;
427 ---------
428 -- Get --
429 ---------
431 procedure Get
432 (File : File_Type;
433 Item : out Character)
435 ch : int;
437 begin
438 FIO.Check_Read_Status (AP (File));
440 if File.Before_Upper_Half_Character then
441 File.Before_Upper_Half_Character := False;
442 Item := File.Saved_Upper_Half_Character;
444 elsif File.Before_LM then
445 File.Before_LM := False;
446 File.Col := 1;
448 if File.Before_LM_PM then
449 File.Line := 1;
450 File.Page := File.Page + 1;
451 File.Before_LM_PM := False;
452 else
453 File.Line := File.Line + 1;
454 end if;
455 end if;
457 loop
458 ch := Getc (File);
460 if ch = EOF then
461 raise End_Error;
463 elsif ch = LM then
464 File.Line := File.Line + 1;
465 File.Col := 1;
467 elsif ch = PM and then File.Is_Regular_File then
468 File.Page := File.Page + 1;
469 File.Line := 1;
471 else
472 Item := Character'Val (ch);
473 File.Col := File.Col + 1;
474 return;
475 end if;
476 end loop;
477 end Get;
479 procedure Get (Item : out Character) is
480 begin
481 Get (Current_In, Item);
482 end Get;
484 procedure Get
485 (File : File_Type;
486 Item : out String)
488 ch : int;
489 J : Natural;
491 begin
492 FIO.Check_Read_Status (AP (File));
494 if File.Before_LM then
495 File.Before_LM := False;
496 File.Before_LM_PM := False;
497 File.Col := 1;
499 if File.Before_LM_PM then
500 File.Line := 1;
501 File.Page := File.Page + 1;
502 File.Before_LM_PM := False;
504 else
505 File.Line := File.Line + 1;
506 end if;
507 end if;
509 J := Item'First;
510 while J <= Item'Last loop
511 ch := Getc (File);
513 if ch = EOF then
514 raise End_Error;
516 elsif ch = LM then
517 File.Line := File.Line + 1;
518 File.Col := 1;
520 elsif ch = PM and then File.Is_Regular_File then
521 File.Page := File.Page + 1;
522 File.Line := 1;
524 else
525 Item (J) := Character'Val (ch);
526 J := J + 1;
527 File.Col := File.Col + 1;
528 end if;
529 end loop;
530 end Get;
532 procedure Get (Item : out String) is
533 begin
534 Get (Current_In, Item);
535 end Get;
537 -------------------
538 -- Get_Immediate --
539 -------------------
541 procedure Get_Immediate
542 (File : File_Type;
543 Item : out Character)
545 ch : int;
547 begin
548 FIO.Check_Read_Status (AP (File));
550 if File.Before_Upper_Half_Character then
551 File.Before_Upper_Half_Character := False;
552 Item := File.Saved_Upper_Half_Character;
554 elsif File.Before_LM then
555 File.Before_LM := False;
556 File.Before_LM_PM := False;
557 Item := Character'Val (LM);
559 else
560 ch := Getc_Immed (File);
562 if ch = EOF then
563 raise End_Error;
564 else
565 Item :=
566 (if not Is_Start_Of_Encoding (Character'Val (ch), File.WC_Method)
567 then Character'Val (ch)
568 else Get_Upper_Half_Char_Immed (Character'Val (ch), File));
569 end if;
570 end if;
571 end Get_Immediate;
573 procedure Get_Immediate
574 (Item : out Character)
576 begin
577 Get_Immediate (Current_In, Item);
578 end Get_Immediate;
580 procedure Get_Immediate
581 (File : File_Type;
582 Item : out Character;
583 Available : out Boolean)
585 ch : int;
586 end_of_file : int;
587 avail : int;
589 procedure getc_immediate_nowait
590 (stream : FILEs;
591 ch : out int;
592 end_of_file : out int;
593 avail : out int);
594 pragma Import (C, getc_immediate_nowait, "getc_immediate_nowait");
596 begin
597 FIO.Check_Read_Status (AP (File));
598 Available := True;
600 if File.Before_Upper_Half_Character then
601 File.Before_Upper_Half_Character := False;
602 Item := File.Saved_Upper_Half_Character;
604 elsif File.Before_LM then
605 File.Before_LM := False;
606 File.Before_LM_PM := False;
607 Item := Character'Val (LM);
609 else
610 getc_immediate_nowait (File.Stream, ch, end_of_file, avail);
612 if ferror (File.Stream) /= 0 then
613 raise Device_Error;
615 elsif end_of_file /= 0 then
616 raise End_Error;
618 elsif avail = 0 then
619 Available := False;
620 Item := ASCII.NUL;
622 else
623 Available := True;
625 Item :=
626 (if Is_Start_Of_Encoding (Character'Val (ch), File.WC_Method)
627 then Character'Val (ch)
628 else Get_Upper_Half_Char_Immed (Character'Val (ch), File));
629 end if;
630 end if;
632 end Get_Immediate;
634 procedure Get_Immediate
635 (Item : out Character;
636 Available : out Boolean)
638 begin
639 Get_Immediate (Current_In, Item, Available);
640 end Get_Immediate;
642 --------------
643 -- Get_Line --
644 --------------
646 procedure Get_Line
647 (File : File_Type;
648 Item : out String;
649 Last : out Natural)
651 ch : int;
653 begin
654 FIO.Check_Read_Status (AP (File));
655 Last := Item'First - 1;
657 -- Immediate exit for null string, this is a case in which we do not
658 -- need to test for end of file and we do not skip a line mark under
659 -- any circumstances.
661 if Last >= Item'Last then
662 return;
663 end if;
665 -- Here we have at least one character, if we are immediately before
666 -- a line mark, then we will just skip past it storing no characters.
668 if File.Before_LM then
669 File.Before_LM := False;
670 File.Before_LM_PM := False;
672 -- Otherwise we need to read some characters
674 else
675 ch := Getc (File);
677 -- If we are at the end of file now, it means we are trying to
678 -- skip a file terminator and we raise End_Error (RM A.10.7(20))
680 if ch = EOF then
681 raise End_Error;
682 end if;
684 -- Loop through characters. Don't bother if we hit a page mark,
685 -- since in normal files, page marks can only follow line marks
686 -- in any case and we only promise to treat the page nonsense
687 -- correctly in the absense of such rogue page marks.
689 loop
690 -- Exit the loop if read is terminated by encountering line mark
692 exit when ch = LM;
694 -- Otherwise store the character, note that we know that ch is
695 -- something other than LM or EOF. It could possibly be a page
696 -- mark if there is a stray page mark in the middle of a line,
697 -- but this is not an official page mark in any case, since
698 -- official page marks can only follow a line mark. The whole
699 -- page business is pretty much nonsense anyway, so we do not
700 -- want to waste time trying to make sense out of non-standard
701 -- page marks in the file! This means that the behavior of
702 -- Get_Line is different from repeated Get of a character, but
703 -- that's too bad. We only promise that page numbers etc make
704 -- sense if the file is formatted in a standard manner.
706 -- Note: we do not adjust the column number because it is quicker
707 -- to adjust it once at the end of the operation than incrementing
708 -- it each time around the loop.
710 Last := Last + 1;
711 Item (Last) := Character'Val (ch);
713 -- All done if the string is full, this is the case in which
714 -- we do not skip the following line mark. We need to adjust
715 -- the column number in this case.
717 if Last = Item'Last then
718 File.Col := File.Col + Count (Item'Length);
719 return;
720 end if;
722 -- Otherwise read next character. We also exit from the loop if
723 -- we read an end of file. This is the case where the last line
724 -- is not terminated with a line mark, and we consider that there
725 -- is an implied line mark in this case (this is a non-standard
726 -- file, but it is nice to treat it reasonably).
728 ch := Getc (File);
729 exit when ch = EOF;
730 end loop;
731 end if;
733 -- We have skipped past, but not stored, a line mark. Skip following
734 -- page mark if one follows, but do not do this for a non-regular
735 -- file (since otherwise we get annoying wait for an extra character)
737 File.Line := File.Line + 1;
738 File.Col := 1;
740 if File.Before_LM_PM then
741 File.Line := 1;
742 File.Before_LM_PM := False;
743 File.Page := File.Page + 1;
745 elsif File.Is_Regular_File then
746 ch := Getc (File);
748 if ch = PM and then File.Is_Regular_File then
749 File.Line := 1;
750 File.Page := File.Page + 1;
751 else
752 Ungetc (ch, File);
753 end if;
754 end if;
755 end Get_Line;
757 procedure Get_Line
758 (Item : out String;
759 Last : out Natural)
761 begin
762 Get_Line (Current_In, Item, Last);
763 end Get_Line;
765 function Get_Line (File : File_Type) return String is
766 Buffer : String (1 .. 500);
767 Last : Natural;
769 function Get_Rest (S : String) return String;
770 -- This is a recursive function that reads the rest of the line and
771 -- returns it. S is the part read so far.
773 --------------
774 -- Get_Rest --
775 --------------
777 function Get_Rest (S : String) return String is
779 -- Each time we allocate a buffer the same size as what we have
780 -- read so far. This limits us to a logarithmic number of calls
781 -- to Get_Rest and also ensures only a linear use of stack space.
783 Buffer : String (1 .. S'Length);
784 Last : Natural;
786 begin
787 Get_Line (File, Buffer, Last);
789 declare
790 R : constant String := S & Buffer (1 .. Last);
791 begin
792 if Last < Buffer'Last then
793 return R;
794 else
795 return Get_Rest (R);
796 end if;
797 end;
798 end Get_Rest;
800 -- Start of processing for Get_Line
802 begin
803 Get_Line (File, Buffer, Last);
805 if Last < Buffer'Last then
806 return Buffer (1 .. Last);
807 else
808 return Get_Rest (Buffer (1 .. Last));
809 end if;
810 end Get_Line;
812 function Get_Line return String is
813 begin
814 return Get_Line (Current_In);
815 end Get_Line;
817 -------------------------
818 -- Get_Upper_Half_Char --
819 -------------------------
821 function Get_Upper_Half_Char
822 (C : Character;
823 File : File_Type) return Character
825 Result : Wide_Character;
827 function In_Char return Character;
828 -- Function used to obtain additional characters it the wide character
829 -- sequence is more than one character long.
831 function WC_In is new Char_Sequence_To_Wide_Char (In_Char);
833 -------------
834 -- In_Char --
835 -------------
837 function In_Char return Character is
838 ch : constant Integer := Getc (File);
839 begin
840 if ch = EOF then
841 raise End_Error;
842 else
843 return Character'Val (ch);
844 end if;
845 end In_Char;
847 -- Start of processing for Get_Upper_Half_Char
849 begin
850 Result := WC_In (C, File.WC_Method);
852 if Wide_Character'Pos (Result) > 16#FF# then
853 raise Constraint_Error with
854 "invalid wide character in Text_'I'O input";
855 else
856 return Character'Val (Wide_Character'Pos (Result));
857 end if;
858 end Get_Upper_Half_Char;
860 -------------------------------
861 -- Get_Upper_Half_Char_Immed --
862 -------------------------------
864 function Get_Upper_Half_Char_Immed
865 (C : Character;
866 File : File_Type) return Character
868 Result : Wide_Character;
870 function In_Char return Character;
871 -- Function used to obtain additional characters it the wide character
872 -- sequence is more than one character long.
874 function WC_In is new Char_Sequence_To_Wide_Char (In_Char);
876 -------------
877 -- In_Char --
878 -------------
880 function In_Char return Character is
881 ch : constant Integer := Getc_Immed (File);
882 begin
883 if ch = EOF then
884 raise End_Error;
885 else
886 return Character'Val (ch);
887 end if;
888 end In_Char;
890 -- Start of processing for Get_Upper_Half_Char_Immed
892 begin
893 Result := WC_In (C, File.WC_Method);
895 if Wide_Character'Pos (Result) > 16#FF# then
896 raise Constraint_Error with
897 "invalid wide character in Text_'I'O input";
898 else
899 return Character'Val (Wide_Character'Pos (Result));
900 end if;
901 end Get_Upper_Half_Char_Immed;
903 ----------
904 -- Getc --
905 ----------
907 function Getc (File : File_Type) return int is
908 ch : int;
910 begin
911 ch := fgetc (File.Stream);
913 if ch = EOF and then ferror (File.Stream) /= 0 then
914 raise Device_Error;
915 else
916 return ch;
917 end if;
918 end Getc;
920 ----------------
921 -- Getc_Immed --
922 ----------------
924 function Getc_Immed (File : File_Type) return int is
925 ch : int;
926 end_of_file : int;
928 procedure getc_immediate
929 (stream : FILEs; ch : out int; end_of_file : out int);
930 pragma Import (C, getc_immediate, "getc_immediate");
932 begin
933 FIO.Check_Read_Status (AP (File));
935 if File.Before_LM then
936 File.Before_LM := False;
937 File.Before_LM_PM := False;
938 ch := LM;
940 else
941 getc_immediate (File.Stream, ch, end_of_file);
943 if ferror (File.Stream) /= 0 then
944 raise Device_Error;
945 elsif end_of_file /= 0 then
946 return EOF;
947 end if;
948 end if;
950 return ch;
951 end Getc_Immed;
953 ------------------------------
954 -- Has_Upper_Half_Character --
955 ------------------------------
957 function Has_Upper_Half_Character (Item : String) return Boolean is
958 begin
959 for J in Item'Range loop
960 if Character'Pos (Item (J)) >= 16#80# then
961 return True;
962 end if;
963 end loop;
965 return False;
966 end Has_Upper_Half_Character;
968 -------------
969 -- Is_Open --
970 -------------
972 function Is_Open (File : File_Type) return Boolean is
973 begin
974 return FIO.Is_Open (AP (File));
975 end Is_Open;
977 ----------
978 -- Line --
979 ----------
981 -- Note: we assume that it is impossible in practice for the line
982 -- to exceed the value of Count'Last, i.e. no check is required for
983 -- overflow raising layout error.
985 function Line (File : File_Type) return Positive_Count is
986 begin
987 FIO.Check_File_Open (AP (File));
988 return File.Line;
989 end Line;
991 function Line return Positive_Count is
992 begin
993 return Line (Current_Out);
994 end Line;
996 -----------------
997 -- Line_Length --
998 -----------------
1000 function Line_Length (File : File_Type) return Count is
1001 begin
1002 FIO.Check_Write_Status (AP (File));
1003 return File.Line_Length;
1004 end Line_Length;
1006 function Line_Length return Count is
1007 begin
1008 return Line_Length (Current_Out);
1009 end Line_Length;
1011 ----------------
1012 -- Look_Ahead --
1013 ----------------
1015 procedure Look_Ahead
1016 (File : File_Type;
1017 Item : out Character;
1018 End_Of_Line : out Boolean)
1020 ch : int;
1022 begin
1023 FIO.Check_Read_Status (AP (File));
1025 -- If we are logically before a line mark, we can return immediately
1027 if File.Before_LM then
1028 End_Of_Line := True;
1029 Item := ASCII.NUL;
1031 -- If we are before an upper half character just return it (this can
1032 -- happen if there are two calls to Look_Ahead in a row).
1034 elsif File.Before_Upper_Half_Character then
1035 End_Of_Line := False;
1036 Item := File.Saved_Upper_Half_Character;
1038 -- Otherwise we must read a character from the input stream
1040 else
1041 ch := Getc (File);
1043 if ch = LM
1044 or else ch = EOF
1045 or else (ch = PM and then File.Is_Regular_File)
1046 then
1047 End_Of_Line := True;
1048 Ungetc (ch, File);
1049 Item := ASCII.NUL;
1051 -- Case where character obtained does not represent the start of an
1052 -- encoded sequence so it stands for itself and we can unget it with
1053 -- no difficulty.
1055 elsif not Is_Start_Of_Encoding
1056 (Character'Val (ch), File.WC_Method)
1057 then
1058 End_Of_Line := False;
1059 Ungetc (ch, File);
1060 Item := Character'Val (ch);
1062 -- For the start of an encoding, we read the character using the
1063 -- Get_Upper_Half_Char routine. It will occupy more than one byte
1064 -- so we can't put it back with ungetc. Instead we save it in the
1065 -- control block, setting a flag that everyone interested in reading
1066 -- characters must test before reading the stream.
1068 else
1069 Item := Get_Upper_Half_Char (Character'Val (ch), File);
1070 End_Of_Line := False;
1071 File.Saved_Upper_Half_Character := Item;
1072 File.Before_Upper_Half_Character := True;
1073 end if;
1074 end if;
1075 end Look_Ahead;
1077 procedure Look_Ahead
1078 (Item : out Character;
1079 End_Of_Line : out Boolean)
1081 begin
1082 Look_Ahead (Current_In, Item, End_Of_Line);
1083 end Look_Ahead;
1085 ----------
1086 -- Mode --
1087 ----------
1089 function Mode (File : File_Type) return File_Mode is
1090 begin
1091 return To_TIO (FIO.Mode (AP (File)));
1092 end Mode;
1094 ----------
1095 -- Name --
1096 ----------
1098 function Name (File : File_Type) return String is
1099 begin
1100 return FIO.Name (AP (File));
1101 end Name;
1103 --------------
1104 -- New_Line --
1105 --------------
1107 procedure New_Line
1108 (File : File_Type;
1109 Spacing : Positive_Count := 1)
1111 begin
1112 -- Raise Constraint_Error if out of range value. The reason for this
1113 -- explicit test is that we don't want junk values around, even if
1114 -- checks are off in the caller.
1116 if not Spacing'Valid then
1117 raise Constraint_Error;
1118 end if;
1120 FIO.Check_Write_Status (AP (File));
1122 for K in 1 .. Spacing loop
1123 Putc (LM, File);
1124 File.Line := File.Line + 1;
1126 if File.Page_Length /= 0
1127 and then File.Line > File.Page_Length
1128 then
1129 Putc (PM, File);
1130 File.Line := 1;
1131 File.Page := File.Page + 1;
1132 end if;
1133 end loop;
1135 File.Col := 1;
1136 end New_Line;
1138 procedure New_Line (Spacing : Positive_Count := 1) is
1139 begin
1140 New_Line (Current_Out, Spacing);
1141 end New_Line;
1143 --------------
1144 -- New_Page --
1145 --------------
1147 procedure New_Page (File : File_Type) is
1148 begin
1149 FIO.Check_Write_Status (AP (File));
1151 if File.Col /= 1 or else File.Line = 1 then
1152 Putc (LM, File);
1153 end if;
1155 Putc (PM, File);
1156 File.Page := File.Page + 1;
1157 File.Line := 1;
1158 File.Col := 1;
1159 end New_Page;
1161 procedure New_Page is
1162 begin
1163 New_Page (Current_Out);
1164 end New_Page;
1166 -----------
1167 -- Nextc --
1168 -----------
1170 function Nextc (File : File_Type) return int is
1171 ch : int;
1173 begin
1174 ch := fgetc (File.Stream);
1176 if ch = EOF then
1177 if ferror (File.Stream) /= 0 then
1178 raise Device_Error;
1179 end if;
1181 else
1182 if ungetc (ch, File.Stream) = EOF then
1183 raise Device_Error;
1184 end if;
1185 end if;
1187 return ch;
1188 end Nextc;
1190 ----------
1191 -- Open --
1192 ----------
1194 procedure Open
1195 (File : in out File_Type;
1196 Mode : File_Mode;
1197 Name : String;
1198 Form : String := "")
1200 Dummy_File_Control_Block : Text_AFCB;
1201 pragma Warnings (Off, Dummy_File_Control_Block);
1202 -- Yes, we know this is never assigned a value, only the tag
1203 -- is used for dispatching purposes, so that's expected.
1205 begin
1206 FIO.Open (File_Ptr => AP (File),
1207 Dummy_FCB => Dummy_File_Control_Block,
1208 Mode => To_FCB (Mode),
1209 Name => Name,
1210 Form => Form,
1211 Amethod => 'T',
1212 Creat => False,
1213 Text => True);
1215 File.Self := File;
1216 Set_WCEM (File);
1217 end Open;
1219 ----------
1220 -- Page --
1221 ----------
1223 -- Note: we assume that it is impossible in practice for the page
1224 -- to exceed the value of Count'Last, i.e. no check is required for
1225 -- overflow raising layout error.
1227 function Page (File : File_Type) return Positive_Count is
1228 begin
1229 FIO.Check_File_Open (AP (File));
1230 return File.Page;
1231 end Page;
1233 function Page return Positive_Count is
1234 begin
1235 return Page (Current_Out);
1236 end Page;
1238 -----------------
1239 -- Page_Length --
1240 -----------------
1242 function Page_Length (File : File_Type) return Count is
1243 begin
1244 FIO.Check_Write_Status (AP (File));
1245 return File.Page_Length;
1246 end Page_Length;
1248 function Page_Length return Count is
1249 begin
1250 return Page_Length (Current_Out);
1251 end Page_Length;
1253 ---------
1254 -- Put --
1255 ---------
1257 procedure Put
1258 (File : File_Type;
1259 Item : Character)
1261 begin
1262 FIO.Check_Write_Status (AP (File));
1264 if File.Line_Length /= 0 and then File.Col > File.Line_Length then
1265 New_Line (File);
1266 end if;
1268 -- If lower half character, or brackets encoding, output directly
1270 if Character'Pos (Item) < 16#80#
1271 or else File.WC_Method = WCEM_Brackets
1272 then
1273 if fputc (Character'Pos (Item), File.Stream) = EOF then
1274 raise Device_Error;
1275 end if;
1277 -- Case of upper half character with non-brackets encoding
1279 else
1280 Put_Encoded (File, Item);
1281 end if;
1283 File.Col := File.Col + 1;
1284 end Put;
1286 procedure Put (Item : Character) is
1287 begin
1288 FIO.Check_Write_Status (AP (Current_Out));
1290 if Current_Out.Line_Length /= 0
1291 and then Current_Out.Col > Current_Out.Line_Length
1292 then
1293 New_Line (Current_Out);
1294 end if;
1296 -- If lower half character, or brackets encoding, output directly
1298 if Character'Pos (Item) < 16#80#
1299 or else Default_WCEM = WCEM_Brackets
1300 then
1301 if fputc (Character'Pos (Item), Current_Out.Stream) = EOF then
1302 raise Device_Error;
1303 end if;
1305 -- Case of upper half character with non-brackets encoding
1307 else
1308 Put_Encoded (Current_Out, Item);
1309 end if;
1311 Current_Out.Col := Current_Out.Col + 1;
1312 end Put;
1314 ---------
1315 -- Put --
1316 ---------
1318 procedure Put
1319 (File : File_Type;
1320 Item : String)
1322 begin
1323 FIO.Check_Write_Status (AP (File));
1325 -- Only have something to do if string is non-null
1327 if Item'Length > 0 then
1329 -- If we have bounded lines, or if the file encoding is other than
1330 -- Brackets and the string has at least one upper half character,
1331 -- then output the string character by character.
1333 if File.Line_Length /= 0
1334 or else (File.WC_Method /= WCEM_Brackets
1335 and then Has_Upper_Half_Character (Item))
1336 then
1337 for J in Item'Range loop
1338 Put (File, Item (J));
1339 end loop;
1341 -- Otherwise we can output the entire string at once. Note that if
1342 -- there are LF or FF characters in the string, we do not bother to
1343 -- count them as line or page terminators.
1345 else
1346 FIO.Write_Buf (AP (File), Item'Address, Item'Length);
1347 File.Col := File.Col + Item'Length;
1348 end if;
1349 end if;
1350 end Put;
1352 procedure Put (Item : String) is
1353 begin
1354 Put (Current_Out, Item);
1355 end Put;
1357 -----------------
1358 -- Put_Encoded --
1359 -----------------
1361 procedure Put_Encoded (File : File_Type; Char : Character) is
1362 procedure Out_Char (C : Character);
1363 -- Procedure to output one character of an upper half encoded sequence
1365 procedure WC_Out is new Wide_Char_To_Char_Sequence (Out_Char);
1367 --------------
1368 -- Out_Char --
1369 --------------
1371 procedure Out_Char (C : Character) is
1372 begin
1373 Putc (Character'Pos (C), File);
1374 end Out_Char;
1376 -- Start of processing for Put_Encoded
1378 begin
1379 WC_Out (Wide_Character'Val (Character'Pos (Char)), File.WC_Method);
1380 end Put_Encoded;
1382 --------------
1383 -- Put_Line --
1384 --------------
1386 procedure Put_Line
1387 (File : File_Type;
1388 Item : String)
1390 Ilen : Natural := Item'Length;
1391 Istart : Natural := Item'First;
1393 begin
1394 FIO.Check_Write_Status (AP (File));
1396 -- If we have bounded lines, or if the file encoding is other than
1397 -- Brackets and the string has at least one upper half character, then
1398 -- output the string character by character.
1400 if File.Line_Length /= 0
1401 or else (File.WC_Method /= WCEM_Brackets
1402 and then Has_Upper_Half_Character (Item))
1403 then
1404 for J in Item'Range loop
1405 Put (File, Item (J));
1406 end loop;
1408 New_Line (File);
1409 return;
1410 end if;
1412 -- Normal case where we do not need to output character by character
1414 -- We setup a single string that has the necessary terminators and
1415 -- then write it with a single call. The reason for doing this is
1416 -- that it gives better behavior for the use of Put_Line in multi-
1417 -- tasking programs, since often the OS will treat the entire put
1418 -- operation as an atomic operation.
1420 -- We only do this if the message is 512 characters or less in length,
1421 -- since otherwise Put_Line would use an unbounded amount of stack
1422 -- space and could cause undetected stack overflow. If we have a
1423 -- longer string, then output the first part separately to avoid this.
1425 if Ilen > 512 then
1426 FIO.Write_Buf (AP (File), Item'Address, size_t (Ilen - 512));
1427 Istart := Istart + Ilen - 512;
1428 Ilen := 512;
1429 end if;
1431 -- Now prepare the string with its terminator
1433 declare
1434 Buffer : String (1 .. Ilen + 2);
1435 Plen : size_t;
1437 begin
1438 Buffer (1 .. Ilen) := Item (Istart .. Item'Last);
1439 Buffer (Ilen + 1) := Character'Val (LM);
1441 if File.Page_Length /= 0
1442 and then File.Line > File.Page_Length
1443 then
1444 Buffer (Ilen + 2) := Character'Val (PM);
1445 Plen := size_t (Ilen) + 2;
1446 File.Line := 1;
1447 File.Page := File.Page + 1;
1449 else
1450 Plen := size_t (Ilen) + 1;
1451 File.Line := File.Line + 1;
1452 end if;
1454 FIO.Write_Buf (AP (File), Buffer'Address, Plen);
1456 File.Col := 1;
1457 end;
1458 end Put_Line;
1460 procedure Put_Line (Item : String) is
1461 begin
1462 Put_Line (Current_Out, Item);
1463 end Put_Line;
1465 ----------
1466 -- Putc --
1467 ----------
1469 procedure Putc (ch : int; File : File_Type) is
1470 begin
1471 if fputc (ch, File.Stream) = EOF then
1472 raise Device_Error;
1473 end if;
1474 end Putc;
1476 ----------
1477 -- Read --
1478 ----------
1480 -- This is the primitive Stream Read routine, used when a Text_IO file
1481 -- is treated directly as a stream using Text_IO.Streams.Stream.
1483 procedure Read
1484 (File : in out Text_AFCB;
1485 Item : out Stream_Element_Array;
1486 Last : out Stream_Element_Offset)
1488 Discard_ch : int;
1489 pragma Warnings (Off, Discard_ch);
1491 begin
1492 -- Need to deal with Before_Upper_Half_Character ???
1494 if File.Mode /= FCB.In_File then
1495 raise Mode_Error;
1496 end if;
1498 -- Deal with case where our logical and physical position do not match
1499 -- because of being after an LM or LM-PM sequence when in fact we are
1500 -- logically positioned before it.
1502 if File.Before_LM then
1504 -- If we are before a PM, then it is possible for a stream read
1505 -- to leave us after the LM and before the PM, which is a bit
1506 -- odd. The easiest way to deal with this is to unget the PM,
1507 -- so we are indeed positioned between the characters. This way
1508 -- further stream read operations will work correctly, and the
1509 -- effect on text processing is a little weird, but what can
1510 -- be expected if stream and text input are mixed this way?
1512 if File.Before_LM_PM then
1513 Discard_ch := ungetc (PM, File.Stream);
1514 File.Before_LM_PM := False;
1515 end if;
1517 File.Before_LM := False;
1519 Item (Item'First) := Stream_Element (Character'Pos (ASCII.LF));
1521 if Item'Length = 1 then
1522 Last := Item'Last;
1524 else
1525 Last :=
1526 Item'First +
1527 Stream_Element_Offset
1528 (fread (buffer => Item'Address,
1529 index => size_t (Item'First + 1),
1530 size => 1,
1531 count => Item'Length - 1,
1532 stream => File.Stream));
1533 end if;
1535 return;
1536 end if;
1538 -- Now we do the read. Since this is a text file, it is normally in
1539 -- text mode, but stream data must be read in binary mode, so we
1540 -- temporarily set binary mode for the read, resetting it after.
1541 -- These calls have no effect in a system (like Unix) where there is
1542 -- no distinction between text and binary files.
1544 set_binary_mode (fileno (File.Stream));
1546 Last :=
1547 Item'First +
1548 Stream_Element_Offset
1549 (fread (Item'Address, 1, Item'Length, File.Stream)) - 1;
1551 if Last < Item'Last then
1552 if ferror (File.Stream) /= 0 then
1553 raise Device_Error;
1554 end if;
1555 end if;
1557 set_text_mode (fileno (File.Stream));
1558 end Read;
1560 -----------
1561 -- Reset --
1562 -----------
1564 procedure Reset
1565 (File : in out File_Type;
1566 Mode : File_Mode)
1568 begin
1569 -- Don't allow change of mode for current file (RM A.10.2(5))
1571 if (File = Current_In or else
1572 File = Current_Out or else
1573 File = Current_Error)
1574 and then To_FCB (Mode) /= File.Mode
1575 then
1576 raise Mode_Error;
1577 end if;
1579 Terminate_Line (File);
1580 FIO.Reset (AP (File)'Unrestricted_Access, To_FCB (Mode));
1581 File.Page := 1;
1582 File.Line := 1;
1583 File.Col := 1;
1584 File.Line_Length := 0;
1585 File.Page_Length := 0;
1586 File.Before_LM := False;
1587 File.Before_LM_PM := False;
1588 end Reset;
1590 procedure Reset (File : in out File_Type) is
1591 begin
1592 Terminate_Line (File);
1593 FIO.Reset (AP (File)'Unrestricted_Access);
1594 File.Page := 1;
1595 File.Line := 1;
1596 File.Col := 1;
1597 File.Line_Length := 0;
1598 File.Page_Length := 0;
1599 File.Before_LM := False;
1600 File.Before_LM_PM := False;
1601 end Reset;
1603 -------------
1604 -- Set_Col --
1605 -------------
1607 procedure Set_Col
1608 (File : File_Type;
1609 To : Positive_Count)
1611 ch : int;
1613 begin
1614 -- Raise Constraint_Error if out of range value. The reason for this
1615 -- explicit test is that we don't want junk values around, even if
1616 -- checks are off in the caller.
1618 if not To'Valid then
1619 raise Constraint_Error;
1620 end if;
1622 FIO.Check_File_Open (AP (File));
1624 -- Output case
1626 if Mode (File) >= Out_File then
1628 -- Error if we attempt to set Col to a value greater than the
1629 -- maximum permissible line length.
1631 if File.Line_Length /= 0 and then To > File.Line_Length then
1632 raise Layout_Error;
1633 end if;
1635 -- If we are behind current position, then go to start of new line
1637 if To < File.Col then
1638 New_Line (File);
1639 end if;
1641 -- Loop to output blanks till we are at the required column
1643 while File.Col < To loop
1644 Put (File, ' ');
1645 end loop;
1647 -- Input case
1649 else
1650 -- If we are logically before a LM, but physically after it, the
1651 -- file position still reflects the position before the LM, so eat
1652 -- it now and adjust the file position appropriately.
1654 if File.Before_LM then
1655 File.Before_LM := False;
1656 File.Before_LM_PM := False;
1657 File.Line := File.Line + 1;
1658 File.Col := 1;
1659 end if;
1661 -- Loop reading characters till we get one at the required Col value
1663 loop
1664 -- Read next character. The reason we have to read ahead is to
1665 -- skip formatting characters, the effect of Set_Col is to set
1666 -- us to a real character with the right Col value, and format
1667 -- characters don't count.
1669 ch := Getc (File);
1671 -- Error if we hit an end of file
1673 if ch = EOF then
1674 raise End_Error;
1676 -- If line mark, eat it and adjust file position
1678 elsif ch = LM then
1679 File.Line := File.Line + 1;
1680 File.Col := 1;
1682 -- If recognized page mark, eat it, and adjust file position
1684 elsif ch = PM and then File.Is_Regular_File then
1685 File.Page := File.Page + 1;
1686 File.Line := 1;
1687 File.Col := 1;
1689 -- Otherwise this is the character we are looking for, so put it
1690 -- back in the input stream (we have not adjusted the file
1691 -- position yet, so everything is set right after this ungetc).
1693 elsif To = File.Col then
1694 Ungetc (ch, File);
1695 return;
1697 -- Keep skipping characters if we are not there yet, updating the
1698 -- file position past the skipped character.
1700 else
1701 File.Col := File.Col + 1;
1702 end if;
1703 end loop;
1704 end if;
1705 end Set_Col;
1707 procedure Set_Col (To : Positive_Count) is
1708 begin
1709 Set_Col (Current_Out, To);
1710 end Set_Col;
1712 ---------------
1713 -- Set_Error --
1714 ---------------
1716 procedure Set_Error (File : File_Type) is
1717 begin
1718 FIO.Check_Write_Status (AP (File));
1719 Current_Err := File;
1720 end Set_Error;
1722 ---------------
1723 -- Set_Input --
1724 ---------------
1726 procedure Set_Input (File : File_Type) is
1727 begin
1728 FIO.Check_Read_Status (AP (File));
1729 Current_In := File;
1730 end Set_Input;
1732 --------------
1733 -- Set_Line --
1734 --------------
1736 procedure Set_Line
1737 (File : File_Type;
1738 To : Positive_Count)
1740 begin
1741 -- Raise Constraint_Error if out of range value. The reason for this
1742 -- explicit test is that we don't want junk values around, even if
1743 -- checks are off in the caller.
1745 if not To'Valid then
1746 raise Constraint_Error;
1747 end if;
1749 FIO.Check_File_Open (AP (File));
1751 if To = File.Line then
1752 return;
1753 end if;
1755 if Mode (File) >= Out_File then
1756 if File.Page_Length /= 0 and then To > File.Page_Length then
1757 raise Layout_Error;
1758 end if;
1760 if To < File.Line then
1761 New_Page (File);
1762 end if;
1764 while File.Line < To loop
1765 New_Line (File);
1766 end loop;
1768 else
1769 while To /= File.Line loop
1770 Skip_Line (File);
1771 end loop;
1772 end if;
1773 end Set_Line;
1775 procedure Set_Line (To : Positive_Count) is
1776 begin
1777 Set_Line (Current_Out, To);
1778 end Set_Line;
1780 ---------------------
1781 -- Set_Line_Length --
1782 ---------------------
1784 procedure Set_Line_Length (File : File_Type; To : Count) is
1785 begin
1786 -- Raise Constraint_Error if out of range value. The reason for this
1787 -- explicit test is that we don't want junk values around, even if
1788 -- checks are off in the caller.
1790 if not To'Valid then
1791 raise Constraint_Error;
1792 end if;
1794 FIO.Check_Write_Status (AP (File));
1795 File.Line_Length := To;
1796 end Set_Line_Length;
1798 procedure Set_Line_Length (To : Count) is
1799 begin
1800 Set_Line_Length (Current_Out, To);
1801 end Set_Line_Length;
1803 ----------------
1804 -- Set_Output --
1805 ----------------
1807 procedure Set_Output (File : File_Type) is
1808 begin
1809 FIO.Check_Write_Status (AP (File));
1810 Current_Out := File;
1811 end Set_Output;
1813 ---------------------
1814 -- Set_Page_Length --
1815 ---------------------
1817 procedure Set_Page_Length (File : File_Type; To : Count) is
1818 begin
1819 -- Raise Constraint_Error if out of range value. The reason for this
1820 -- explicit test is that we don't want junk values around, even if
1821 -- checks are off in the caller.
1823 if not To'Valid then
1824 raise Constraint_Error;
1825 end if;
1827 FIO.Check_Write_Status (AP (File));
1828 File.Page_Length := To;
1829 end Set_Page_Length;
1831 procedure Set_Page_Length (To : Count) is
1832 begin
1833 Set_Page_Length (Current_Out, To);
1834 end Set_Page_Length;
1836 --------------
1837 -- Set_WCEM --
1838 --------------
1840 procedure Set_WCEM (File : in out File_Type) is
1841 Start : Natural;
1842 Stop : Natural;
1844 begin
1845 File.WC_Method := WCEM_Brackets;
1846 FIO.Form_Parameter (File.Form.all, "wcem", Start, Stop);
1848 if Start = 0 then
1849 File.WC_Method := WCEM_Brackets;
1851 else
1852 if Stop = Start then
1853 for J in WC_Encoding_Letters'Range loop
1854 if File.Form (Start) = WC_Encoding_Letters (J) then
1855 File.WC_Method := J;
1856 return;
1857 end if;
1858 end loop;
1859 end if;
1861 Close (File);
1862 raise Use_Error with "invalid WCEM form parameter";
1863 end if;
1864 end Set_WCEM;
1866 ---------------
1867 -- Skip_Line --
1868 ---------------
1870 procedure Skip_Line
1871 (File : File_Type;
1872 Spacing : Positive_Count := 1)
1874 ch : int;
1876 begin
1877 -- Raise Constraint_Error if out of range value. The reason for this
1878 -- explicit test is that we don't want junk values around, even if
1879 -- checks are off in the caller.
1881 if not Spacing'Valid then
1882 raise Constraint_Error;
1883 end if;
1885 FIO.Check_Read_Status (AP (File));
1887 for L in 1 .. Spacing loop
1888 if File.Before_LM then
1889 File.Before_LM := False;
1891 -- Note that if File.Before_LM_PM is currently set, we also have
1892 -- to reset it (because it makes sense for Before_LM_PM to be set
1893 -- only when Before_LM is also set). This is done later on in this
1894 -- subprogram, as soon as Before_LM_PM has been taken into account
1895 -- for the purpose of page and line counts.
1897 else
1898 ch := Getc (File);
1900 -- If at end of file now, then immediately raise End_Error. Note
1901 -- that we can never be positioned between a line mark and a page
1902 -- mark, so if we are at the end of file, we cannot logically be
1903 -- before the implicit page mark that is at the end of the file.
1905 -- For the same reason, we do not need an explicit check for a
1906 -- page mark. If there is a FF in the middle of a line, the file
1907 -- is not in canonical format and we do not care about the page
1908 -- numbers for files other than ones in canonical format.
1910 if ch = EOF then
1911 raise End_Error;
1912 end if;
1914 -- If not at end of file, then loop till we get to an LM or EOF.
1915 -- The latter case happens only in non-canonical files where the
1916 -- last line is not terminated by LM, but we don't want to blow
1917 -- up for such files, so we assume an implicit LM in this case.
1919 loop
1920 exit when ch = LM or ch = EOF;
1921 ch := Getc (File);
1922 end loop;
1923 end if;
1925 -- We have got past a line mark, now, for a regular file only,
1926 -- see if a page mark immediately follows this line mark and
1927 -- if so, skip past the page mark as well. We do not do this
1928 -- for non-regular files, since it would cause an undesirable
1929 -- wait for an additional character.
1931 File.Col := 1;
1932 File.Line := File.Line + 1;
1934 if File.Before_LM_PM then
1935 File.Page := File.Page + 1;
1936 File.Line := 1;
1937 File.Before_LM_PM := False;
1939 elsif File.Is_Regular_File then
1940 ch := Getc (File);
1942 -- Page mark can be explicit, or implied at the end of the file
1944 if (ch = PM or else ch = EOF)
1945 and then File.Is_Regular_File
1946 then
1947 File.Page := File.Page + 1;
1948 File.Line := 1;
1949 else
1950 Ungetc (ch, File);
1951 end if;
1952 end if;
1953 end loop;
1955 File.Before_Upper_Half_Character := False;
1956 end Skip_Line;
1958 procedure Skip_Line (Spacing : Positive_Count := 1) is
1959 begin
1960 Skip_Line (Current_In, Spacing);
1961 end Skip_Line;
1963 ---------------
1964 -- Skip_Page --
1965 ---------------
1967 procedure Skip_Page (File : File_Type) is
1968 ch : int;
1970 begin
1971 FIO.Check_Read_Status (AP (File));
1973 -- If at page mark already, just skip it
1975 if File.Before_LM_PM then
1976 File.Before_LM := False;
1977 File.Before_LM_PM := False;
1978 File.Page := File.Page + 1;
1979 File.Line := 1;
1980 File.Col := 1;
1981 return;
1982 end if;
1984 -- This is a bit tricky, if we are logically before an LM then
1985 -- it is not an error if we are at an end of file now, since we
1986 -- are not really at it.
1988 if File.Before_LM then
1989 File.Before_LM := False;
1990 File.Before_LM_PM := False;
1991 ch := Getc (File);
1993 -- Otherwise we do raise End_Error if we are at the end of file now
1995 else
1996 ch := Getc (File);
1998 if ch = EOF then
1999 raise End_Error;
2000 end if;
2001 end if;
2003 -- Now we can just rumble along to the next page mark, or to the
2004 -- end of file, if that comes first. The latter case happens when
2005 -- the page mark is implied at the end of file.
2007 loop
2008 exit when ch = EOF
2009 or else (ch = PM and then File.Is_Regular_File);
2010 ch := Getc (File);
2011 end loop;
2013 File.Page := File.Page + 1;
2014 File.Line := 1;
2015 File.Col := 1;
2016 File.Before_Upper_Half_Character := False;
2017 end Skip_Page;
2019 procedure Skip_Page is
2020 begin
2021 Skip_Page (Current_In);
2022 end Skip_Page;
2024 --------------------
2025 -- Standard_Error --
2026 --------------------
2028 function Standard_Error return File_Type is
2029 begin
2030 return Standard_Err;
2031 end Standard_Error;
2033 function Standard_Error return File_Access is
2034 begin
2035 return Standard_Err'Access;
2036 end Standard_Error;
2038 --------------------
2039 -- Standard_Input --
2040 --------------------
2042 function Standard_Input return File_Type is
2043 begin
2044 return Standard_In;
2045 end Standard_Input;
2047 function Standard_Input return File_Access is
2048 begin
2049 return Standard_In'Access;
2050 end Standard_Input;
2052 ---------------------
2053 -- Standard_Output --
2054 ---------------------
2056 function Standard_Output return File_Type is
2057 begin
2058 return Standard_Out;
2059 end Standard_Output;
2061 function Standard_Output return File_Access is
2062 begin
2063 return Standard_Out'Access;
2064 end Standard_Output;
2066 --------------------
2067 -- Terminate_Line --
2068 --------------------
2070 procedure Terminate_Line (File : File_Type) is
2071 begin
2072 FIO.Check_File_Open (AP (File));
2074 -- For file other than In_File, test for needing to terminate last line
2076 if Mode (File) /= In_File then
2078 -- If not at start of line definition need new line
2080 if File.Col /= 1 then
2081 New_Line (File);
2083 -- For files other than standard error and standard output, we
2084 -- make sure that an empty file has a single line feed, so that
2085 -- it is properly formatted. We avoid this for the standard files
2086 -- because it is too much of a nuisance to have these odd line
2087 -- feeds when nothing has been written to the file.
2089 -- We also avoid this for files opened in append mode, in
2090 -- accordance with (RM A.8.2(10))
2092 elsif (File /= Standard_Err and then File /= Standard_Out)
2093 and then (File.Line = 1 and then File.Page = 1)
2094 and then Mode (File) = Out_File
2095 then
2096 New_Line (File);
2097 end if;
2098 end if;
2099 end Terminate_Line;
2101 ------------
2102 -- Ungetc --
2103 ------------
2105 procedure Ungetc (ch : int; File : File_Type) is
2106 begin
2107 if ch /= EOF then
2108 if ungetc (ch, File.Stream) = EOF then
2109 raise Device_Error;
2110 end if;
2111 end if;
2112 end Ungetc;
2114 -----------
2115 -- Write --
2116 -----------
2118 -- This is the primitive Stream Write routine, used when a Text_IO file
2119 -- is treated directly as a stream using Text_IO.Streams.Stream.
2121 procedure Write
2122 (File : in out Text_AFCB;
2123 Item : Stream_Element_Array)
2125 pragma Warnings (Off, File);
2126 -- Because in this implementation we don't need IN OUT, we only read
2128 function Has_Translated_Characters return Boolean;
2129 -- return True if Item array contains a character which will be
2130 -- translated under the text file mode. There is only one such
2131 -- character under DOS based systems which is character 10.
2133 text_translation_required : Boolean;
2134 for text_translation_required'Size use Character'Size;
2135 pragma Import (C, text_translation_required,
2136 "__gnat_text_translation_required");
2138 Siz : constant size_t := Item'Length;
2140 -------------------------------
2141 -- Has_Translated_Characters --
2142 -------------------------------
2144 function Has_Translated_Characters return Boolean is
2145 begin
2146 for K in Item'Range loop
2147 if Item (K) = 10 then
2148 return True;
2149 end if;
2150 end loop;
2151 return False;
2152 end Has_Translated_Characters;
2154 Needs_Binary_Write : constant Boolean :=
2155 text_translation_required
2156 and then Has_Translated_Characters;
2158 -- Start of processing for Write
2160 begin
2161 if File.Mode = FCB.In_File then
2162 raise Mode_Error;
2163 end if;
2165 -- Now we do the write. Since this is a text file, it is normally in
2166 -- text mode, but stream data must be written in binary mode, so we
2167 -- temporarily set binary mode for the write, resetting it after. This
2168 -- is done only if needed (i.e. there is some characters in Item which
2169 -- needs to be written using the binary mode).
2170 -- These calls have no effect in a system (like Unix) where there is
2171 -- no distinction between text and binary files.
2173 -- Since the character translation is done at the time the buffer is
2174 -- written (this is true under Windows) we first flush current buffer
2175 -- with text mode if needed.
2177 if Needs_Binary_Write then
2178 if fflush (File.Stream) = -1 then
2179 raise Device_Error;
2180 end if;
2182 set_binary_mode (fileno (File.Stream));
2183 end if;
2185 if fwrite (Item'Address, 1, Siz, File.Stream) /= Siz then
2186 raise Device_Error;
2187 end if;
2189 -- At this point we need to flush the buffer using the binary mode then
2190 -- we reset to text mode.
2192 if Needs_Binary_Write then
2193 if fflush (File.Stream) = -1 then
2194 raise Device_Error;
2195 end if;
2197 set_text_mode (fileno (File.Stream));
2198 end if;
2199 end Write;
2201 -- Use "preallocated" strings to avoid calling "new" during the
2202 -- elaboration of the run time. This is needed in the tasking case to
2203 -- avoid calling Task_Lock too early. A filename is expected to end with a
2204 -- null character in the runtime, here the null characters are added just
2205 -- to have a correct filename length.
2207 Err_Name : aliased String := "*stderr" & ASCII.NUL;
2208 In_Name : aliased String := "*stdin" & ASCII.NUL;
2209 Out_Name : aliased String := "*stdout" & ASCII.NUL;
2211 begin
2212 -------------------------------
2213 -- Initialize Standard Files --
2214 -------------------------------
2216 for J in WC_Encoding_Method loop
2217 if WC_Encoding = WC_Encoding_Letters (J) then
2218 Default_WCEM := J;
2219 end if;
2220 end loop;
2222 -- Note: the names in these files are bogus, and probably it would be
2223 -- better for these files to have no names, but the ACVC test insist!
2224 -- We use names that are bound to fail in open etc.
2226 Standard_Err.Stream := stderr;
2227 Standard_Err.Name := Err_Name'Access;
2228 Standard_Err.Form := Null_Str'Unrestricted_Access;
2229 Standard_Err.Mode := FCB.Out_File;
2230 Standard_Err.Is_Regular_File := is_regular_file (fileno (stderr)) /= 0;
2231 Standard_Err.Is_Temporary_File := False;
2232 Standard_Err.Is_System_File := True;
2233 Standard_Err.Is_Text_File := True;
2234 Standard_Err.Access_Method := 'T';
2235 Standard_Err.Self := Standard_Err;
2236 Standard_Err.WC_Method := Default_WCEM;
2238 Standard_In.Stream := stdin;
2239 Standard_In.Name := In_Name'Access;
2240 Standard_In.Form := Null_Str'Unrestricted_Access;
2241 Standard_In.Mode := FCB.In_File;
2242 Standard_In.Is_Regular_File := is_regular_file (fileno (stdin)) /= 0;
2243 Standard_In.Is_Temporary_File := False;
2244 Standard_In.Is_System_File := True;
2245 Standard_In.Is_Text_File := True;
2246 Standard_In.Access_Method := 'T';
2247 Standard_In.Self := Standard_In;
2248 Standard_In.WC_Method := Default_WCEM;
2250 Standard_Out.Stream := stdout;
2251 Standard_Out.Name := Out_Name'Access;
2252 Standard_Out.Form := Null_Str'Unrestricted_Access;
2253 Standard_Out.Mode := FCB.Out_File;
2254 Standard_Out.Is_Regular_File := is_regular_file (fileno (stdout)) /= 0;
2255 Standard_Out.Is_Temporary_File := False;
2256 Standard_Out.Is_System_File := True;
2257 Standard_Out.Is_Text_File := True;
2258 Standard_Out.Access_Method := 'T';
2259 Standard_Out.Self := Standard_Out;
2260 Standard_Out.WC_Method := Default_WCEM;
2262 FIO.Chain_File (AP (Standard_In));
2263 FIO.Chain_File (AP (Standard_Out));
2264 FIO.Chain_File (AP (Standard_Err));
2266 FIO.Make_Unbuffered (AP (Standard_Out));
2267 FIO.Make_Unbuffered (AP (Standard_Err));
2269 end Ada.Text_IO;