00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "ViconFileSource.h"
00020
00021 using namespace std;
00022 using namespace Marsyas;
00023
00024 ViconFileSource::ViconFileSource(mrs_string name):MarSystem("ViconFileSource",name)
00025 {
00026
00027
00028
00029 vfp_ = 0;
00030
00031 addControls();
00032 }
00033
00034
00035 ViconFileSource::~ViconFileSource()
00036 {
00037 if (vfp_ != NULL)
00038 fclose(vfp_);
00039 }
00040
00041 void
00042 ViconFileSource::addControls()
00043 {
00044 addctrl("mrs_bool/hasData", true);
00045 addctrl("mrs_natural/size", 0);
00046 addctrl("mrs_string/markers", " ");
00047 addctrl("mrs_string/filename", "dviconfile");
00048 setctrlState("mrs_string/filename", true);
00049 }
00050
00051
00052
00053 MarSystem*
00054 ViconFileSource::clone() const
00055 {
00056 return new ViconFileSource(*this);
00057 }
00058
00059
00060
00061 void
00062 ViconFileSource::getHeader(mrs_string filename)
00063 {
00064
00065 vfp_ = fopen(filename.c_str(), "r");
00066 if (vfp_)
00067 {
00068
00069 char buffer[4096];
00070 fgets(buffer, 4096, vfp_);
00071 stringstream line(buffer);
00072 char entry[256];
00073 fileObs_ = 0;
00074 while (line.getline(entry, 256, ','))
00075 {
00076
00077 fileObs_++;
00078 }
00079 setctrl("mrs_natural/onObservations", fileObs_);
00080 setctrl("mrs_string/markers", buffer);
00081 }
00082 }
00083
00084
00085 void
00086 ViconFileSource::myUpdate(MarControlPtr sender)
00087 {
00088 (void) sender;
00089 inObservations_ = getctrl("mrs_natural/inObservations")->to<mrs_natural>();
00090 israte_ = getctrl("mrs_real/israte")->to<mrs_real>();
00091
00092
00093 if (filename_ != getctrl("mrs_string/filename")->to<mrs_string>())
00094 {
00095 filename_ = getctrl("mrs_string/filename")->to<mrs_string>();
00096 getHeader(filename_);
00097
00098 }
00099
00100 setctrl("mrs_natural/onSamples", inSamples_);
00101 setctrl("mrs_natural/onObservations", fileObs_);
00102 setctrl("mrs_real/osrate", israte_);
00103
00104
00105 }
00106
00107 void
00108 ViconFileSource::myProcess(realvec& in, realvec& out)
00109 {
00110 (void) in;
00111
00112 mrs_natural o,t;
00113
00114 for (t = 0; t < inSamples_; t++)
00115 {
00116 bool notValidLine = true;
00117 char buffer[4096];
00118 while (notValidLine)
00119 {
00120 char *res;
00121 res = fgets(buffer, 4096, vfp_);
00122 if (res == NULL)
00123 {
00124 setctrl("mrs_bool/hasData",false);
00125 return;
00126 }
00127
00128 stringstream line(buffer);
00129 stringstream pline(buffer);
00130 char entry[256];
00131 notValidLine = false;
00132 for (o=0; o < onObservations_; o++)
00133 {
00134 line.getline(entry, 256, ',');
00135 if (!strcmp(entry,""))
00136 {
00137 for (mrs_natural j=0; j < o; j++)
00138 out(j,t) = 0.0;
00139 notValidLine = true;
00140 }
00141 else
00142 out(o,t) = (mrs_real)atof(entry);
00143
00144 if (notValidLine) break;
00145 }
00146 }
00147 }
00148 }
00149
00150
00151
00152
00153
00154