EvolvingObjects
eoFileMonitor.cpp
00001 #ifdef _MSC_VER
00002 // to avoid long name warnings
00003 #pragma warning(disable:4786)
00004 #endif
00005 
00006 #include <iostream>
00007 #include <fstream>
00008 #include <stdexcept>
00009 
00010 #include <utils/eoFileMonitor.h>
00011 #include <utils/compatibility.h>
00012 #include <utils/eoParam.h>
00013 
00014 using namespace std;
00015 
00016 void eoFileMonitor::printHeader(std::ostream& os)
00017 {
00018     iterator it = vec.begin();
00019 
00020     os << (*it)->longName();
00021 
00022     ++it;
00023 
00024     for (; it != vec.end(); ++it)
00025     {
00026         // use the longName of the eoParam for the header
00027         os << delim.c_str() << (*it)->longName();
00028     }
00029     os << std::endl;
00030 }
00031 
00032 void eoFileMonitor::printHeader()
00033 {
00034     // create file
00035     ofstream os(filename.c_str());
00036 
00037     if (!os)
00038     {
00039         string str = "eoFileMonitor could not open: " + filename;
00040         throw runtime_error(str);
00041     }
00042 
00043     printHeader(os);
00044 }
00045 
00046 eoMonitor& eoFileMonitor::operator()(void)
00047 {
00048     ofstream os(filename.c_str(),
00049         overwrite ?
00050             ios_base::out|ios_base::trunc // truncate
00051             :
00052             ios_base::out|ios_base::app   // append
00053         );
00054 
00055     if (!os)
00056     {
00057         string str = "eoFileMonitor could not write to: " + filename;
00058         throw runtime_error(str);
00059     }
00060 
00061     if (
00062             header      // we want to write headers
00063         &&  firstcall   // we do not want to write headers twice
00064         && !keep        // if we append to an existing file, headers are useless
00065         && !overwrite   // we do not want to write headers if the file is to be overwriten
00066     ) {
00067         printHeader();
00068         firstcall = false;
00069     }
00070 
00071     return operator()(os);
00072 }
00073 
00074 eoMonitor& eoFileMonitor::operator()(std::ostream& os)
00075 {
00076 
00077     iterator it = vec.begin();
00078 
00079     os << (*it)->getValue();
00080 
00081     for(++it; it != vec.end(); ++it)
00082     {
00083         os << delim.c_str() << (*it)->getValue();
00084     }
00085 
00086     os << std::endl;
00087 
00088     return *this;
00089 }
 All Classes Namespaces Files Functions Variables Typedefs Friends