00001 #ifndef FILERepr_HPP
00002 #define FILERepr_HPP
00003
00007 #include <stdio.h>
00008 #include <cerrno>
00009 #include "common/log.h"
00010 #include "io/FileReprBase.hpp"
00011
00019 class FILERepr : public FileReprBase
00020 {
00021 public:
00022 typedef FileReprBase::params_t params_t;
00023
00024 FILERepr( const params_t* par )
00025 throw(std::ios_base::failure) : FileReprBase(par)
00026 {
00027 if(par->mode == READ)
00028 {
00029 file = fopen(par->file_name,"r");
00030 if (!file)
00031 {
00032 std::string s(strerror(errno));
00033 log_err(0,"error opening input file %s: %s",
00034 par->file_name, s.c_str());
00035 throw std::ios_base::failure(strerror(errno));
00036 }
00037 }
00038 else
00039 {
00040 file = fopen(par->file_name, "wt");
00041 if (!file)
00042 {
00043 std::string s(strerror(errno));
00044 log_err(0,"error opening output file %s: %s",
00045 par->file_name, s.c_str());
00046 throw std::ios_base::failure(s);
00047 }
00048 }
00049 setbuffer(file,file_buffer,par->file_buffer_size);
00050 }
00051
00052 ~FILERepr()
00053 {
00054 if(file)
00055 fclose(file);
00056 }
00057 void rewind()
00058 {
00059 fseek(file, SEEK_SET, 0);
00060 }
00061 void flush()
00062 {
00063 fflush(file);
00064 }
00065
00066 std::streamsize readFromFile(char* buffer, std::streamsize buffer_size)
00067 {
00068 return fread(buffer,1,buffer_size, file);
00069 }
00070
00071 void writeToFile( const char* str, std::streamsize n )
00072 {
00073 fwrite( str, n, 1, file );
00074 }
00075 protected :
00077 FILE* file;
00078
00079 };
00080
00081 #endif