Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/CsvFileSource.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2010 George Tzanetakis <gtzan@cs.uvic.ca>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include "CsvFileSource.h"
00020 
00021 using namespace std;
00022 using namespace Marsyas;
00023 
00024 CsvFileSource::CsvFileSource(mrs_string name):MarSystem("CsvFileSource",name)
00025 {
00026   //type_ = "CsvFileSource";
00027   //name_ = name;
00028 
00029   vfp_ = 0;
00030   fileObs_ = 0;
00031   filename_ = EMPTYSTRING;
00032 
00033   addControls();
00034 }
00035 
00036 
00037 CsvFileSource::~CsvFileSource()
00038 {
00039   if (vfp_ != NULL)
00040     fclose(vfp_);
00041 }
00042 
00043 void
00044 CsvFileSource::addControls()
00045 {
00046   addctrl("mrs_bool/hasData", true);
00047   addctrl("mrs_natural/size", 0);
00048   addctrl("mrs_string/filename", EMPTYSTRING);
00049   setctrlState("mrs_string/filename", true);
00050 }
00051 
00052 
00053 
00054 MarSystem*
00055 CsvFileSource::clone() const
00056 {
00057   return new CsvFileSource(*this);
00058 }
00059 
00060 
00061 
00062 void
00063 CsvFileSource::getHeader(mrs_string filename)
00064 {
00065   if (vfp_ != NULL) {
00066     fclose(vfp_);
00067   }
00068   // Need to read Csv File Header
00069   vfp_ = fopen(filename.c_str(), "r");
00070   if (vfp_)
00071   {
00072     // read first line from file
00073     char buffer[4096];
00074     char *res;
00075     res = fgets(buffer, 4096, vfp_);
00076     if (res == NULL) {
00077       cout<<"CsvFileSource: error reading file "<<filename<<endl;
00078     }
00079     stringstream line(buffer);
00080     char entry[256];
00081     fileObs_ = 0;
00082     while (line.getline(entry, 256, ','))
00083     {
00084       fileObs_++;
00085     }
00086     setctrl("mrs_natural/onObservations", fileObs_);
00087     lines_done_ = 0;
00088 
00089     string obs(buffer);
00090     ctrl_onObsNames_->setValue(obs, NOUPDATE);
00091     setctrl("mrs_bool/hasData", true);
00092   } else {
00093     MRSWARN("CsvFileSource: error reading file " + filename);
00094   }
00095 }
00096 
00097 
00098 void
00099 CsvFileSource::myUpdate(MarControlPtr sender)
00100 {
00101   (void) sender;  //suppress warning of unused parameter(s)
00102   inObservations_ = getctrl("mrs_natural/inObservations")->to<mrs_natural>();
00103   israte_ = getctrl("mrs_real/israte")->to<mrs_real>();
00104   setctrl("mrs_real/osrate", israte_);
00105   setctrl("mrs_natural/onSamples", inSamples_);
00106 
00107   if (filename_ != getctrl("mrs_string/filename")->to<mrs_string>())
00108   {
00109     filename_ = getctrl("mrs_string/filename")->to<mrs_string>();
00110     if (filename_ == EMPTYSTRING) {
00111       setctrl("mrs_natural/onObservations", 0);
00112       return;
00113     }
00114 
00115     // count lines, subtract 1 for header
00116     fileSamples_ = 0;
00117     ifstream input(filename_.c_str());
00118     string line;
00119     while (input.good())
00120     {
00121       fileSamples_++;
00122       getline(input, line);
00123     }
00124     input.close();
00125     fileSamples_ -= 2;
00126 
00127     getHeader(filename_);
00128 
00129   }
00130 
00131   setctrl("mrs_natural/onObservations", fileObs_);
00132 
00133 
00134 }
00135 
00136 void
00137 CsvFileSource::myProcess(realvec& in, realvec& out)
00138 {
00139   (void) in;
00140   //checkFlow(in,out);
00141   mrs_natural o,t;
00142 
00143   for (t = 0; t < inSamples_; t++)
00144   {
00145     bool notValidLine = true;
00146     char buffer[4096];
00147     while (notValidLine)
00148     {
00149       char *res;
00150       res = fgets(buffer, 4096, vfp_);
00151       if (res == NULL)
00152       {
00153         setctrl("mrs_bool/hasData",false);
00154         return;
00155       }
00156 
00157       stringstream line(buffer);
00158       stringstream pline(buffer);
00159       char entry[256];
00160       notValidLine = false;
00161       for (o=0; o < onObservations_; o++)
00162       {
00163         line.getline(entry, 256, ',');
00164         if (!strcmp(entry,""))
00165         {
00166           for (mrs_natural j=0; j < o; j++)
00167             out(j,t) = 0.0;
00168           notValidLine = true;
00169         }
00170         else
00171           out(o,t) = (mrs_real)atof(entry);
00172 
00173         if (notValidLine) break;
00174       }
00175       lines_done_++;
00176       if (lines_done_ >= fileSamples_)
00177       {
00178         setctrl("mrs_bool/hasData",false);
00179       }
00180     }
00181   }
00182 }
00183 
00184 
00185 
00186 
00187 
00188