00001 #ifndef StreamRepr_HPP 00002 #define StreamRepr_HPP 00003 00008 #include <fstream> 00009 #include <cerrno> 00010 #include "common/log.h" 00011 #include "io/FileReprBase.hpp" 00012 00018 class StreamRepr : public FileReprBase 00019 { 00020 public: 00021 typedef FileReprBase::params_t params_t; 00022 00023 StreamRepr( const params_t* par ) 00024 throw(std::ios_base::failure) : FileReprBase(par) 00025 { 00026 if(par->mode == READ) 00027 { 00028 input_stream.open(par->file_name, std::ios::in); 00029 if ( !input_stream.is_open() ) 00030 { 00031 std::string s(strerror(errno)); 00032 log_err(0,"error opening input file %s: %s", par->file_name, s.c_str()); 00033 throw std::ios_base::failure(s); 00034 } 00035 input_stream.rdbuf()->pubsetbuf(file_buffer, par->file_buffer_size); 00036 } 00037 else 00038 { 00039 output_stream.open(par->file_name, std::ios::out); 00040 if(!output_stream.is_open()) 00041 { 00042 std::string s(strerror(errno)); 00043 log_err(0,"error opening output file %s: %s", par->file_name, s.c_str()); 00044 throw std::ios_base::failure(s); 00045 } 00046 output_stream.rdbuf()->pubsetbuf(file_buffer, par->file_buffer_size); 00047 } 00048 } 00049 00050 ~StreamRepr() 00051 { 00052 if ( input_stream.is_open() ) 00053 input_stream.close(); 00054 if ( output_stream.is_open() ) 00055 output_stream.close(); 00056 } 00057 00058 void rewind() 00059 { 00060 input_stream.clear(); 00061 input_stream.seekg(0, std::ios::beg); 00062 } 00063 void flush() 00064 { 00065 output_stream.flush(); 00066 } 00067 00068 std::streamsize readFromFile(char* buffer, std::streamsize buffer_size) 00069 { 00070 return input_stream.readsome(buffer, buffer_size); 00071 } 00072 void writeToFile( const char* str, std::streamsize n ) 00073 { 00074 output_stream.write( str, n ); 00075 } 00076 protected: 00078 std::ifstream input_stream; 00080 std::ofstream output_stream; 00081 }; 00082 00083 #endif