00001 #ifndef SDXFILEREADER_H
00002 #define SDXFILEREADER_H
00003
00004
00005 #include <iostream>
00006 #include <fstream>
00007 #include <string>
00008
00009 namespace SDX {
00010 class ContentHandler;
00011
00012 namespace Helpers {
00016 template <class T> class FileReader : public T {
00017 public:
00021 FileReader(std::string filePath, SDX::ContentHandler* contentHandler = 0) :
00022 T(0, contentHandler),
00023 m_filePath(filePath)
00024 {
00025 }
00026
00030 void setFilePath(std::string filePath){
00031 m_filePath = filePath;
00032 }
00033 private:
00034 void openStream(){
00035 m_stream = new std::ifstream(m_filePath.c_str());
00036
00037 bool success = !m_stream->fail();
00038 if(!success){
00039 delete m_stream;
00040 m_stream = 0;
00041 } else
00042 T::setStream(m_stream);
00043 }
00044 void closeStream(){
00045 if(!m_stream)
00046 return;
00047
00048 m_stream->close();
00049 delete m_stream;
00050 m_stream = 0;
00051 }
00052 private:
00053 std::string m_filePath;
00054 std::ifstream* m_stream;
00055 };
00056 }
00057 }
00058
00059 #endif