From 9e332c40671f8db0abab3f889f558d7450d03fa3 Mon Sep 17 00:00:00 2001
From: Nicolay Korslund <korslund@gmail.com>
Date: Fri, 1 Jan 2010 17:06:41 +0100
Subject: [PATCH] Fixed and tested all VFS tests

---
 vfs/clients/ogre_archive.cpp     |  4 +--
 vfs/clients/ogre_archive.h       |  5 ++--
 vfs/servers/ogre_vfs.cpp         |  9 +++---
 vfs/servers/physfs_vfs.h         |  2 +-
 vfs/tests/Makefile               |  2 +-
 vfs/tests/dummy_test.cpp         |  8 +++--
 vfs/tests/dummy_vfs.cpp          | 50 +++++++++++++++++---------------
 vfs/tests/ogre_client_test.cpp   |  2 +-
 vfs/tests/ogre_resource_test.cpp |  2 +-
 vfs/tests/server_common.cpp      |  2 +-
 10 files changed, 46 insertions(+), 40 deletions(-)

diff --git a/vfs/clients/ogre_archive.cpp b/vfs/clients/ogre_archive.cpp
index 0def218db..e9612d7d9 100644
--- a/vfs/clients/ogre_archive.cpp
+++ b/vfs/clients/ogre_archive.cpp
@@ -7,8 +7,8 @@ using namespace Mangle::Stream;
 
 Ogre::DataStreamPtr MangleArchive::open(const Ogre::String& filename) const
 {
-  return Ogre::DataStreamPtr(new MangleDataStream
-                             (filename, vfs->open(filename), true));
+  return Ogre::DataStreamPtr(new Mangle2OgreStream
+                             (filename, vfs->open(filename)));
 }
 
 static void fill(Ogre::FileInfoList &out, FileInfoList &in)
diff --git a/vfs/clients/ogre_archive.h b/vfs/clients/ogre_archive.h
index 84f23eb8a..22964dc35 100644
--- a/vfs/clients/ogre_archive.h
+++ b/vfs/clients/ogre_archive.h
@@ -26,7 +26,8 @@ class MangleArchive : public Ogre::Archive
 
   bool isCaseSensitive() const { return vfs->isCaseSensitive; }
 
-  // These do nothing. You have to load / unload the archive manually.
+  // These do nothing. You have to load / unload the archive in the
+  // constructor/destructor.
   void load() {}
   void unload() {}
 
@@ -34,7 +35,7 @@ class MangleArchive : public Ogre::Archive
     { return vfs->isFile(filename); }
 
   time_t getModifiedTime(const Ogre::String& filename)
-    { return vfs->stat(filename).time; }
+    { return vfs->stat(filename)->time; }
 
   Ogre::DataStreamPtr open(const Ogre::String& filename) const;
 
diff --git a/vfs/servers/ogre_vfs.cpp b/vfs/servers/ogre_vfs.cpp
index 0fc051a8a..ab496a192 100644
--- a/vfs/servers/ogre_vfs.cpp
+++ b/vfs/servers/ogre_vfs.cpp
@@ -2,6 +2,7 @@
 #include "../../stream/servers/ogre_datastream.h"
 
 using namespace Mangle::VFS;
+using namespace Mangle::Stream;
 
 OgreVFS::OgreVFS(const std::string &_group)
   : group(_group)
@@ -18,10 +19,10 @@ OgreVFS::OgreVFS(const std::string &_group)
     group = gm->getWorldResourceGroupName();
 }
 
-Mangle::Stream::StreamPtr OgreVFS::open(const std::string &name)
+StreamPtr OgreVFS::open(const std::string &name)
 {
   Ogre::DataStreamPtr data = gm->openResource(name, group);
-  return Strea::StreamPtr(new Stream::OgreStream(data));
+  return StreamPtr(new OgreStream(data));
 }
 
 static void fill(FileInfoList &out, Ogre::FileInfoList &in, bool dirs)
@@ -39,7 +40,7 @@ static void fill(FileInfoList &out, Ogre::FileInfoList &in, bool dirs)
     }
 }
 
