CPP-TOOLBOX
Loading...
Searching...
No Matches
fs_utils.hpp
Go to the documentation of this file.
1#ifndef FS_UTILS_HPP
2#define FS_UTILS_HPP
3
4#include <filesystem>
5#include <string>
6#include <vector>
7
12
13namespace fs_utils {
14
20bool path_exists(const std::string &path);
21
30std::string normalize_path_for_os(const std::string &path);
31
37std::string get_path_delimiter();
38
44std::filesystem::path expand_tilde(const std::filesystem::path &path);
45
51std::filesystem::path get_directory_from_filepath(const std::filesystem::path &filepath);
52
58std::string get_containing_directory(const std::string &filepath);
59
65std::string get_filename_from_path(const std::string &path_str);
66
72std::filesystem::path get_parent_directory(const std::filesystem::path &current_dir);
73
78std::filesystem::path get_home_directory();
79
86bool has_extension(const std::filesystem::path &file_path, const std::string &extension);
87
94std::vector<std::filesystem::path> list_files_and_directories(const std::filesystem::path &path,
95 bool include_hidden = false);
96
102std::vector<std::filesystem::path> list_files_in_directory(const std::filesystem::path &path);
103
110std::vector<std::filesystem::path> list_files_matching_regex(const std::filesystem::path &path,
111 const std::string &pattern_str);
112
120std::vector<std::filesystem::path> rec_get_all_files(const std::string &base_dir,
121 const std::vector<std::string> &ignore_dirs, int limit = 1000);
122
129bool file_exists_in_same_dir(const std::filesystem::path &file_path, const std::filesystem::path &target_file_name);
130
136bool create_directory(const std::filesystem::path &dir_path);
137
149bool create_file_with_content(const std::filesystem::path &file_path, const std::string &content);
150
164bool create_file_with_content_if_different(const std::filesystem::path &file_path, const std::string &content);
165
171std::string get_directory_of_file(const std::string &file_path);
172
179std::filesystem::path get_relative_path(const std::filesystem::path &source, const std::filesystem::path &target);
180
181} // namespace fs_utils
182
183#endif // FS_UTILS_HPP
Definition __init__.py:1
std::filesystem::path get_parent_directory(const std::filesystem::path &current_dir)
Get the parent directory of a given path.
Definition fs_utils.cpp:107
std::vector< std::filesystem::path > list_files_in_directory(const std::filesystem::path &path)
List only files in a given directory (non-recursive).
Definition fs_utils.cpp:149
std::string get_filename_from_path(const std::string &path_str)
Extract the filename from a full path string.
Definition fs_utils.cpp:103
std::string normalize_path_for_os(const std::string &path)
Normalize a path string to match the current operating system's conventions.
Definition fs_utils.cpp:11
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.
Definition fs_utils.cpp:242
std::string get_path_delimiter()
Get the directory delimiter for the current operating system.
Definition fs_utils.cpp:24
bool create_file_with_content(const std::filesystem::path &file_path, const std::string &content)
Create a new file and write content to it.
Definition fs_utils.cpp:221
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.
Definition fs_utils.cpp:275
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.
Definition fs_utils.cpp:173
bool create_directory(const std::filesystem::path &dir_path)
Create a new directory.
Definition fs_utils.cpp:208
std::vector< std::filesystem::path > list_files_and_directories(const std::filesystem::path &path, bool include_hidden)
List files and directories in a given path.
Definition fs_utils.cpp:132
std::string get_containing_directory(const std::string &filepath)
Get the containing directory as a string.
Definition fs_utils.cpp:97
std::filesystem::path expand_tilde(const std::filesystem::path &path)
Expand a leading tilde (~) to the user's home directory.
Definition fs_utils.cpp:26
std::vector< std::filesystem::path > rec_get_all_files(const std::string &base_dir, const std::vector< std::string > &ignore_dirs, int limit)
Recursively get all files from a base directory.
Definition fs_utils.cpp:40
std::filesystem::path get_home_directory()
Get the home directory of the current user.
Definition fs_utils.cpp:118
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.
Definition fs_utils.cpp:154
bool path_exists(const std::string &path)
Check if a filesystem path exists.
Definition fs_utils.cpp:9
std::string get_directory_of_file(const std::string &file_path)
Get the directory containing a given file.
Definition fs_utils.cpp:270
bool has_extension(const std::filesystem::path &file_path, const std::string &extension)
Check if a file path has a specific extension.
Definition fs_utils.cpp:88
std::filesystem::path get_directory_from_filepath(const std::filesystem::path &filepath)
Extract the directory component from a full file path.
Definition fs_utils.cpp:84