namespace fs

Overview

An essential interface for manipulation with files and directories. Wraps file managing system calls. More…

namespace fs {

// structs

struct FileDirContent;

// global functions

bool FileExists(const char* filename);
bool FileExists(const std::string& filename);
bool FileCopy(const std::string& src, const std::string& dest);
bool FileRename(const std::string& src_file, const std::string& new_name);
bool FileRemove(const std::string& file);
bool DirectoryExists(const std::string& dir_path);
bool CreateDir(const std::string& dir_path);
bool CreateDirHierarchy(const std::string& dir_path);
bool RemoveDir(const std::string& dir_path, bool recursive = true);
std::string GetExtension(const std::string& filename);
bool HasExtenstion(const std::string& filename, const std::string& extenstion);
bool IsFileType(const std::string& filename, const file_types::Type type);
bool IsFileType(const std::string& filename, const std::vector<file_types::Type>& types);
std::string FileToString(const std::string& file_name);
bool StringToFile(const std::string& file_name, const std::string& str);

template <typename T>
std::vector<T> FileToVector(const std::string& file_name);

std::vector<FileDirContent> ListDirectoryContents(std::string direcory_path, const bool recursive = false);
std::vector<std::string> ListDirectoryFileNames(const std::string& direcory_path, const std::string& extension = "");
std::vector<std::string> ListDirectoryFileNames(const std::string& direcory_path, const file_types::Type file_type);
std::vector<std::string> ListDirectoryFileNames(const std::string& direcory_path, const std::vector<file_types::Type>& file_types);
bool DirectoryContainsAny(const std::string& direcory_path, const file_types::Type file_type);
bool DirectoryContainsAny(const std::string& direcory_path, const std::vector<file_types::Type>& file_types);
std::string GetDirectoryPathOfFile(const std::string& file_name_with_path);
std::string GetFileName(const std::string& file_name_with_path, bool strip_extension = false);
std::string GetAbsolutePath(const std::string& local_path);
std::string Execute(const char* command, const size_t buffer_size = 128);
int RunExe(const std::string& exe_path, const std::string& cmd_arguments = "");
std::string StripExtension(const std::string& file_name_with_extension);
bool FileExistsInDirectory(const std::string& dir_path, const std::string& filename, bool strip_extension = false);
std::string GetExeDir();

} // namespace fs

Detailed Documentation

An essential interface for manipulation with files and directories. Wraps file managing system calls.

Global Functions

bool FileExists(const char* filename)

Checks if the specified file exist.

bool FileExists(const std::string& filename)

Checks if the specified file exist.

bool FileCopy(const std::string& src, const std::string& dest)

Makes a copy of a source file to a destination file. Destination directory must exist.

bool FileRename(const std::string& src_file, const std::string& new_name)

Changes name of a source file to a new name.

bool FileRemove(const std::string& file)

Deletes a specified file from the system. Deleted file will not appear in recycle bin.

bool DirectoryExists(const std::string& dir_path)

Checks whether a specified directory exists. May contain the last slash but is not required.

bool CreateDir(const std::string& dir_path)

Creates a specified directory.

This function does not create directory hierarchies. If you wish to create hierarchy, use CreateDirHierarchy function instead.

Parameters:

dir_path

Specifies path and name of directory. May contain the last slash but is not required.

bool CreateDirHierarchy(const std::string& dir_path)

Creates a specified directory hierarchy.

This function creates directory hierarchies. If parent folder does not exists, function creates it recursively.

Parameters:

file_path

Specifies path and name of directory. May contain the last slash but is not required.

bool RemoveDir(const std::string& dir_path, bool recursive = true)

Removes input directory if it is empty or if recursive is set to true.

std::string GetExtension(const std::string& filename)

Returns string representing file extension, in lower case with no dot.

bool HasExtenstion(const std::string& filename, const std::string& extenstion)

Checks whether the file has a specific extension. An extension parameter is case sensitive and can be specified with or without a dot.

bool IsFileType(const std::string& filename, const file_types::Type type)

Checks whether the file is of a specified type.

bool IsFileType(const std::string& filename, const std::vector<file_types::Type>& types)

Checks whether the file is any of the specified types.

std::string FileToString(const std::string& file_name)

Loads contents of a text-based file and stores it in a string.

bool StringToFile(const std::string& file_name, const std::string& str)

Exports the string to a text file.

template <typename T>
std::vector<T> FileToVector(const std::string& file_name)

Loads contents of a text-based file and stores it in a vector. The file must contain only elements of templated type.

std::vector<FileDirContent> ListDirectoryContents(std::string direcory_path, const bool recursive = false)

Returns list of all contents in a specified directory.

std::vector<std::string> ListDirectoryFileNames(const std::string& direcory_path, const std::string& extension = "")

Returns list of all file contents in a specified directory (ignores directories). Providing extension parameter returns only files with specified extension.

std::vector<std::string> ListDirectoryFileNames(const std::string& direcory_path, const file_types::Type file_type)

Returns a list of all file contents in a specified directory (ignores directories) with extension of the specified file type.

std::vector<std::string> ListDirectoryFileNames(const std::string& direcory_path, const std::vector<file_types::Type>& file_types)

Returns a list of all file contents in a specified directory (ignores directories) with any of the extensions of the specified file types.

bool DirectoryContainsAny(const std::string& direcory_path, const file_types::Type file_type)

Checks specified directory if it contains file(s) of specified type.

bool DirectoryContainsAny(const std::string& direcory_path, const std::vector<file_types::Type>& file_types)

Checks specified directory if it contains file(s) of any of the specified types.

std::string GetDirectoryPathOfFile(const std::string& file_name_with_path)

Takes specified file path and strips everything after the last directory separator (including the separator).

std::string GetFileName(const std::string& file_name_with_path, bool strip_extension = false)

Strips everything before the last directory separator, including the separator. If parameter strip_extension is true, strips also extension from resulting file name.

std::string GetAbsolutePath(const std::string& local_path)

Retrieves full system path to a specified local file or directory. Specified local path may contain non existing directories and files.

std::string Execute(const char* command, const size_t buffer_size = 128)

Executes command as if it was called from the command line.

Parameters:

command

Command to be called.

buffer_size

Determines size of temporal buffer used to obtain output.

Returns:

Standard output generated by the executed command.

int RunExe(const std::string& exe_path, const std::string& cmd_arguments = "")

Runs executable in command line.

std::string StripExtension(const std::string& file_name_with_extension)

Strips the extension from a file name.

Parameters:

file_name_with_extension

Specifies name of the file.

bool FileExistsInDirectory(const std::string& dir_path, const std::string& filename, bool strip_extension = false)

Checks whether a specified file exists in directory.

Parameters:

dir_path

Specifies path and name of directory. May contain the last slash but is not required.

filename

Specifies name of file. May contain the extension.

strip_extension

If it is true, strips also extension from file name. Default is false.