From d5228c6f5f9965e836f0d791444eb09c9ded4bb0 Mon Sep 17 00:00:00 2001
From: Nicolay Korslund <korslund@gmail.com>
Date: Mon, 7 Jun 2010 12:44:39 +0200
Subject: [PATCH] Cleaned up superflous functions in BSA archive. Started
 working on input system.

---
 bsa/bsa_archive.cpp                  | 75 ++-----------------------
 game/esm_store/store.hpp             |  4 ++
 game/render/cell.hpp                 |  1 +
 input/tests/.gitignore               |  2 +
 input/tests/Makefile                 |  9 +++
 input/tests/funcbind_test.cpp        | 84 ++++++++++++++++++++++++++++
 input/tests/output/funcbind_test.out | 19 +++++++
 input/tests/test.sh                  | 18 ++++++
 testall.sh                           |  2 +
 9 files changed, 145 insertions(+), 69 deletions(-)
 create mode 100644 input/tests/.gitignore
 create mode 100644 input/tests/Makefile
 create mode 100644 input/tests/funcbind_test.cpp
 create mode 100644 input/tests/output/funcbind_test.out
 create mode 100755 input/tests/test.sh

diff --git a/bsa/bsa_archive.cpp b/bsa/bsa_archive.cpp
index c12eb715a8..15768b5f0e 100644
--- a/bsa/bsa_archive.cpp
+++ b/bsa/bsa_archive.cpp
@@ -48,8 +48,7 @@ public:
   void load() {}
   void unload() {}
 
-  // Open a file in the archive.
-  DataStreamPtr open(const String& filename) const
+  DataStreamPtr open(const String& filename, bool recursive = true) const
   {
     // Get a non-const reference to arc. This is a hack and it's all
     // OGRE's fault. You should NOT expect an open() command not to
@@ -64,6 +63,10 @@ public:
     return DataStreamPtr(new Mangle2OgreStream(strm));
   }
 
+  // Check if the file exists.
+  bool exists(const String& filename) { return arc.exists(filename.c_str()); }
+  time_t getModifiedTime(const String&) { return 0; }
+
   // This is never called as far as I can see.
   StringVectorPtr list(bool recursive = true, bool dirs = false)
   {
@@ -71,6 +74,7 @@ public:
     StringVectorPtr ptr = StringVectorPtr(new StringVector());
     return ptr;
   }
+
   // Also never called.
   FileInfoListPtr listFileInfo(bool recursive = true, bool dirs = false)
   {
@@ -79,8 +83,6 @@ public:
     return ptr;
   }
 
-  time_t getModifiedTime(const String&) { return 0; }
-
   // After load() is called, find("*") is called once. It doesn't seem
   // to matter that we return an empty list, exists() gets called on
   // the correct files anyway.
@@ -104,11 +106,6 @@ public:
   FileInfoListPtr findFileInfo(const String& pattern, bool recursive = true,
                                bool dirs = false)
   {
-    /*
-    std::cout << "findFileInfo(" << pattern << ", " << recursive
-              << ", " << dirs << ")\n";
-    */
-
     FileInfoListPtr ptr = FileInfoListPtr(new FileInfoList());
 
     // Check if the file exists (only works for single files - wild
@@ -128,66 +125,6 @@ public:
 
     return ptr;
   }
-
-  // Check if the file exists.
-  bool exists(const String& filename) { return arc.exists(filename.c_str()); }
-  
-  // Fill out all types of virtual members from Ogre framework
-  StringVectorPtr list(bool recursive = true)
-  {
-    StringVectorPtr ptr = StringVectorPtr(new StringVector());
-    return ptr;
-  }
-  
-  StringVectorPtr find(const String& pattern, bool recursive = true)
-  {
-    StringVectorPtr ptr = StringVectorPtr(new StringVector());
-    return ptr;
-  }
-  
-  FileInfoListPtr listFileInfo(bool recursive = true)
-  {
-    FileInfoListPtr ptr = FileInfoListPtr(new FileInfoList());
-    return ptr;
-  }
-
-  FileInfoListPtr findFileInfo(const String& pattern, bool recursive = true)
-  {
-    FileInfoListPtr ptr = FileInfoListPtr(new FileInfoList());
-
-    // Check if the file exists (only works for single files - wild
-    // cards and recursive search isn't implemented.)
-    if(exists(pattern)) {
-        FileInfo fi;
-        fi.archive = this;
-        fi.filename = pattern;
-        // It apparently doesn't matter that we return bogus
-        // information
-        fi.path = "";
-        fi.compressedSize = fi.uncompressedSize = 0;
-
-        ptr->push_back(fi);
-    }
-
-    return ptr;
-  }
-
-  DataStreamPtr open(const String& filename, bool recursive = true) const
-  {
-    // Get a non-const reference to arc. This is a hack and it's all
-    // OGRE's fault. You should NOT expect an open() command not to
-    // have any side effects on the archive, and hence this function
-    // should not have been declared const in the first place.
-    BSAFile *narc = (BSAFile*)&arc;
-
-    // Open the file
-    StreamPtr strm = narc->getFile(filename.c_str());
-
-    // Wrap it into an Ogre::DataStream.
-    return DataStreamPtr(new Mangle2OgreStream(strm));
-  }
-
-  
 };
 
 // An archive factory for BSA archives
