file_io: add initialization for fileTYPE and more support for zip.

This commit is contained in:
sorgelig
2019-11-20 13:40:01 +08:00
parent b33d930bfa
commit 0b31ad20a7
2 changed files with 26 additions and 37 deletions

View File

@@ -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;
}

View File

@@ -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
{