casacore
Loading...
Searching...
No Matches
Path.h
Go to the documentation of this file.
1//# Path.h: Path name of a file
2//# Copyright (C) 1993,1994,1995,1996,1997,1998,1999,2000
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26
27#ifndef CASA_PATH_H
28#define CASA_PATH_H
29
30//# Includes
31#include <casacore/casa/aips.h>
32#include <casacore/casa/BasicSL/String.h>
33
34
35namespace casacore { //# NAMESPACE CASACORE - BEGIN
36
37// <summary>
38// Path name of a file
39// </summary>
40// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
41// </reviewed>
42
43// <prerequisite>
44// <li> Basic knowledge of the UNIX file system
45// </prerequisite>
46
47// <etymology>
48// The term 'path' is the standard term for describing the location of a file
49// in a hierarchy of possibly nested directories. In order to find a
50// particular file you must travel a specific path strating from a known
51// point. We use the term in its standard sense in this class.
52// </etymology>
53
54// <synopsis>
55// This class can be used to describe a pathname. One can also create,
56// validate, parse (get base or directory names or original, expanded or
57// absolute names), query and append strings. The client programmer can
58// give a string, at construction, which describes a path. This string can
59// be a relative or an absolute name. Environment variables and a tilde
60// (with or without user name) can also be used in the string and will
61// be expanded by the function expandedName.
62// <br> The function
63// Once a Path has been constructed, you can query the object for its
64// original name, expanded name, absolute name, the name of the directory
65// where it is found or the name of only the file. Expanding the path name
66// means that possible environment variables and tilde get expanded.
67// There are also functions to get the length or maximum length of a path.
68// Pathnames can also be checked on correctness and they can be checked
69// if they conform the POSIX standard.
70// </synopsis>
71
72// <example>
73// In this example a few pathnames are created.
74// <srcblock>
75// Path test1("~/test/$TEST1/.."); // absolute path
76// Path test2("/$HOME/./analyse"); // absolute path
77// Path test3("myFile"); // relative path
78//
79// cout << test1.originalName() << endl;
80//
81// // Test1 is according the POSIX standard
82// if (test1.isStrictlyPosix()){
83// cout << "test1 is strictly POSIX << endl;
84// }
85//
86// // Test1 is valid
87// if (test1.isValid()){
88// cout << test1.isValid() << endl;
89// }
90//
91// // if "TEST1=$TEST2 and TEST2=$TEST1"(recursive environment variables)
92// // an exception will be thrown. ~ is replaced by the homedirectory
93// cout << test1.expandedName() << endl;
94// // $HOME is expanded
95// cout << test2.expandedName() << endl;
96// cout << test1.absoluteName() << endl;
97// cout << test2.absoluteName() << endl;
98// cout << test2.baseName() << endl;
99// cout << test1.dirName() << endl;
100// cout << test3.originalName() << endl; // myFile is returned
101// cout << test3.expandedName() << endl; // Nothing is changed
102// cout << test3.absoluteName() << endl; // The current working directory
103// is placed before 'myFile'
104// cout << test3.baseName() << endl; // The current working directory
105// // is returned
106// cout << test3.dirName() << endl; // myFile is returned
107// </srcblock>
108// </example>
109
110// <motivation>
111// Programmer convenience and (eventually) OS independence.
112// </motivation>
113
114// <todo asof=$DATE$>
115// <li> To make the class OS independent some functions should be rebuild.
116// These functions could be expandedName or absoluteName.
117// <li> The function expandedName or absoluteName could map the filename to
118// the native convention
119// <li> A (maybe static) function contractName(const String& pathName)
120// could be implemented to remove . and .. from the file name.
121// </todo>
122
123
124class Path
125{
126public:
127 // Default constructor, the path is set to . (working directory).
129
130 // Construct a path with the given name.
131 // When the name is empty, it is set to . (working directory).
132 // It is not checked if the path name is valid.
133 // Function isValid() can be used for that purpose.
134 Path (const String& pathName);
135
136 // Copy constructor, copy semantics.
137 Path (const Path& that);
138
139 // Destructor
141
142 // Assignment, copy semantics.
143 Path& operator= (const Path& that);
144
145 // Append a string to the path name.
146 // When the current path does not end with a / and the string to append
147 // does not start with a /, an intermediate / is also added.
148 void append (const String& string);
149
150 // Returns the string as given at construction.
151 const String& originalName () const;
152
153 // Return a string giving the expanded pathname.
154 // This means that the environment variables are expanded and the tilde
155 // is replaced by the home directory. An expanded name can still
156 // be a relative path.
157 // An exception is thrown when converting a recursive environment
158 // variable results in an endless loop (that is, more than 25
159 // substitutions).
160 const String& expandedName () const;
161
162 // Return the string which giving the absolute pathname.
163 // It is generated from the expanded pathname by adding
164 // the working directory when needed.
165 const String& absoluteName () const;
166
167 // Return the realpath which is the absolute pathname with possible
168 // symlinks resolved. It also resolves //, /./, /../ and trailing /.
169 // <br>The path must be an existing file or directory.
170 // It uses the system's realpath function. In case it fails,
171 // an exception is thrown.
173
174 // Check if pathname is valid. This function checks for: double slashes,
175 // non-printable characters, pathname length and filename lengths, this
176 // function is more OS-specific.
177 Bool isValid() const;
178
179 // Check if pathname is valid according the POSIX standard.
180 // This function checks for
181 // double slashes, non-printable characters,pathname length and filename
182 // lenghts, all according to the POSIX-standard.
184
185 // Return length of path name
186 uInt length() const;
187
188 // Return the maximum length a path name can have.
190
191 // Return the basename of the path; this is only the name of the file.
192 // It takes it from the expanded path name.
194
195 // Return the dirname of the path; this is the directory where the
196 // filename is found. It takes it from the expanded path name.
197 // <br>To get the absolute dirname one could do:
198 // <srcblock>
199 // Path tmpPath (myPath.dirName());
200 // String absDir (tmpPath.absoluteName());
201 // </srcblock>
202 // or
203 // <srcblock>
204 // Path tmpPath (myPath.absoluteName());
205 // String absDir (tmpPath.dirName());
206 // </srcblock>
208
209 // Strip otherName from this name. If stripped, the result gets a
210 // leading ././
211 // If not stripped, it is tried if name can be stripped from otherName.
212 // If stripped, the result gets a trailing /.
213 // If still not stripped, it is tried to strip the directory of otherName.
214 // If that succeeds, the result gets a leading ./
215 // This is used by RefTable and TableKeyword to ensure that the
216 // name of a subtable or referenced table is always relative to
217 // the main table.
218 static String stripDirectory (const String& name, const String& otherName);
219
220 // If the name starts with ././ add otherName to it.
221 // If the name ends with /. strip name from otherName and return the
222 // remainder.
223 // If the name starts with ./ add the directory of otherName to it.
224 // It is the opposite of stripDirectory.
225 static String addDirectory (const String& name, const String& otherName);
226
227
228private:
229 // Strings to describe the pathname in three different ways.
231 // These variables are pointer to strings because the functions which use
232 // these variables are const functions. This means that they would not be
233 // able to modify the string, now they can.
236
237 // Define the maximum number of bytes in a pathname
238 // This definition does not use Posix values.
240 // Define the maximum number of bytes in a filename
241 // This definition does not use Posix values.
243
244
245 // This function is used by expandedName to replace the tilde and to
246 // expand the environment variables
247 String expandName (const String& inString) const;
248
249 // This function is used by absoluteName to make a name absolute,
250 // this means that the name is described from the root
251 String makeAbsoluteName (const String& inString) const;
252
253 // Remove . and .. from the path name.
254 // Also multiple slashes are replaced by a single.
255 String removeDots (const String& inString) const;
256
257 // This function is used by expandName and absoluteName. It sets the
258 // integer "count" on the next slash or on the end of a string
259 void getNextName (const String& inString, uInt& count) const;
260};
261
262
263inline const String& Path::originalName() const
264{
265 return itsOriginalPathName;
266}
267
268
269
270} //# NAMESPACE CASACORE - END
271
272#endif
String dirName() const
Return the dirname of the path; this is the directory where the filename is found.
~Path()
Destructor.
String itsExpandedPathName
Definition Path.h:235
uInt maxLength() const
Return the maximum length a path name can have.
String resolvedName() const
Return the realpath which is the absolute pathname with possible symlinks resolved.
const String & expandedName() const
Return a string giving the expanded pathname.
static String addDirectory(const String &name, const String &otherName)
If the name starts with.
Path()
Default constructor, the path is set to.
uInt length() const
Return length of path name.
String itsOriginalPathName
Strings to describe the pathname in three different ways.
Definition Path.h:230
void getNextName(const String &inString, uInt &count) const
This function is used by expandName and absoluteName.
const String & originalName() const
Returns the string as given at construction.
Definition Path.h:263
Bool isValid() const
Check if pathname is valid.
const String & absoluteName() const
Return the string which giving the absolute pathname.
String expandName(const String &inString) const
This function is used by expandedName to replace the tilde and to expand the environment variables.
Bool isStrictlyPosix() const
Check if pathname is valid according the POSIX standard.
String itsAbsolutePathName
These variables are pointer to strings because the functions which use these variables are const func...
Definition Path.h:234
static String stripDirectory(const String &name, const String &otherName)
Strip otherName from this name.
Path(const String &pathName)
Construct a path with the given name.
String baseName() const
Return the basename of the path; this is only the name of the file.
static uInt getMaxNameSize()
Define the maximum number of bytes in a filename This definition does not use Posix values.
void append(const String &string)
Append a string to the path name.
Path & operator=(const Path &that)
Assignment, copy semantics.
String makeAbsoluteName(const String &inString) const
This function is used by absoluteName to make a name absolute, this means that the name is described ...
static uInt getMaxPathNameSize()
Define the maximum number of bytes in a pathname This definition does not use Posix values.
String removeDots(const String &inString) const
Remove.
Path(const Path &that)
Copy constructor, copy semantics.
String: the storage and methods of handling collections of characters.
Definition String.h:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40