X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp%2Fio.cpp;h=f9c5805a114d8173c1f2576ee1aa4973487b4e87;hb=f5e5e8522b94a6b81a137d4bca7665ef15bcd2c6;hp=7996be6a14b45ae63bf1057d3e89f6485902ee57;hpb=e24b4ec1a0fb3ba58a8ea67cd8d8affe3c5a0a71;p=blank.git diff --git a/src/app/io.cpp b/src/app/io.cpp index 7996be6..f9c5805 100644 --- a/src/app/io.cpp +++ b/src/app/io.cpp @@ -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