Easy3D 2.5.3
easy3d::file_system Namespace Reference

A very basic filesystem implementation. More...

Functions

bool is_file (const std::string &path)
 Tests if 'path' is an existing file. More...
 
bool is_directory (const std::string &path)
 Tests if 'path' is an existing directory. More...
 
bool create_directory (const std::string &path)
 Creates a directory entitled 'path'. More...
 
bool delete_file (const std::string &path)
 Deletes the file 'path'. More...
 
bool delete_directory (const std::string &path)
 Deletes the directory 'path' (and its contents will be deleted recursively). More...
 
bool delete_contents (const std::string &path)
 Deletes the contents of the directory 'path' (the directory will not be deleted). More...
 
void get_directory_entries (const std::string &path, std::vector< std::string > &entries, bool recursive)
 Queries the entries of a directory (including subdirectories and files). More...
 
void get_files (const std::string &path, std::vector< std::string > &files, bool recursive)
 Queries file entries of a directory 'path'. More...
 
void get_sub_directories (const std::string &path, std::vector< std::string > &subs, bool recursive)
 Query subdirectory entries of a directory 'path'. More...
 
std::string current_working_directory ()
 Query the current working directory. More...
 
bool set_current_working_directory (const std::string &path)
 Set the current working directory. More...
 
std::string home_directory ()
 Query the home path for the current user. More...
 
std::string executable ()
 Query the name of this executable. More...
 
std::string executable_directory ()
 Query the directory where the executable file is located. More...
 
bool rename_file (const std::string &old_name, const std::string &new_name)
 Rename the file from 'old_name' to 'new_name'. More...
 
time_t time_stamp (const std::string &path)
 Query the time stamp of a file or directory. More...
 
std::string time_string (const std::string &path)
 Query the time stamp of a file or directory as a string. More...
 
std::ifstream::pos_type file_size (const std::string &path)
 Query the size of the file. More...
 
std::string parent_directory (const std::string &path)
 Query the parent path from full name of a file or directory (e.g., /a/b/c.Ext => /a/b) More...
 
std::string extension (const std::string &path, bool lower=true)
 Query the file extension without dot (e.g., /a/b/c.Ext => Ext). More...
 
std::string simple_name (const std::string &path)
 Gets file name without path but with extension (e.g, /a/b/c.Ext => c.Ext) More...
 
std::string base_name (const std::string &path)
 Gets file name without path and last extension (e.g., c:/file.ext1.ext2 => file.ext1; /a/b/c.Ext => c). More...
 
std::string name_less_extension (const std::string &path)
 Gets file path without last extension (e.g., /a/b/c.Ext => /a/b/c; file.ext1.ext2 => file.ext1). More...
 
std::string name_less_all_extensions (const std::string &path)
 Gets file path without all extensions (e.g., /a/b/c.Ext => /a/b/c; file.ext1.ext2 => file). More...
 
std::string replace_extension (std::string const &path, const std::string &ext)
 Replaces the extension of the given file with 'ext'. If the file name does not have an extension, the expected new extension is appended. More...
 
std::string path_root (const std::string &path)
 Gets root part of a path ("/" or "C:"), or an empty string if none found. More...
 
bool is_absolute_path (const std::string &path)
 Tests if path is absolute, as !get_path_root(path).empty(). . More...
 
std::string relative_path (const std::string &from, const std::string &to)
 Compute the relative path from 'from' to 'to'. More...
 
std::string absolute_path (const std::string &path)
 Converts to absolute path (i.e., removes .. and . from a path string). More...
 
std::string convert_to_windows_style (const std::string &path)
 Converts the path to Windows style, i.e., forward slashes (/) to back slashes (\). More...
 
std::string convert_to_unix_style (const std::string &path)
 Converts the path to Unix style, i.e., back slashes (\) to forward slashes (/). More...
 
std::string convert_to_native_style (const std::string &path)
 Convert a path string such that it uses the current platform's path separators. More...
 
