Added PhysFS VFS server +tests

pull/21/head
Nicolay Korslund 15 years ago
parent 262cb9255c
commit d763b9dbb6

@ -0,0 +1,34 @@
#ifndef MANGLE_STREAM_OGRESERVER_H
#define MANGLE_STREAM_OGRESERVER_H
#include <physfs.h>
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

@ -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 <physfs.h>
#include <assert.h>
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

@ -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 $@

@ -1,11 +1,8 @@
#include "../imp_server/ogre_vfs.h"
#include <iostream>
#include <Ogre.h>
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;
}

@ -0,0 +1,21 @@
#include "../imp_server/physfs_vfs.h"
#include "server_common.cpp"
#include <physfs.h>
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;
}

@ -0,0 +1,33 @@
#include <iostream>
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
}
Loading…
Cancel
Save