CPP-TOOLBOX
Loading...
Searching...
No Matches
fs_utils Namespace Reference

Namespaces

namespace  directory_modifiction_callback_system
 
namespace  main
 

Functions

bool path_exists (const std::string &path)
 Check if a filesystem path exists.
 
std::string normalize_path_for_os (const std::string &path)
 Normalize a path string to match the current operating system's conventions.
 
std::string get_path_delimiter ()
 Get the directory delimiter for the current operating system.
 
std::filesystem::path expand_tilde (const std::filesystem::path &path)
 Expand a leading tilde (~) to the user's home directory.
 
std::vector< std::filesystem::path > rec_get_all_files (const std::string &base_dir, const std::vector< std::string > &ignore_dirs, int limit=1000)
 Recursively get all files from a base directory.
 
std::filesystem::path get_directory_from_filepath (const std::filesystem::path &filepath)
 Extract the directory component from a full file path.
 
bool has_extension (const std::filesystem::path &file_path, const std::string &extension)
 Check if a file path has a specific extension.
 
std::string get_containing_directory (const std::string &filepath)
 Get the containing directory as a string.
 
std::string get_filename_from_path (const std::string &path_str)
 Extract the filename from a full path string.
 
std::filesystem::path get_parent_directory (const std::filesystem::path &current_dir)
 Get the parent directory of a given path.
 
std::filesystem::path get_home_directory ()
 Get the home directory of the current user.
 
std::vector< std::filesystem::path > list_files_and_directories (const std::filesystem::path &path, bool include_hidden=false)
 List files and directories in a given path.
 
std::vector< std::filesystem::path > list_files_in_directory (const std::filesystem::path &path)
 List only files in a given directory (non-recursive).
 
std::vector< std::filesystem::path > list_files_matching_regex (const std::filesystem::path &path, const std::string &pattern_str)
 List files matching a regular expression pattern.
 
bool file_exists_in_same_dir (const std::filesystem::path &file_path, const std::filesystem::path &target_file_name)
 Check if a target file exists in the same directory as another file.
 
bool create_directory (const std::filesystem::path &dir_path)
 Create a new directory.
 
bool create_file_with_content (const std::filesystem::path &file_path, const std::string &content)
 Create a new file and write content to it.
 
bool create_file_with_content_if_different (const std::filesystem::path &file_path, const std::string &content)
 Create or update a file only if the new content differs from the current one.
 
std::string get_directory_of_file (const std::string &file_path)
 Get the directory containing a given file.
 
std::filesystem::path get_relative_path (const std::filesystem::path &source, const std::filesystem::path &target)
 Get the relative path from a source to a target.
 

Function Documentation

◆ create_directory()

bool fs_utils::create_directory ( const std::filesystem::path & dir_path)

Create a new directory.

Parameters
dir_pathPath of the directory to create.
Returns
true if the directory was created, false otherwise.

◆ create_file_with_content()

bool fs_utils::create_file_with_content ( const std::filesystem::path & file_path,
const std::string & content )

Create a new file and write content to it.

Ensures the parent directory exists before writing. If the directory cannot be created or the file cannot be opened for writing, the function returns false.

Parameters
file_pathPath of the file to create.
contentContent to write into the file.
Returns
true if the file was created and written successfully, false otherwise.

◆ create_file_with_content_if_different()

bool fs_utils::create_file_with_content_if_different ( const std::filesystem::path & file_path,
const std::string & content )

Create or update a file only if the new content differs from the current one.

If the target file already exists, its contents are read and compared with the new content. The file is rewritten only if the content differs. Otherwise, no write operation is performed.

Internally calls create_file_with_content() for the actual writing and directory creation.

Parameters
file_pathPath of the file to create or update.
contentContent to write into the file.
Returns
true if the file was created, updated, or already identical, false otherwise.

◆ expand_tilde()

std::filesystem::path fs_utils::expand_tilde ( const std::filesystem::path & path)

Expand a leading tilde (~) to the user's home directory.

Parameters
pathPath potentially starting with "~".
Returns
Expanded filesystem path.

◆ file_exists_in_same_dir()

