OSDN Git Service

gcc/ada/
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-fileio.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --                       S Y S T E M . F I L E _ I O                        --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2006, 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 2,  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.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  This package provides support for the routines described in (RM A.8.2)
35 --  which are common to Text_IO, Direct_IO, Sequential_IO and Stream_IO.
36
37 with Interfaces.C_Streams;
38
39 with System.File_Control_Block;
40
41 package System.File_IO is
42
43    package FCB renames System.File_Control_Block;
44    package ICS renames Interfaces.C_Streams;
45
46    ---------------------
47    -- File Management --
48    ---------------------
49
50    procedure Open
51      (File_Ptr  : in out FCB.AFCB_Ptr;
52       Dummy_FCB : FCB.AFCB'Class;
53       Mode      : FCB.File_Mode;
54       Name      : String;
55       Form      : String;
56       Amethod   : Character;
57       Creat     : Boolean;
58       Text      : Boolean;
59       C_Stream  : ICS.FILEs := ICS.NULL_Stream);
60    --  This routine is used for both Open and Create calls:
61    --
62    --    File_Ptr is the file type, which must be null on entry
63    --    (i.e. the file must be closed before the call).
64    --
65    --    Dummy_FCB is a default initialized file control block of appropriate
66    --    type. Note that the tag of this record indicates the type and length
67    --    of the control block. This control block is used only for the purpose
68    --    of providing the controlling argument for calling the write version
69    --    of Allocate_AFCB. It has no other purpose, and its fields are never
70    --    read or written.
71    --
72    --    Mode is the required mode
73    --
74    --    Name is the file name, with a null string indicating that a temporary
75    --    file is to be created (only permitted in create mode, not open mode)
76    --
77    --    Creat is True for a create call, and false for an open call
78    --
79    --    Text is set True to open the file in text mode (w+t or r+t) instead
80    --    of the usual binary mode open (w+b or r+b).
81    --
82    --    Form is the form string given in the open or create call, this is
83    --    stored in the AFCB, but otherwise is not used by this or any other
84    --    routine in this unit (except Form which retrieves the original value)
85    --
86    --    Amethod indicates the access method
87    --
88    --      D = Direct_IO
89    --      Q = Sequential_IO
90    --      S = Stream_IO
91    --      T = Text_IO
92    --      W = Wide_Text_IO
93    --
94    --    C_Stream is left at its default value for the normal case of an
95    --    Open or Create call as defined in the RM. The only time this is
96    --    non-null is for the Open call from Ada.xxx_IO.C_Streams.Open.
97    --
98    --  On return, if the open/create succeeds, then the fields of File are
99    --  filled in, and this value is copied to the heap. File_Ptr points to
100    --  this allocated file control block. If the open/create fails, then the
101    --  fields of File are undefined, and File_Ptr is unchanged.
102
103    procedure Close (File : in out FCB.AFCB_Ptr);
104    --  The file is closed, all storage associated with it is released, and
105    --  File is set to null. Note that this routine calls AFCB_Close to perform
106    --  any specialized close actions, then closes the file at the system level,
107    --  then frees the mode and form strings, and finally calls AFCB_Free to
108    --  free the file control block itself, setting File to null.
109
110    procedure Delete (File : in out FCB.AFCB_Ptr);
111    --  The indicated file is unlinked
112
113    procedure Reset (File : in out FCB.AFCB_Ptr; Mode : FCB.File_Mode);
114    --  The file is reset, and the mode changed as indicated
115
116    procedure Reset (File : in out FCB.AFCB_Ptr);
117    --  The files is reset, and the mode is unchanged
118
119    function Mode (File : FCB.AFCB_Ptr) return FCB.File_Mode;
120    --  Returns the mode as supplied by create, open or reset
121
122    function Name (File : FCB.AFCB_Ptr) return String;
123    --  Returns the file name as supplied by Open or Create. Raises Use_Error
124    --  if used with temporary files or standard files.
125
126    function Form (File : FCB.AFCB_Ptr) return String;
127    --  Returns the form as supplied by create, open or reset
128    --  The string is normalized to all lower case letters.
129
130    function Is_Open (File : FCB.AFCB_Ptr) return Boolean;
131    --  Determines if file is open or not
132
133    ----------------------
134    -- Utility Routines --
135    ----------------------
136
137    --  Some internal routines not defined in A.8.2. These are routines which
138    --  provide required common functionality shared by separate packages.
139
140    procedure Chain_File (File : FCB.AFCB_Ptr);
141    --  Used to chain the given file into the list of open files. Normally this
142    --  is done implicitly by Open. Chain_File is used for the special cases of
143    --  the system files defined by Text_IO (stdin, stdout, stderr) which are
144    --  not opened in the normal manner. Note that the caller is responsible
145    --  for task lock out to protect the global data structures if this is
146    --  necessary (it is needed for the calls from within this unit itself,
147    --  but not required for the calls from Text_IO and Wide_Text_IO that
148    --  are made during elaboration of the environment task).
149
150    procedure Check_File_Open (File : FCB.AFCB_Ptr);
151    --  If the current file is not open, then Status_Error is raised.
152    --  Otherwise control returns normally (with File pointing to the
153    --  control block for the open file.
154
155    procedure Check_Read_Status (File : FCB.AFCB_Ptr);
156    --  If the current file is not open, then Status_Error is raised. If
157    --  the file is open, then the mode is checked to ensure that reading
158    --  is permitted, and if not Mode_Error is raised, otherwise control
159    --  returns normally.
160
161    procedure Check_Write_Status (File : FCB.AFCB_Ptr);
162    --  If the current file is not open, then Status_Error is raised. If
163    --  the file is open, then the mode is checked to ensure that writing
164    --  is permitted, and if not Mode_Error is raised, otherwise control
165    --  returns normally.
166
167    function End_Of_File (File : FCB.AFCB_Ptr) return Boolean;
168    --  File must be opened in read mode. True is returned if the stream is
169    --  currently positioned at the end of file, otherwise False is returned.
170    --  The position of the stream is not affected.
171
172    procedure Flush (File : FCB.AFCB_Ptr);
173    --  Flushes the stream associated with the given file. The file must be
174    --  open and in write mode (if not, an appropriate exception is raised)
175
176    function Form_Boolean
177      (Form    : String;
178       Keyword : String;
179       Default : Boolean)
180       return    Boolean;
181    --  Searches form string for an entry of the form Keyword=xx where xx is
182    --  either Yes/No or y/n. Returns True if Yes or Y is found, False if No
183    --  or N is found. If the keyword parameter is not found, returns the
184    --  value given as Default. May raise Use_Error if a form string syntax
185    --  error is detected. Keyword and Form must be in lower case.
186
187    function Form_Integer
188      (Form    : String;
189       Keyword : String;
190       Default : Integer)
191       return    Integer;
192    --  Searches form string for an entry of the form Keyword=xx where xx is
193    --  an unsigned decimal integer in the range 0 to 999_999. Returns this
194    --  integer value if it is found. If the keyword parameter is not found,
195    --  returns the value given as Default. Raise Use_Error if a form string
196    --  syntax error is detected. Keyword and Form must be in lower case.
197
198    procedure Form_Parameter
199      (Form    : String;
200       Keyword : String;
201       Start   : out Natural;
202       Stop    : out Natural);
203    --  Searches form string for an entry of the form Keyword=xx and if found
204    --  Sets Start and Stop to the first and last characters of xx. Keyword
205    --  and Form must be in lower case. If no entry matches, then Start and
206    --  Stop are set to zero on return. Use_Error is raised if a malformed
207    --  string is detected, but there is no guarantee of full syntax checking.
208
209    procedure Read_Buf
210      (File : FCB.AFCB_Ptr;
211       Buf  : Address;
212       Siz  : Interfaces.C_Streams.size_t);
213    --  Reads Siz bytes from File.Stream into Buf. The caller has checked
214    --  that the file is open in read mode. Raises an exception if Siz bytes
215    --  cannot be read (End_Error if no data was read, Data_Error if a partial
216    --  buffer was read, Device_Error if an error occurs).
217
218    procedure Read_Buf
219      (File  : FCB.AFCB_Ptr;
220       Buf   : Address;
221       Siz   : Interfaces.C_Streams.size_t;
222       Count : out Interfaces.C_Streams.size_t);
223    --  Reads Siz bytes from File.Stream into Buf. The caller has checked
224    --  that the file is open in read mode. Device Error is raised if an error
225    --  occurs. Count is the actual number of bytes read, which may be less
226    --  than Siz if the end of file is encountered.
227
228    procedure Append_Set (File : FCB.AFCB_Ptr);
229    --  If the mode of the file is Append_File, then the file is positioned
230    --  at the end of file using fseek, otherwise this call has no effect.
231
232    procedure Write_Buf
233      (File : FCB.AFCB_Ptr;
234       Buf  : Address;
235       Siz  : Interfaces.C_Streams.size_t);
236    --  Writes size_t bytes to File.Stream from Buf. The caller has checked
237    --  that the file is open in write mode. Raises Device_Error if the
238    --  complete buffer cannot be written.
239
240    procedure Make_Unbuffered (File : FCB.AFCB_Ptr);
241
242    procedure Make_Line_Buffered
243      (File     : FCB.AFCB_Ptr;
244       Line_Siz : Interfaces.C_Streams.size_t);
245
246    procedure Make_Buffered
247      (File     : FCB.AFCB_Ptr;
248       Buf_Siz  : Interfaces.C_Streams.size_t);
249
250 private
251    pragma Inline (Check_Read_Status);
252    pragma Inline (Check_Write_Status);
253    pragma Inline (Mode);
254
255 end System.File_IO;