From c1fb7369bc8ce56abf5a8693dc56d741d675ae3b Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 5 May 2017 18:54:26 +0300 Subject: Rename virtfszip into fszip. --- src/fs/virtfs/fs.cpp | 12 +- src/fs/virtfs/fszip.cpp | 668 +++++++++++++++++++++++++++++++++++++++ src/fs/virtfs/fszip.h | 93 ++++++ src/fs/virtfs/virtfszip.cpp | 668 --------------------------------------- src/fs/virtfs/virtfszip.h | 93 ------ src/fs/virtfs/virtfsziprwops.cpp | 4 +- src/fs/virtfs/virtfsziprwops.h | 4 +- src/fs/virtfs/zip_unittest.cc | 18 +- 8 files changed, 780 insertions(+), 780 deletions(-) create mode 100644 src/fs/virtfs/fszip.cpp create mode 100644 src/fs/virtfs/fszip.h delete mode 100644 src/fs/virtfs/virtfszip.cpp delete mode 100644 src/fs/virtfs/virtfszip.h (limited to 'src/fs/virtfs') diff --git a/src/fs/virtfs/fs.cpp b/src/fs/virtfs/fs.cpp index 9c3228775..c4f1e7f42 100644 --- a/src/fs/virtfs/fs.cpp +++ b/src/fs/virtfs/fs.cpp @@ -27,7 +27,7 @@ #include "fs/virtfs/file.h" #include "fs/virtfs/fsdir.h" #include "fs/virtfs/fsfuncs.h" -#include "fs/virtfs/virtfszip.h" +#include "fs/virtfs/fszip.h" #include "fs/virtfs/virtlist.h" #include "fs/virtfs/virtzipentry.h" #include "fs/virtfs/zipreader.h" @@ -56,7 +56,7 @@ namespace VirtFs { updateDirSeparator(); FsDir::init(name); - VirtFsZip::init(); + FsZip::init(); } void updateDirSeparator() @@ -459,7 +459,7 @@ namespace VirtFs prepareFsPath(newDir); if (Files::existsLocal(newDir) == false) { - reportNonTests("VirtFsZip::addToSearchPath file not exists: %s", + reportNonTests("FsZip::addToSearchPath file not exists: %s", newDir.c_str()); return false; } @@ -471,12 +471,12 @@ namespace VirtFs } if (searchEntryByRootInternal(newDir) != nullptr) { - reportAlways("VirtFsZip::addToSearchPath already exists: %s", + reportAlways("FsZip::addToSearchPath already exists: %s", newDir.c_str()); return false; } VirtZipEntry *const entry = new VirtZipEntry(newDir, - VirtFsZip::getFuncs()); + FsZip::getFuncs()); if (ZipReader::readArchiveInfo(entry) == false) { delete entry; @@ -558,7 +558,7 @@ namespace VirtFs bool deinit() { FsDir::deinit(); - VirtFsZip::deinit(); + FsZip::deinit(); FOR_EACH (std::vector::iterator, it, mEntries) { FsEntry *const entry = *it; diff --git a/src/fs/virtfs/fszip.cpp b/src/fs/virtfs/fszip.cpp new file mode 100644 index 000000000..3781380ae --- /dev/null +++ b/src/fs/virtfs/fszip.cpp @@ -0,0 +1,668 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013-2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "fs/virtfs/fszip.h" + +#include "fs/virtfs/file.h" +#include "fs/virtfs/fsfuncs.h" +#include "fs/virtfs/virtfsziprwops.h" +#include "fs/virtfs/virtlist.h" +#include "fs/virtfs/virtzipentry.h" +#include "fs/virtfs/zipreader.h" +#include "fs/virtfs/ziplocalheader.h" + +#include "utils/checkutils.h" +#include "utils/stringutils.h" + +#include "debug.h" + +extern const char *dirSeparator; + +namespace +{ + VirtFs::FsFuncs funcs; +} // namespace + +namespace VirtFs +{ + +namespace FsZip +{ + FsFuncs *getFuncs() + { + return &funcs; + } + + void deinit() + { + } + + void init() + { + initFuncs(&funcs); + } + + void initFuncs(FsFuncs *restrict const ptr) + { + ptr->close = &FsZip::close; + ptr->read = &FsZip::read; + ptr->write = &FsZip::write; + ptr->fileLength = &FsZip::fileLength; + ptr->tell = &FsZip::tell; + ptr->seek = &FsZip::seek; + ptr->eof = &FsZip::eof; + ptr->exists = &FsZip::exists; + ptr->getRealDir = &FsZip::getRealDir; + ptr->enumerate = &FsZip::enumerate; + ptr->isDirectory = &FsZip::isDirectory; + ptr->openRead = &FsZip::openRead; + ptr->openWrite = &FsZip::openWrite; + ptr->openAppend = &FsZip::openAppend; + ptr->loadFile = &FsZip::loadFile; + ptr->getFiles = &FsZip::getFiles; + ptr->getFilesWithDir = &FsZip::getFilesWithDir; + ptr->getDirs = &FsZip::getDirs; + ptr->rwops_seek = &FsZip::rwops_seek; + ptr->rwops_read = &FsZip::rwops_read; + ptr->rwops_write = &FsZip::rwops_write; + ptr->rwops_close = &FsZip::rwops_close; +#ifdef USE_SDL2 + ptr->rwops_size = &FsZip::rwops_size; +#endif // USE_SDL2 + } + + bool getRealDir(FsEntry *restrict const entry, + const std::string &filename, + const std::string &dirName, + std::string &realDir) + { + VirtZipEntry *const zipEntry = static_cast(entry); + FOR_EACH (std::vector::const_iterator, + it2, + zipEntry->mHeaders) + { + if ((*it2)->fileName == filename) + { + realDir = entry->root; + return true; + } + } + FOR_EACH (std::vector::const_iterator, + it2, + zipEntry->mDirs) + { + if (*it2 == dirName) + { + realDir = entry->root; + return true; + } + } + return false; + } + + bool exists(FsEntry *restrict const entry, + const std::string &filename, + const std::string &dirName) + { + VirtZipEntry *const zipEntry = static_cast(entry); + FOR_EACH (std::vector::const_iterator, + it2, + zipEntry->mHeaders) + { + if ((*it2)->fileName == filename) + return true; + } + FOR_EACH (std::vector::const_iterator, + it2, + zipEntry->mDirs) + { + if (*it2 == dirName) + return true; + } + return false; + } + + void enumerate(FsEntry *restrict const entry, + const std::string &dirName, + StringVect &names) + { + VirtZipEntry *const zipEntry = static_cast(entry); + if (dirName == dirSeparator) + { + FOR_EACH (std::vector::const_iterator, + it2, + zipEntry->mHeaders) + { + ZipLocalHeader *const header = *it2; + std::string fileName = header->fileName; + // skip subdirs from enumeration + const size_t idx = fileName.find(dirSeparator); + if (idx != std::string::npos) + fileName.erase(idx); + bool found(false); + FOR_EACH (StringVectCIter, itn, names) + { + if (*itn == fileName) + { + found = true; + break; + } + } + if (found == false) + names.push_back(fileName); + } + } + else + { + FOR_EACH (std::vector::const_iterator, + it2, + zipEntry->mHeaders) + { + ZipLocalHeader *const header = *it2; + std::string fileName = header->fileName; + if (findCutFirst(fileName, dirName) == true) + { + // skip subdirs from enumeration + const size_t idx = fileName.find(dirSeparator); + if (idx != std::string::npos) + fileName.erase(idx); + bool found(false); + FOR_EACH (StringVectCIter, itn, names) + { + if (*itn == fileName) + { + found = true; + break; + } + } + if (found == false) + names.push_back(fileName); + } + } + } + } + + void getFiles(FsEntry *restrict const entry, + const std::string &dirName, + StringVect &names) + { + VirtZipEntry *const zipEntry = static_cast(entry); + if (dirName == dirSeparator) + { + FOR_EACH (std::vector::const_iterator, + it2, + zipEntry->mHeaders) + { + ZipLocalHeader *const header = *it2; + std::string fileName = header->fileName; + // skip subdirs from enumeration + const size_t idx = fileName.find(dirSeparator); + if (idx != std::string::npos) + fileName.erase(idx); + bool found(false); + FOR_EACH (StringVectCIter, itn, names) + { + if (*itn == fileName) + { + found = true; + break; + } + } + if (found == false) + { + std::string dirName2 = pathJoin(dirName, fileName); + if (findLast(dirName2, std::string(dirSeparator)) == false) + dirName2 += dirSeparator; + FOR_EACH (std::vector::const_iterator, + it, + zipEntry->mDirs) + { + if (*it == dirName2) + { + found = true; + break; + } + } + if (found == false) + names.push_back(fileName); + } + } + } + else + { + FOR_EACH (std::vector::const_iterator, + it2, + zipEntry->mHeaders) + { + ZipLocalHeader *const header = *it2; + std::string fileName = header->fileName; + if (findCutFirst(fileName, dirName) == true) + { + // skip subdirs from enumeration + const size_t idx = fileName.find(dirSeparator); + if (idx != std::string::npos) + fileName.erase(idx); + bool found(false); + FOR_EACH (StringVectCIter, itn, names) + { + if (*itn == fileName) + { + found = true; + break; + } + } + if (found == false) + { + std::string dirName2 = pathJoin(dirName, fileName); + if (findLast(dirName2, std::string(dirSeparator)) == + false) + { + dirName2 += dirSeparator; + } + FOR_EACH (std::vector::const_iterator, + it, + zipEntry->mDirs) + { + if (*it == dirName2) + { + found = true; + break; + } + } + if (found == false) + names.push_back(fileName); + } + } + } + } + } + + void getFilesWithDir(FsEntry *restrict const entry, + const std::string &dirName, + StringVect &names) + { + VirtZipEntry *const zipEntry = static_cast(entry); + if (dirName == dirSeparator) + { + FOR_EACH (std::vector::const_iterator, + it2, + zipEntry->mHeaders) + { + ZipLocalHeader *const header = *it2; + std::string fileName = header->fileName; + // skip subdirs from enumeration + const size_t idx = fileName.find(dirSeparator); + if (idx != std::string::npos) + fileName.erase(idx); + bool found(false); + FOR_EACH (StringVectCIter, itn, names) + { + if (*itn == fileName) + { + found = true; + break; + } + } + if (found == false) + { + std::string dirName2 = pathJoin(dirName, fileName); + if (findLast(dirName2, std::string(dirSeparator)) == false) + dirName2 += dirSeparator; + FOR_EACH (std::vector::const_iterator, + it, + zipEntry->mDirs) + { + if (*it == dirName2) + { + found = true; + break; + } + } + if (found == false) + names.push_back(pathJoin(dirName, fileName)); + } + } + } + else + { + FOR_EACH (std::vector::const_iterator, + it2, + zipEntry->mHeaders) + { + ZipLocalHeader *const header = *it2; + std::string fileName = header->fileName; + if (findCutFirst(fileName, dirName) == true) + { + // skip subdirs from enumeration + const size_t idx = fileName.find(dirSeparator); + if (idx != std::string::npos) + fileName.erase(idx); + bool found(false); + FOR_EACH (StringVectCIter, itn, names) + { + if (*itn == fileName) + { + found = true; + break; + } + } + if (found == false) + { + std::string dirName2 = pathJoin(dirName, fileName); + if (findLast(dirName2, std::string(dirSeparator)) == + false) + { + dirName2 += dirSeparator; + } + FOR_EACH (std::vector::const_iterator, + it, + zipEntry->mDirs) + { + if (*it == dirName2) + { + found = true; + break; + } + } + if (found == false) + names.push_back(pathJoin(dirName, fileName)); + } + } + } + } + } + + void getDirs(FsEntry *restrict const entry, + const std::string &dirName, + StringVect &names) + { + VirtZipEntry *const zipEntry = static_cast(entry); + if (dirName == dirSeparator) + { + FOR_EACH (std::vector::const_iterator, + it2, + zipEntry->mHeaders) + { + ZipLocalHeader *const header = *it2; + std::string fileName = header->fileName; + // skip subdirs from enumeration + const size_t idx = fileName.find(dirSeparator); + if (idx != std::string::npos) + fileName.erase(idx); + bool found(false); + FOR_EACH (StringVectCIter, itn, names) + { + if (*itn == fileName) + { + found = true; + break; + } + } + if (found == false) + { + std::string dirName2 = pathJoin(dirName, fileName); + if (findLast(dirName2, std::string(dirSeparator)) == false) + dirName2 += dirSeparator; + FOR_EACH (std::vector::const_iterator, + it, + zipEntry->mDirs) + { + if (*it == dirName2) + { + found = true; + break; + } + } + if (found == true) + names.push_back(fileName); + } + } + } + else + { + FOR_EACH (std::vector::const_iterator, + it2, + zipEntry->mHeaders) + { + ZipLocalHeader *const header = *it2; + std::string fileName = header->fileName; + if (findCutFirst(fileName, dirName) == true) + { + // skip subdirs from enumeration + const size_t idx = fileName.find(dirSeparator); + if (idx != std::string::npos) + fileName.erase(idx); + bool found(false); + FOR_EACH (StringVectCIter, itn, names) + { + if (*itn == fileName) + { + found = true; + break; + } + } + if (found == false) + { + std::string dirName2 = pathJoin(dirName, fileName); + if (findLast(dirName2, std::string(dirSeparator)) == + false) + { + dirName2 += dirSeparator; + } + FOR_EACH (std::vector::const_iterator, + it, + zipEntry->mDirs) + { + if (*it == dirName2) + { + found = true; + break; + } + } + if (found == true) + names.push_back(fileName); + } + } + } + } + } + + bool isDirectory(FsEntry *restrict const entry, + const std::string &dirName, + bool &isDirFlag) + { + VirtZipEntry *const zipEntry = static_cast(entry); + FOR_EACH (std::vector::const_iterator, + it2, + zipEntry->mDirs) + { + if (*it2 == dirName) + { + isDirFlag = true; + return true; + } + } + return false; + } + + void freeList(VirtList *restrict const handle) + { + delete handle; + } + + File *openRead(FsEntry *restrict const entry, + const std::string &filename) + { + VirtZipEntry *const zipEntry = static_cast(entry); + FOR_EACH (std::vector::const_iterator, + it2, + zipEntry->mHeaders) + { + const ZipLocalHeader *restrict const header = *it2; + if (header->fileName == filename) + { + const uint8_t *restrict const buf = + ZipReader::readFile(header); + if (buf == nullptr) + return nullptr; + File *restrict const file = new File(&funcs, + buf, + header->uncompressSize); + return file; + } + } + return nullptr; + } + + File *openWrite(FsEntry *restrict const entry A_UNUSED, + const std::string &filename A_UNUSED) + { + reportAlways("VirtFs::openWrite for zip not implemented."); + return nullptr; + } + + File *openAppend(FsEntry *restrict const entry A_UNUSED, + const std::string &filename A_UNUSED) + { + reportAlways("VirtFs::openAppend for zip not implemented."); + return nullptr; + } + + int close(File *restrict const file) + { + if (file == nullptr) + return 0; + delete file; + return 1; + } + + int64_t read(File *restrict const file, + void *restrict const buffer, + const uint32_t objSize, + const uint32_t objCount) + { + if (file == nullptr || + objSize == 0 || + objCount == 0) + { + return 0; + } + if (buffer == nullptr) + { + reportAlways("FsZip::read buffer is null"); + return 0; + } + const size_t pos = file->mPos; + const size_t sz = file->mSize; + // if outside of buffer, return + if (pos >= sz) + return 0; + // pointer to start for buffer ready to read + const uint8_t *restrict const memPtr = file->mBuf + pos; + // left buffer size from pos to end + const uint32_t memSize = CAST_U32(sz - pos); + // number of objects possible to read + uint32_t memCount = memSize / objSize; + if (memCount == 0) + return 0; + // limit number of possible objects to read to objCount + if (memCount > objCount) + memCount = objCount; + // number of bytes to read from buffer + const size_t memEnd = memCount * objSize; + memcpy(buffer, memPtr, memEnd); + file->mPos += memEnd; + return memCount; + } + + int64_t write(File *restrict const file A_UNUSED, + const void *restrict const buffer A_UNUSED, + const uint32_t objSize A_UNUSED, + const uint32_t objCount A_UNUSED) + { + return 0; + } + + int64_t fileLength(File *restrict const file) + { + if (file == nullptr) + return -1; + + return file->mSize; + } + + int64_t tell(File *restrict const file) + { + if (file == nullptr) + return -1; + + return file->mPos; + } + + int seek(File *restrict const file, + const uint64_t pos) + { + if (file == nullptr) + return 0; + + if (pos > file->mSize) + return 0; + file->mPos = pos; + return 1; + } + + int eof(File *restrict const file) + { + if (file == nullptr) + return -1; + + return file->mPos >= file->mSize; + } + + const char *loadFile(FsEntry *restrict const entry, + const std::string &restrict filename, + int &restrict fileSize) + { + VirtZipEntry *const zipEntry = static_cast(entry); + FOR_EACH (std::vector::const_iterator, + it2, + zipEntry->mHeaders) + { + const ZipLocalHeader *restrict const header = *it2; + if (header->fileName == filename) + { + const uint8_t *restrict const buf = + ZipReader::readFile(header); + if (buf == nullptr) + return nullptr; + + logger->log("Loaded %s/%s", + entry->root.c_str(), + filename.c_str()); + + fileSize = header->uncompressSize; + return reinterpret_cast(buf); + } + } + return nullptr; + } +} // namespace FsZip + +} // namespace VirtFs diff --git a/src/fs/virtfs/fszip.h b/src/fs/virtfs/fszip.h new file mode 100644 index 000000000..03981f0ec --- /dev/null +++ b/src/fs/virtfs/fszip.h @@ -0,0 +1,93 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013-2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef UTILS_VIRTFSZIP_H +#define UTILS_VIRTFSZIP_H + +#include "utils/stringvector.h" + +#include "localconsts.h" + +namespace VirtFs +{ + +struct File; +struct VirtList; +struct FsFuncs; +struct FsEntry; + +namespace FsZip +{ + FsFuncs *getFuncs(); + void init(); + void initFuncs(FsFuncs *restrict const ptr); + void deinit(); + bool exists(FsEntry *restrict const entry, + const std::string &filename, + const std::string &dirName); + void enumerate(FsEntry *restrict const entry, + const std::string &dirName, + StringVect &names); + void getFiles(FsEntry *restrict const entry, + const std::string &dirName, + StringVect &names); + void getFilesWithDir(FsEntry *restrict const entry, + const std::string &dirName, + StringVect &names); + void getDirs(FsEntry *restrict const entry, + const std::string &dirName, + StringVect &names); + bool isDirectory(FsEntry *restrict const entry, + const std::string &dirName, + bool &isDirFlag); + void freeList(VirtList *restrict const handle); + File *openRead(FsEntry *restrict const entry, + const std::string &filename); + File *openWrite(FsEntry *restrict const entry, + const std::string &filename); + File *openAppend(FsEntry *restrict const entry, + const std::string &filename); + File *openReadInternal(const std::string &filename); + bool getRealDir(FsEntry *restrict const entry, + const std::string &filename, + const std::string &dirName, + std::string &realDir); + int64_t read(File *restrict const handle, + void *restrict const buffer, + const uint32_t objSize, + const uint32_t objCount); + int64_t write(File *restrict const file, + const void *restrict const buffer, + const uint32_t objSize, + const uint32_t objCount); + int close(File *restrict const file); + int64_t fileLength(File *restrict const file); + int64_t tell(File *restrict const file); + int seek(File *restrict const file, + const uint64_t pos); + int eof(File *restrict const file); + const char *loadFile(FsEntry *restrict const entry, + const std::string &restrict fileName, + int &restrict fileSize); +} // namespace FsZip + +} // namespace VirtFs + +#endif // UTILS_VIRTFSZIP_H diff --git a/src/fs/virtfs/virtfszip.cpp b/src/fs/virtfs/virtfszip.cpp deleted file mode 100644 index 0f58dd5f5..000000000 --- a/src/fs/virtfs/virtfszip.cpp +++ /dev/null @@ -1,668 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2013-2017 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "fs/virtfs/virtfszip.h" - -#include "fs/virtfs/file.h" -#include "fs/virtfs/fsfuncs.h" -#include "fs/virtfs/virtfsziprwops.h" -#include "fs/virtfs/virtlist.h" -#include "fs/virtfs/virtzipentry.h" -#include "fs/virtfs/zipreader.h" -#include "fs/virtfs/ziplocalheader.h" - -#include "utils/checkutils.h" -#include "utils/stringutils.h" - -#include "debug.h" - -extern const char *dirSeparator; - -namespace -{ - VirtFs::FsFuncs funcs; -} // namespace - -namespace VirtFs -{ - -namespace VirtFsZip -{ - FsFuncs *getFuncs() - { - return &funcs; - } - - void deinit() - { - } - - void init() - { - initFuncs(&funcs); - } - - void initFuncs(FsFuncs *restrict const ptr) - { - ptr->close = &VirtFsZip::close; - ptr->read = &VirtFsZip::read; - ptr->write = &VirtFsZip::write; - ptr->fileLength = &VirtFsZip::fileLength; - ptr->tell = &VirtFsZip::tell; - ptr->seek = &VirtFsZip::seek; - ptr->eof = &VirtFsZip::eof; - ptr->exists = &VirtFsZip::exists; - ptr->getRealDir = &VirtFsZip::getRealDir; - ptr->enumerate = &VirtFsZip::enumerate; - ptr->isDirectory = &VirtFsZip::isDirectory; - ptr->openRead = &VirtFsZip::openRead; - ptr->openWrite = &VirtFsZip::openWrite; - ptr->openAppend = &VirtFsZip::openAppend; - ptr->loadFile = &VirtFsZip::loadFile; - ptr->getFiles = &VirtFsZip::getFiles; - ptr->getFilesWithDir = &VirtFsZip::getFilesWithDir; - ptr->getDirs = &VirtFsZip::getDirs; - ptr->rwops_seek = &VirtFsZip::rwops_seek; - ptr->rwops_read = &VirtFsZip::rwops_read; - ptr->rwops_write = &VirtFsZip::rwops_write; - ptr->rwops_close = &VirtFsZip::rwops_close; -#ifdef USE_SDL2 - ptr->rwops_size = &VirtFsZip::rwops_size; -#endif // USE_SDL2 - } - - bool getRealDir(FsEntry *restrict const entry, - const std::string &filename, - const std::string &dirName, - std::string &realDir) - { - VirtZipEntry *const zipEntry = static_cast(entry); - FOR_EACH (std::vector::const_iterator, - it2, - zipEntry->mHeaders) - { - if ((*it2)->fileName == filename) - { - realDir = entry->root; - return true; - } - } - FOR_EACH (std::vector::const_iterator, - it2, - zipEntry->mDirs) - { - if (*it2 == dirName) - { - realDir = entry->root; - return true; - } - } - return false; - } - - bool exists(FsEntry *restrict const entry, - const std::string &filename, - const std::string &dirName) - { - VirtZipEntry *const zipEntry = static_cast(entry); - FOR_EACH (std::vector::const_iterator, - it2, - zipEntry->mHeaders) - { - if ((*it2)->fileName == filename) - return true; - } - FOR_EACH (std::vector::const_iterator, - it2, - zipEntry->mDirs) - { - if (*it2 == dirName) - return true; - } - return false; - } - - void enumerate(FsEntry *restrict const entry, - const std::string &dirName, - StringVect &names) - { - VirtZipEntry *const zipEntry = static_cast(entry); - if (dirName == dirSeparator) - { - FOR_EACH (std::vector::const_iterator, - it2, - zipEntry->mHeaders) - { - ZipLocalHeader *const header = *it2; - std::string fileName = header->fileName; - // skip subdirs from enumeration - const size_t idx = fileName.find(dirSeparator); - if (idx != std::string::npos) - fileName.erase(idx); - bool found(false); - FOR_EACH (StringVectCIter, itn, names) - { - if (*itn == fileName) - { - found = true; - break; - } - } - if (found == false) - names.push_back(fileName); - } - } - else - { - FOR_EACH (std::vector::const_iterator, - it2, - zipEntry->mHeaders) - { - ZipLocalHeader *const header = *it2; - std::string fileName = header->fileName; - if (findCutFirst(fileName, dirName) == true) - { - // skip subdirs from enumeration - const size_t idx = fileName.find(dirSeparator); - if (idx != std::string::npos) - fileName.erase(idx); - bool found(false); - FOR_EACH (StringVectCIter, itn, names) - { - if (*itn == fileName) - { - found = true; - break; - } - } - if (found == false) - names.push_back(fileName); - } - } - } - } - - void getFiles(FsEntry *restrict const entry, - const std::string &dirName, - StringVect &names) - { - VirtZipEntry *const zipEntry = static_cast(entry); - if (dirName == dirSeparator) - { - FOR_EACH (std::vector::const_iterator, - it2, - zipEntry->mHeaders) - { - ZipLocalHeader *const header = *it2; - std::string fileName = header->fileName; - // skip subdirs from enumeration - const size_t idx = fileName.find(dirSeparator); - if (idx != std::string::npos) - fileName.erase(idx); - bool found(false); - FOR_EACH (StringVectCIter, itn, names) - { - if (*itn == fileName) - { - found = true; - break; - } - } - if (found == false) - { - std::string dirName2 = pathJoin(dirName, fileName); - if (findLast(dirName2, std::string(dirSeparator)) == false) - dirName2 += dirSeparator; - FOR_EACH (std::vector::const_iterator, - it, - zipEntry->mDirs) - { - if (*it == dirName2) - { - found = true; - break; - } - } - if (found == false) - names.push_back(fileName); - } - } - } - else - { - FOR_EACH (std::vector::const_iterator, - it2, - zipEntry->mHeaders) - { - ZipLocalHeader *const header = *it2; - std::string fileName = header->fileName; - if (findCutFirst(fileName, dirName) == true) - { - // skip subdirs from enumeration - const size_t idx = fileName.find(dirSeparator); - if (idx != std::string::npos) - fileName.erase(idx); - bool found(false); - FOR_EACH (StringVectCIter, itn, names) - { - if (*itn == fileName) - { - found = true; - break; - } - } - if (found == false) - { - std::string dirName2 = pathJoin(dirName, fileName); - if (findLast(dirName2, std::string(dirSeparator)) == - false) - { - dirName2 += dirSeparator; - } - FOR_EACH (std::vector::const_iterator, - it, - zipEntry->mDirs) - { - if (*it == dirName2) - { - found = true; - break; - } - } - if (found == false) - names.push_back(fileName); - } - } - } - } - } - - void getFilesWithDir(FsEntry *restrict const entry, - const std::string &dirName, - StringVect &names) - { - VirtZipEntry *const zipEntry = static_cast(entry); - if (dirName == dirSeparator) - { - FOR_EACH (std::vector::const_iterator, - it2, - zipEntry->mHeaders) - { - ZipLocalHeader *const header = *it2; - std::string fileName = header->fileName; - // skip subdirs from enumeration - const size_t idx = fileName.find(dirSeparator); - if (idx != std::string::npos) - fileName.erase(idx); - bool found(false); - FOR_EACH (StringVectCIter, itn, names) - { - if (*itn == fileName) - { - found = true; - break; - } - } - if (found == false) - { - std::string dirName2 = pathJoin(dirName, fileName); - if (findLast(dirName2, std::string(dirSeparator)) == false) - dirName2 += dirSeparator; - FOR_EACH (std::vector::const_iterator, - it, - zipEntry->mDirs) - { - if (*it == dirName2) - { - found = true; - break; - } - } - if (found == false) - names.push_back(pathJoin(dirName, fileName)); - } - } - } - else - { - FOR_EACH (std::vector::const_iterator, - it2, - zipEntry->mHeaders) - { - ZipLocalHeader *const header = *it2; - std::string fileName = header->fileName; - if (findCutFirst(fileName, dirName) == true) - { - // skip subdirs from enumeration - const size_t idx = fileName.find(dirSeparator); - if (idx != std::string::npos) - fileName.erase(idx); - bool found(false); - FOR_EACH (StringVectCIter, itn, names) - { - if (*itn == fileName) - { - found = true; - break; - } - } - if (found == false) - { - std::string dirName2 = pathJoin(dirName, fileName); - if (findLast(dirName2, std::string(dirSeparator)) == - false) - { - dirName2 += dirSeparator; - } - FOR_EACH (std::vector::const_iterator, - it, - zipEntry->mDirs) - { - if (*it == dirName2) - { - found = true; - break; - } - } - if (found == false) - names.push_back(pathJoin(dirName, fileName)); - } - } - } - } - } - - void getDirs(FsEntry *restrict const entry, - const std::string &dirName, - StringVect &names) - { - VirtZipEntry *const zipEntry = static_cast(entry); - if (dirName == dirSeparator) - { - FOR_EACH (std::vector::const_iterator, - it2, - zipEntry->mHeaders) - { - ZipLocalHeader *const header = *it2; - std::string fileName = header->fileName; - // skip subdirs from enumeration - const size_t idx = fileName.find(dirSeparator); - if (idx != std::string::npos) - fileName.erase(idx); - bool found(false); - FOR_EACH (StringVectCIter, itn, names) - { - if (*itn == fileName) - { - found = true; - break; - } - } - if (found == false) - { - std::string dirName2 = pathJoin(dirName, fileName); - if (findLast(dirName2, std::string(dirSeparator)) == false) - dirName2 += dirSeparator; - FOR_EACH (std::vector::const_iterator, - it, - zipEntry->mDirs) - { - if (*it == dirName2) - { - found = true; - break; - } - } - if (found == true) - names.push_back(fileName); - } - } - } - else - { - FOR_EACH (std::vector::const_iterator, - it2, - zipEntry->mHeaders) - { - ZipLocalHeader *const header = *it2; - std::string fileName = header->fileName; - if (findCutFirst(fileName, dirName) == true) - { - // skip subdirs from enumeration - const size_t idx = fileName.find(dirSeparator); - if (idx != std::string::npos) - fileName.erase(idx); - bool found(false); - FOR_EACH (StringVectCIter, itn, names) - { - if (*itn == fileName) - { - found = true; - break; - } - } - if (found == false) - { - std::string dirName2 = pathJoin(dirName, fileName); - if (findLast(dirName2, std::string(dirSeparator)) == - false) - { - dirName2 += dirSeparator; - } - FOR_EACH (std::vector::const_iterator, - it, - zipEntry->mDirs) - { - if (*it == dirName2) - { - found = true; - break; - } - } - if (found == true) - names.push_back(fileName); - } - } - } - } - } - - bool isDirectory(FsEntry *restrict const entry, - const std::string &dirName, - bool &isDirFlag) - { - VirtZipEntry *const zipEntry = static_cast(entry); - FOR_EACH (std::vector::const_iterator, - it2, - zipEntry->mDirs) - { - if (*it2 == dirName) - { - isDirFlag = true; - return true; - } - } - return false; - } - - void freeList(VirtList *restrict const handle) - { - delete handle; - } - - File *openRead(FsEntry *restrict const entry, - const std::string &filename) - { - VirtZipEntry *const zipEntry = static_cast(entry); - FOR_EACH (std::vector::const_iterator, - it2, - zipEntry->mHeaders) - { - const ZipLocalHeader *restrict const header = *it2; - if (header->fileName == filename) - { - const uint8_t *restrict const buf = - ZipReader::readFile(header); - if (buf == nullptr) - return nullptr; - File *restrict const file = new File(&funcs, - buf, - header->uncompressSize); - return file; - } - } - return nullptr; - } - - File *openWrite(FsEntry *restrict const entry A_UNUSED, - const std::string &filename A_UNUSED) - { - reportAlways("VirtFs::openWrite for zip not implemented."); - return nullptr; - } - - File *openAppend(FsEntry *restrict const entry A_UNUSED, - const std::string &filename A_UNUSED) - { - reportAlways("VirtFs::openAppend for zip not implemented."); - return nullptr; - } - - int close(File *restrict const file) - { - if (file == nullptr) - return 0; - delete file; - return 1; - } - - int64_t read(File *restrict const file, - void *restrict const buffer, - const uint32_t objSize, - const uint32_t objCount) - { - if (file == nullptr || - objSize == 0 || - objCount == 0) - { - return 0; - } - if (buffer == nullptr) - { - reportAlways("VirtFsZip::read buffer is null"); - return 0; - } - const size_t pos = file->mPos; - const size_t sz = file->mSize; - // if outside of buffer, return - if (pos >= sz) - return 0; - // pointer to start for buffer ready to read - const uint8_t *restrict const memPtr = file->mBuf + pos; - // left buffer size from pos to end - const uint32_t memSize = CAST_U32(sz - pos); - // number of objects possible to read - uint32_t memCount = memSize / objSize; - if (memCount == 0) - return 0; - // limit number of possible objects to read to objCount - if (memCount > objCount) - memCount = objCount; - // number of bytes to read from buffer - const size_t memEnd = memCount * objSize; - memcpy(buffer, memPtr, memEnd); - file->mPos += memEnd; - return memCount; - } - - int64_t write(File *restrict const file A_UNUSED, - const void *restrict const buffer A_UNUSED, - const uint32_t objSize A_UNUSED, - const uint32_t objCount A_UNUSED) - { - return 0; - } - - int64_t fileLength(File *restrict const file) - { - if (file == nullptr) - return -1; - - return file->mSize; - } - - int64_t tell(File *restrict const file) - { - if (file == nullptr) - return -1; - - return file->mPos; - } - - int seek(File *restrict const file, - const uint64_t pos) - { - if (file == nullptr) - return 0; - - if (pos > file->mSize) - return 0; - file->mPos = pos; - return 1; - } - - int eof(File *restrict const file) - { - if (file == nullptr) - return -1; - - return file->mPos >= file->mSize; - } - - const char *loadFile(FsEntry *restrict const entry, - const std::string &restrict filename, - int &restrict fileSize) - { - VirtZipEntry *const zipEntry = static_cast(entry); - FOR_EACH (std::vector::const_iterator, - it2, - zipEntry->mHeaders) - { - const ZipLocalHeader *restrict const header = *it2; - if (header->fileName == filename) - { - const uint8_t *restrict const buf = - ZipReader::readFile(header); - if (buf == nullptr) - return nullptr; - - logger->log("Loaded %s/%s", - entry->root.c_str(), - filename.c_str()); - - fileSize = header->uncompressSize; - return reinterpret_cast(buf); - } - } - return nullptr; - } -} // namespace VirtFsZip - -} // namespace VirtFs diff --git a/src/fs/virtfs/virtfszip.h b/src/fs/virtfs/virtfszip.h deleted file mode 100644 index 60ecb3a8f..000000000 --- a/src/fs/virtfs/virtfszip.h +++ /dev/null @@ -1,93 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2013-2017 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef UTILS_VIRTFSZIP_H -#define UTILS_VIRTFSZIP_H - -#include "utils/stringvector.h" - -#include "localconsts.h" - -namespace VirtFs -{ - -struct File; -struct VirtList; -struct FsFuncs; -struct FsEntry; - -namespace VirtFsZip -{ - FsFuncs *getFuncs(); - void init(); - void initFuncs(FsFuncs *restrict const ptr); - void deinit(); - bool exists(FsEntry *restrict const entry, - const std::string &filename, - const std::string &dirName); - void enumerate(FsEntry *restrict const entry, - const std::string &dirName, - StringVect &names); - void getFiles(FsEntry *restrict const entry, - const std::string &dirName, - StringVect &names); - void getFilesWithDir(FsEntry *restrict const entry, - const std::string &dirName, - StringVect &names); - void getDirs(FsEntry *restrict const entry, - const std::string &dirName, - StringVect &names); - bool isDirectory(FsEntry *restrict const entry, - const std::string &dirName, - bool &isDirFlag); - void freeList(VirtList *restrict const handle); - File *openRead(FsEntry *restrict const entry, - const std::string &filename); - File *openWrite(FsEntry *restrict const entry, - const std::string &filename); - File *openAppend(FsEntry *restrict const entry, - const std::string &filename); - File *openReadInternal(const std::string &filename); - bool getRealDir(FsEntry *restrict const entry, - const std::string &filename, - const std::string &dirName, - std::string &realDir); - int64_t read(File *restrict const handle, - void *restrict const buffer, - const uint32_t objSize, - const uint32_t objCount); - int64_t write(File *restrict const file, - const void *restrict const buffer, - const uint32_t objSize, - const uint32_t objCount); - int close(File *restrict const file); - int64_t fileLength(File *restrict const file); - int64_t tell(File *restrict const file); - int seek(File *restrict const file, - const uint64_t pos); - int eof(File *restrict const file); - const char *loadFile(FsEntry *restrict const entry, - const std::string &restrict fileName, - int &restrict fileSize); -} // namespace VirtFsZip - -} // namespace VirtFs - -#endif // UTILS_VIRTFSZIP_H diff --git a/src/fs/virtfs/virtfsziprwops.cpp b/src/fs/virtfs/virtfsziprwops.cpp index d0c386bab..60199cc83 100644 --- a/src/fs/virtfs/virtfsziprwops.cpp +++ b/src/fs/virtfs/virtfsziprwops.cpp @@ -32,7 +32,7 @@ namespace VirtFs { -namespace VirtFsZip +namespace FsZip { RWOPSINT rwops_seek(SDL_RWops *const rw, const RWOPSINT offset, @@ -148,6 +148,6 @@ namespace VirtFsZip } #endif // USE_SDL2 -} // namespace VirtFsZip +} // namespace FsZip } // namespace VirtFs diff --git a/src/fs/virtfs/virtfsziprwops.h b/src/fs/virtfs/virtfsziprwops.h index b2fa65319..508b2213c 100644 --- a/src/fs/virtfs/virtfsziprwops.h +++ b/src/fs/virtfs/virtfsziprwops.h @@ -30,7 +30,7 @@ struct SDL_RWops; namespace VirtFs { -namespace VirtFsZip +namespace FsZip { RWOPSINT rwops_seek(SDL_RWops *const rw, const RWOPSINT offset, @@ -48,7 +48,7 @@ namespace VirtFsZip RWOPSINT rwops_size(SDL_RWops *const rw); #endif // USE_SDL2 -} // namespace VirtFsZip +} // namespace FsZip } // namespace VirtFs diff --git a/src/fs/virtfs/zip_unittest.cc b/src/fs/virtfs/zip_unittest.cc index 3853bee79..39aa17ae2 100644 --- a/src/fs/virtfs/zip_unittest.cc +++ b/src/fs/virtfs/zip_unittest.cc @@ -24,7 +24,7 @@ #include "fs/files.h" -#include "fs/virtfs/virtfszip.h" +#include "fs/virtfs/fszip.h" #include "fs/virtfs/virtzipentry.h" #include "fs/virtfs/zipreader.h" #include "fs/virtfs/ziplocalheader.h" @@ -49,7 +49,7 @@ TEST_CASE("Zip readArchiveInfo") name = prefix + "data/test/test.zip"; VirtFs::VirtZipEntry *const entry = new VirtFs::VirtZipEntry(name, - VirtFs::VirtFsZip::getFuncs()); + VirtFs::FsZip::getFuncs()); std::vector &headers = entry->mHeaders; REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry)); @@ -70,7 +70,7 @@ TEST_CASE("Zip readArchiveInfo") name = prefix + "data/test/test2.zip"; VirtFs::VirtZipEntry *const entry = new VirtFs::VirtZipEntry(name, - VirtFs::VirtFsZip::getFuncs()); + VirtFs::FsZip::getFuncs()); std::vector &headers = entry->mHeaders; REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry)); @@ -132,7 +132,7 @@ TEST_CASE("Zip readArchiveInfo") name = prefix + "data/test/test3.zip"; VirtFs::VirtZipEntry *const entry = new VirtFs::VirtZipEntry(name, - VirtFs::VirtFsZip::getFuncs()); + VirtFs::FsZip::getFuncs()); std::vector &headers = entry->mHeaders; REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry)); @@ -153,7 +153,7 @@ TEST_CASE("Zip readArchiveInfo") name = prefix + "data/test/test4.zip"; VirtFs::VirtZipEntry *const entry = new VirtFs::VirtZipEntry(name, - VirtFs::VirtFsZip::getFuncs()); + VirtFs::FsZip::getFuncs()); std::vector &headers = entry->mHeaders; REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry)); @@ -184,7 +184,7 @@ TEST_CASE("Zip readCompressedFile") name = prefix + "data/test/test2.zip"; VirtFs::VirtZipEntry *const entry = new VirtFs::VirtZipEntry(name, - VirtFs::VirtFsZip::getFuncs()); + VirtFs::FsZip::getFuncs()); std::vector &headers = entry->mHeaders; REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry)); @@ -218,7 +218,7 @@ TEST_CASE("Zip readFile") name = prefix + "data/test/test.zip"; VirtFs::VirtZipEntry *const entry = new VirtFs::VirtZipEntry(name, - VirtFs::VirtFsZip::getFuncs()); + VirtFs::FsZip::getFuncs()); std::vector &headers = entry->mHeaders; REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry)); @@ -242,7 +242,7 @@ TEST_CASE("Zip readFile") name = prefix + "data/test/test2.zip"; VirtFs::VirtZipEntry *const entry = new VirtFs::VirtZipEntry(name, - VirtFs::VirtFsZip::getFuncs()); + VirtFs::FsZip::getFuncs()); std::vector &headers = entry->mHeaders; REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry)); @@ -273,7 +273,7 @@ TEST_CASE("Zip readFile") name = prefix + "data/test/test3.zip"; VirtFs::VirtZipEntry *const entry = new VirtFs::VirtZipEntry(name, - VirtFs::VirtFsZip::getFuncs()); + VirtFs::FsZip::getFuncs()); std::vector &headers = entry->mHeaders; REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry)); -- cgit v1.2.3-60-g2f50