]> git.localhorst.tv Git - blank.git/blobdiff - src/app/io.cpp
save and load world seed
[blank.git] / src / app / io.cpp
index 7996be6a14b45ae63bf1057d3e89f6485902ee57..f9c5805a114d8173c1f2576ee1aa4973487b4e87 100644 (file)
@@ -21,7 +21,23 @@ bool is_dir(const char *path) {
        if (stat(path, &info) != 0) {
                return false;
        }
-       return (info.st_mode & S_IFDIR) != 0;
+       return S_ISDIR(info.st_mode);
+#endif
+}
+
+bool is_file(const char *path) {
+#ifdef _WIN32
+       struct _stat info;
+       if (_stat(path, &info) != 0) {
+               return false;
+       }
+       return (info.st_mode & _S_IFREG) != 0;
+#else
+       struct stat info;
+       if (stat(path, &info) != 0) {
+               return false;
+       }
+       return S_ISREG(info.st_mode);
 #endif
 }
 
@@ -37,7 +53,7 @@ bool make_dir(const char *path) {
 
 
 bool make_dirs(const std::string &path) {
-       if (make_dir(path.c_str())) {
+       if (make_dir(path)) {
                return true;
        }
 
@@ -54,16 +70,27 @@ bool make_dirs(const std::string &path) {
                                if (pos == std::string::npos) {
                                        return false;
                                }
+                               if (pos == path.length() - 1) {
+                                       // trailing separator, would make final make_dir fail
+#ifdef _WIN32
+                                        pos = path.find_last_of("\\/", pos - 1);
+#else
+                                        pos = path.find_last_of('/', pos - 1);
+#endif
+                                       if (pos == std::string::npos) {
+                                               return false;
+                                       }
+                               }
                                if (!make_dirs(path.substr(0, pos))) {
                                        return false;
                                }
                        }
                        // try again
-                       return make_dir(path.c_str());
+                       return make_dir(path);
 
                case EEXIST:
                        // something's there, check if it's a dir and we're good
-                       return is_dir(path.c_str());
+                       return is_dir(path);
 
                default:
                        // whatever else went wrong, it can't be good