char native_path_separator ()
 Gets the path separator of the current platform. More...
 
bool is_native_style (const std::string &path)
 Checks sif the path contains only the current platform's path separators. More...
 
bool copy_file (const std::string &original, const std::string &copy)
 Makes a copy of an existing file. More...
 
bool file_contains_string (const std::string &filename, const std::string &str)
 Checks if a file contains string 'str'. More...
 
void read_file_to_string (const std::string &filename, std::string &str)
 Reads the contents of a file into a string. More...
 
void write_string_to_file (const std::string &str, const std::string &filename)
 Writes the string into a file. More...
 

Detailed Description

A very basic filesystem implementation.

Attention
Functions are not sufficiently tested.
Todo:
[Liangliang] Comprehensive tests.
See also
ghc::filesystem - A C++17-like filesystem implementation for C++11/C++147/C++17 https://github.com/gulrak/filesystem/blob/master/include/ghc/filesystem.hpp

Function Documentation

◆ absolute_path()

std::string easy3d::file_system::absolute_path ( const std::string &  path)

Converts to absolute path (i.e., removes .. and . from a path string).

Parameters
pathThe path string.
Returns
The absolute path.

◆ base_name()

std::string easy3d::file_system::base_name ( const std::string &  path)

Gets file name without path and last extension (e.g., c:/file.ext1.ext2 => file.ext1; /a/b/c.Ext => c).

Parameters
pathThe full path of a file.
Returns
The string of the file name without path and last extension

◆ convert_to_native_style()

std::string easy3d::file_system::convert_to_native_style ( const std::string &  path)

Convert a path string such that it uses the current platform's path separators.

Parameters
pathThe path string.
Returns
The path in native style.
See also
convert_to_windows_style(), convert_to_unix_style().
Examples
Tutorial_203_Viewer_wxWidgets.

◆ convert_to_unix_style()

std::string easy3d::file_system::convert_to_unix_style ( const std::string &  path)

Converts the path to Unix style, i.e., back slashes (\) to forward slashes (/).

Parameters
pathThe path string.
Returns
The path in Unix style.
See also
convert_to_windows_style(), convert_to_native_style().

◆ convert_to_windows_style()

std::string easy3d::file_system::convert_to_windows_style ( const std::string &  path)

Converts the path to Windows style, i.e., forward slashes (/) to back slashes (\).

Parameters
pathThe path string.
Returns
The path in Windows style.
See also
convert_to_unix_style(), convert_to_native_style().

◆ copy_file()

bool easy3d::file_system::copy_file ( const std::string &  original,
const std::string &  copy 
)

Makes a copy of an existing file.

Parameters
originalThe file name of the original file.
copyThe file name of the copy
Returns
true on success.

◆ create_directory()

bool easy3d::file_system::create_directory ( const std::string &  path)

Creates a directory entitled 'path'.

Parameters
pathThe full path of the directory to be created.
Returns
ture If the directory has been successfully created or already exists.

◆ current_working_directory()

std::string easy3d::file_system::current_working_directory ( )

Query the current working directory.

Returns
The string representing the current working directory.

◆ delete_contents()

bool easy3d::file_system::delete_contents ( const std::string &  path)

Deletes the contents of the directory 'path' (the directory will not be deleted).

Parameters
pathThe full path of a directory.
Returns
ture If the contents of directory have been successfully deleted or don't exist.
Todo:
[Liangliang] Test if path should be absolute.

◆ delete_directory()

bool easy3d::file_system::delete_directory ( const std::string &  path)

Deletes the directory 'path' (and its contents will be deleted recursively).

Parameters
pathThe full path of a directory.
Returns
ture If the directory has been successfully deleted or doesn't exist.
Todo:
[Liangliang] Test if path should be absolute.

◆ delete_file()

bool easy3d::file_system::delete_file ( const std::string &  path)

Deletes the file 'path'.