-FileInfoList OgreVFS::list(const std::string& dir,
+FileInfoListPtr OgreVFS::list(const std::string& dir,
                             bool recurse,
                             bool dirs) const
 {
@@ -49,7 +50,7 @@ FileInfoList OgreVFS::list(const std::string& dir,
   return res;
 }
 
-FileInfoList OgreVFS::find(const std::string& pattern,
+FileInfoListPtr OgreVFS::find(const std::string& pattern,
                             bool recursive,
                             bool dirs) const
 {
diff --git a/vfs/servers/physfs_vfs.h b/vfs/servers/physfs_vfs.h
index f85cc54b8..49acc398d 100644
--- a/vfs/servers/physfs_vfs.h
+++ b/vfs/servers/physfs_vfs.h
@@ -27,7 +27,7 @@ class PhysVFS : public VFS
   /// Open a new data stream. Deleting the object should be enough to
   /// close it.
   virtual Stream::StreamPtr open(const std::string &name)
-    { return new Stream::StreamPtr(Stream::PhysFile(PHYSFS_openRead(name.c_str()))); }
+    { return Stream::StreamPtr(new Stream::PhysFile(PHYSFS_openRead(name.c_str()))); }
 
   /// Check for the existence of a file
   virtual bool isFile(const std::string &name) const
diff --git a/vfs/tests/Makefile b/vfs/tests/Makefile
index 4595c82ea..c9963cfa1 100644
--- a/vfs/tests/Makefile
+++ b/vfs/tests/Makefile
@@ -6,7 +6,7 @@ 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 ../clients/wrapper.h ../clients/ogre_archive.h ../clients/ogre_archive.cpp
+ogre_client_test: ogre_client_test.cpp dummy_vfs.cpp ../vfs.h ../clients/ogre_archive.h ../clients/ogre_archive.cpp
 	$(GCC) $< ../clients/ogre_archive.cpp -o $@ $(I_OGRE) $(L_OGRE)
 
 ogre_resource_test: ogre_resource_test.cpp
diff --git a/vfs/tests/dummy_test.cpp b/vfs/tests/dummy_test.cpp
index b65f38335..8d7fc7dd6 100644
--- a/vfs/tests/dummy_test.cpp
+++ b/vfs/tests/dummy_test.cpp
@@ -5,7 +5,7 @@
 
 using namespace std;
 
-void print(FileInfo inf)
+void print(FileInfo &inf)
 {
   cout << "name: " << inf.name << endl;
   cout << "basename: " << inf.basename << endl;
@@ -13,12 +13,14 @@ void print(FileInfo inf)
   cout << "size: " << inf.size << endl;
   cout << "time: " << inf.time << endl;
 }
+void print(FileInfoPtr inf) { print(*inf); }
 
-void print(FileInfoList lst)
+void print(FileInfoList &lst)
 {
   for(int i=0; i<lst.size(); i++)
     print(lst[i]);
 }
+void print(FileInfoListPtr lst) { print(*lst); }
 
 int main()
 {
@@ -33,7 +35,7 @@ int main()
   cout << endl;
   print(vfs.stat("dir"));
 
-  Stream *inp = vfs.open("file1");
+  StreamPtr inp = vfs.open("file1");
   cout << "filesize: " << inp->size() << endl;
 
   return 0;
diff --git a/vfs/tests/dummy_vfs.cpp b/vfs/tests/dummy_vfs.cpp
index e7c77a753..7a39da932 100644
--- a/vfs/tests/dummy_vfs.cpp
+++ b/vfs/tests/dummy_vfs.cpp
@@ -3,9 +3,10 @@
 #include <assert.h>
 #include <string.h>
 
-#include "../../stream/tests/dummy_input.cpp"
+#include "../../stream/servers/memory_stream.h"
 
 using namespace Mangle::VFS;
+using namespace Mangle::Stream;
 
 class DummyVFS : public VFS
 {
@@ -13,14 +14,15 @@ public:
   DummyVFS()
   {
     hasFind = false;
+    hasList = true;
     isCaseSensitive = true;
   }
 
   // We only support opening 'file1' at the moment.
-  Mangle::Stream::Stream *open(const std::string &name)
+  StreamPtr open(const std::string &name)
   {
     assert(name == "file1");
-    return new DummyInput();
+    return StreamPtr(new MemoryStream("hello world", 11));
   }
 
   bool isFile(const std::string &name) const
@@ -35,31 +37,31 @@ public:
   }
 
   /// Get info about a single file
-  FileInfo stat(const std::string &name) const
+  FileInfoPtr stat(const std::string &name) const
   {
-    FileInfo fi;
-    fi.name = name;
-    fi.time = 0;
+    FileInfoPtr fi(new FileInfo);
+    fi->name = name;
+    fi->time = 0;
 
     if(isFile(name))
       {
         if(name == "dir/file2")
           {
-            fi.basename = "file2";
-            fi.size = 2;
+            fi->basename = "file2";
+            fi->size = 2;
           }
         else
           {
-            fi.basename = "file1";
-            fi.size = 1;
+            fi->basename = "file1";
+            fi->size = 1;
           }
-        fi.isDir = false;
+        fi->isDir = false;
       }
     else if(isDir(name))
       {
-        fi.basename = "dir";
-        fi.isDir = true;
-        fi.size = 0;
+        fi->basename = "dir";
+        fi->isDir = true;
+        fi->size = 0;
       }
     else assert(0);
 
@@ -69,13 +71,13 @@ public:
   /// List all entries in a given directory. A blank dir should be
   /// interpreted as a the root/current directory of the archive. If
   /// dirs is true, list directories instead of files.
-  virtual FileInfoList list(const std::string& dir = "",
-                            bool recurse=true,
-                            bool dirs=false) const
+  virtual FileInfoListPtr list(const std::string& dir = "",
+                               bool recurse=true,
+                               bool dirs=false) const
   {
     assert(dir == "");
 
-    FileInfoList fl;
+    FileInfoListPtr fl(new FileInfoList);
 
     FileInfo fi;
 
@@ -86,14 +88,14 @@ public:
         fi.isDir = false;
         fi.size = 1;
         fi.time = 0;
-        fl.push_back(fi);
+        fl->push_back(fi);
 
         if(recurse)
           {
             fi.name = "dir/file2";
             fi.basename = "file2";
             fi.size = 2;
-            fl.push_back(fi);
+            fl->push_back(fi);
           }
       }
     else
@@ -103,13 +105,13 @@ public:
         fi.isDir = true;
         fi.size = 0;
         fi.time = 0;
-        fl.push_back(fi);
+        fl->push_back(fi);
       }
     return fl;
   }
 
-  FileInfoList find(const std::string& pattern,
+  FileInfoListPtr find(const std::string& pattern,
                     bool recursive=true,
                     bool dirs=false) const
-  { assert(0); return FileInfoList(); }
+  { assert(0); }
 };
diff --git a/vfs/tests/ogre_client_test.cpp b/vfs/tests/ogre_client_test.cpp
index d38add4da..542ac9c7e 100644
--- a/vfs/tests/ogre_client_test.cpp
+++ b/vfs/tests/ogre_client_test.cpp
@@ -17,7 +17,7 @@ void print(StringVectorPtr lst)
 
 int main()
 {
-  VFS *vfs = new DummyVFS();
+  VFSPtr vfs(new DummyVFS());
   MangleArchive arc(vfs, "dummy");
 
   cout << "Case: " << arc.isCaseSensitive() << endl;
diff --git a/vfs/tests/ogre_resource_test.cpp b/vfs/tests/ogre_resource_test.cpp
index eadb1153f..8965adaa1 100644
--- a/vfs/tests/ogre_resource_test.cpp
+++ b/vfs/tests/ogre_resource_test.cpp
@@ -6,7 +6,7 @@
   This isn't really a test of our implementation, but a test of using
   the Ogre resource system to find files. If the Ogre interface
   changes and you have to change this test, you will have to change
-  the ogre_vfs.cpp implementation equivalently.
+  the servers/ogre_vfs.cpp implementation equivalently.
 
  */
 
diff --git a/vfs/tests/server_common.cpp b/vfs/tests/server_common.cpp
index ff16ac7f2..1834bc25a 100644
--- a/vfs/tests/server_common.cpp
+++ b/vfs/tests/server_common.cpp
@@ -14,7 +14,7 @@ void find(VFS &vfs, const std::string &file)
       return;
     }
 
-  Stream *data = vfs.open(file);
+  StreamPtr data = vfs.open(file);
 
   cout << "Size: " << data->size() << endl;