-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_utils.cpp
100 lines (87 loc) · 3.47 KB
/
fs_utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "fs_utils.hpp"
#include <iostream>
std::string get_directory_from_filepath(const std::string &filepath) {
std::filesystem::path path(filepath);
return path.parent_path().string();
}
bool has_extension(const std::string &file_path, const std::string extension) {
// Get the file extension
std::filesystem::path path(file_path);
std::string file_extension = path.extension().string();
// Compare extensions (case insensitive)
return (file_extension == extension ||
(file_extension.size() > 1 && file_extension[0] == '.' && file_extension.substr(1) == extension));
}
std::string get_parent_directory(const std::string ¤t_dir) {
std::filesystem::path path(current_dir);
// Check if the current directory is the root directory
if (path == path.root_path()) {
// Already at the root directory, do nothing
return current_dir;
}
// Get the parent directory
path = path.parent_path();
return path.string();
}
std::string get_home_directory() {
#ifdef _WIN32
const char *home_drive = std::getenv("HOMEDRIVE");
const char *home_path = std::getenv("HOMEPATH");
if (home_drive && home_path) {
return std::string(home_drive) + home_path;
}
return std::getenv("USERPROFILE") ? std::getenv("USERPROFILE") : "";
#else
const char *home = std::getenv("HOME");
return home ? home : "";
#endif
}
std::vector<std::string> list_files_and_directories(const std::string &path, bool include_hidden) {
std::vector<std::string> paths;
try {
for (const std::filesystem::directory_entry &entry : std::filesystem::directory_iterator(path)) {
// Check if the file is hidden (starts with a dot on Unix-like systems)
if (!include_hidden && entry.path().filename().string().starts_with(".")) {
continue;
}
paths.push_back(entry.path().string());
}
} catch (const std::filesystem::filesystem_error &e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return paths;
}
/**
* @brief Checks if a given path is a directory.
*
* @param path The path to check.
* @return true if the path is a directory, false otherwise.
*/
bool is_directory(const std::string path) {
try {
if (std::filesystem::exists(path)) { // Check if the path exists
return std::filesystem::is_directory(path); // Check if it's a directory
}
std::cerr << "Error: Path does not exist." << std::endl;
return false; // Return false if the path doesn't exist
} catch (const std::filesystem::filesystem_error &e) {
std::cerr << "Filesystem error: " << e.what() << std::endl;
return false; // Return false if an exception occurs
}
}
// Function to list all files in a directory
std::vector<std::string> list_files_in_directory(const std::string &path) {
return list_files_and_directories(path, false); // Exclude hidden files by default
}
// Function to list all files in a directory that match a regex pattern
std::vector<std::string> list_files_matching_regex(const std::string &path, const std::regex &pattern) {
std::vector<std::string> all_files = list_files_in_directory(path);
std::vector<std::string> matching_files;
// Iterate through all files and match with regex pattern
for (const std::string &file : all_files) {
if (std::regex_search(file, pattern)) {
matching_files.push_back(file);
}
}
return matching_files;
}