bool fs_utils::file_exists_in_same_dir ( const std::filesystem::path & file_path,
const std::filesystem::path & target_file_name )

Check if a target file exists in the same directory as another file.

Parameters
file_pathPath to a reference file.
target_file_nameName of the target file.
Returns
true if the target file exists in the same directory, false otherwise.

◆ get_containing_directory()

std::string fs_utils::get_containing_directory ( const std::string & filepath)

Get the containing directory as a string.

Parameters
filepathFull file path.
Returns
Directory containing the file, as a string.

◆ get_directory_from_filepath()

std::filesystem::path fs_utils::get_directory_from_filepath ( const std::filesystem::path & filepath)

Extract the directory component from a full file path.

Parameters
filepathFull file path.
Returns
Directory containing the file.

◆ get_directory_of_file()

std::string fs_utils::get_directory_of_file ( const std::string & file_path)

Get the directory containing a given file.

Parameters
file_pathFile path.
Returns
Directory containing the file, as a string.

◆ get_filename_from_path()

std::string fs_utils::get_filename_from_path ( const std::string & path_str)

Extract the filename from a full path string.

Parameters
path_strInput path string.
Returns
Filename portion of the path.

◆ get_home_directory()

std::filesystem::path fs_utils::get_home_directory ( )

Get the home directory of the current user.

Returns
Filesystem path to the home directory.

◆ get_parent_directory()

std::filesystem::path fs_utils::get_parent_directory ( const std::filesystem::path & current_dir)

Get the parent directory of a given path.

Parameters
current_dirInput path.
Returns
Parent directory of the input path.

◆ get_path_delimiter()

std::string fs_utils::get_path_delimiter ( )

Get the directory delimiter for the current operating system.

Returns
"/" on POSIX systems, "\\" on Windows.

◆ get_relative_path()

std::filesystem::path fs_utils::get_relative_path ( const std::filesystem::path & source,
const std::filesystem::path & target )

Get the relative path from a source to a target.

Parameters
sourceBase path.
targetTarget path.
Returns
Relative path from source to target.

◆ has_extension()

bool fs_utils::has_extension ( const std::filesystem::path & file_path,
const std::string & extension )

Check if a file path has a specific extension.

Parameters
file_pathPath to the file.
extensionExtension to check (with or without dot).
Returns
true if the file has the specified extension, false otherwise.

◆ list_files_and_directories()

std::vector< std::filesystem::path > fs_utils::list_files_and_directories ( const std::filesystem::path & path,
bool include_hidden = false )

List files and directories in a given path.

Parameters
pathDirectory to list.
include_hiddenWhether to include hidden files (default: false).
Returns
Vector of filesystem paths.

◆ list_files_in_directory()

std::vector< std::filesystem::path > fs_utils::list_files_in_directory ( const std::filesystem::path & path)

List only files in a given directory (non-recursive).

Parameters
pathDirectory to list.
Returns
Vector of file paths.

◆ list_files_matching_regex()

std::vector< std::filesystem::path > fs_utils::list_files_matching_regex ( const std::filesystem::path & path,
const std::string & pattern_str )

List files matching a regular expression pattern.

Parameters
pathDirectory to search.
pattern_strRegular expression pattern as a string.
Returns
Vector of matching file paths.

◆ normalize_path_for_os()

std::string fs_utils::normalize_path_for_os ( const std::string & path)

Normalize a path string to match the current operating system's conventions.

Converts delimiters and resolves inconsistencies.

Parameters
pathInput path string.
Returns
Normalized path string.

◆ path_exists()

bool fs_utils::path_exists ( const std::string & path)

Check if a filesystem path exists.

Parameters
pathPath string to check.
Returns
true if the path exists, false otherwise.

◆ rec_get_all_files()

std::vector< std::filesystem::path > fs_utils::rec_get_all_files ( const std::string & base_dir,
const std::vector< std::string > & ignore_dirs,
int limit = 1000 )

Recursively get all files from a base directory.

Parameters
base_dirDirectory to search.
ignore_dirsDirectories to exclude.
limitMaximum number of files to return (default: 1000).
Returns
Vector of file paths.