1
-------------------------------------------------------------------------------
3
-- <STRONG>Copyright © 2001, 2002 by Thomas Wolf.</STRONG>
5
-- This piece of software is free software; you can redistribute it and/or
6
-- modify it under the terms of the GNU General Public License as published
7
-- by the Free Software Foundation; either version 2, or (at your option)
8
-- any later version. This software is distributed in the hope that it will
9
-- be useful, but <EM>without any warranty</EM>; without even the implied
10
-- warranty of <EM>merchantability or fitness for a particular purpose.</EM>
11
-- See the GNU General Public License for more details. You should have
12
-- received a copy of the GNU General Public License with this distribution,
13
-- see file "<A HREF="GPL.txt">GPL.txt</A>". If not, write to the Free
14
-- Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18
-- As a special exception from the GPL, if other files instantiate generics
19
-- from this unit, or you link this unit with other files to produce an
20
-- executable, this unit does not by itself cause the resulting executable
21
-- to be covered by the GPL. This exception does not however invalidate any
22
-- other reasons why the executable file might be covered by the GPL.
26
-- Thomas Wolf (TW) <E_MAIL>
30
-- Operations for manipulating file names. The package is intended for
31
-- use on Windows or Unix systems. Upon elaboration, it tries to figure
32
-- out the host operating system by examining the @PATH@ environment
33
-- variable: if that contains Windows-looking pathes (i.e., a '\' is found
34
-- before a any '/'), it assumes it's being used on Windows. If, on the
35
-- other hand, it finds a '/' first, it assumes Unix. If it finds neither,
36
-- it uses @GNAT.Os_Lib.Directory_Separator@ as its directory separator.
37
-- (If you intend to use this package on a non-GNAT system, you'll have
38
-- to change the body of this package as appropriate.)
40
-- All operations in this package are pur string manipulation operations.
41
-- There are no file system operations involved.
49
-- 19-MAR-2002 TW Initial version.
50
-- 03-MAY-2002 TW Added 'Drive' and 'Node'.
52
-------------------------------------------------------------------------------
54
pragma License (Modified_GPL);
56
package Util.Pathes is
58
pragma Elaborate_Body;
60
-- A @Full_Name@ can have a path component, and a file name. Both are
61
-- optional, i.e., a @Full_Name@ can also be only a path, or only a
62
-- file name. A @File_Name@ may or may not contain an extension. This
63
-- package considers anything beyond the last '.' in a file name the
64
-- extension, unless that last period is the first character of the file
65
-- name, in which case the extension is the empty string (this handles
66
-- names like "@.cshrc@" as they are common on Unix-like systems).
68
-- This package uses a purely syntactical distinction between file and
69
-- directory names: if the @Full_Name@ terminates in a directory
70
-- separator, it is considered a directory name; otherwise it is taken
73
Path_Error : exception;
75
function Directory_Separator
77
-- Returns the directory separator used. Returns whatever the package
78
-- elaboration concluded was the directory separator on the host
82
(Full_Name : in String;
83
Separator : in Character := Directory_Separator)
85
-- Returns the extension of @Full_Name@, which may be the empty string.
88
(Full_Name : in String;
89
Separator : in Character := Directory_Separator)
91
-- Returns the file name component of @Full_Name@ including a possible
92
-- extension. Returns the empty string if @Full_Name@ is a path only.
95
(Full_Name : in String;
96
Separator : in Character := Directory_Separator)
98
-- Returns the file name component of @Full_Name@ <EM>without</EM> a
99
-- possible extension. Returns the empty string if @Full_Name@ is a path
103
(Full_Name : in String;
104
Separator : in Character := Directory_Separator)
106
-- Returns "" if @Full_Name@ does not contain a path component; otherwise
107
-- it returns the path component (absolute or relative) including the
108
-- final directory separator.
111
(Full_Name : in String;
112
Separator : in Character := Directory_Separator)
114
-- Returns "" if @Full_Name@ does not contain a drive component; otherwise
115
-- it returns the drive specification (ending in a colon).
118
(Full_Name : in String;
119
Separator : in Character := Directory_Separator)
123
(Full_Name : in String;
124
Separator : in Character := Directory_Separator)
128
(Full_Name : in String;
129
Separator : in Character := Directory_Separator)
134
Separator : in Character := Directory_Separator)
136
-- @Path@ is assumed to be a path only.
137
-- Returns <CODE>'.' & Separator</CODE> if @Path@ is empty, otherwise
138
-- returns @Path@ terminated with a @Separator@.
140
function Is_Absolute_Path
142
Separator : in Character := Directory_Separator)
144
-- Returns @True@ if the name given has an absolute path component.
148
File_Name : in String;
149
Separator : in Character := Directory_Separator)
151
-- Concatenates the @Path@ with the @File_Name@, which may contain a
152
-- relative path. Returns @Path@ if @File_Name@ = "", and @File_Name@ if
153
-- @Path@ = "". Otherwise, raises @Path_Error@ if @File_Name@ contains an
154
-- <EM>absolute</EM> path component. Note that @File_Name@ not necessarily
155
-- must contain a file name, it may also be a relative path.
157
function Replace_File_Name
158
(Full_Name : in String;
159
File_Name : in String;
160
Separator : in Character := Directory_Separator)
163
-- <CODE>Concat (Path (Full_Name, Separator), File_Name,
164
-- Separator)</CODE>.
166
function Replace_Extension
167
(Full_Name : in String;
168
Extension : in String;
169
Separator : in Character := Directory_Separator)
171
-- Replaces the extension of @Full_Name@. @Extension@ should not include
172
-- a leading period! If @Full_Name@ has a file name component, but no
173
-- extension yet, a period and the @Extension@ is appended. Raises
174
-- @Path_Error@ if @Full_Name@ doesn't have a file name component at all.
178
Separator : in Character := Directory_Separator)
180
-- Tries to move up one directory in @Path@, which is interpreted as a
181
-- path name even if it has no trailing @Separator@. Raises @Path_Error@
182
-- if @Path@ is an absolute path and specifies a root directory. If
183
-- @Path@ is a relative path, the result it may start with "../". the
184
-- result has a trailing @Separator@.
187
(Full_Name : in String;
188
Separator : in Character := Directory_Separator)
190
-- Cleans up @Full_name@, which is interpreted as a path even if it has
191
-- no trailing @Separator@. Removes all extraneous separators and "./"
192
-- sequences, and also executes "../" components. Raises @Path_Error@ if
193
-- the path is invalid, e.g. execution of "../" would go beyond the root
194
-- directory, if @Full_Name@ is an absolute path. If @Full_Name@ is a
195
-- relative path, the resulting path may start with "./" or a sequence
196
-- of "../". The result has a trailing @Separator@, unless it is a
197
-- Windows relative path name consisting only of a drive letter and a
198
-- colon, in which case it ends with the colon.
202
pragma Inline (Directory_Separator);