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#ifndef CASA_PATH_H
27#define CASA_PATH_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/BasicSL/String.h>
32
33namespace casacore { // # NAMESPACE CASACORE - BEGIN
34
35// <summary>
36// Path name of a file
37// </summary>
38// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
39// </reviewed>
40
41// <prerequisite>
42// <li> Basic knowledge of the UNIX file system
43// </prerequisite>
44
45// <etymology>
46// The term 'path' is the standard term for describing the location of a file
47// in a hierarchy of possibly nested directories. In order to find a
48// particular file you must travel a specific path strating from a known
49// point. We use the term in its standard sense in this class.
50// </etymology>
51
52// <synopsis>
53// This class can be used to describe a pathname. One can also create,
54// validate, parse (get base or directory names or original, expanded or
55// absolute names), query and append strings. The client programmer can
56// give a string, at construction, which describes a path. This string can
57// be a relative or an absolute name. Environment variables and a tilde
58// (with or without user name) can also be used in the string and will
59// be expanded by the function expandedName.
60// <br> The function
61// Once a Path has been constructed, you can query the object for its
62// original name, expanded name, absolute name, the name of the directory
63// where it is found or the name of only the file. Expanding the path name
64// means that possible environment variables and tilde get expanded.
65// There are also functions to get the length or maximum length of a path.
66// Pathnames can also be checked on correctness and they can be checked
67// if they conform the POSIX standard.
68// </synopsis>
69
70// <example>
71// In this example a few pathnames are created.
72// <srcblock>
73// Path test1("~/test/$TEST1/.."); // absolute path
74// Path test2("/$HOME/./analyse"); // absolute path
75// Path test3("myFile"); // relative path
76//
77// cout << test1.originalName() << endl;
78//
79// // Test1 is according the POSIX standard
80// if (test1.isStrictlyPosix()){
81// cout << "test1 is strictly POSIX << endl;
82// }
83//
84// // Test1 is valid
85// if (test1.isValid()){
86// cout << test1.isValid() << endl;
87// }
88//
89// // if "TEST1=$TEST2 and TEST2=$TEST1"(recursive environment variables)
90// // an exception will be thrown. ~ is replaced by the homedirectory
91// cout << test1.expandedName() << endl;
92// // $HOME is expanded
93// cout << test2.expandedName() << endl;
94// cout << test1.absoluteName() << endl;
95// cout << test2.absoluteName() << endl;
96// cout << test2.baseName() << endl;
97// cout << test1.dirName() << endl;
98// cout << test3.originalName() << endl; // myFile is returned
99// cout << test3.expandedName() << endl; // Nothing is changed
100// cout << test3.absoluteName() << endl; // The current working directory
101// is placed before 'myFile'
102// cout << test3.baseName() << endl; // The current working directory
103// // is returned
104// cout << test3.dirName() << endl; // myFile is returned
105// </srcblock>
106// </example>
107
108// <motivation>
109// Programmer convenience and (eventually) OS independence.
110// </motivation>
111
112// <todo asof=$DATE$>
113// <li> To make the class OS independent some functions should be rebuild.
114// These functions could be expandedName or absoluteName.
115// <li> The function expandedName or absoluteName could map the filename to
116// the native convention
117// <li> A (maybe static) function contractName(const String& pathName)
118// could be implemented to remove . and .. from the file name.
119// </todo>
121class Path {
122 public:
123 // Default constructor, the path is set to . (working directory).
124 Path();
125
126 // Construct a path with the given name.
127 // When the name is empty, it is set to . (working directory).
128 // It is not checked if the path name is valid.
129 // Function isValid() can be used for that purpose.
130 Path(const String& pathName);
131
132 // Copy constructor, copy semantics.
133 Path(const Path& that);
134
135 // Destructor
136 ~Path();
137
138 // Assignment, copy semantics.
139 Path& operator=(const Path& that);
140
141 // Append a string to the path name.
142 // When the current path does not end with a / and the string to append
143 // does not start with a /, an intermediate / is also added.
144 void append(const String& string);
145
146 // Returns the string as given at construction.
147 const String& originalName() const;
148
149 // Return a string giving the expanded pathname.
150 // This means that the environment variables are expanded and the tilde
151 // is replaced by the home directory. An expanded name can still
152 // be a relative path.
153 // An exception is thrown when converting a recursive environment
154 // variable results in an endless loop (that is, more than 25
155 // substitutions).
156 const String& expandedName() const;
157
158 // Return the string which giving the absolute pathname.
159 // It is generated from the expanded pathname by adding
160 // the working directory when needed.
161 const String& absoluteName() const;
162
163 // Return the realpath which is the absolute pathname with possible
164 // symlinks resolved. It also resolves //, /./, /../ and trailing /.
165 // <br>The path must be an existing file or directory.
166 // It uses the system's realpath function. In case it fails,
167 // an exception is thrown.
168 String resolvedName() const;
169
170 // Check if pathname is valid. This function checks for: double slashes,
171 // non-printable characters, pathname length and filename lengths, this
172 // function is more OS-specific.
173 Bool isValid() const;
174
175 // Check if pathname is valid according the POSIX standard.
176 // This function checks for
177 // double slashes, non-printable characters,pathname length and filename
178 // lenghts, all according to the POSIX-standard.
179 Bool isStrictlyPosix() const;
180
181 // Return length of path name
182 uInt length() const;
183
184 // Return the maximum length a path name can have.
185 uInt maxLength() const;
186
187 // Return the basename of the path; this is only the name of the file.
188 // It takes it from the expanded path name.
189 String baseName() const;
190
191 // Return the dirname of the path; this is the directory where the
192 // filename is found. It takes it from the expanded path name.
193 // <br>To get the absolute dirname one could do:
194 // <srcblock>
195 // Path tmpPath (myPath.dirName());
196 // String absDir (tmpPath.absoluteName());
197 // </srcblock>
198 // or
199 // <srcblock>
200 // Path tmpPath (myPath.absoluteName());
201 // String absDir (tmpPath.dirName());
202 // </srcblock>
203 String dirName() const;
204
205 // Strip otherName from this name. If stripped, the result gets a
206 // leading ././
207 // If not stripped, it is tried if name can be stripped from otherName.
208 // If stripped, the result gets a trailing /.
209 // If still not stripped, it is tried to strip the directory of otherName.
210 // If that succeeds, the result gets a leading ./
211 // This is used by RefTable and TableKeyword to ensure that the
212 // name of a subtable or referenced table is always relative to
213 // the main table.
214 static String stripDirectory(const String& name, const String& otherName);
215
216 // If the name starts with ././ add otherName to it.
217 // If the name ends with /. strip name from otherName and return the
218 // remainder.
219 // If the name starts with ./ add the directory of otherName to it.
220 // It is the opposite of stripDirectory.
221 static String addDirectory(const String& name, const String& otherName);
222
223 private:
224 // Strings to describe the pathname in three different ways.
226 // These variables are pointer to strings because the functions which use
227 // these variables are const functions. This means that they would not be
228 // able to modify the string, now they can.
231
232 // Define the maximum number of bytes in a pathname
233 // This definition does not use Posix values.
234 static uInt getMaxPathNameSize();
235 // Define the maximum number of bytes in a filename
236 // This definition does not use Posix values.
237 static uInt getMaxNameSize();
238
239 // This function is used by expandedName to replace the tilde and to
240 // expand the environment variables
241 String expandName(const String& inString) const;
242
243 // This function is used by absoluteName to make a name absolute,
244 // this means that the name is described from the root
245 String makeAbsoluteName(const String& inString) const;
246
247 // Remove . and .. from the path name.
248 // Also multiple slashes are replaced by a single.
249 String removeDots(const String& inString) const;
250
251 // This function is used by expandName and absoluteName. It sets the
252 // integer "count" on the next slash or on the end of a string
253 void getNextName(const String& inString, uInt& count) const;
254};
256inline const String& Path::originalName() const { return itsOriginalPathName; }
257
258} // namespace casacore
259
260#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:229
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:224
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:255
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:228
static String stripDirectory(const String &name, const String &otherName)
Strip otherName from this 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.
String: the storage and methods of handling collections of characters.
Definition String.h:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
String name() const
Return the name of the field.
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40