Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

FDRepr.hpp

Go to the documentation of this file.
00001 #ifndef FDRepr_HPP
00002 #define FDRepr_HPP
00003 
00008 #include <stdio.h>
00009 #include <string>
00010 #include <cerrno>
00011 #include <string.h>
00012 #include "io/FileReprBase.hpp"  
00013 #include <sys/types.h>
00014 #include <sys/fcntl.h>
00015 #include <unistd.h>
00016 #include "common/log.h"
00017 #include <iostream>
00018 
00024 class FDRepr : public FileReprBase
00025 {
00026 public:
00027   typedef FileReprBase::params_t params_t;
00028 
00029       FDRepr( const params_t* par ) 
00030          throw(std::ios_base::failure) : FileReprBase(par)
00031       {
00032          if(par->mode == WRITE)
00033          {
00034             fdout=open(par->file_name,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
00035             if (fdout<0) {
00036                std::string s(strerror(errno));
00037                log_err(0,"error opening output file %s: %s",par->file_name,s.c_str());
00038                throw std::ios_base::failure(s);
00039             }
00040             bufend=file_buffer+file_buffer_size;
00041             buffree=file_buffer;
00042          }
00043          else
00044          {
00045             fdout=open(par->file_name,O_RDONLY);
00046             if (fdout<0) {
00047                std::string s(strerror(errno));
00048                log_err(0,"error opening input file %s: %s",par->file_name,s.c_str());
00049                throw std::ios_base::failure(s);
00050             }
00051          }
00052       }
00053 
00054   ~FDRepr()
00055   {
00056     if (mode==WRITE)
00057       flush();
00058     close(fdout);
00059   }
00060   void rewind()
00061   {
00062     lseek(fdout,0,SEEK_SET);
00063   }
00064   void flush()
00065   {
00066     bufend=buffree;
00067     buffree=file_buffer;
00068     while (buffree<bufend) {
00069       int ret=::write(fdout,buffree,bufend-buffree);
00070       if (ret<0) {
00071         perror("write");
00072       } else
00073         buffree+=ret;
00074     }
00075     bufend=file_buffer+file_buffer_size;
00076     buffree=file_buffer;
00077   }
00078 
00079   std::streamsize readFromFile(char* buffer, std::streamsize buffer_size)
00080   {
00081     //we make no use of the file buffer in the read case
00082     ssize_t ret=read(fdout,buffer,buffer_size);
00083     if (ret==-1) {
00084       perror("read");
00085       return 0;
00086     }
00087     return ret;
00088   }
00089 
00090   void writeToFile( const char* buf, std::streamsize len )
00091   {
00092     if ((int)len>bufend-buffree) {
00093       int part=bufend-buffree;
00094       mempcpy(buffree,buf,part);
00095       buffree=file_buffer;
00096       while (buffree<bufend) {
00097         int ret=::write(fdout,buffree,bufend-buffree);
00098         if (ret<0) {
00099           perror("write");
00100         } else
00101           buffree+=ret;
00102       }
00103       buffree=(char*)mempcpy(file_buffer,buf+part,len-part);
00104     } else {
00105       buffree=(char*)mempcpy(buffree,buf,len);
00106     }
00107   }
00108 protected :
00110   int fdout;
00111 private :
00113   char *bufend,*buffree;
00114 
00115 };
00116 
00117 
00118 #endif

Generated on Sun Sep 17 17:50:38 2006 for FIM environment by  doxygen 1.4.4