-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
38 lines (32 loc) · 1.47 KB
/
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
#include "utils.h"
bool createDirectory(const std::string& path) {
// Attempt to create the directory.
int result = mkdir(path.c_str(), 0777);
// Check if the directory was created successfully.
if (result == 0) {
std::cout << "Directory created successfully." << std::endl;
return true;
} else if (errno == EEXIST) {
// If the failure was because the directory already exists.
std::cout << "Directory already exists." << std::endl;
return true; // This is true in the sense that the desired end state (directory exists) is true.
} else {
// If directory creation failed for a reason other than it already existing.
std::cerr << "Failed to create directory: " << strerror(errno) << std::endl;
return false;
}
}
string extractSecondLastFolder(const string &filePath) {
size_t lastSlashPos = filePath.rfind('/'); // Find the last slash
// Handle cases where there is no slash or only a single leading slash, indicating no parent directories
if (lastSlashPos == string::npos || lastSlashPos == 0) {
return "";
}
size_t secondLastSlashPos = filePath.rfind('/', lastSlashPos - 1); // Find the second last slash
if (secondLastSlashPos == string::npos) {
return "";
}
// Extract the folder name between the second last slash and the last slash
string folder = filePath.substr(secondLastSlashPos + 1, lastSlashPos - secondLastSlashPos - 1);
return folder;
}