diff --git a/game/esm_store/store.hpp b/game/esm_store/store.hpp
index a09891e6ae..db97d4357a 100644
--- a/game/esm_store/store.hpp
+++ b/game/esm_store/store.hpp
@@ -12,6 +12,10 @@
   loading code. Cutting down dependencies also help on the general
   maintainability.
 
+  TODO FIXME: Cleanup. The RecLists store pointers to new'd objects,
+  but these are never deleted anywhere. Right now this data is
+  persistant through the application lifetime so it doesn't matter,
+  but fix it later.
  */
 
 #include "esm/records.hpp"
diff --git a/game/render/cell.hpp b/game/render/cell.hpp
index 391613cb48..7358fbedf0 100644
--- a/game/render/cell.hpp
+++ b/game/render/cell.hpp
@@ -11,6 +11,7 @@ namespace Render
      rendering objects from the given cell into the given rendering
      scene.
 
+     TODO FIXME: Doesn't do full cleanup yet.
    */
   class CellRender
   {
diff --git a/input/tests/.gitignore b/input/tests/.gitignore
new file mode 100644
index 0000000000..d736d3ec99
--- /dev/null
+++ b/input/tests/.gitignore
@@ -0,0 +1,2 @@
+*_test
+
diff --git a/input/tests/Makefile b/input/tests/Makefile
new file mode 100644
index 0000000000..5246db9b5a
--- /dev/null
+++ b/input/tests/Makefile
@@ -0,0 +1,9 @@
+GCC=g++
+
+all: funcbind_test
+
+funcbind_test: funcbind_test.cpp
+	$(GCC) $^ -o $@
+
+clean:
+	rm *_test
diff --git a/input/tests/funcbind_test.cpp b/input/tests/funcbind_test.cpp
new file mode 100644
index 0000000000..178da9d32b
--- /dev/null
+++ b/input/tests/funcbind_test.cpp
@@ -0,0 +1,84 @@
+#include <iostream>
+using namespace std;
+
+#include <string>
+#include <vector>
+#include <boost/function.hpp>
+#include <assert.h>
+
+typedef boost::function<void()> Action;
+
+struct FuncBind
+{
+  std::string name;
+  Action action;
+};
+
+class Binder
+{
+  std::vector<FuncBind> bindings;
+
+public:
+  /**
+     Initialize the struct by telling it how many functions you have
+     to bind. The largest index you intend to use should be number-1.
+  */
+  Binder(int number) : bindings(number) {}
+
+  void bind(int index, Action action, const std::string &name="")
+  {
+    assert(index >= 0 && index < bindings.size());
+    FuncBind &fb = bindings[index];
+    fb.action = action;
+    fb.name = name;
+  }
+
+  void unbind(int index)
+  {
+    assert(index >= 0 && index < bindings.size());
+    FuncBind &fb = bindings[index];
+    fb = FuncBind();
+  }
+
+  void call(int index)
+  {
+    assert(index >= 0 && index < bindings.size());
+    FuncBind &fb = bindings[index];
+    if(fb.action)
+      {
+        cout << "Calling '" << fb.name << "'\n";
+        fb.action();
+      }
+    else
+      cout << "No function\n";
+  }
+};
+
+void f1()
+{
+  cout << "In f1()\n";
+}
+
+void f2()
+{
+  cout << "In f2()\n";
+}
+
+int main()
+{
+  cout << "This will test the function binding system\n";
+
+  Binder bnd(5);
+
+  bnd.bind(0, &f1, "This is action 1");
+  bnd.bind(1, &f2);
+  bnd.bind(2, &f1, "This is action 3");
+
+  for(int i=0; i<5; i++)
+    {
+      cout << "\nCalling " << i << endl;
+      bnd.call(i);
+    }
+
+  return 0;
+}
diff --git a/input/tests/output/funcbind_test.out b/input/tests/output/funcbind_test.out
new file mode 100644
index 0000000000..21d5a7f44a
--- /dev/null
+++ b/input/tests/output/funcbind_test.out
@@ -0,0 +1,19 @@
+This will test the function binding system
+
+Calling 0
+Calling 'This is action 1'
+In f1()
+
+Calling 1
+Calling ''
+In f2()
+
+Calling 2
+Calling 'This is action 3'
+In f1()
+
+Calling 3
+No function
+
+Calling 4
+No function
diff --git a/input/tests/test.sh b/input/tests/test.sh
new file mode 100755
index 0000000000..2d07708adc
--- /dev/null
+++ b/input/tests/test.sh
@@ -0,0 +1,18 @@
+#!/bin/bash
+
+make || exit
+
+mkdir -p output
+
+PROGS=*_test
+
+for a in $PROGS; do
+    if [ -f "output/$a.out" ]; then
+        echo "Running $a:"
+        ./$a | diff output/$a.out -
+    else
+        echo "Creating $a.out"
+        ./$a > "output/$a.out"
+        git add "output/$a.out"
+    fi
+done
diff --git a/testall.sh b/testall.sh
index 51a6e029d1..9e9bd55b59 100755
--- a/testall.sh
+++ b/testall.sh
@@ -2,6 +2,7 @@
 
 function run()
 {
+  echo
   echo "$1/tests/:"
   cd "$1/tests/"
   ./test.sh
@@ -9,6 +10,7 @@ function run()
 }
 
 run tools
+run input
 run bsa
 run nif
 run nifogre