summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2017-05-16 17:50:47 +0300
committerAndrei Karas <akaras@inbox.ru>2017-05-16 17:50:47 +0300
commit3cf47056f239f1096590c27c1d401904a54f84fc (patch)
tree6e2dc17b22f9afb8079698b38d7dd12ed971b9ad
parent5f66478632d388bfae78cbbb938a42eac3956db2 (diff)
downloadplus-3cf47056f239f1096590c27c1d401904a54f84fc.tar.gz
plus-3cf47056f239f1096590c27c1d401904a54f84fc.tar.bz2
plus-3cf47056f239f1096590c27c1d401904a54f84fc.tar.xz
plus-3cf47056f239f1096590c27c1d401904a54f84fc.zip
Add subdir support in VirtFs::loadFile.
-rw-r--r--src/fs/virtfs/fsdir.cpp4
-rw-r--r--src/fs/virtfs/fsdir.h2
-rw-r--r--src/fs/virtfs/fsfuncs.h2
-rw-r--r--src/fs/virtfs/fszip.cpp5
-rw-r--r--src/fs/virtfs/fszip.h2
-rw-r--r--src/fs/virtfs/virtfs1_unittest.cc66
6 files changed, 75 insertions, 6 deletions
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<DirEntry*>(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<ZipEntry*>(entry);
+ const std::string subDir = zipEntry->subDir;
+ if (subDir != dirSeparator)
+ filename = pathJoin(subDir, filename);
FOR_EACH (std::vector<ZipLocalHeader*>::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<const void*>(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<const void*>(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<const void*>(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(".");