Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/CsvSink.cpp
Go to the documentation of this file.
00001 #include "CsvSink.h"
00002 #include <iomanip>
00003 
00004 using namespace std;
00005 
00006 namespace Marsyas {
00007 
00008 CsvSink::CsvSink(const string & name):
00009   MarSystem("CsvSink", name)
00010 {
00011   addControl("mrs_string/filename", string());
00012   addControl("mrs_string/separator", string(" "));
00013 
00014   setControlState("mrs_string/filename", true);
00015   setControlState("mrs_string/separator", true);
00016 }
00017 
00018 CsvSink::CsvSink(const CsvSink & other):
00019   MarSystem(other)
00020 {}
00021 
00022 CsvSink::~CsvSink()
00023 {
00024   m_file.close();
00025 }
00026 
00027 void CsvSink::myUpdate( MarControlPtr cause )
00028 {
00029   MarSystem::myUpdate(cause);
00030 
00031   const string & new_filename = getControl("mrs_string/filename")->to<string>();
00032   if (new_filename != m_filename)
00033   {
00034     m_file.close();
00035 
00036     m_filename = new_filename;
00037 
00038     if (!m_filename.empty())
00039     {
00040       m_file.open(m_filename.c_str(), ofstream::out);
00041       m_file.precision(10);
00042     }
00043   }
00044 
00045   m_separator = getControl("mrs_string/separator")->to<string>();
00046 }
00047 
00048 void CsvSink::myProcess( realvec & in, realvec & out )
00049 {
00050   out = in;
00051 
00052   if (!m_file.is_open())
00053     return;
00054 
00055   if (inObservations_ < 1)
00056     return;
00057 
00058   for (mrs_natural s = 0; s < inSamples_; ++s)
00059   {
00060     m_file << in(0,s);
00061 
00062     for (mrs_natural o = 1; o < inObservations_; ++o)
00063     {
00064       m_file << m_separator;
00065       m_file << in(o,s);
00066     }
00067 
00068     m_file << endl;
00069   }
00070 }
00071 
00072 } // namespace Marsyas