Nathanael Nerode <neroden@gcc.gnu.org> PR ada/5904
[gcc.git] / gcc / ada / g-dirope.ads
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 -- --
10 -- Copyright (C) 1998-2001 Ada Core Technologies, Inc. --
11 -- --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
22 -- --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
29 -- --
30 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
31 -- --
32 ------------------------------------------------------------------------------
33
34 -- Directory operations
35
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.
39
40 -- See also child package GNAT.Directory_Operations.Iteration
41
42 with Ada.Strings.Maps;
43
44 package GNAT.Directory_Operations is
45
46 subtype Dir_Name_Str is String;
47 -- A subtype used in this package to represent string values that are
48 -- directory names. A directory name is a prefix for files that appear
49 -- with in the directory. This means that for UNIX systems, the string
50 -- includes a final '/', and for DOS-like systems, it includes a final
51 -- '\' character. It can also include drive letters if the operating
52 -- system provides for this. The final '/' or '\' in a Dir_Name_Str is
53 -- optional when passed as a procedure or function in parameter.
54
55 type Dir_Type is limited private;
56 -- A value used to reference a directory. Conceptually this value includes
57 -- the identity of the directory, and a sequential position within it.
58
59 Null_Dir : constant Dir_Type;
60 -- Represent the value for an uninitialized or closed directory
61
62 Directory_Error : exception;
63 -- Exception raised if the directory cannot be opened, read, closed,
64 -- created or if it is not possible to change the current execution
65 -- environment directory.
66
67 Dir_Separator : constant Character;
68 -- Running system default directory separator
69
70 --------------------------------
71 -- Basic Directory operations --
72 --------------------------------
73
74 procedure Change_Dir (Dir_Name : Dir_Name_Str);
75 -- Changes the working directory of the current execution environment
76 -- to the directory named by Dir_Name. Raises Directory_Error if Dir_Name
77 -- does not exist.
78
79 procedure Make_Dir (Dir_Name : Dir_Name_Str);
80 -- Create a new directory named Dir_Name. Raises Directory_Error if
81 -- Dir_Name cannot be created.
82
83 procedure Remove_Dir (Dir_Name : Dir_Name_Str);
84 -- Remove the directory named Dir_Name. Raises Directory_Error if Dir_Name
85 -- cannot be removed.
86
87 function Get_Current_Dir return Dir_Name_Str;
88 -- Returns the current working directory for the execution environment.
89
90 procedure Get_Current_Dir (Dir : out Dir_Name_Str; Last : out Natural);
91 -- Returns the current working directory for the execution environment
92 -- The name is returned in Dir_Name. Last is the index in Dir_Name such
93 -- that Dir_Name (Last) is the last character written. If Dir_Name is
94 -- too small for the directory name, the name will be truncated before
95 -- being copied to Dir_Name.
96
97 -------------------------
98 -- Pathname Operations --
99 -------------------------
100
101 subtype Path_Name is String;
102 -- All routines using Path_Name handle both styles (UNIX and DOS) of
103 -- directory separators (either slash or back slash).
104
105 function Dir_Name (Path : Path_Name) return Dir_Name_Str;
106 -- Returns directory name for Path. This is similar to the UNIX dirname
107 -- command. Everything after the last directory separator is removed. If
108 -- there is no directory separator the current working directory is
109 -- returned.
110
111 function Base_Name
112 (Path : Path_Name;
113 Suffix : String := "")
114 return String;
115 -- Any directory prefix is removed. If Suffix is non-empty and is a
116 -- suffix of Path, it is removed. This is equivalent to the UNIX basename
117 -- command. The following rule is always true:
118 --
119 -- 'Path' and 'Dir_Name (Path) & Directory_Separator & Base_Name (Path)'
120 -- represent the same file.
121 --
122 -- This function is not case-sensitive on systems that have a non
123 -- case-sensitive file system like Windows, OS/2 and VMS.
124
125 function File_Extension (Path : Path_Name) return String;
126 -- Return the file extension. This is the string after the last dot
127 -- character in File_Name (Path). It returns the empty string if no
128 -- extension is found. The returned value does contains the file
129 -- extension separator (dot character).
130
131 function File_Name (Path : Path_Name) return String;
132 -- Returns the file name and the file extension if present. It removes all
133 -- path information. This is equivalent to Base_Name with default Extension
134 -- value.
135
136 type Path_Style is (UNIX, DOS, System_Default);
137
138 function Format_Pathname
139 (Path : Path_Name;
140 Style : Path_Style := System_Default)
141 return Path_Name;
142 -- Removes all double directory separator and converts all '\' to '/' if
143 -- Style is UNIX and converts all '/' to '\' if Style is set to DOS. This
144 -- function will help to provide a consistent naming scheme running for
145 -- different environments. If style is set to System_Default the routine
146 -- will use the default directory separator on the running environment.
147
148 function Expand_Path (Path : Path_Name) return Path_Name;
149 -- Returns Path with environment variables (string preceded by a dollar
150 -- sign) replaced by the current environment variable value. For example,
151 -- $HOME/mydir will be replaced by /home/joe/mydir if $HOME environment
152 -- variable is set to /home/joe. The variable can be surrounded by the
153 -- characters '{' and '}' (curly bracket) if needed as in ${HOME}/mydir.
154 -- If an environment variable does not exists the variable will be replaced
155 -- by the empty string. Two dollar signs are replaced by a single dollar
156 -- sign. Note that a variable must start with a letter. If there is no
157 -- closing curly bracket for an opening one there is no translation done,
158 -- so for example ${VAR/toto is returned as ${VAR/toto.
159
160 ---------------
161 -- Iterators --
162 ---------------
163
164 procedure Open (Dir : out Dir_Type; Dir_Name : Dir_Name_Str);
165 -- Opens the directory named by Dir_Name and returns a Dir_Type value
166 -- that refers to this directory, and is positioned at the first entry.
167 -- Raises Directory_Error if Dir_Name cannot be accessed. In that case
168 -- Dir will be set to Null_Dir.
169
170 procedure Close (Dir : in out Dir_Type);
171 -- Closes the directory stream refered to by Dir. After calling Close
172 -- Is_Open will return False. Dir will be set to Null_Dir.
173 -- Raises Directory_Error if Dir has not be opened (Dir = Null_Dir).
174
175 function Is_Open (Dir : Dir_Type) return Boolean;
176 -- Returns True if Dir is open, or False otherwise.
177
178 procedure Read
179 (Dir : in out Dir_Type;
180 Str : out String;
181 Last : out Natural);
182 -- Reads the next entry from the directory and sets Str to the name
183 -- of that entry. Last is the index in Str such that Str (Last) is the
184 -- last character written. Last is 0 when there are no more files in the
185 -- directory. If Str is too small for the file name, the file name will
186 -- be truncated before being copied to Str. The list of files returned
187 -- includes directories in systems providing a hierarchical directory
188 -- structure, including . (the current directory) and .. (the parent
189 -- directory) in systems providing these entries. The directory is
190 -- returned in target-OS form. Raises Directory_Error if Dir has not
191 -- be opened (Dir = Null_Dir).
192
193 function Read_Is_Thread_Safe return Boolean;
194 -- Indicates if procedure Read is thread safe. On systems where the
195 -- target system supports this functionality, Read is thread safe,
196 -- and this function returns True (e.g. this will be the case on any
197 -- UNIX or UNIX-like system providing a correct implementation of the
198 -- function readdir_r). If the system cannot provide a thread safe
199 -- implementation of Read, then this function returns False.
200
201 private
202
203 type Dir_Type_Value;
204 type Dir_Type is access Dir_Type_Value;
205
206 Null_Dir : constant Dir_Type := null;
207
208 pragma Import (C, Dir_Separator, "__gnat_dir_separator");
209
210 Dir_Seps : constant Ada.Strings.Maps.Character_Set :=
211 Ada.Strings.Maps.To_Set ("/\");
212 -- UNIX and DOS style directory separators.
213
214 end GNAT.Directory_Operations;