=== modified file 'src/model.cpp'
@@ -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'
@@ -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'
@@ -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'
@@ -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'
@@ -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 */