Parameters
pathThe full path of a file.
Returns
ture If the file has been successfully deleted or doesn't exist.
Todo:
[Liangliang] Test if path should be absolute.

◆ executable()

std::string easy3d::file_system::executable ( )

Query the name of this executable.

Returns
The string representing the full path of this executable, e.g., C:/a/b/c.exe

◆ executable_directory()

std::string easy3d::file_system::executable_directory ( )

Query the directory where the executable file is located.

Returns
The string representing the directory in which the executable resides.

◆ extension()

std::string easy3d::file_system::extension ( const std::string &  path,
bool  lower = true 
)

Query the file extension without dot (e.g., /a/b/c.Ext => Ext).

Parameters
pathThe full path of a file.
lowerConverts the string to lower case if true.
Returns
The string of the file extension.
Examples
Tutorial_203_Viewer_wxWidgets, Tutorial_204_Viewer_Qt, and Tutorial_308_TexturedMesh.

◆ file_contains_string()

bool easy3d::file_system::file_contains_string ( const std::string &  filename,
const std::string &  str 
)

Checks if a file contains string 'str'.

Parameters
filenameThe string of the file name.
strThe string to be checked.
Returns
true if the file contains the string.

◆ file_size()

std::ifstream::pos_type easy3d::file_system::file_size ( const std::string &  path)

Query the size of the file.

Parameters
pathThe full path of the file.
Returns
The size of the file (in bytes).

◆ get_directory_entries()

void easy3d::file_system::get_directory_entries ( const std::string &  path,
std::vector< std::string > &  entries,
bool  recursive 
)

Queries the entries of a directory (including subdirectories and files).

Parameters
pathThe full path of a directory.
entriesReturn the entries of the directory. Result strings are simple names (i.e., without the path part, e.g., cat.png).
recursivePerforms the query recursively if true.
See also
get_files(), get_sub_directories().

◆ get_files()

void easy3d::file_system::get_files ( const std::string &  path,
std::vector< std::string > &  files,
bool  recursive 
)

Queries file entries of a directory 'path'.

Parameters
pathThe full path of a directory.
filesReturn the file entries of 'path'. Result strings are simple names (i.e., without the 'path' part, e.g., cat.png).
recursivePerform the query recursively if true.
See also
get_directory_entries(), get_sub_directories().

◆ get_sub_directories()

void easy3d::file_system::get_sub_directories ( const std::string &  path,
std::vector< std::string > &  subs,
bool  recursive 
)

Query subdirectory entries of a directory 'path'.

Parameters
pathThe full path of a directory.
subsReturn the subdirectory entries of 'path'. Result strings are only the names of the subdirectories (i.e., without the 'path' part).
recursivePerform the query recursively if true.
See also
get_directory_entries(), get_files().

◆ home_directory()

std::string easy3d::file_system::home_directory ( )

Query the home path for the current user.

Returns
The string representing the home directory of the current user.

◆ is_absolute_path()

bool easy3d::file_system::is_absolute_path ( const std::string &  path)

Tests if path is absolute, as !get_path_root(path).empty(). .

Parameters
pathThe path.
Returns
true if the path is absolute. Tests if path is absolute, as !get_path_root(path).empty().

◆ is_directory()

bool easy3d::file_system::is_directory ( const std::string &  path)

Tests if 'path' is an existing directory.

Parameters
pathThe full path of a directory.
Returns
ture If the directory 'path' exists.
Examples
Tutorial_204_Viewer_Qt.

◆ is_file()

bool easy3d::file_system::is_file ( const std::string &  path)

Tests if 'path' is an existing file.

Parameters
pathThe full path of a file (including file extension).
Returns
ture If the file 'path' exists.
Examples
Tutorial_308_TexturedMesh.

◆ is_native_style()

bool easy3d::file_system::is_native_style ( const std::string &  path)

Checks sif the path contains only the current platform's path separators.

Returns
true if the path contains only the current platform's path separators.

◆ name_less_all_extensions()

