annotate gcc/ada/libgnat/g-dirope.ads @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT COMPILER COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- G N A T . D I R E C T O R Y _ O P E R A T I O N S --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
9 -- Copyright (C) 1998-2019, AdaCore --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 -- Directory operations
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 -- This package provides routines for manipulating directories. A directory
kono
parents:
diff changeset
35 -- can be treated as a file, using open and close routines, and a scanning
kono
parents:
diff changeset
36 -- routine is provided for iterating through the entries in a directory.
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 -- See also child package GNAT.Directory_Operations.Iteration
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 with System;
kono
parents:
diff changeset
41 with Ada.Strings.Maps;
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 package GNAT.Directory_Operations is
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 subtype Dir_Name_Str is String;
kono
parents:
diff changeset
46 -- A subtype used in this package to represent string values that are
kono
parents:
diff changeset
47 -- directory names. A directory name is a prefix for files that appear
kono
parents:
diff changeset
48 -- with in the directory. This means that for UNIX systems, the string
kono
parents:
diff changeset
49 -- includes a final '/', and for DOS-like systems, it includes a final
kono
parents:
diff changeset
50 -- '\' character. It can also include drive letters if the operating
kono
parents:
diff changeset
51 -- system provides for this. The final '/' or '\' in a Dir_Name_Str is
kono
parents:
diff changeset
52 -- optional when passed as a procedure or function in parameter.
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 type Dir_Type is limited private;
kono
parents:
diff changeset
55 -- A value used to reference a directory. Conceptually this value includes
kono
parents:
diff changeset
56 -- the identity of the directory, and a sequential position within it.
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 Null_Dir : constant Dir_Type;
kono
parents:
diff changeset
59 -- Represent the value for an uninitialized or closed directory
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 Directory_Error : exception;
kono
parents:
diff changeset
62 -- Exception raised if the directory cannot be opened, read, closed,
kono
parents:
diff changeset
63 -- created or if it is not possible to change the current execution
kono
parents:
diff changeset
64 -- environment directory.
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 Dir_Separator : constant Character;
kono
parents:
diff changeset
67 -- Running system default directory separator
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 --------------------------------
kono
parents:
diff changeset
70 -- Basic Directory operations --
kono
parents:
diff changeset
71 --------------------------------
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 procedure Change_Dir (Dir_Name : Dir_Name_Str);
kono
parents:
diff changeset
74 -- Changes the working directory of the current execution environment
kono
parents:
diff changeset
75 -- to the directory named by Dir_Name. Raises Directory_Error if Dir_Name
kono
parents:
diff changeset
76 -- does not exist.
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 procedure Make_Dir (Dir_Name : Dir_Name_Str);
kono
parents:
diff changeset
79 -- Create a new directory named Dir_Name. Raises Directory_Error if
kono
parents:
diff changeset
80 -- Dir_Name cannot be created.
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 procedure Remove_Dir
kono
parents:
diff changeset
83 (Dir_Name : Dir_Name_Str;
kono
parents:
diff changeset
84 Recursive : Boolean := False);
kono
parents:
diff changeset
85 -- Remove the directory named Dir_Name. If Recursive is set to True, then
kono
parents:
diff changeset
86 -- Remove_Dir removes all the subdirectories and files that are in
kono
parents:
diff changeset
87 -- Dir_Name. Raises Directory_Error if Dir_Name cannot be removed.
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 function Get_Current_Dir return Dir_Name_Str;
kono
parents:
diff changeset
90 -- Returns the current working directory for the execution environment
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 procedure Get_Current_Dir (Dir : out Dir_Name_Str; Last : out Natural);
kono
parents:
diff changeset
93 -- Returns the current working directory for the execution environment
kono
parents:
diff changeset
94 -- The name is returned in Dir_Name. Last is the index in Dir_Name such
kono
parents:
diff changeset
95 -- that Dir_Name (Last) is the last character written. If Dir_Name is
kono
parents:
diff changeset
96 -- too small for the directory name, the name will be truncated before
kono
parents:
diff changeset
97 -- being copied to Dir_Name.
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 -------------------------
kono
parents:
diff changeset
100 -- Pathname Operations --
kono
parents:
diff changeset
101 -------------------------
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 subtype Path_Name is String;
kono
parents:
diff changeset
104 -- All routines using Path_Name handle both styles (UNIX and DOS) of
kono
parents:
diff changeset
105 -- directory separators (either slash or back slash).
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 function Dir_Name (Path : Path_Name) return Dir_Name_Str;
kono
parents:
diff changeset
108 -- Returns directory name for Path. This is similar to the UNIX dirname
kono
parents:
diff changeset
109 -- command. Everything after the last directory separator is removed. If
kono
parents:
diff changeset
110 -- there is no directory separator the current working directory is
kono
parents:
diff changeset
111 -- returned. Note that the contents of Path is case-sensitive on
kono
parents:
diff changeset
112 -- systems that have case-sensitive file names (like Unix), and
kono
parents:
diff changeset
113 -- non-case-sensitive on systems where the file system is also non-
kono
parents:
diff changeset
114 -- case-sensitive (such as Windows).
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 function Base_Name
kono
parents:
diff changeset
117 (Path : Path_Name;
kono
parents:
diff changeset
118 Suffix : String := "") return String;
kono
parents:
diff changeset
119 -- Any directory prefix is removed. A directory prefix is defined as
kono
parents:
diff changeset
120 -- text up to and including the last directory separator character in
kono
parents:
diff changeset
121 -- the input string. In addition if Path ends with the string given for
kono
parents:
diff changeset
122 -- Suffix, then it is also removed. Note that Suffix here can be an
kono
parents:
diff changeset
123 -- arbitrary string (it is not required to be a file extension). This
kono
parents:
diff changeset
124 -- is equivalent to the UNIX basename command. The following rule is
kono
parents:
diff changeset
125 -- always true:
kono
parents:
diff changeset
126 --
kono
parents:
diff changeset
127 -- 'Path' and 'Dir_Name (Path) & Dir_Separator & Base_Name (Path)'
kono
parents:
diff changeset
128 -- represent the same file.
kono
parents:
diff changeset
129 --
kono
parents:
diff changeset
130 -- The comparison of Suffix is case-insensitive on systems like Windows
kono
parents:
diff changeset
131 -- where the file search is case-insensitive (e.g. on such systems,
kono
parents:
diff changeset
132 -- Base_Name ("/Users/AdaCore/BB12.patch", ".Patch") returns "BB12").
kono
parents:
diff changeset
133 --
kono
parents:
diff changeset
134 -- Note that the index bounds of the result match the corresponding indexes
kono
parents:
diff changeset
135 -- in the Path string (you cannot assume that the lower bound of the
kono
parents:
diff changeset
136 -- returned string is one).
kono
parents:
diff changeset
137
kono
parents:
diff changeset
138 function File_Extension (Path : Path_Name) return String;
kono
parents:
diff changeset
139 -- Return the file extension. This is defined as the string after the
kono
parents:
diff changeset
140 -- last dot, including the dot itself. For example, if the file name
kono
parents:
diff changeset
141 -- is "file1.xyz.adq", then the returned value would be ".adq". If no
kono
parents:
diff changeset
142 -- dot is present in the file name, or the last character of the file
kono
parents:
diff changeset
143 -- name is a dot, then the null string is returned.
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 function File_Name (Path : Path_Name) return String;
kono
parents:
diff changeset
146 -- Returns the file name and the file extension if present. It removes all
kono
parents:
diff changeset
147 -- path information. This is equivalent to Base_Name with default Extension
kono
parents:
diff changeset
148 -- value.
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 type Path_Style is (UNIX, DOS, System_Default);
kono
parents:
diff changeset
151 function Format_Pathname
kono
parents:
diff changeset
152 (Path : Path_Name;
kono
parents:
diff changeset
153 Style : Path_Style := System_Default) return Path_Name;
kono
parents:
diff changeset
154 -- Removes all double directory separator and converts all '\' to '/' if
kono
parents:
diff changeset
155 -- Style is UNIX and converts all '/' to '\' if Style is set to DOS. This
kono
parents:
diff changeset
156 -- function will help to provide a consistent naming scheme running for
kono
parents:
diff changeset
157 -- different environments. If style is set to System_Default the routine
kono
parents:
diff changeset
158 -- will use the default directory separator on the running environment.
kono
parents:
diff changeset
159 --
kono
parents:
diff changeset
160 -- The Style argument indicates the syntax to be used for path names:
kono
parents:
diff changeset
161 --
kono
parents:
diff changeset
162 -- DOS
kono
parents:
diff changeset
163 -- Use '\' as the directory separator (default on Windows)
kono
parents:
diff changeset
164 --
kono
parents:
diff changeset
165 -- UNIX
kono
parents:
diff changeset
166 -- Use '/' as the directory separator (default on all other systems)
kono
parents:
diff changeset
167 --
kono
parents:
diff changeset
168 -- System_Default
kono
parents:
diff changeset
169 -- Use the default style for the current system
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 type Environment_Style is (UNIX, DOS, Both, System_Default);
kono
parents:
diff changeset
172 function Expand_Path
kono
parents:
diff changeset
173 (Path : Path_Name;
kono
parents:
diff changeset
174 Mode : Environment_Style := System_Default) return Path_Name;
kono
parents:
diff changeset
175 -- Returns Path with environment variables replaced by the current
kono
parents:
diff changeset
176 -- environment variable value. For example, $HOME/mydir will be replaced
kono
parents:
diff changeset
177 -- by /home/joe/mydir if $HOME environment variable is set to /home/joe and
kono
parents:
diff changeset
178 -- Mode is UNIX. If an environment variable does not exist the variable
kono
parents:
diff changeset
179 -- will be replaced by the empty string. Two dollar or percent signs are
kono
parents:
diff changeset
180 -- replaced by a single dollar/percent sign. Note that a variable must
kono
parents:
diff changeset
181 -- start with a letter.
kono
parents:
diff changeset
182 --
kono
parents:
diff changeset
183 -- The Mode argument indicates the recognized syntax for environment
kono
parents:
diff changeset
184 -- variables as follows:
kono
parents:
diff changeset
185 --
kono
parents:
diff changeset
186 -- UNIX
kono
parents:
diff changeset
187 -- Environment variables use $ as prefix and can use curly brackets
kono
parents:
diff changeset
188 -- as in ${HOME}/mydir. If there is no closing curly bracket for an
kono
parents:
diff changeset
189 -- opening one then no translation is done, so for example ${VAR/toto
kono
parents:
diff changeset
190 -- is returned as ${VAR/toto. The use of {} brackets is required if
kono
parents:
diff changeset
191 -- the environment variable name contains other than alphanumeric
kono
parents:
diff changeset
192 -- characters.
kono
parents:
diff changeset
193 --
kono
parents:
diff changeset
194 -- DOS
kono
parents:
diff changeset
195 -- Environment variables uses % as prefix and suffix (e.g. %HOME%/dir).
kono
parents:
diff changeset
196 -- The name DOS refer to "DOS-like" environment. This includes all
kono
parents:
diff changeset
197 -- Windows systems.
kono
parents:
diff changeset
198 --
kono
parents:
diff changeset
199 -- Both
kono
parents:
diff changeset
200 -- Recognize both forms described above.
kono
parents:
diff changeset
201 --
kono
parents:
diff changeset
202 -- System_Default
kono
parents:
diff changeset
203 -- Uses either DOS on Windows, and UNIX on all other systems, depending
kono
parents:
diff changeset
204 -- on the running environment.
kono
parents:
diff changeset
205
kono
parents:
diff changeset
206 ---------------
kono
parents:
diff changeset
207 -- Iterators --
kono
parents:
diff changeset
208 ---------------
kono
parents:
diff changeset
209
kono
parents:
diff changeset
210 procedure Open (Dir : out Dir_Type; Dir_Name : Dir_Name_Str);
kono
parents:
diff changeset
211 -- Opens the directory named by Dir_Name and returns a Dir_Type value
kono
parents:
diff changeset
212 -- that refers to this directory, and is positioned at the first entry.
kono
parents:
diff changeset
213 -- Raises Directory_Error if Dir_Name cannot be accessed. In that case
kono
parents:
diff changeset
214 -- Dir will be set to Null_Dir.
kono
parents:
diff changeset
215
kono
parents:
diff changeset
216 procedure Close (Dir : in out Dir_Type);
kono
parents:
diff changeset
217 -- Closes the directory stream referred to by Dir. After calling Close
kono
parents:
diff changeset
218 -- Is_Open will return False. Dir will be set to Null_Dir.
kono
parents:
diff changeset
219 -- Raises Directory_Error if Dir has not be opened (Dir = Null_Dir).
kono
parents:
diff changeset
220
kono
parents:
diff changeset
221 function Is_Open (Dir : Dir_Type) return Boolean;
kono
parents:
diff changeset
222 -- Returns True if Dir is open, or False otherwise
kono
parents:
diff changeset
223
kono
parents:
diff changeset
224 procedure Read
kono
parents:
diff changeset
225 (Dir : Dir_Type;
kono
parents:
diff changeset
226 Str : out String;
kono
parents:
diff changeset
227 Last : out Natural);
kono
parents:
diff changeset
228 -- Reads the next entry from the directory and sets Str to the name
kono
parents:
diff changeset
229 -- of that entry. Last is the index in Str such that Str (Last) is the
kono
parents:
diff changeset
230 -- last character written. Last is 0 when there are no more files in the
kono
parents:
diff changeset
231 -- directory. If Str is too small for the file name, the file name will
kono
parents:
diff changeset
232 -- be truncated before being copied to Str. The list of files returned
kono
parents:
diff changeset
233 -- includes directories in systems providing a hierarchical directory
kono
parents:
diff changeset
234 -- structure, including . (the current directory) and .. (the parent
kono
parents:
diff changeset
235 -- directory) in systems providing these entries. The directory is
kono
parents:
diff changeset
236 -- returned in target-OS form. Raises Directory_Error if Dir has not
kono
parents:
diff changeset
237 -- be opened (Dir = Null_Dir).
kono
parents:
diff changeset
238
kono
parents:
diff changeset
239 function Read_Is_Thread_Safe return Boolean;
kono
parents:
diff changeset
240 -- Indicates if procedure Read is thread safe. On systems where the
kono
parents:
diff changeset
241 -- target system supports this functionality, Read is thread safe,
kono
parents:
diff changeset
242 -- and this function returns True (e.g. this will be the case on any
kono
parents:
diff changeset
243 -- UNIX or UNIX-like system providing a correct implementation of the
kono
parents:
diff changeset
244 -- function readdir_r). If the system cannot provide a thread safe
kono
parents:
diff changeset
245 -- implementation of Read, then this function returns False.
kono
parents:
diff changeset
246
kono
parents:
diff changeset
247 private
kono
parents:
diff changeset
248
kono
parents:
diff changeset
249 type Dir_Type_Value is new System.Address;
kono
parents:
diff changeset
250 -- Low-level address directory structure as returned by opendir in C
kono
parents:
diff changeset
251
kono
parents:
diff changeset
252 type Dir_Type is access Dir_Type_Value;
kono
parents:
diff changeset
253
kono
parents:
diff changeset
254 Null_Dir : constant Dir_Type := null;
kono
parents:
diff changeset
255
kono
parents:
diff changeset
256 pragma Import (C, Dir_Separator, "__gnat_dir_separator");
kono
parents:
diff changeset
257
kono
parents:
diff changeset
258 Dir_Seps : constant Ada.Strings.Maps.Character_Set :=
kono
parents:
diff changeset
259 Ada.Strings.Maps.To_Set ("/\");
kono
parents:
diff changeset
260 -- UNIX and DOS style directory separators
kono
parents:
diff changeset
261
kono
parents:
diff changeset
262 end GNAT.Directory_Operations;