Simbody  3.8
Pathname.h
Go to the documentation of this file.
1 #ifndef SimTK_SimTKCOMMON_PATHNAME_H_
2 #define SimTK_SimTKCOMMON_PATHNAME_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKcommon *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2009-15 Stanford University and the Authors. *
13  * Authors: Michael Sherman, Carmichael Ong *
14  * Contributors: *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
33 #include <string>
34 #include <stdexcept>
35 
36 namespace SimTK {
37 
92 public:
93 
94 
133  static void deconstructPathname( const std::string& pathname,
134  bool& dontApplySearchPath,
135  std::string& directory,
136  std::string& fileName,
137  std::string& extension);
138 
164  static void deconstructPathnameUsingSpecifiedWorkingDirectory(const std::string& swd,
165  const std::string& pathname,
166  std::string& directory,
167  std::string& fileName,
168  std::string& extension);
169 
173  static void deconstructAbsolutePathname(const std::string& pathname,
174  std::string& directory,
175  std::string& fileName,
176  std::string& extension) {
177  bool dontApplySearchPath;
178  deconstructPathname(pathname, dontApplySearchPath, directory, fileName, extension);
179  if (!dontApplySearchPath)
180  directory = getCurrentWorkingDirectory() + directory;
181  }
182 
200  static std::string getAbsolutePathname(const std::string& pathname) {
201  std::string directory, fileName, extension;
202  deconstructAbsolutePathname(pathname, directory, fileName, extension);
203  return directory + fileName + extension;
204  }
205 
209  static std::string getAbsoluteDirectoryPathname(const std::string& dirPathname) {
210  std::string absPath = getAbsolutePathname(dirPathname);
211  if (!absPath.empty() && absPath[absPath.size()-1] != getPathSeparatorChar())
212  absPath += getPathSeparatorChar();
213  return absPath;
214  }
215 
220  (const std::string& swd, const std::string& pathname) {
221  std::string directory, fileName, extension;
222  deconstructPathnameUsingSpecifiedWorkingDirectory
223  (swd, pathname, directory, fileName, extension);
224  return directory + fileName + extension;
225  }
226 
230  static std::string
232  (const std::string& swd, const std::string& dirPathname) {
233  std::string absPath =
234  getAbsolutePathnameUsingSpecifiedWorkingDirectory(swd, dirPathname);
235  if (!absPath.empty() && absPath[absPath.size()-1] != getPathSeparatorChar())
236  absPath += getPathSeparatorChar();
237  return absPath;
238  }
239 
242  static bool fileExists(const std::string& pathname);
243 
247  static std::string getDefaultInstallDir();
248 
254  static std::string addDirectoryOffset(const std::string& base,
255  const std::string& offset);
256 
260  static std::string getInstallDir(const std::string& envInstallDir,
261  const std::string& offsetFromDefaultInstallDir);
262 
264  static std::string getThisExecutablePath();
267  static std::string getThisExecutableDirectory();
283  static bool getFunctionLibraryDirectory(void* func,
284  std::string& absolutePathname);
292  static std::string getCurrentWorkingDirectory(const std::string& drive="");
296  static std::string getRootDirectory(const std::string& drive="");
299  static std::string getCurrentDriveLetter();
302  static std::string getCurrentDrive();
305  static bool environmentVariableExists(const std::string& name);
311  static std::string getEnvironmentVariable(const std::string& name);
315  static std::string getPathSeparator();
319  static char getPathSeparatorChar();
321  static bool isPathSeparator(char c) {
322  return c=='/' || c=='\\';
323  }
324 };
325 
326 } // namespace SimTK
327 
328 #endif // SimTK_SimTKCOMMON_PATHNAME_H_
329 
330 
This file defines the Array_<T,X> class and related support classes including base classes ArrayViewC...
Mandatory first inclusion for any Simbody source or header file.
#define SimTK_SimTKCOMMON_EXPORT
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:224
This class encapsulates the handling of file and directory pathnames in a platform-independent manner...
Definition: Pathname.h:91
static std::string addDirectoryOffset(const std::string &base, const std::string &offset)
Append a subdirectory offset to an existing pathname (relative or absolute).
static void deconstructPathname(const std::string &pathname, bool &dontApplySearchPath, std::string &directory, std::string &fileName, std::string &extension)
Dismantle a supplied pathname into its component parts.
static std::string getCurrentDrive()
On Windows, return the current drive letter in lowercase, followed by ":"; on other platforms just re...
static void deconstructAbsolutePathname(const std::string &pathname, std::string &directory, std::string &fileName, std::string &extension)
Give back the deconstructed canonicalized absolute pathname for a given path.
Definition: Pathname.h:173
static std::string getCurrentWorkingDirectory(const std::string &drive="")
Get the absolute pathname of the current working directory including a trailing separator character.
static std::string getRootDirectory(const std::string &drive="")
Get the canonicalized name of the root directory.
static std::string getAbsoluteDirectoryPathname(const std::string &dirPathname)
This is the same as getAbsolutePathname() except that the final segment is interpreted as a directory...
Definition: Pathname.h:209
static std::string getAbsoluteDirectoryPathnameUsingSpecifiedWorkingDirectory(const std::string &swd, const std::string &dirPathname)
Same as getAbsoluteDirectoryPathname() but using a specified working directory rather than the curren...
Definition: Pathname.h:232
static std::string getEnvironmentVariable(const std::string &name)
Return the value of the named environment variable or the empty string if the variable is not found.
static std::string getThisExecutableDirectory()
Get the absolute pathname of the directory which contains the currently executing program.
static std::string getCurrentDriveLetter()
On Windows, return the current drive letter in lowercase, with no trailing ":"; on other platforms re...
static std::string getAbsolutePathname(const std::string &pathname)
Get canonicalized absolute pathname from a given pathname which can be relative or absolute.
Definition: Pathname.h:200
static bool isPathSeparator(char c)
Returns true if the character is slash or backslash.
Definition: Pathname.h:321
static std::string getThisExecutablePath()
Get the absolute pathname of the currently executing program.
static std::string getPathSeparator()
Return this platform's pathname separator character as a string.
static bool fileExists(const std::string &pathname)
Return true if the given pathname names a file that exists and is readable.
static std::string getDefaultInstallDir()
Get the default installation directory for this platform.
static void deconstructPathnameUsingSpecifiedWorkingDirectory(const std::string &swd, const std::string &pathname, std::string &directory, std::string &fileName, std::string &extension)
An extension of deconstructPathname().
static std::string getInstallDir(const std::string &envInstallDir, const std::string &offsetFromDefaultInstallDir)
Find the installation directory for something, using the named installation directory environment var...
static std::string getAbsolutePathnameUsingSpecifiedWorkingDirectory(const std::string &swd, const std::string &pathname)
Same as getAbsolutePathname() but using a specified working directory rather than the current working...
Definition: Pathname.h:220
static bool getFunctionLibraryDirectory(void *func, std::string &absolutePathname)
Get the absolute pathname of the directory which contains the library/binary from which func was load...
static char getPathSeparatorChar()
Return this platform's pathname separator character as a char.
static bool environmentVariableExists(const std::string &name)
Return true if the named environment variable is present in the environment.
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37