]> git.localhorst.tv Git - blank.git/blob - src/io/filesystem.cpp
more parameters for world generation
[blank.git] / src / io / filesystem.cpp
1 #include "filesystem.hpp"
2
3 #include <cerrno>
4 #ifdef _WIN32
5 #  include <direct.h>
6 #endif
7 #include <sys/stat.h>
8
9
10 namespace blank {
11
12 bool is_dir(const char *path) {
13 #ifdef _WIN32
14         struct _stat info;
15         if (_stat(path, &info) != 0) {
16                 return false;
17         }
18         return (info.st_mode & _S_IFDIR) != 0;
19 #else
20         struct stat info;
21         if (stat(path, &info) != 0) {
22                 return false;
23         }
24         return S_ISDIR(info.st_mode);
25 #endif
26 }
27
28 bool is_file(const char *path) {
29 #ifdef _WIN32
30         struct _stat info;
31         if (_stat(path, &info) != 0) {
32                 return false;
33         }
34         return (info.st_mode & _S_IFREG) != 0;
35 #else
36         struct stat info;
37         if (stat(path, &info) != 0) {
38                 return false;
39         }
40         return S_ISREG(info.st_mode);
41 #endif
42 }
43
44
45 bool make_dir(const char *path) {
46 #ifdef _WIN32
47         int ret = _mkdir(path);
48 #else
49         int ret = mkdir(path, 0777);
50 #endif
51         return ret == 0;
52 }
53
54
55 bool make_dirs(const std::string &path) {
56         if (make_dir(path)) {
57                 return true;
58         }
59
60         switch (errno) {
61
62                 case ENOENT:
63                         // missing component
64                         {
65 #ifdef _WIN32
66                                 auto pos = path.find_last_of("\\/");
67 #else
68                                 auto pos = path.find_last_of('/');
69 #endif
70                                 if (pos == std::string::npos) {
71                                         return false;
72                                 }
73                                 if (pos == path.length() - 1) {
74                                         // trailing separator, would make final make_dir fail
75 #ifdef _WIN32
76                                          pos = path.find_last_of("\\/", pos - 1);
77 #else
78                                          pos = path.find_last_of('/', pos - 1);
79 #endif
80                                         if (pos == std::string::npos) {
81                                                 return false;
82                                         }
83                                 }
84                                 if (!make_dirs(path.substr(0, pos))) {
85                                         return false;
86                                 }
87                         }
88                         // try again
89                         return make_dir(path);
90
91                 case EEXIST:
92                         // something's there, check if it's a dir and we're good
93                         return is_dir(path);
94
95                 default:
96                         // whatever else went wrong, it can't be good
97                         return false;
98
99         }
100 }
101
102 }