From 3cf47056f239f1096590c27c1d401904a54f84fc Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 16 May 2017 17:50:47 +0300 Subject: Add subdir support in VirtFs::loadFile. --- src/fs/virtfs/fsdir.cpp | 4 +-- src/fs/virtfs/fsdir.h | 2 +- src/fs/virtfs/fsfuncs.h | 2 +- src/fs/virtfs/fszip.cpp | 5 ++- src/fs/virtfs/fszip.h | 2 +- src/fs/virtfs/virtfs1_unittest.cc | 66 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 75 insertions(+), 6 deletions(-) (limited to 'src/fs/virtfs') diff --git a/src/fs/virtfs/fsdir.cpp b/src/fs/virtfs/fsdir.cpp index 2e69dedb1..bafe3d2e3 100644 --- a/src/fs/virtfs/fsdir.cpp +++ b/src/fs/virtfs/fsdir.cpp @@ -458,11 +458,11 @@ namespace FsDir } const char *loadFile(FsEntry *restrict const entry, - const std::string &restrict filename, + std::string filename, int &restrict fileSize) { const DirEntry *const dirEntry = static_cast(entry); - const std::string path = entry->root + filename; + const std::string path = entry->root + entry->subDir + filename; if (Files::existsLocal(path) == false) return nullptr; FILEHTYPE fd = FILEOPEN(path.c_str(), diff --git a/src/fs/virtfs/fsdir.h b/src/fs/virtfs/fsdir.h index 704d7e916..ada58943e 100644 --- a/src/fs/virtfs/fsdir.h +++ b/src/fs/virtfs/fsdir.h @@ -95,7 +95,7 @@ namespace FsDir const uint64_t pos); int eof(File *restrict const file); const char *loadFile(FsEntry *restrict const entry, - const std::string &restrict fileName, + std::string fileName, int &restrict fileSize); } // namespace FsDir diff --git a/src/fs/virtfs/fsfuncs.h b/src/fs/virtfs/fsfuncs.h index 6c5428b49..989ee558e 100644 --- a/src/fs/virtfs/fsfuncs.h +++ b/src/fs/virtfs/fsfuncs.h @@ -111,7 +111,7 @@ struct FsFuncs final const std::string &filename); int (*eof) (File *restrict const file); const char *(*loadFile) (FsEntry *restrict const entry, - const std::string &restrict fileName, + std::string fileName, int &restrict fileSize); RWOPSINT (*rwops_seek) (SDL_RWops *const rw, diff --git a/src/fs/virtfs/fszip.cpp b/src/fs/virtfs/fszip.cpp index f2f16bcbb..91d323cc1 100644 --- a/src/fs/virtfs/fszip.cpp +++ b/src/fs/virtfs/fszip.cpp @@ -658,10 +658,13 @@ namespace FsZip } const char *loadFile(FsEntry *restrict const entry, - const std::string &restrict filename, + std::string filename, int &restrict fileSize) { ZipEntry *const zipEntry = static_cast(entry); + const std::string subDir = zipEntry->subDir; + if (subDir != dirSeparator) + filename = pathJoin(subDir, filename); FOR_EACH (std::vector::const_iterator, it2, zipEntry->mHeaders) diff --git a/src/fs/virtfs/fszip.h b/src/fs/virtfs/fszip.h index fbd6dba4d..68466a25c 100644 --- a/src/fs/virtfs/fszip.h +++ b/src/fs/virtfs/fszip.h @@ -84,7 +84,7 @@ namespace FsZip const uint64_t pos); int eof(File *restrict const file); const char *loadFile(FsEntry *restrict const entry, - const std::string &restrict fileName, + std::string fileName, int &restrict fileSize); } // namespace FsZip diff --git a/src/fs/virtfs/virtfs1_unittest.cc b/src/fs/virtfs/virtfs1_unittest.cc index 78787f9d0..4c046b621 100644 --- a/src/fs/virtfs/virtfs1_unittest.cc +++ b/src/fs/virtfs/virtfs1_unittest.cc @@ -2649,6 +2649,72 @@ TEST_CASE("VirtFs1 loadFile2") delete2(logger); } +TEST_CASE("VirtFs1 loadFile3") +{ + VirtFs::init("."); + int fileSize = 0; + logger = new Logger(); + std::string name("data/test/test.zip"); + std::string prefix; + if (Files::existsLocal(name) == false) + prefix = "../" + prefix; + + VirtFs::mountDir2(prefix + "data", + "test", + Append_false); + + const char *const buffer = VirtFs::loadFile("test.txt", fileSize); + REQUIRE(static_cast(buffer) != nullptr); + REQUIRE(fileSize == 23); + REQUIRE(strncmp(buffer, "test line 1\ntest line 2", 23) == 0); + delete [] buffer; + + VirtFs::unmountDir2(prefix + "data", + "test"); + VirtFs::deinit(); + delete2(logger); +} + +TEST_CASE("VirtFs1 loadFile4") +{ + VirtFs::init("."); + int fileSize = 0; + logger = new Logger(); + std::string name("data/test/test.zip"); + std::string prefix; + if (Files::existsLocal(name) == false) + prefix = "../" + prefix; + + VirtFs::mountZip2(prefix + "data/test/test2.zip", + "dir2", + Append_false); + + SECTION("test 1") + { + const char *restrict buffer = VirtFs::loadFile("test.txt", + fileSize); + REQUIRE(static_cast(buffer) != nullptr); + REQUIRE(fileSize == 23); + REQUIRE(strncmp(buffer, "test line 1\ntest line 2", 23) == 0); + delete [] buffer; + } + + SECTION("test 2") + { + const char *restrict buffer = VirtFs::loadFile("test.txt", + fileSize); + REQUIRE(static_cast(buffer) != nullptr); + REQUIRE(fileSize == 23); + REQUIRE(strncmp(buffer, "test line 1\ntest line 2", 23) == 0); + delete [] buffer; + } + + VirtFs::unmountZip2(prefix + "data/test/test2.zip", + "dir2"); + VirtFs::deinit(); + delete2(logger); +} + TEST_CASE("VirtFs1 rwops_read1") { VirtFs::init("."); -- cgit v1.2.3-70-g09d2