SST 15.0
Structural Simulation Toolkit
SST::Util::Filesystem Class Reference

Class used to manage files and directories. More...

#include <filesystem.h>

Public Member Functions

bool setBasePath (const std::string &base_path)
 Set the base path to be use when relative paths are used.
 
bool ensureDirectoryExists (std::filesystem::path p, bool strip_filename=false)
 This function will ensure a directory exists, and if it doesn't already exist, it will create it.
 
std::string createUniqueDirectory (std::filesystem::path dir_name)
 Creates a unique directory.
 
FILE * fopen (const std::string &filename, const char *mode)
 Open a file using std::fopen().
 
std::string getAbsolutePath (const std::string &path)
 Get the absolute path for a directory or name.
 
std::ofstream ofstream (const std::string &filename, std::ios_base::openmode mode=std::ios_base::out)
 Open a file using ofstream.
 
std::string getBasePath () const
 Get the base_path set by setBasePath.
 

Static Public Member Functions

static std::string getAbsolutePath (const std::string &path, std::string base_path)
 Get the absolute path for a directory or name.
 
static std::string getRandomName (size_t length=8)
 Gets a random file name for use as a temporary file or directory.
 

Detailed Description

Class used to manage files and directories.

One of the features is that you can set the base directory to be used for all files and directories specified with a relative path.

Member Function Documentation

◆ createUniqueDirectory()

std::string SST::Util::Filesystem::createUniqueDirectory ( std::filesystem::path dir_name)

Creates a unique directory.

If the specified directory already exists, the function will append _1 to the directory name. If that is also taken, it will increment the ID until it finds the lowest index that is not used. The function will also create any directory in the path that does not already exist, but only the final directory will be made unique.

Parameters
dir_nameName of directory to create. Directory can be relative or absolute and all intermediate paths that don't exist will be created. The unique name only applies to the last level directory.
Returns
Absolute path of the created directory
Exceptions
std::filesystem::filesystem_errorThrown when the call sees an underlying OS API error (see std::filesystem::create_directories()); the most likely cause is a permissions error, or an file named identically to one of the directory names that needs to be created.
std::invalid_argumentThrown when the passed in path starts with ~/ and the home directory cannot be determined.

References ensureDirectoryExists(), and getAbsolutePath().

◆ ensureDirectoryExists()

bool SST::Util::Filesystem::ensureDirectoryExists ( std::filesystem::path p,
bool strip_filename = false )

This function will ensure a directory exists, and if it doesn't already exist, it will create it.

If the directory starts with / or ~/ (where ~ represents your home directory) then the path will be considered absolute. Otherwise, the path will be relative to base path set using setBasePath(), or the current working directory if a base path has not been set, or the filename begins with ./ or ../. All intermediate directories that don't exist will be created.

Parameters
pathPath to verify or create
strip_filenameSet to true if input is a file and you need to strip the filename to get the directory name. Default is false.
Returns
false if directory exists but is not writable, true otherwise
Exceptions
std::filesystem::filesystem_errorThrown when the directory does not exist and the function tries to create it but sees an underlying OS API error (see std::filesystem::create_directories()); the most likely cause is a permissions error, or an identically named file.
std::invalid_argumentThrown when the passed in path starts with ~/ and the home directory cannot be determined.

References getAbsolutePath().

Referenced by createUniqueDirectory(), fopen(), getAbsolutePath(), and ofstream().

◆ fopen()

FILE * SST::Util::Filesystem::fopen ( const std::string & filename,
const char * mode )

Open a file using std::fopen().

If the passed-in path is relative, it will be relative to the path set using setBasePath(). If setBasePath() was not called, then it will be relative to the current working directory. The FILE* returned from this function can be closed by using std::fclose().

Parameters
filenameName of file to open.
moderead/write mode as defined by std::fopen()
Returns
FILE* to opened file

References ensureDirectoryExists(), and getAbsolutePath().

◆ getAbsolutePath() [1/2]

std::string SST::Util::Filesystem::getAbsolutePath ( const std::string & path)

Get the absolute path for a directory or name.

If the directory starts with / or ~/ (where ~ represents your home directory) then the path will be considered absolute. Otherwise, the path will be relative to the base_path, or the current working directory if no base_path was set or the path starts with ./ or ../

Parameters
pathPath to turn into an absolute path, based on the base_path of the filesystem object
Returns
returns the absolute path
Exceptions
std::invalid_argumentThrown when base_path is not absolute (in format, there is no check to see if the directory actually exists) and the passed in path is relative. Also thrown when the passed in path starts with ~/ and the home directory cannot be determined.

References ensureDirectoryExists(), and getAbsolutePath().

Referenced by createUniqueDirectory(), ensureDirectoryExists(), fopen(), getAbsolutePath(), ofstream(), and setBasePath().

◆ getAbsolutePath() [2/2]

std::string SST::Util::Filesystem::getAbsolutePath ( const std::string & path,
std::string base_path )
static

Get the absolute path for a directory or name.

If the directory starts with / or ~/ (where ~ represents your home directory) then the path will be considered absolute. Otherwise, the path will be relative to the set base_path, or the current working directory if no base_path was set, or the path starts with ./ or ../

Parameters
pathPath to turn into an absolute path
base_pathPath to use as the base path for relative paths passed into the path parameter. If an empty string is passed in, then the current working directory will be used as the base_path (default behavior if you don't pass in a base_path). base_path must be an absolute path and cannot include a ~
Exceptions
std::invalid_argumentThrown when base_path is not absolute (in format, there is no check to see if the directory actually exists) and the passed in path is relative. Also thrown when the passed in path starts with ~/ and the home directory cannot be determined.

◆ getBasePath()

std::string SST::Util::Filesystem::getBasePath ( ) const
inline

Get the base_path set by setBasePath.

Returns
base_path set by setBasePath. This will be an absolute path

◆ ofstream()

std::ofstream SST::Util::Filesystem::ofstream ( const std::string & filename,
std::ios_base::openmode mode = std::ios_base::out )

Open a file using ofstream.

If the passed-in path is relative, it will be relative to the path set using setBasePath(). If setBasePath() was not called, then it will be relative to the current working directory.

Parameters
filenameName of file to open
moderead/write mode as defined by std::ofstream
Returns
ofstream object representing the new opened file

References ensureDirectoryExists(), and getAbsolutePath().

◆ setBasePath()

bool SST::Util::Filesystem::setBasePath ( const std::string & base_path)

Set the base path to be use when relative paths are used.

If a relative path is passed into this function, then the current working directory will be used as the base path. When the base_path is set, the class will check to see if the path already exists, and if not, whether or not the user has permissions to create the directory. If the directory doesn't exist and the user doesn't have permission to create it, the function will return false and thee base_path will remain unchanged. If the directory doesn't exist and the user has permissions to create it, the directory will be created on demand.

Parameters
base_pathPath to set as the base path. The path can be absolute or relative, and a path starting with ~/ with the ~ expanded to the user's home directory.
Returns
True if path is valid and either exists and is writable by the user or can be created, false otherwise.

References getAbsolutePath().


The documentation for this class was generated from the following files: