From 0b31ad20a7cecd283ea3eea7638befdd53119fee Mon Sep 17 00:00:00 2001 From: sorgelig Date: Wed, 20 Nov 2019 13:40:01 +0800 Subject: [PATCH] file_io: add initialization for fileTYPE and more support for zip. --- file_io.cpp | 56 ++++++++++++++++++++--------------------------------- file_io.h | 7 +++++-- 2 files changed, 26 insertions(+), 37 deletions(-) diff --git a/file_io.cpp b/file_io.cpp index 023289f..11517cf 100644 --- a/file_io.cpp +++ b/file_io.cpp @@ -42,6 +42,21 @@ static int iFirstEntry = 0; static char full_path[2100]; +fileTYPE::fileTYPE() +{ + filp = 0; + mode = 0; + type = 0; + zip = 0; + size = 0; + offset = 0; +} + +int fileTYPE::opened() +{ + return filp || zip; +} + struct fileZipArchive { mz_zip_archive archive; @@ -200,7 +215,6 @@ void FileClose(fileTYPE *file) mz_zip_reader_end(&file->zip->archive); delete file->zip; - file->zip = nullptr; } if (file->filp) @@ -216,6 +230,8 @@ void FileClose(fileTYPE *file) file->type = 0; } } + + file->zip = nullptr; file->filp = nullptr; } @@ -553,40 +569,10 @@ int FileSaveJoymap(const char *name, void *pBuffer, int size) int FileLoad(const char *name, void *pBuffer, int size) { - if (name[0] != '/') sprintf(full_path, "%s/%s", getRootDir(), name); - else strcpy(full_path, name); - - int fd = open(full_path, O_RDONLY); - if (fd < 0) - { - printf("FileLoad(open) File:%s, error: %d.\n", full_path, fd); - return 0; - } - - struct stat64 st; - int ret = fstat64(fd, &st); - if (ret < 0) - { - printf("FileLoad(fstat) File:%s, error: %d.\n", full_path, ret); - close(fd); - return 0; - } - - if (!pBuffer) - { - close(fd); - return (int)st.st_size; - } - - ret = read(fd, pBuffer, size ? size : st.st_size); - close(fd); - - if (ret < 0) - { - printf("FileLoad(read) File:%s, error: %d.\n", full_path, ret); - return 0; - } - + fileTYPE f; + if (!FileOpen(&f, name)) return 0; + int ret = FileReadAdv(&f, pBuffer, size); + FileClose(&f); return ret; } diff --git a/file_io.h b/file_io.h index 8a388ca..b0bcb36 100644 --- a/file_io.h +++ b/file_io.h @@ -9,8 +9,11 @@ struct fileZipArchive; -typedef struct +struct fileTYPE { + fileTYPE(); + int opened(); + FILE *filp; int mode; int type; @@ -19,7 +22,7 @@ typedef struct __off64_t offset; char path[1024]; char name[261]; -} fileTYPE; +}; struct direntext_t {