2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / ada / g-dirope.ads
blobae790de9492d84e584af8bb5da7cb47080d46db0
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T . D I R E C T O R Y _ O P E R A T I O N S --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 1998-2003 Ada Core Technologies, 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 ------------------------------------------------------------------------------
34 -- Directory operations
36 -- This package provides routines for manipulating directories. A directory
37 -- can be treated as a file, using open and close routines, and a scanning
38 -- routine is provided for iterating through the entries in a directory.
40 -- See also child package GNAT.Directory_Operations.Iteration
42 -- Note: support on OpenVMS is limited to the support of Unix-style
43 -- directory names (OpenVMS native directory format is not supported).
44 -- Read individual entries for more specific notes on OpenVMS support.
46 with Ada.Strings.Maps;
48 package GNAT.Directory_Operations is
50 subtype Dir_Name_Str is String;
51 -- A subtype used in this package to represent string values that are
52 -- directory names. A directory name is a prefix for files that appear
53 -- with in the directory. This means that for UNIX systems, the string
54 -- includes a final '/', and for DOS-like systems, it includes a final
55 -- '\' character. It can also include drive letters if the operating
56 -- system provides for this. The final '/' or '\' in a Dir_Name_Str is
57 -- optional when passed as a procedure or function in parameter.
58 -- On OpenVMS, only Unix style path names are supported, not VMS style,
59 -- but the directory and file names are not case sensitive.
61 type Dir_Type is limited private;
62 -- A value used to reference a directory. Conceptually this value includes
63 -- the identity of the directory, and a sequential position within it.
65 Null_Dir : constant Dir_Type;
66 -- Represent the value for an uninitialized or closed directory
68 Directory_Error : exception;
69 -- Exception raised if the directory cannot be opened, read, closed,
70 -- created or if it is not possible to change the current execution
71 -- environment directory.
73 Dir_Separator : constant Character;
74 -- Running system default directory separator
76 --------------------------------
77 -- Basic Directory operations --
78 --------------------------------
80 procedure Change_Dir (Dir_Name : Dir_Name_Str);
81 -- Changes the working directory of the current execution environment
82 -- to the directory named by Dir_Name. Raises Directory_Error if Dir_Name
83 -- does not exist.
85 procedure Make_Dir (Dir_Name : Dir_Name_Str);
86 -- Create a new directory named Dir_Name. Raises Directory_Error if
87 -- Dir_Name cannot be created.
89 procedure Remove_Dir
90 (Dir_Name : Dir_Name_Str;
91 Recursive : Boolean := False);
92 -- Remove the directory named Dir_Name. If Recursive is set to True, then
93 -- Remove_Dir removes all the subdirectories and files that are in
94 -- Dir_Name. Raises Directory_Error if Dir_Name cannot be removed.
96 function Get_Current_Dir return Dir_Name_Str;
97 -- Returns the current working directory for the execution environment.
99 procedure Get_Current_Dir (Dir : out Dir_Name_Str; Last : out Natural);
100 -- Returns the current working directory for the execution environment
101 -- The name is returned in Dir_Name. Last is the index in Dir_Name such
102 -- that Dir_Name (Last) is the last character written. If Dir_Name is
103 -- too small for the directory name, the name will be truncated before
104 -- being copied to Dir_Name.
106 -------------------------
107 -- Pathname Operations --
108 -------------------------
110 subtype Path_Name is String;
111 -- All routines using Path_Name handle both styles (UNIX and DOS) of
112 -- directory separators (either slash or back slash).
114 function Dir_Name (Path : Path_Name) return Dir_Name_Str;
115 -- Returns directory name for Path. This is similar to the UNIX dirname
116 -- command. Everything after the last directory separator is removed. If
117 -- there is no directory separator the current working directory is
118 -- returned. Note that the contents of Path is case-sensitive on
119 -- systems that have case-sensitive file names (like Unix), and
120 -- non-case-sensitive on systems where the file system is also non-
121 -- case-sensitive (such as Windows, and OpenVMS).
123 function Base_Name
124 (Path : Path_Name;
125 Suffix : String := "")
126 return String;
127 -- Any directory prefix is removed. If Suffix is non-empty and is a
128 -- suffix of Path, it is removed. This is equivalent to the UNIX basename
129 -- command. The following rule is always true:
131 -- 'Path' and 'Dir_Name (Path) & Directory_Separator & Base_Name (Path)'
132 -- represent the same file.
134 -- This function is not case-sensitive on systems that have a non
135 -- case-sensitive file system like Windows and OpenVMS.
137 function File_Extension (Path : Path_Name) return String;
138 -- Return the file extension. This is defined as the string after the
139 -- last dot, including the dot itself. For example, if the file name
140 -- is "file1.xyz.adq", then the returned value would be ".adq". If no
141 -- dot is present in the file name, or the last character of the file
142 -- name is a dot, then the null string is returned.
144 function File_Name (Path : Path_Name) return String;
145 -- Returns the file name and the file extension if present. It removes all
146 -- path information. This is equivalent to Base_Name with default Extension
147 -- value.
149 type Path_Style is
150 (UNIX,
151 -- Use '/' as the directory separator. The default on Unix systems
152 -- and on OpenVMS.
154 DOS,
155 -- Use '\' as the directory separator. The default on Windows.
157 System_Default);
159 function Format_Pathname
160 (Path : Path_Name;
161 Style : Path_Style := System_Default)
162 return Path_Name;
163 -- Removes all double directory separator and converts all '\' to '/' if
164 -- Style is UNIX and converts all '/' to '\' if Style is set to DOS. This
165 -- function will help to provide a consistent naming scheme running for
166 -- different environments. If style is set to System_Default the routine
167 -- will use the default directory separator on the running environment.
169 type Environment_Style is
170 (UNIX,
171 -- Environment variables and OpenVMS logical names use $ as prefix and
172 -- can use curly brackets as in ${HOME}/mydir. If there is no closing
173 -- curly bracket for an opening one then translation is done, so for
174 -- example ${VAR/toto is returned as ${VAR/toto.
176 DOS,
177 -- Environment variables uses % as prefix and suffix
178 -- (e.g. %HOME%/mydir). The name DOS refer to "DOS-like" environment.
179 -- This includes al Windows systems.
181 Both,
182 -- Recognize both forms described above.
184 System_Default);
185 -- Uses either UNIX on Unix and OpenVMS systems, or DOS on Windows and
186 -- OS/2 depending on the running environment.
188 function Expand_Path
189 (Path : Path_Name;
190 Mode : Environment_Style := System_Default)
191 return Path_Name;
192 -- Returns Path with environment variables (or logical names on OpenVMS)
193 -- replaced by the current environment variable value. For example,
194 -- $HOME/mydir will be replaced by /home/joe/mydir if $HOME environment
195 -- variable is set to /home/joe and Mode is UNIX. If an environment
196 -- variable does not exists the variable will be replaced by the empty
197 -- string. Two dollar or percent signs are replaced by a single
198 -- dollar/percent sign. Note that a variable must start with a letter.
200 ---------------
201 -- Iterators --
202 ---------------
204 procedure Open (Dir : out Dir_Type; Dir_Name : Dir_Name_Str);
205 -- Opens the directory named by Dir_Name and returns a Dir_Type value
206 -- that refers to this directory, and is positioned at the first entry.
207 -- Raises Directory_Error if Dir_Name cannot be accessed. In that case
208 -- Dir will be set to Null_Dir.
210 procedure Close (Dir : in out Dir_Type);
211 -- Closes the directory stream refered to by Dir. After calling Close
212 -- Is_Open will return False. Dir will be set to Null_Dir.
213 -- Raises Directory_Error if Dir has not be opened (Dir = Null_Dir).
215 function Is_Open (Dir : Dir_Type) return Boolean;
216 -- Returns True if Dir is open, or False otherwise.
218 procedure Read
219 (Dir : in out Dir_Type;
220 Str : out String;
221 Last : out Natural);
222 -- Reads the next entry from the directory and sets Str to the name
223 -- of that entry. Last is the index in Str such that Str (Last) is the
224 -- last character written. Last is 0 when there are no more files in the
225 -- directory. If Str is too small for the file name, the file name will
226 -- be truncated before being copied to Str. The list of files returned
227 -- includes directories in systems providing a hierarchical directory
228 -- structure, including . (the current directory) and .. (the parent
229 -- directory) in systems providing these entries. The directory is
230 -- returned in target-OS form. Raises Directory_Error if Dir has not
231 -- be opened (Dir = Null_Dir).
233 function Read_Is_Thread_Safe return Boolean;
234 -- Indicates if procedure Read is thread safe. On systems where the
235 -- target system supports this functionality, Read is thread safe,
236 -- and this function returns True (e.g. this will be the case on any
237 -- UNIX or UNIX-like system providing a correct implementation of the
238 -- function readdir_r). If the system cannot provide a thread safe
239 -- implementation of Read, then this function returns False.
241 private
243 type Dir_Type_Value;
244 type Dir_Type is access Dir_Type_Value;
246 Null_Dir : constant Dir_Type := null;
248 pragma Import (C, Dir_Separator, "__gnat_dir_separator");
250 Dir_Seps : constant Ada.Strings.Maps.Character_Set :=
251 Ada.Strings.Maps.To_Set ("/\");
252 -- UNIX and DOS style directory separators.
254 end GNAT.Directory_Operations;