1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-11-25 17:53:47 +00:00
Aquaria/ExternalLibs/ttvfs_zip/VFSFileZip.cpp

177 lines
3.5 KiB
C++
Raw Normal View History

#include "VFSFileZip.h"
#include "VFSInternal.h"
#include "VFSTools.h"
#include "VFSDir.h"
2014-04-06 17:19:33 +00:00
#include <stdio.h>
#include "miniz.h"
VFS_NAMESPACE_START
ZipFile::ZipFile(const char *name, ZipArchiveRef *zref, unsigned int fileIdx)
2014-04-06 17:19:33 +00:00
: File(joinPath(zref->fullname(), name).c_str())
, _buf(NULL)
, _pos(0)
, _archiveHandle(zref)
, _bufSize(0)
2014-04-06 17:19:33 +00:00
, _fileIdx(fileIdx)
, _mode("rb") // binary mode by default
{
}
2014-04-06 17:19:33 +00:00
ZipFile::~ZipFile()
{
2014-04-06 17:19:33 +00:00
close();
}
2014-04-06 17:19:33 +00:00
bool ZipFile::open(const char *mode /* = NULL */)
{
_pos = 0;
2014-04-06 17:19:33 +00:00
if(!mode)
mode = "rb";
2017-01-12 22:52:59 +00:00
else if(strchr(mode, 'w') || strchr(mode, 'a'))
return false; // writing not yet supported
2014-04-06 17:19:33 +00:00
if(_mode != mode)
{
2014-04-06 17:19:33 +00:00
delete [] _buf;
_buf = NULL;
_mode = mode;
}
return true; // does not have to be opened
}
2014-04-06 17:19:33 +00:00
bool ZipFile::isopen() const
{
return true; // is always open
}
2014-04-06 17:19:33 +00:00
bool ZipFile::iseof() const
{
return _pos >= _bufSize;
}
2014-04-06 17:19:33 +00:00
void ZipFile::close()
{
2014-04-06 17:19:33 +00:00
//flush(); // TODO: write to zip file on close
2017-01-12 22:52:59 +00:00
delete [] _buf;
2014-04-06 17:19:33 +00:00
_buf = NULL;
_bufSize = 0;
}
2014-04-06 17:19:33 +00:00
bool ZipFile::seek(vfspos pos, int whence)
{
2014-04-06 17:19:33 +00:00
const vfspos end = 0xFFFFFFFF;
switch(whence)
{
case SEEK_SET:
if(pos >= end) // zip files have uint32 range only
return false;
_pos = pos;
break;
case SEEK_CUR:
if(_pos + pos >= end)
return false;
_pos += pos;
break;
case SEEK_END:
if(pos >= _bufSize)
2014-04-06 17:19:33 +00:00
return false;
_pos = _bufSize - pos;
2014-04-06 17:19:33 +00:00
break;
default:
return false;
}
return true;
}
2014-04-06 17:19:33 +00:00
bool ZipFile::flush()
{
// FIXME: use this to actually write to zip file?
return false;
}
2014-04-06 17:19:33 +00:00
vfspos ZipFile::getpos() const
{
return _pos;
}
2014-04-07 02:16:15 +00:00
size_t ZipFile::read(void *dst, size_t bytes)
{
2014-04-06 17:19:33 +00:00
if(!_buf && !unpack())
return 0;
char *startptr = _buf + _pos;
char *endptr = _buf + size();
2014-04-07 02:16:15 +00:00
bytes = std::min<size_t>(endptr - startptr, bytes); // limit in case reading over buffer size
memcpy(dst, startptr, bytes); // binary copy
_pos += bytes;
return bytes;
}
2014-04-07 02:16:15 +00:00
size_t ZipFile::write(const void *src, size_t bytes)
{
2014-04-06 17:19:33 +00:00
// TODO: implement actually writing to the underlying Zip file.
2014-04-15 13:04:33 +00:00
//printf("NYI: ZipFile::write()");
2014-04-06 17:19:33 +00:00
return 0;
}
#define MZ ((mz_zip_archive*)_archiveHandle->mz)
2014-04-06 17:19:33 +00:00
vfspos ZipFile::size()
{
if(_buf && _bufSize)
return _bufSize;
if(!_archiveHandle->openRead())
return npos;
// FIXME: this is not 100% safe. The file index may change if the zip file is changed externally while closed
mz_zip_archive_file_stat zstat;
if(!mz_zip_reader_file_stat(MZ, _fileIdx, &zstat))
return npos;
return (vfspos)zstat.m_uncomp_size;
}
2014-04-06 17:19:33 +00:00
bool ZipFile::unpack()
{
close(); // delete the buffer
const vfspos sz = size(); // will reopen the file
2017-01-12 22:52:59 +00:00
if(sz == npos)
return false;
2017-01-12 22:52:59 +00:00
_buf = new char[size_t(sz) + 1];
if(!_buf)
2014-04-06 17:19:33 +00:00
return false;
2017-01-12 22:52:59 +00:00
if(!mz_zip_reader_extract_to_mem(MZ, _fileIdx, _buf, (size_t)sz, 0))
2014-04-06 17:19:33 +00:00
{
delete [] _buf;
_buf = NULL;
return false; // this should not happen
}
_bufSize = sz;
// In case of text data, make sure the buffer is always terminated with '\0'.
// Won't hurt for binary data, so just do it in all cases.
2014-04-06 17:19:33 +00:00
_buf[sz] = 0;
if(_mode.find("b") == std::string::npos) // text mode?
{
_bufSize = (vfspos)strnNLcpy(_buf, _buf);
}
2014-04-06 17:19:33 +00:00
return true;
}
VFS_NAMESPACE_END