2015-06-23 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / ada / g-dirope.ads
blobc3c207f2e99e65a85a853242aa0d3d78d7ed56d0
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-2014, AdaCore --
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 -- Directory operations
34 -- This package provides routines for manipulating directories. A directory
35 -- can be treated as a file, using open and close routines, and a scanning
36 -- routine is provided for iterating through the entries in a directory.
38 -- See also child package GNAT.Directory_Operations.Iteration
40 with System;
41 with Ada.Strings.Maps;
43 package GNAT.Directory_Operations is
45 subtype Dir_Name_Str is String;
46 -- A subtype used in this package to represent string values that are
47 -- directory names. A directory name is a prefix for files that appear
48 -- with in the directory. This means that for UNIX systems, the string
49 -- includes a final '/', and for DOS-like systems, it includes a final
50 -- '\' character. It can also include drive letters if the operating
51 -- system provides for this. The final '/' or '\' in a Dir_Name_Str is
52 -- optional when passed as a procedure or function in parameter.
54 type Dir_Type is limited private;
55 -- A value used to reference a directory. Conceptually this value includes
56 -- the identity of the directory, and a sequential position within it.
58 Null_Dir : constant Dir_Type;
59 -- Represent the value for an uninitialized or closed directory
61 Directory_Error : exception;
62 -- Exception raised if the directory cannot be opened, read, closed,
63 -- created or if it is not possible to change the current execution
64 -- environment directory.
66 Dir_Separator : constant Character;
67 -- Running system default directory separator
69 --------------------------------
70 -- Basic Directory operations --
71 --------------------------------
73 procedure Change_Dir (Dir_Name : Dir_Name_Str);
74 -- Changes the working directory of the current execution environment
75 -- to the directory named by Dir_Name. Raises Directory_Error if Dir_Name
76 -- does not exist.
78 procedure Make_Dir (Dir_Name : Dir_Name_Str);
79 -- Create a new directory named Dir_Name. Raises Directory_Error if
80 -- Dir_Name cannot be created.
82 procedure Remove_Dir
83 (Dir_Name : Dir_Name_Str;
84 Recursive : Boolean := False);
85 -- Remove the directory named Dir_Name. If Recursive is set to True, then
86 -- Remove_Dir removes all the subdirectories and files that are in
87 -- Dir_Name. Raises Directory_Error if Dir_Name cannot be removed.
89 function Get_Current_Dir return Dir_Name_Str;
90 -- Returns the current working directory for the execution environment
92 procedure Get_Current_Dir (Dir : out Dir_Name_Str; Last : out Natural);
93 -- Returns the current working directory for the execution environment
94 -- The name is returned in Dir_Name. Last is the index in Dir_Name such
95 -- that Dir_Name (Last) is the last character written. If Dir_Name is
96 -- too small for the directory name, the name will be truncated before
97 -- being copied to Dir_Name.
99 -------------------------
100 -- Pathname Operations --
101 -------------------------
103 subtype Path_Name is String;
104 -- All routines using Path_Name handle both styles (UNIX and DOS) of
105 -- directory separators (either slash or back slash).
107 function Dir_Name (Path : Path_Name) return Dir_Name_Str;
108 -- Returns directory name for Path. This is similar to the UNIX dirname
109 -- command. Everything after the last directory separator is removed. If
110 -- there is no directory separator the current working directory is
111 -- returned. Note that the contents of Path is case-sensitive on
112 -- systems that have case-sensitive file names (like Unix), and
113 -- non-case-sensitive on systems where the file system is also non-
114 -- case-sensitive (such as Windows).
116 function Base_Name
117 (Path : Path_Name;
118 Suffix : String := "") return String;
119 -- Any directory prefix is removed. A directory prefix is defined as
120 -- text up to and including the last directory separator character in
121 -- the input string. In addition if Path ends with the string given for
122 -- Suffix, then it is also removed. Note that Suffix here can be an
123 -- arbitrary string (it is not required to be a file extension). This
124 -- is equivalent to the UNIX basename command. The following rule is
125 -- always true:
127 -- 'Path' and 'Dir_Name (Path) & Dir_Separator & Base_Name (Path)'
128 -- represent the same file.
130 -- The comparison of Suffix is case-insensitive on systems like Windows
131 -- where the file search is case-insensitive (e.g. on such systems,
132 -- Base_Name ("/Users/AdaCore/BB12.patch", ".Patch") returns "BB12").
134 -- Note that the index bounds of the result match the corresponding indexes
135 -- in the Path string (you cannot assume that the lower bound of the
136 -- returned string is one).
138 function File_Extension (Path : Path_Name) return String;
139 -- Return the file extension. This is defined as the string after the
140 -- last dot, including the dot itself. For example, if the file name
141 -- is "file1.xyz.adq", then the returned value would be ".adq". If no
142 -- dot is present in the file name, or the last character of the file
143 -- name is a dot, then the null string is returned.
145 function File_Name (Path : Path_Name) return String;
146 -- Returns the file name and the file extension if present. It removes all
147 -- path information. This is equivalent to Base_Name with default Extension
148 -- value.
150 type Path_Style is (UNIX, DOS, System_Default);
151 function Format_Pathname
152 (Path : Path_Name;
153 Style : Path_Style := System_Default) return Path_Name;
154 -- Removes all double directory separator and converts all '\' to '/' if
155 -- Style is UNIX and converts all '/' to '\' if Style is set to DOS. This
156 -- function will help to provide a consistent naming scheme running for
157 -- different environments. If style is set to System_Default the routine
158 -- will use the default directory separator on the running environment.
160 -- The Style argument indicates the syntax to be used for path names:
162 -- DOS
163 -- Use '\' as the directory separator (default on Windows)
165 -- UNIX
166 -- Use '/' as the directory separator (default on all other systems)
168 -- System_Default
169 -- Use the default style for the current system
171 type Environment_Style is (UNIX, DOS, Both, System_Default);
172 function Expand_Path
173 (Path : Path_Name;
174 Mode : Environment_Style := System_Default) return Path_Name;
175 -- Returns Path with environment variables replaced by the current
176 -- environment variable value. For example, $HOME/mydir will be replaced
177 -- by /home/joe/mydir if $HOME environment variable is set to /home/joe and
178 -- Mode is UNIX. If an environment variable does not exist the variable
179 -- will be replaced by the empty string. Two dollar or percent signs are
180 -- replaced by a single dollar/percent sign. Note that a variable must
181 -- start with a letter.
183 -- The Mode argument indicates the recognized syntax for environment
184 -- variables as follows:
186 -- UNIX
187 -- Environment variables use $ as prefix and can use curly brackets
188 -- as in ${HOME}/mydir. If there is no closing curly bracket for an
189 -- opening one then no translation is done, so for example ${VAR/toto
190 -- is returned as ${VAR/toto. The use of {} brackets is required if
191 -- the environment variable name contains other than alphanumeric
192 -- characters.
194 -- DOS
195 -- Environment variables uses % as prefix and suffix (e.g. %HOME%/dir).
196 -- The name DOS refer to "DOS-like" environment. This includes all
197 -- Windows systems.
199 -- Both
200 -- Recognize both forms described above.
202 -- System_Default
203 -- Uses either DOS on Windows, and UNIX on all other systems, depending
204 -- on the running environment.
206 ---------------
207 -- Iterators --
208 ---------------
210 procedure Open (Dir : out Dir_Type; Dir_Name : Dir_Name_Str);
211 -- Opens the directory named by Dir_Name and returns a Dir_Type value
212 -- that refers to this directory, and is positioned at the first entry.
213 -- Raises Directory_Error if Dir_Name cannot be accessed. In that case
214 -- Dir will be set to Null_Dir.
216 procedure Close (Dir : in out Dir_Type);
217 -- Closes the directory stream referred to by Dir. After calling Close
218 -- Is_Open will return False. Dir will be set to Null_Dir.
219 -- Raises Directory_Error if Dir has not be opened (Dir = Null_Dir).
221 function Is_Open (Dir : Dir_Type) return Boolean;
222 -- Returns True if Dir is open, or False otherwise
224 procedure Read
225 (Dir : Dir_Type;
226 Str : out String;
227 Last : out Natural);
228 -- Reads the next entry from the directory and sets Str to the name
229 -- of that entry. Last is the index in Str such that Str (Last) is the
230 -- last character written. Last is 0 when there are no more files in the
231 -- directory. If Str is too small for the file name, the file name will
232 -- be truncated before being copied to Str. The list of files returned
233 -- includes directories in systems providing a hierarchical directory
234 -- structure, including . (the current directory) and .. (the parent
235 -- directory) in systems providing these entries. The directory is
236 -- returned in target-OS form. Raises Directory_Error if Dir has not
237 -- be opened (Dir = Null_Dir).
239 function Read_Is_Thread_Safe return Boolean;
240 -- Indicates if procedure Read is thread safe. On systems where the
241 -- target system supports this functionality, Read is thread safe,
242 -- and this function returns True (e.g. this will be the case on any
243 -- UNIX or UNIX-like system providing a correct implementation of the
244 -- function readdir_r). If the system cannot provide a thread safe
245 -- implementation of Read, then this function returns False.
247 private
249 type Dir_Type_Value is new System.Address;
250 -- Low-level address directory structure as returned by opendir in C
252 -- Note that we used to define this type in the body of this package,
253 -- but this was causing troubles in the context of .NET code generation
254 -- (because Taft amendment types are not fully implemented and cause
255 -- undefined references to the class), so we moved the type declaration
256 -- to the spec's private part, which is no problem in any case here.
258 type Dir_Type is access Dir_Type_Value;
260 Null_Dir : constant Dir_Type := null;
262 pragma Import (C, Dir_Separator, "__gnat_dir_separator");
264 Dir_Seps : constant Ada.Strings.Maps.Character_Set :=
265 Ada.Strings.Maps.To_Set ("/\");
266 -- UNIX and DOS style directory separators
268 end GNAT.Directory_Operations;