FileUtils#
Currently, this API is unsupported in Python
-
class FileUtils#
A class implementing methods to work with files.
Public Static Functions
-
static void createDir(const std::string &dirName)#
Creates a directory with the given name, if it does not exist.
If there is a directory with the given name already, does nothing.
- Parameters:
dirName – The name of the directory to create.
-
static void removeDir(const std::string &dirName)#
removes a directory with the given name.
If there is no directory with the given name, does nothing.
- Parameters:
dirName – The name of the directory to remove.
-
static void createCleanDir(const std::string &dirName)#
Creates a clean directory with the given name.
If there is a directory with the given name already, all of its content is cleaned.
- Parameters:
dirName – The name of the directory to create.
- Throws:
runtime_error – If the length of “dirName” is less than 3.
-
static std::string createUniqueTempDir()#
Creates a new unique directory.
Returns the path of the newly-created directory. Uses HelayersConfig::getUniqueTempDir().
-
static long reportFileSize(const std::string &path, const std::string &logicalName = "")#
Prints the size of the given directory.
- Parameters:
path – The relative path of the directory to report its size.
logicalName – The title used in the size report message. If empty, “path” is used instead.
-
static bool fileExists(const std::string &filePath)#
Returns true iff the file of the given path exists.
- Parameters:
filePath – The relative path of the checked file
-
static std::ifstream openIfstream(const std::string &filePath, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::binary, std::ios_base::iostate exceptions = std::ifstream::failbit | std::ifstream::badbit)#
Open a file input stream.
Turns on flags to throw exception in case of subsequent read failure.
- Parameters:
filePath – File to open
mode – The requested mode for the file
exceptions – The requested exception mask for the file
- Throws:
runtime_error – if file can’t be opened.
-
static std::ofstream openOfstream(const std::string &filePath, std::ios_base::openmode mode = std::ios_base::out | std::ios_base::binary, std::ios_base::iostate exceptions = std::ifstream::failbit | std::ifstream::badbit)#
Open a file output stream.
Turns on flags to throw exception in case of subsequent write failure.
- Parameters:
filePath – File to open
mode – The requested mode for the file
exceptions – The requested exception mask for the file
- Throws:
runtime_error – if file can’t be opened.
-
static std::string extractExtension(const std::string &filePath)#
Returns the extension of the file specified by the given path.
The extension returned includes the “.” character in the beginning. If the file has no extension, returns an empty string.
- Parameters:
filePath – Path of file.
-
static bool pathHasSeparators(const std::string &filePath)#
Returns true if the given path contains slashes or backslashes.
- Parameters:
filePath – File path to check.
-
static std::vector<std::string> extractFileExtensions(const std::vector<std::string> &files, const std::set<std::string> &validFileExtensions = {})#
Returns a vector whose i-th element is the file extension of the i-th file name in the given “files” vector.
- Parameters:
files – The names of the files to extract their extensions.
validFileExtensions – The valid file extensions that are allowed to be in “files” vector. If validFileExtensions is empty, all extensions are considered valid.
- Throws:
invalid_argument – If one of the file names in “files” vector has an invalid extension.
Sets the position of the given istreams to the beginning of their content.
- Parameters:
streams – The istreams to rewind.
Sorts the given file names and their associated input streams by their file extensions, in an ascending order.
The sorted files and input streams will be stored in place.
- Parameters:
streams – The input streams to sort.
files – The file names to sort.
validFileExtensions – The valid file extensions that are allowed to be in “files” vector. If validFileExtensions is empty, all extensions are considered valid.
- Throws:
invalid_argument – If one of the file names in “files” vector has an invalid extension.
-
static void createDir(const std::string &dirName)#