diff --git a/file_io.cpp b/file_io.cpp index def1b4d..2c1664a 100644 --- a/file_io.cpp +++ b/file_io.cpp @@ -185,45 +185,44 @@ static int isPathDirectory(const char *path, int use_zip = 1) char *zip_path, *file_path; if (use_zip && FileIsZipped(full_path, &zip_path, &file_path)) { + if (!*file_path) + { + return 1; + } - if (!*file_path) - { - return 1; - } - - if (!OpenZipfileCached(full_path, 0)) - { - printf("isPathDirectory(OpenZipfileCached) Zip:%s, error:%s\n", zip_path, - mz_zip_get_error_string(mz_zip_get_last_error(&last_zip_archive))); - return 0; - } + if (!OpenZipfileCached(full_path, 0)) + { + printf("isPathDirectory(OpenZipfileCached) Zip:%s, error:%s\n", zip_path, + mz_zip_get_error_string(mz_zip_get_last_error(&last_zip_archive))); + return 0; + } // Folder names always end with a slash in the zip // file central directory. strcat(file_path, "/"); + // Some zip files don't have directory entries + // Use the locate_file call to try and find the directory entry first, since + // this is a binary search (usually) If that fails then scan for the first + // entry that starts with file_path - // Some zip files don't have directory entries - // Use the locate_file call to try and find the directory entry first, since - // this is a binary search (usually) If that fails then scan for the first - // entry that starts with file_path + const int file_index = mz_zip_reader_locate_file(&last_zip_archive, file_path, NULL, 0); + if (file_index >= 0 && mz_zip_reader_is_file_a_directory(&last_zip_archive, file_index)) + { + return 1; + } - const int file_index = mz_zip_reader_locate_file(&last_zip_archive, file_path, NULL, 0); - if (file_index >= 0 && mz_zip_reader_is_file_a_directory(&last_zip_archive, file_index)) - { - return 1; - } - - for (size_t i = 0; i < mz_zip_reader_get_num_files(&last_zip_archive); i++) { - char zip_fname[256]; - mz_zip_reader_get_filename(&last_zip_archive, i, &zip_fname[0], sizeof(zip_fname)); - if (strcasestr(zip_fname, file_path)) - { - return 1; - } - } - return 0; - } + for (size_t i = 0; i < mz_zip_reader_get_num_files(&last_zip_archive); i++) + { + char zip_fname[256]; + mz_zip_reader_get_filename(&last_zip_archive, i, &zip_fname[0], sizeof(zip_fname)); + if (strcasestr(zip_fname, file_path)) + { + return 1; + } + } + return 0; + } else { int stmode = get_stmode(full_path); @@ -246,17 +245,17 @@ static int isPathRegularFile(const char *path, int use_zip = 1) char *zip_path, *file_path; if (use_zip && FileIsZipped(full_path, &zip_path, &file_path)) { - //If there's no path into the zip file, don't bother opening it, we're a "directory" - if (!*file_path) - { - return 0; - } - if (!OpenZipfileCached(full_path, 0)) - { - //printf("isPathRegularFile(mz_zip_reader_init_file) Zip:%s, error:%s\n", zip_path, - // mz_zip_get_error_string(mz_zip_get_last_error(&z))); - return 0; - } + //If there's no path into the zip file, don't bother opening it, we're a "directory" + if (!*file_path) + { + return 0; + } + if (!OpenZipfileCached(full_path, 0)) + { + //printf("isPathRegularFile(mz_zip_reader_init_file) Zip:%s, error:%s\n", zip_path, + // mz_zip_get_error_string(mz_zip_get_last_error(&z))); + return 0; + } const int file_index = mz_zip_reader_locate_file(&last_zip_archive, file_path, NULL, 0); if (file_index < 0) { @@ -1262,30 +1261,30 @@ void AdjustDirectory(char *path) } } -static const char *GetRelativeFileName(const char *folder, const char *path) { - if (strcasestr(path, folder) == path) { - const char *subpath = path + strlen(folder); - if (*subpath != '\0') - { - if (*subpath == '/') - { - return subpath+1; - } - return subpath; - } - } - return NULL; +static const char *GetRelativeFileName(const char *folder, const char *path) +{ + if (!*folder) return path; + if (strcasestr(path, folder) == path) + { + const char *subpath = path + strlen(folder); + if (*subpath == '/') return subpath + 1; + } + return NULL; } static bool IsInSameFolder(const char *folder, const char *path) { if (strcasestr(path, folder) == path) { - const char *subpath = path + strlen(folder) + 1; - if (*subpath != '\0') + const char *subpath = path + strlen(folder); + if (*subpath == '/') { - const char *slash = strchr(subpath, '/'); - return !slash || *(slash + 1) == '\0'; + subpath++; + if (*subpath != '\0') + { + const char *slash = strchr(subpath, '/'); + return !slash || *(slash + 1) == '\0'; + } } } return false;