diff mbox

[Branch,~glmark2-dev/glmark2/trunk] Rev 120: Access resources in an abstract way using std::istream.

Message ID 20110810135821.1453.71644.launchpad@loganberry.canonical.com
State Accepted
Headers show

Commit Message

alexandros.frantzis@linaro.org Aug. 10, 2011, 1:58 p.m. UTC
Merge authors:
  Alexandros Frantzis (afrantzis)
------------------------------------------------------------
revno: 120 [merge]
committer: Alexandros Frantzis <alexandros.frantzis@linaro.org>
branch nick: trunk
timestamp: Wed 2011-08-10 16:56:41 +0300
message:
  Access resources in an abstract way using std::istream.
modified:
  src/model.cpp
  src/shader-source.cpp
  src/texture.cpp
  src/util.cpp
  src/util.h


--
lp:glmark2
https://code.launchpad.net/~glmark2-dev/glmark2/trunk

You are subscribed to branch lp:glmark2.
To unsubscribe from this branch go to https://code.launchpad.net/~glmark2-dev/glmark2/trunk/+edit-subscription
diff mbox

Patch

=== modified file 'src/model.cpp'
--- src/model.cpp	2011-07-19 13:42:53 +0000
+++ src/model.cpp	2011-08-10 13:52:04 +0000
@@ -25,9 +25,10 @@ 
 #include "vec.h"
 #include "log.h"
 #include "options.h"
+#include "util.h"
 
-#include <fstream>
 #include <sstream>
+#include <memory>
 
 #define read_or_fail(file, dst, size) do { \
     file.read(reinterpret_cast<char *>((dst)), (size)); \
@@ -164,7 +165,9 @@ 
 
     Log::debug("Loading model from 3ds file '%s'\n", filename.c_str());
 
-    std::ifstream input_file(filename.c_str());
+    const std::auto_ptr<std::istream> input_file_ptr(Util::get_resource(filename));
+    std::istream& input_file(*input_file_ptr);
+
     if (!input_file) {
         Log::error("Could not open 3ds file '%s'\n", filename.c_str());
         return false;

=== modified file 'src/shader-source.cpp'
--- src/shader-source.cpp	2011-07-28 08:44:31 +0000
+++ src/shader-source.cpp	2011-08-10 13:53:02 +0000
@@ -20,11 +20,13 @@ 
  *  Alexandros Frantzis (glmark2)
  */
 
-#include <fstream>
+#include <istream>
+#include <memory>
 
 #include "shader-source.h"
 #include "log.h"
 #include "vec.h"
+#include "util.h"
 
 /** 
  * Loads the contents of a file into a string.
@@ -35,8 +37,9 @@ 
 bool
 ShaderSource::load_file(const std::string& filename, std::string& str)
 {
-    using std::ifstream;
-    ifstream inputFile(filename.c_str());
+    std::auto_ptr<std::istream> is_ptr(Util::get_resource(filename));
+    std::istream& inputFile(*is_ptr);
+
     if (!inputFile)
     {
         Log::error("Failed to open \"%s\"\n", filename.c_str());

=== modified file 'src/texture.cpp'
--- src/texture.cpp	2011-07-14 17:23:51 +0000
+++ src/texture.cpp	2011-08-10 13:52:40 +0000
@@ -23,24 +23,21 @@ 
  */
 #include "texture.h"
 #include "log.h"
+#include "util.h"
 
 #include <cstdarg>
 #include <png.h>
+#include <memory>
 
 class PNGState
 {
 public:
     PNGState() :
-        fp_(0),
         png_(0),
         info_(0),
         rows_(0) {}
     ~PNGState()
     {
-        if (fp_)
-        {
-           fclose(fp_);
-        }
         if (png_)
         {
             png_destroy_read_struct(&png_, &info_, 0);
@@ -55,8 +52,8 @@ 
 
         Log::debug("Reading PNG file %s\n", filename.c_str());
 
-        fp_ = fopen(filename.c_str(), "rb");
-        if (!fp_) {
+        const std::auto_ptr<std::istream> is_ptr(Util::get_resource(filename));
+        if (!(*is_ptr)) {
             Log::error("Cannot open file %s!\n", filename.c_str());
             return false;
         }
@@ -81,7 +78,7 @@ 
         }
 
         /* Read the image information and data */
-        png_init_io(png_, fp_);
+        png_set_read_fn(png_, reinterpret_cast<voidp>(is_ptr.get()), png_read_fn);
 
         png_read_png(png_, info_, png_transforms, 0);
 
@@ -101,7 +98,11 @@ 
     }
     const unsigned char* row(unsigned int idx) const { return rows_[idx]; }
 private:
-    FILE* fp_;
+    static void png_read_fn(png_structp png_ptr, png_bytep data, png_size_t length)
+    {
+        std::istream *is = reinterpret_cast<std::istream*>(png_get_io_ptr(png_ptr));
+        is->read(reinterpret_cast<char *>(data), length);
+    }
     png_structp png_;
     png_infop info_;
     png_bytepp rows_;

=== modified file 'src/util.cpp'
--- src/util.cpp	2011-08-09 10:34:49 +0000
+++ src/util.cpp	2011-08-10 13:32:04 +0000
@@ -22,6 +22,7 @@ 
  */
 
 #include <sstream>
+#include <fstream>
 
 #include "util.h"
 
@@ -42,3 +43,10 @@ 
         elems.push_back(item);
 }
 
+std::istream *
+Util::get_resource(const std::string &path)
+{
+    std::ifstream *ifs = new std::ifstream(path.c_str());
+
+    return static_cast<std::istream *>(ifs);
+}

=== modified file 'src/util.h'
--- src/util.h	2011-08-09 10:34:49 +0000
+++ src/util.h	2011-08-10 13:32:04 +0000
@@ -26,9 +26,11 @@ 
 
 #include <string>
 #include <vector>
+#include <istream>
 
 struct Util {
     static void split(const std::string &s, char delim, std::vector<std::string> &elems);
+    static std::istream *get_resource(const std::string &path);
 };
 
 #endif /* UTIL_H */