mirror of https://github.com/OpenMW/openmw.git
Added PhysFS VFS server +tests
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
|
@ -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…
Reference in New Issue