OSDN Git Service

gcc/ada/
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-direct.ads
1 ------------------------------------------------------------------------------
2 --                                                                        --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --                      A D A . D I R E C T O R I E S                       --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --          Copyright (C) 2004-2007, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- This specification is derived for use with GNAT from AI-00248,  which is --
12 -- expected to be a part of a future expected revised Ada Reference Manual. --
13 -- The copyright notice above, and the license provisions that follow apply --
14 -- solely to the  contents of the part following the private keyword.       --
15 --                                                                          --
16 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
17 -- terms of the  GNU General Public License as published  by the Free Soft- --
18 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
19 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
20 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
21 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
22 -- for  more details.  You should have  received  a copy of the GNU General --
23 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
24 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
25 -- Boston, MA 02110-1301, USA.                                              --
26 --                                                                          --
27 -- As a special exception,  if other files  instantiate  generics from this --
28 -- unit, or you link  this unit with other files  to produce an executable, --
29 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
30 -- covered  by the  GNU  General  Public  License.  This exception does not --
31 -- however invalidate  any other reasons why  the executable file  might be --
32 -- covered by the  GNU Public License.                                      --
33 --                                                                          --
34 -- GNAT was originally developed  by the GNAT team at  New York University. --
35 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
36 --                                                                          --
37 ------------------------------------------------------------------------------
38
39 --  Ada 2005: Implementation of Ada.Directories (AI95-00248). Note that this
40 --  unit is available without -gnat05. That seems reasonable, since you only
41 --  get it if you explicitly ask for it.
42
43 --  External files may be classified as directories, special files, or ordinary
44 --  files. A directory is an external file that is a container for files on
45 --  the target system. A special file is an external file that cannot be
46 --  created or read by a predefined Ada Input-Output package. External files
47 --  that are not special files or directories are called ordinary files.
48
49 --  A file name is a string identifying an external file. Similarly, a
50 --  directory name is a string identifying a directory. The interpretation of
51 --  file names and directory names is implementation-defined.
52
53 --  The full name of an external file is a full specification of the name of
54 --  the file. If the external environment allows alternative specifications of
55 --  the name (for example, abbreviations), the full name should not use such
56 --  alternatives. A full name typically will include the names of all of
57 --  directories that contain the item. The simple name of an external file is
58 --  the name of the item, not including any containing directory names. Unless
59 --  otherwise specified, a file name or directory name parameter to a
60 --  predefined Ada input-output subprogram can be a full name, a simple name,
61 --  or any other form of name supported by the implementation.
62
63 --  The default directory is the directory that is used if a directory or
64 --  file name is not a full name (that is, when the name does not fully
65 --  identify all of the containing directories).
66
67 --  A directory entry is a single item in a directory, identifying a single
68 --  external file (including directories and special files).
69
70 --  For each function that returns a string, the lower bound of the returned
71 --  value is 1.
72
73 with Ada.Calendar;
74 with Ada.Finalization;
75 with Ada.IO_Exceptions;
76 with Ada.Strings.Unbounded;
77
78 package Ada.Directories is
79
80    -----------------------------------
81    -- Directory and File Operations --
82    -----------------------------------
83
84    function Current_Directory return String;
85    --  Returns the full directory name for the current default directory. The
86    --  name returned shall be suitable for a future call to Set_Directory.
87    --  The exception Use_Error is propagated if a default directory is not
88    --  supported by the external environment.
89
90    procedure Set_Directory (Directory : String);
91    --  Sets the current default directory. The exception Name_Error is
92    --  propagated if the string given as Directory does not identify an
93    --  existing directory. The exception Use_Error is propagated if the
94    --  external environment does not support making Directory (in the absence
95    --  of Name_Error) a default directory.
96
97    procedure Create_Directory
98      (New_Directory : String;
99       Form          : String := "");
100    --  Creates a directory with name New_Directory. The Form parameter can be
101    --  used to give system-dependent characteristics of the directory; the
102    --  interpretation of the Form parameter is implementation-defined. A null
103    --  string for Form specifies the use of the default options of the
104    --  implementation of the new directory. The exception Name_Error is
105    --  propagated if the string given as New_Directory does not allow the
106    --  identification of a directory. The exception Use_Error is propagated if
107    --  the external environment does not support the creation of a directory
108    --  with the given name (in the absence of Name_Error) and form.
109
110    procedure Delete_Directory (Directory : String);
111    --  Deletes an existing empty directory with name Directory. The exception
112    --  Name_Error is propagated if the string given as Directory does not
113    --  identify an existing directory. The exception Use_Error is propagated
114    --  if the external environment does not support the deletion of the
115    --  directory (or some portion of its contents) with the given name (in the
116    --  absence of Name_Error).
117
118    procedure Create_Path
119      (New_Directory : String;
120       Form          : String := "");
121    --  Creates zero or more directories with name New_Directory. Each
122    --  non-existent directory named by New_Directory is created. For example,
123    --  on a typical Unix system, Create_Path ("/usr/me/my"); would create
124    --  directory "me" in directory "usr", then create directory "my" in
125    --  directory "me". The Form can be used to give system-dependent
126    --  characteristics of the directory; the interpretation of the Form
127    --  parameter is implementation-defined. A null string for Form specifies
128    --  the use of the default options of the implementation of the new
129    --  directory. The exception Name_Error is propagated if the string given
130    --  as New_Directory does not allow the identification of any directory.
131    --  The exception Use_Error is propagated if the external environment does
132    --  not support the creation of any directories with the given name (in the
133    --  absence of Name_Error) and form.
134
135    procedure Delete_Tree (Directory : String);
136    --  Deletes an existing directory with name Directory. The directory and
137    --  all of its contents (possibly including other directories) are deleted.
138    --  The exception Name_Error is propagated if the string given as Directory
139    --  does not identify an existing directory. The exception Use_Error is
140    --  propagated if the external environment does not support the deletion of
141    --  the directory or some portion of its contents with the given name (in
142    --  the absence of Name_Error). If Use_Error is propagated, it is
143    --  unspecified if a portion of the contents of the directory are deleted.
144
145    procedure Delete_File (Name : String);
146    --  Deletes an existing ordinary or special file with Name. The exception
147    --  Name_Error is propagated if the string given as Name does not identify
148    --  an existing ordinary or special external file. The exception Use_Error
149    --  is propagated if the external environment does not support the deletion
150    --  of the file with the given name (in the absence of Name_Error).
151
152    procedure Rename (Old_Name, New_Name : String);
153    --  Renames an existing external file (including directories) with Old_Name
154    --  to New_Name. The exception Name_Error is propagated if the string given
155    --  as Old_Name does not identify an existing external file. The exception
156    --  Use_Error is propagated if the external environment does not support the
157    --  renaming of the file with the given name (in the absence of Name_Error).
158    --  In particular, Use_Error is propagated if a file or directory already
159    --  exists with New_Name.
160
161    procedure Copy_File
162      (Source_Name   : String;
163       Target_Name   : String;
164       Form          : String := "");
165    --  Copies the contents of the existing external file with Source_Name
166    --  to Target_Name. The resulting external file is a duplicate of the source
167    --  external file. The Form can be used to give system-dependent
168    --  characteristics of the resulting external file; the interpretation of
169    --  the Form parameter is implementation-defined. Exception Name_Error is
170    --  propagated if the string given as Source_Name does not identify an
171    --  existing external ordinary or special file or if the string given as
172    --  Target_Name does not allow the identification of an external file.
173    --  The exception Use_Error is propagated if the external environment does
174    --  not support the creating of the file with the name given by Target_Name
175    --  and form given by Form, or copying of the file with the name given by
176    --  Source_Name (in the absence of Name_Error).
177
178    ----------------------------------------
179    -- File and directory name operations --
180    ----------------------------------------
181
182    function Full_Name (Name : String) return String;
183    --  Returns the full name corresponding to the file name specified by Name.
184    --  The exception Name_Error is propagated if the string given as Name does
185    --  not allow the identification of an external file (including directories
186    --  and special files).
187
188    function Simple_Name (Name : String) return String;
189    --  Returns the simple name portion of the file name specified by Name. The
190    --  exception Name_Error is propagated if the string given as Name does not
191    --  allow the identification of an external file (including directories and
192    --  special files).
193
194    function Containing_Directory (Name : String) return String;
195    --  Returns the name of the containing directory of the external file
196    --  (including directories) identified by Name. If more than one directory
197    --  can contain Name, the directory name returned is implementation-defined.
198    --  The exception Name_Error is propagated if the string given as Name does
199    --  not allow the identification of an external file. The exception
200    --  Use_Error is propagated if the external file does not have a containing
201    --  directory.
202
203    function Extension (Name : String) return String;
204    --  Returns the extension name corresponding to Name. The extension name is
205    --  a portion of a simple name (not including any separator characters),
206    --  typically used to identify the file class. If the external environment
207    --  does not have extension names, then the null string is returned.
208    --  The exception Name_Error is propagated if the string given as Name does
209    --  not allow the identification of an external file.
210
211    function Base_Name (Name : String) return String;
212    --  Returns the base name corresponding to Name. The base name is the
213    --  remainder of a simple name after removing any extension and extension
214    --  separators. The exception Name_Error is propagated if the string given
215    --  as Name does not allow the identification of an external file
216    --  (including directories and special files).
217
218    function Compose
219      (Containing_Directory : String := "";
220       Name                 : String;
221       Extension            : String := "") return String;
222    --  Returns the name of the external file with the specified
223    --  Containing_Directory, Name, and Extension. If Extension is the null
224    --  string, then Name is interpreted as a simple name; otherwise Name is
225    --  interpreted as a base name. The exception Name_Error is propagated if
226    --  the string given as Containing_Directory is not null and does not allow
227    --  the identification of a directory, or if the string given as Extension
228    --  is not null and is not a possible extension, or if the string given as
229    --  Name is not a possible simple name (if Extension is null) or base name
230    --  (if Extension is non-null).
231
232    --------------------------------
233    -- File and directory queries --
234    --------------------------------
235
236    type File_Kind is (Directory, Ordinary_File, Special_File);
237    --  The type File_Kind represents the kind of file represented by an
238    --  external file or directory.
239
240    type File_Size is range 0 .. Long_Long_Integer'Last;
241    --  The type File_Size represents the size of an external file
242
243    function Exists (Name : String) return Boolean;
244    --  Returns True if external file represented by Name exists, and False
245    --  otherwise. The exception Name_Error is propagated if the string given as
246    --  Name does not allow the identification of an external file (including
247    --  directories and special files).
248
249    function Kind (Name : String) return File_Kind;
250    --  Returns the kind of external file represented by Name. The exception
251    --  Name_Error is propagated if the string given as Name does not allow the
252    --  identification of an existing external file.
253
254    function Size (Name : String) return File_Size;
255    --  Returns the size of the external file represented by Name. The size of
256    --  an external file is the number of stream elements contained in the file.
257    --  If the external file is discontiguous (not all elements exist), the
258    --  result is implementation-defined. If the external file is not an
259    --  ordinary file, the result is implementation-defined. The exception
260    --  Name_Error is propagated if the string given as Name does not allow the
261    --  identification of an existing external file. The exception
262    --  Constraint_Error is propagated if the file size is not a value of type
263    --  File_Size.
264
265    function Modification_Time (Name : String) return Ada.Calendar.Time;
266    --  Returns the time that the external file represented by Name was most
267    --  recently modified. If the external file is not an ordinary file, the
268    --  result is implementation-defined. The exception Name_Error is propagated
269    --  if the string given as Name does not allow the identification of an
270    --  existing external file. The exception Use_Error is propagated if the
271    --  external environment does not support the reading the modification time
272    --  of the file with the name given by Name (in the absence of Name_Error).
273
274    -------------------------
275    -- Directory Searching --
276    -------------------------
277
278    type Directory_Entry_Type is limited private;
279    --  The type Directory_Entry_Type represents a single item in a directory.
280    --  These items can only be created by the Get_Next_Entry procedure in this
281    --  package. Information about the item can be obtained from the functions
282    --  declared in this package. A default initialized object of this type is
283    --  invalid; objects returned from Get_Next_Entry are valid.
284
285    type Filter_Type is array (File_Kind) of Boolean;
286    --  The type Filter_Type specifies which directory entries are provided from
287    --  a search operation. If the Directory component is True, directory
288    --  entries representing directories are provided. If the Ordinary_File
289    --  component is True, directory entries representing ordinary files are
290    --  provided. If the Special_File component is True, directory entries
291    --  representing special files are provided.
292
293    type Search_Type is limited private;
294    --  The type Search_Type contains the state of a directory search. A
295    --  default-initialized Search_Type object has no entries available
296    --  (More_Entries returns False).
297
298    procedure Start_Search
299      (Search    : in out Search_Type;
300       Directory : String;
301       Pattern   : String;
302       Filter    : Filter_Type := (others => True));
303    --  Starts a search in the directory entry in the directory named by
304    --  Directory for entries matching Pattern. Pattern represents a file name
305    --  matching pattern. If Pattern is null, all items in the directory are
306    --  matched; otherwise, the interpretation of Pattern is implementation-
307    --  defined. Only items which match Filter will be returned. After a
308    --  successful call on Start_Search, the object Search may have entries
309    --  available, but it may have no entries available if no files or
310    --  directories match Pattern and Filter. The exception Name_Error is
311    --  propagated if the string given by Directory does not identify an
312    --  existing directory, or if Pattern does not allow the identification of
313    --  any possible external file or directory. The exception Use_Error is
314    --  propagated if the external environment does not support the searching
315    --  of the directory with the given name (in the absence of Name_Error).
316
317    procedure End_Search (Search : in out Search_Type);
318    --  Ends the search represented by Search. After a successful call on
319    --  End_Search, the object Search will have no entries available. Note
320    --  that is is not necessary to call End_Search if the call to Start_Search
321    --  was unsuccessful and raised an exception (but it is harmless to make
322    --  the call in this case).
323
324    function More_Entries (Search : Search_Type) return Boolean;
325    --  Returns True if more entries are available to be returned by a call
326    --  to Get_Next_Entry for the specified search object, and False otherwise.
327
328    procedure Get_Next_Entry
329      (Search          : in out Search_Type;
330       Directory_Entry : out Directory_Entry_Type);
331    --  Returns the next Directory_Entry for the search described by Search that
332    --  matches the pattern and filter. If no further matches are available,
333    --  Status_Error is raised. It is implementation-defined as to whether the
334    --  results returned by this routine are altered if the contents of the
335    --  directory are altered while the Search object is valid (for example, by
336    --  another program). The exception Use_Error is propagated if the external
337    --  environment does not support continued searching of the directory
338    --  represented by Search.
339
340    procedure Search
341      (Directory : String;
342       Pattern   : String;
343       Filter    : Filter_Type := (others => True);
344       Process   : not null access procedure
345                                     (Directory_Entry : Directory_Entry_Type));
346    --  Searches in the directory named by Directory for entries matching
347    --  Pattern. The subprogram designated by Process is called with each
348    --  matching entry in turn. Pattern represents a pattern for matching file
349    --  names. If Pattern is null, all items in the directory are matched;
350    --  otherwise, the interpretation of Pattern is implementation-defined.
351    --  Only items that match Filter will be returned. The exception Name_Error
352    --  is propagated if the string given by Directory does not identify
353    --  an existing directory, or if Pattern does not allow the identification
354    --  of any possible external file or directory. The exception Use_Error is
355    --  propagated if the external environment does not support the searching
356    --  of the directory with the given name (in the absence of Name_Error).
357
358    -------------------------------------
359    -- Operations on Directory Entries --
360    -------------------------------------
361
362    function Simple_Name (Directory_Entry : Directory_Entry_Type) return String;
363    --  Returns the simple external name of the external file (including
364    --  directories) represented by Directory_Entry. The format of the name
365    --  returned is implementation-defined. The exception Status_Error is
366    --  propagated if Directory_Entry is invalid.
367
368    function Full_Name (Directory_Entry : Directory_Entry_Type) return String;
369    --  Returns the full external name of the external file (including
370    --  directories) represented by Directory_Entry. The format of the name
371    --  returned is implementation-defined. The exception Status_Error is
372    --  propagated if Directory_Entry is invalid.
373
374    function Kind (Directory_Entry : Directory_Entry_Type) return File_Kind;
375    --  Returns the kind of external file represented by Directory_Entry. The
376    --  exception Status_Error is propagated if Directory_Entry is invalid.
377
378    function Size (Directory_Entry : Directory_Entry_Type) return File_Size;
379    --  Returns the size of the external file represented by Directory_Entry.
380    --  The size of an external file is the number of stream elements contained
381    --  in the file. If the external file is discontiguous (not all elements
382    --  exist), the result is implementation-defined. If the external file
383    --  represented by Directory_Entry is not an ordinary file, the result is
384    --  implementation-defined. The exception Status_Error is propagated if
385    --  Directory_Entry is invalid. The exception Constraint_Error is propagated
386    --  if the file size is not a value of type File_Size.
387
388    function Modification_Time
389      (Directory_Entry : Directory_Entry_Type) return Ada.Calendar.Time;
390    --  Returns the time that the external file represented by Directory_Entry
391    --  was most recently modified. If the external file represented by
392    --  Directory_Entry is not an ordinary file, the result is
393    --  implementation-defined. The exception Status_Error is propagated if
394    --  Directory_Entry is invalid. The exception Use_Error is propagated if
395    --  the external environment does not support the reading the modification
396    --  time of the file represented by Directory_Entry.
397
398    ----------------
399    -- Exceptions --
400    ----------------
401
402    Status_Error : exception renames Ada.IO_Exceptions.Status_Error;
403    Name_Error   : exception renames Ada.IO_Exceptions.Name_Error;
404    Use_Error    : exception renames Ada.IO_Exceptions.Use_Error;
405    Device_Error : exception renames Ada.IO_Exceptions.Device_Error;
406
407 private
408    type Directory_Entry_Type is record
409       Is_Valid : Boolean := False;
410       Simple   : Ada.Strings.Unbounded.Unbounded_String;
411       Full     : Ada.Strings.Unbounded.Unbounded_String;
412       Kind     : File_Kind := Ordinary_File;
413    end record;
414
415    --  The type Search_Data is defined in the body, so that the spec does not
416    --  depend on packages of the GNAT hierarchy.
417
418    type Search_Data;
419    type Search_Ptr is access Search_Data;
420
421    --  Search_Type need to be a controlled type, because it includes component
422    --  of type Dir_Type (in GNAT.Directory_Operations) that need to be closed
423    --  (if opened) during finalization. The component need to be an access
424    --  value, because Search_Data is not fully defined in the spec.
425
426    type Search_Type is new Ada.Finalization.Controlled with record
427       Value : Search_Ptr;
428    end record;
429
430    procedure Finalize (Search : in out Search_Type);
431    --  Close the directory, if opened, and deallocate Value
432
433    procedure End_Search (Search : in out Search_Type) renames Finalize;
434
435 end Ada.Directories;