00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <cstdlib>
00033
00034 #include <iostream>
00035 #include <fstream>
00036 #include <stdexcept>
00037
00038 #include <utils/edoFileSnapshot.h>
00039 #include <utils/compatibility.h>
00040 #include <utils/eoParam.h>
00041
00042 edoFileSnapshot::edoFileSnapshot(std::string dirname,
00043 unsigned int frequency ,
00044 std::string filename ,
00045 std::string delim ,
00046 unsigned int counter ,
00047 bool rmFiles ,
00048 bool saveFilenames )
00049 : _dirname(dirname), _frequency(frequency),
00050 _filename(filename), _delim(delim),
00051 _counter(counter), _saveFilenames(saveFilenames),
00052 _descOfFiles( NULL ), _boolChanged(true)
00053 {
00054 std::string s = "test -d " + _dirname;
00055
00056 int res = system(s.c_str());
00057
00058
00059
00060 if ( (res == -1) || (res == 127) )
00061 {
00062 throw std::runtime_error("Problem executing test of dir in eoFileSnapshot");
00063 }
00064
00065
00066 if (res)
00067 {
00068 s = std::string("mkdir ") + _dirname;
00069 }
00070 else if (!res && rmFiles)
00071 {
00072 s = std::string("/bin/rm -f ") + _dirname+ "/" + _filename + "*";
00073 }
00074 else
00075 {
00076 s = " ";
00077 }
00078
00079 int dummy;
00080 dummy = system(s.c_str());
00081
00082
00083 _descOfFiles = new std::ofstream( std::string(dirname + "/list_of_files.txt").c_str() );
00084
00085 }
00086
00087 edoFileSnapshot::~edoFileSnapshot()
00088 {
00089 delete _descOfFiles;
00090 }
00091
00092 void edoFileSnapshot::setCurrentFileName()
00093 {
00094 std::ostringstream oscount;
00095 oscount << _counter;
00096 _currentFileName = _dirname + "/" + _filename + oscount.str();
00097 }
00098
00099 eoMonitor& edoFileSnapshot::operator()(void)
00100 {
00101 if (_counter % _frequency)
00102 {
00103 _boolChanged = false;
00104 _counter++;
00105 return (*this);
00106 }
00107 _counter++;
00108 _boolChanged = true;
00109 setCurrentFileName();
00110
00111 std::ofstream os(_currentFileName.c_str());
00112
00113 if (!os)
00114 {
00115 std::string str = "edoFileSnapshot: Could not open " + _currentFileName;
00116 throw std::runtime_error(str);
00117 }
00118
00119 if ( _saveFilenames )
00120 {
00121 *_descOfFiles << _currentFileName.c_str() << std::endl;
00122 }
00123
00124 return operator()(os);
00125 }
00126
00127 eoMonitor& edoFileSnapshot::operator()(std::ostream& os)
00128 {
00129 iterator it = vec.begin();
00130
00131 os << (*it)->getValue();
00132
00133 for ( ++it; it != vec.end(); ++it )
00134 {
00135 os << _delim.c_str() << (*it)->getValue();
00136 }
00137
00138 os << '\n';
00139
00140 return *this;
00141 }