-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
91 lines (81 loc) · 1.88 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
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
#include <string>
// Pasted from:
// https://stackoverflow.com/questions/874134/find-if-string-ends-with-another-string-in-c
bool hasEnding (std::string const &fullString, std::string const &ending) {
if (fullString.length() >= ending.length()) {
return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
} else {
return false;
}
}
#ifdef _WIN32
#include <windows.h>
#include <Shlwapi.h>
bool isValidDirectory(const char *path){
return PathIsDirectory(path);
}
void fillFilenameVector(const char *dir, std::vector<std::string> &files){
// reference: https://msdn.microsoft.com/en-us/library/aa365200(VS.85).aspx
}
int makefolder(const char *path){
return -1;
}
void copyfile(const char *src, const char *dst){
}
#elif __linux__
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>
#include <cstdio>
#include <vector>
#include <string>
bool isValidDirectory(const char *path){
struct stat filestat;
bool res = false;
int err = stat(path, &filestat);
if(err != 0){
int tmp = errno;
fprintf(stderr, "Error %d occured.\n", tmp);
}
if(S_ISDIR(filestat.st_mode)){
res = true;
}
return res;
}
void fillFilenameVector(const char *dir, std::vector<std::string> &files){
if(!isValidDirectory(dir)){
return;
}
DIR *p = opendir(dir);
struct dirent *filep;
if(p == NULL){
return;
}
while((filep = readdir(p)) != NULL){
files.push_back(std::string(filep->d_name));
}
closedir(p);
}
int makefolder(const char *path)
{
Stat st;
int status = 0;
if (stat(path, &st) != 0)
{
/* Directory does not exist. EEXIST for race condition */
if (mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0 && errno != EEXIST) // mode 755
status = -1;
}
else if (!S_ISDIR(st.st_mode))
{
errno = ENOTDIR;
status = -1;
}
return(status);
}
#elif __unix__
#else
#error OS not supported
#endif