std::string easy3d::file_system::name_less_all_extensions ( const std::string &  path)

Gets file path without all extensions (e.g., /a/b/c.Ext => /a/b/c; file.ext1.ext2 => file).

Parameters
pathThe full path of a file.
Returns
The string of the file path without all extensions.

◆ name_less_extension()

std::string easy3d::file_system::name_less_extension ( const std::string &  path)

Gets file path without last extension (e.g., /a/b/c.Ext => /a/b/c; file.ext1.ext2 => file.ext1).

Parameters
pathThe full path of a file.
Returns
The string of the file path without last extension.

◆ native_path_separator()

char easy3d::file_system::native_path_separator ( )

Gets the path separator of the current platform.

Returns
The path separator in native style.

◆ parent_directory()

std::string easy3d::file_system::parent_directory ( const std::string &  path)

Query the parent path from full name of a file or directory (e.g., /a/b/c.Ext => /a/b)

Parameters
pathThe full path of the file or directory.
Returns
The string of the parent path.
Examples
Tutorial_203_Viewer_wxWidgets.

◆ path_root()

std::string easy3d::file_system::path_root ( const std::string &  path)

Gets root part of a path ("/" or "C:"), or an empty string if none found.

Parameters
pathThe path.
Returns
The string representing the root part of the path.

◆ read_file_to_string()

void easy3d::file_system::read_file_to_string ( const std::string &  filename,
std::string &  str 
)

Reads the contents of a file into a string.

Parameters
filenameThe string of the file name.
strThe destination string.

◆ relative_path()

std::string easy3d::file_system::relative_path ( const std::string &  from,
const std::string &  to 
)

Compute the relative path from 'from' to 'to'.

Parameters
fromThe 'from' directory.
toThe 'to' directory.
Returns
If 'to' is in a subdirectory of 'from' then this function returns the subpath, otherwise it just returns the file name.
Attention
The function does not automatically resolve paths as the system does, so be careful to give canonical paths. However, the function interprets slashes ('/') and backslashes ('\') as they were equal.

◆ rename_file()

bool easy3d::file_system::rename_file ( const std::string &  old_name,
const std::string &  new_name 
)

Rename the file from 'old_name' to 'new_name'.

Parameters
old_nameThe full path of the file to be renamed.
new_nameThe full path of the file's new name.
Returns
true on success.

◆ replace_extension()

std::string easy3d::file_system::replace_extension ( std::string const &  path,
const std::string &  ext 
)

Replaces the extension of the given file with 'ext'. If the file name does not have an extension, the expected new extension is appended.

Parameters
pathThe file name.
extThe expected new extension.
Returns
The string of the new file name/
Examples
Tutorial_203_Viewer_wxWidgets, and Tutorial_204_Viewer_Qt.

◆ set_current_working_directory()

bool easy3d::file_system::set_current_working_directory ( const std::string &  path)

Set the current working directory.

Parameters
pathThe string representing the new current working directory.
Returns
true on success.

◆ simple_name()

std::string easy3d::file_system::simple_name ( const std::string &  path)

Gets file name without path but with extension (e.g, /a/b/c.Ext => c.Ext)

Parameters
pathThe full path of a file.
Returns
The string of the file name without path but with extension.
Examples
Tutorial_203_Viewer_wxWidgets, and Tutorial_204_Viewer_Qt.

◆ time_stamp()

time_t easy3d::file_system::time_stamp ( const std::string &  path)

Query the time stamp of a file or directory.

Parameters
pathThe full path of the file or directory.
Returns
The time stamp.

◆ time_string()

std::string easy3d::file_system::time_string ( const std::string &  path)

Query the time stamp of a file or directory as a string.

Parameters
pathThe full path of the file or directory.
Returns
The string of the time stamp.

◆ write_string_to_file()

void easy3d::file_system::write_string_to_file ( const std::string &  str,
const std::string &  filename 
)

Writes the string into a file.

Parameters
strThe string.
filenameThe string of the file name.