From d763b9dbb62fefe0f0ab4c641f051762fb7b1ad9 Mon Sep 17 00:00:00 2001 From: Nicolay Korslund Date: Sat, 26 Dec 2009 13:35:34 +0100 Subject: [PATCH] Added PhysFS VFS server +tests --- stream/imp_server/phys_stream.h | 34 +++++++++++++++ vfs/imp_server/physfs_vfs.h | 71 ++++++++++++++++++++++++++++++++ vfs/tests/Makefile | 6 ++- vfs/tests/ogre_server_test.cpp | 31 ++------------ vfs/tests/physfs_server_test.cpp | 21 ++++++++++ vfs/tests/server_common.cpp | 33 +++++++++++++++ 6 files changed, 167 insertions(+), 29 deletions(-) create mode 100644 stream/imp_server/phys_stream.h create mode 100644 vfs/imp_server/physfs_vfs.h create mode 100644 vfs/tests/physfs_server_test.cpp create mode 100644 vfs/tests/server_common.cpp diff --git a/stream/imp_server/phys_stream.h b/stream/imp_server/phys_stream.h new file mode 100644 index 000000000..956619436 --- /dev/null +++ b/stream/imp_server/phys_stream.h @@ -0,0 +1,34 @@ +#ifndef MANGLE_STREAM_OGRESERVER_H +#define MANGLE_STREAM_OGRESERVER_H + +#include + +namespace Mangle { +namespace Stream { + +/// A Stream wrapping a PHYSFS_file stream from the PhysFS library. +class PhysFile : public InputStream +{ + PHYSFS_file *file; + + public: + PhysFile(PHYSFS_file *inp) : file(inp) + { + isSeekable = true; + hasPosition = true; + hasSize = true; + } + + ~PhysFile() { PHYSFS_close(file); } + + size_t read(void *buf, size_t count) + { return PHYSFS_read(file, buf, 1, count); } + + void seek(size_t pos) { PHYSFS_seek(file, pos); } + size_t tell() const { return PHYSFS_tell(file); } + size_t size() const { return PHYSFS_fileLength(file); } + bool eof() const { return PHYSFS_eof(file); } +}; + +}} // namespaces +#endif diff --git a/vfs/imp_server/physfs_vfs.h b/vfs/imp_server/physfs_vfs.h new file mode 100644 index 000000000..ace3d84c9 --- /dev/null +++ b/vfs/imp_server/physfs_vfs.h @@ -0,0 +1,71 @@ +#ifndef MANGLE_VFS_PHYSFS_SERVER_H +#define MANGLE_VFS_PHYSFS_SERVER_H + +#include "../vfs.h" +#include "../../stream/imp_server/phys_stream.h" + +#include +#include + +namespace Mangle { +namespace VFS { + +/** @brief An interface into the PhysFS virtual file system library + + You have to set up PhysFS on your own before using this class. + */ +class PhysVFS : public VFS +{ + public: + PhysVFS() + { + hasList = true; + hasFind = false; + isCaseSensitive = true; + } + + /// Open a new data stream. Deleting the object should be enough to + /// close it. + virtual Stream::InputStream *open(const std::string &name) + { return new Stream::PhysFile(PHYSFS_openRead(name.c_str())); } + + /// Check for the existence of a file + virtual bool isFile(const std::string &name) const + { return PHYSFS_exists(name.c_str()); } + + /// Checks for a directory + virtual bool isDir(const std::string &name) const + { return PHYSFS_isDirectory(name.c_str()); } + + /// This doesn't work + virtual FileInfo stat(const std::string &name) const + { assert(0); return FileInfo(); } + + virtual FileInfoList list(const std::string& dir = "", + bool recurse=true, + bool dirs=false) const + { + char **files = PHYSFS_enumerateFiles(dir.c_str()); + FileInfoList lst; + + // Add all teh files + int i = 0; + while(files[i] != NULL) + { + FileInfo fi; + fi.name = files[i]; + fi.isDir = false; + + lst.push_back(fi); + } + return lst; + } + + virtual FileInfoList find(const std::string& pattern, + bool recursive=true, + bool dirs=false) const + { assert(0); } +}; + +}} // namespaces +#endif diff --git a/vfs/tests/Makefile b/vfs/tests/Makefile index 77e02f927..2cf722550 100644 --- a/vfs/tests/Makefile +++ b/vfs/tests/Makefile @@ -1,9 +1,10 @@ GCC=g++ -I../ -all: dummy_test ogre_client_test ogre_resource_test ogre_server_test +all: dummy_test ogre_client_test ogre_resource_test ogre_server_test physfs_server_test I_OGRE=$(shell pkg-config --cflags OGRE) L_OGRE=$(shell pkg-config --libs OGRE) +L_PHYSFS=-lphysfs ogre_client_test: ogre_client_test.cpp dummy_vfs.cpp ../vfs.h ../imp_client/wrapper.h ../imp_client/ogre_archive.h ../imp_client/ogre_archive.cpp $(GCC) $< ../imp_client/ogre_archive.cpp -o $@ $(I_OGRE) $(L_OGRE) @@ -14,6 +15,9 @@ ogre_resource_test: ogre_resource_test.cpp ogre_server_test: ogre_server_test.cpp ../vfs.h ../imp_server/ogre_vfs.h ../imp_server/ogre_vfs.cpp $(GCC) $< -o $@ $(I_OGRE) $(L_OGRE) ../imp_server/ogre_vfs.cpp +physfs_server_test: physfs_server_test.cpp ../vfs.h ../imp_server/physfs_vfs.h + $(GCC) $< -o $@ $(L_PHYSFS) + dummy_test: dummy_test.cpp dummy_vfs.cpp ../vfs.h $(GCC) $< -o $@ diff --git a/vfs/tests/ogre_server_test.cpp b/vfs/tests/ogre_server_test.cpp index 4193bda7f..3e0fcbef4 100644 --- a/vfs/tests/ogre_server_test.cpp +++ b/vfs/tests/ogre_server_test.cpp @@ -1,11 +1,8 @@ #include "../imp_server/ogre_vfs.h" -#include #include -using namespace std; -using namespace Mangle::VFS; -using namespace Mangle::Stream; +#include "server_common.cpp" Ogre::Root *root; @@ -26,27 +23,6 @@ void setupOgre() root->addResourceLocation("./", "FileSystem", "General"); } -void find(VFS &vfs, const std::string &file) -{ - cout << "\nFile: " << file << endl; - - if(!vfs.isFile(file)) - { - cout << "File doesn't exist\n"; - return; - } - - InputStream *data = vfs.open(file); - - cout << "Size: " << data->size() << endl; - - char buf[13]; - buf[12] = 0; - data->read(buf, 12); - - cout << "First 12 bytes: " << buf << "\n"; -} - int main() { // Set up the engine @@ -55,9 +31,8 @@ int main() // This is our entry point into the resource file system OgreVFS vfs("General"); - find(vfs, "Makefile"); // From the file system - find(vfs, "testfile.txt"); // From the zip - find(vfs, "blah_bleh"); // Doesn't exist + // Run the test + testAll(vfs); return 0; } diff --git a/vfs/tests/physfs_server_test.cpp b/vfs/tests/physfs_server_test.cpp new file mode 100644 index 000000000..282566dd1 --- /dev/null +++ b/vfs/tests/physfs_server_test.cpp @@ -0,0 +1,21 @@ +#include "../imp_server/physfs_vfs.h" + +#include "server_common.cpp" + +#include + +int main() +{ + // Set up the library and paths + PHYSFS_init("blah"); + PHYSFS_addToSearchPath("test.zip", 1); + PHYSFS_addToSearchPath("./", 1); + + // Create our interface + PhysVFS vfs; + + // Run the test + testAll(vfs); + + return 0; +} diff --git a/vfs/tests/server_common.cpp b/vfs/tests/server_common.cpp new file mode 100644 index 000000000..ddb85b0c3 --- /dev/null +++ b/vfs/tests/server_common.cpp @@ -0,0 +1,33 @@ +#include + +using namespace Mangle::VFS; +using namespace Mangle::Stream; +using namespace std; + +void find(VFS &vfs, const std::string &file) +{ + cout << "\nFile: " << file << endl; + + if(!vfs.isFile(file)) + { + cout << "File doesn't exist\n"; + return; + } + + InputStream *data = vfs.open(file); + + cout << "Size: " << data->size() << endl; + + char buf[13]; + buf[12] = 0; + data->read(buf, 12); + + cout << "First 12 bytes: " << buf << "\n"; +} + +void testAll(VFS &vfs) +{ + find(vfs, "Makefile"); // From the file system + find(vfs, "testfile.txt"); // From the zip + find(vfs, "blah_bleh"); // Doesn't exist +}