-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathHelper.cpp
67 lines (52 loc) · 1.24 KB
/
PathHelper.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
#include "PathHelper.h"
#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
PathHelper::PathHelper()
{
}
PathHelper::~PathHelper()
{
}
bool PathHelper::isValidPath(const char* path) {
return PathHelper::isDir(path) || PathHelper::isFile(path);
}
bool PathHelper::isDir(const char* path) {
DIR * dir = opendir(path);
if (dir != nullptr) {
if (closedir(dir) != 0) {
std::cout << "[ERROR] The directory '" << path << "' was not closed !" << std::endl;
}
return true;
}
else {
return false;
}
}
bool PathHelper::isFile(const char* path) {
FILE* file;
#ifdef __cplusplus
file = fopen(path, "r");
bool isOpen = (file != nullptr);
#else
errno_t err = fopen_s(&file, path, "r");
bool isOpen = (err == 0);
#endif
if (isOpen) {
if (fclose(file) != 0) {
std::cout << "[ERROR] The file '" << path << "' was not closed !" << std::endl;
}
return true;
}
else {
return false;
}
}
char* PathHelper::getEntryPath(const char* path, const char* entry) {
char *entryPath = (char*)malloc(sizeof(char)*(strlen(path) + strlen(entry) + 2));
strcpy(entryPath, path);
strcat(entryPath, "/");
strcat(entryPath, entry);
return entryPath;
}