00001 /* -*- mode: c++ -*- */ 00002 #ifndef __DAQpp_FileIOManager_h__ 00003 #define __DAQpp_FileIOManager_h__ 00004 00005 #include <zlib.h> 00006 #include <DAQ++/Thread.h> 00007 #include <fcntl.h> 00008 #include <string> 00009 #include <DAQ++/exception.h> 00010 00011 namespace DAQpp 00012 { 00013 00014 class FileHeader; 00015 00033 class FileIOManager 00034 { 00035 public: 00036 typedef int (*HeaderFunc)(const char **, char *); 00037 00038 private: 00040 int fd; 00042 gzFile zfd; 00043 00045 Mutex mtx; 00048 class IOthread : public Thread 00049 { 00050 private: 00052 FileIOManager *iom; 00053 public: 00055 IOthread(FileIOManager *_io) : iom(_io) 00056 {} 00058 void run() 00059 { 00060 if (iom) 00061 iom->spy_size(); 00062 } 00063 }; 00064 friend class IOthread; 00065 00067 IOthread thread; 00068 00070 std::string base_name; 00071 00073 std::string extension; 00074 00076 int ifile; 00077 00079 bool zcomp; 00080 00082 unsigned long _size; 00083 00085 unsigned long _max_size; 00086 00088 std::string file_name; 00089 00091 void spy_size(); 00092 void spy_size_intr(); 00093 00095 int new_file(); 00096 00097 00098 00103 bool doSpy; 00104 00106 int write_header_tail( HeaderFunc , char *); 00107 00108 public: 00112 HeaderFunc get_header; 00113 00115 char *header_data; 00116 00120 HeaderFunc get_tail; 00121 00122 // Data that will be passed to HeaderFunc 00123 char *tail_data; 00124 00125 public: 00127 FileIOManager(bool use_compression, unsigned long mx_size ); 00128 00130 virtual ~FileIOManager(); 00131 00133 int open(const std::string &); 00134 00136 int close(); 00137 00139 int write(const char *, size_t); 00140 00142 virtual std::string new_name(); 00143 00145 int file_number() 00146 { 00147 return ifile; 00148 } 00149 00151 void set_max_size(unsigned long x) 00152 { 00153 _max_size = x; 00154 } 00155 00157 unsigned long size() 00158 { 00159 return _size; 00160 } 00161 00163 double relative_size() 00164 { 00165 return ((double)_size) / ((double)_max_size); 00166 } 00167 00169 void Compress(bool t) 00170 { 00171 zcomp = t; 00172 } 00173 00175 bool Compress() 00176 { 00177 return zcomp; 00178 } 00179 00181 const std::string &get_file_name() const 00182 { 00183 return file_name; 00184 } 00185 00187 void set_header_func( HeaderFunc ff, char *dt ) 00188 { 00189 header_data = dt; 00190 get_header = ff; 00191 } 00192 00194 void reset_header_func() 00195 { 00196 get_header = 0; 00197 } 00198 00200 void set_tail_func( HeaderFunc ff, char *dt ) 00201 { 00202 tail_data = dt; 00203 get_tail = ff; 00204 } 00205 00206 // removes the tail "getter" 00207 void reset_tail_func() 00208 { 00209 get_tail = 0; 00210 } 00211 }; 00212 00213 00214 00221 } 00222 00223 #endif