メインページ   モジュール   名前空間一覧   クラス階層   アルファベット順一覧   構成   ファイル一覧   構成メンバ   ファイルメンバ   関連ページ    

TSoftwareDataFileModule.cc

解説を見る。
00001 // =====================================================================
00002 //  $Id: TSoftwareDataFileModule.cc,v 1.5 2004/03/07 10:30:31 goiwai Exp $
00003 //  $Name: CLDAQ-1-14-03 $
00004 //  $Log: TSoftwareDataFileModule.cc,v $
00005 //  Revision 1.5  2004/03/07 10:30:31  goiwai
00006 //  ROOTに組みこむためのおまじないマクロを埋めこみました。
00007 //  全てにおいて完全に動作するわけではありません。
00008 //
00009 //  Revision 1.4  2003/10/06 17:02:40  goiwai
00010 //  *** empty log message ***
00011 //
00012 //  Revision 1.3  2003/07/30 16:19:11  goiwai
00013 //  ファイルにコミットログをつけることにしました.
00014 //
00015 // =====================================================================
00016 #include "TSoftwareDataFileModule.hh"
00017 #include "TDataSegment.hh"
00018 #include "TDataElement.hh"
00019 
00020 TSoftwareDataFileModule::TSoftwareDataFileModule( const Tstring& filename, Tint nchannel )
00021   : TSoftwareModule( nchannel ),
00022     theFileName( filename ), theSeparater( Tspace ), 
00023     theChannel( nchannel, 0.0 ), theBufferLength( tDefaultBufferLength ),
00024     theNumberOfLine( 0 )
00025 {
00026   theCommentStringList.clear();
00027   theCommentStringList.push_back( Tsharp );
00028   theCommentStringList.push_back( Twslash );
00029 
00030   theInputFileStream.open( theFileName.c_str() );
00031   if ( !( theInputFileStream.good() ) || !( theInputFileStream.is_open() ) ) {
00032     Tcerr << "TSoftwareDataFileModule::TSoftwareDataFileModule: ";
00033     Tcerr << "fail to open a file " << theFileName << Tendl;
00034     exit( theStatus = -EFAULT );
00035   }
00036   theNumberOfLine = getNumberOfLine();
00037   Update();
00038 }
00039 
00040 TSoftwareDataFileModule::TSoftwareDataFileModule( const Tstring& filename, const TstringList& comment, Tint nchannel )
00041   : TSoftwareModule( nchannel ), 
00042     theFileName( filename ), theSeparater( Tspace ),
00043     theChannel( nchannel, 0.0 ), theBufferLength( tDefaultBufferLength ),
00044     theNumberOfLine( 0 ), theCommentStringList( comment )
00045 {
00046   theInputFileStream.open( theFileName.c_str() );
00047   if ( !( theInputFileStream.good() ) || !( theInputFileStream.is_open() ) ) {
00048     Tcerr << "TSoftwareDataFileModule::TSoftwareDataFileModule: ";
00049     Tcerr << "fail to open a file " << theFileName << Tendl;
00050     exit( theStatus = -EFAULT );
00051   }
00052   theNumberOfLine = getNumberOfLine();
00053   Update();
00054 }
00055 
00056 TSoftwareDataFileModule::TSoftwareDataFileModule( const Tstring& filename, const Tstring& separater, Tint nchannel )
00057   : TSoftwareModule( nchannel ),
00058     theFileName( filename ), theSeparater( separater ),
00059     theChannel( nchannel, 0.0 ), theBufferLength( tDefaultBufferLength ),
00060     theNumberOfLine( 0 )
00061 {
00062   theCommentStringList.clear();
00063   theCommentStringList.push_back( Tsharp );
00064   theCommentStringList.push_back( Twslash );
00065 
00066   theInputFileStream.open( theFileName.c_str() );
00067   if ( !( theInputFileStream.good() ) || !( theInputFileStream.is_open() ) ) {
00068     Tcerr << "TSoftwareDataFileModule::TSoftwareDataFileModule: ";
00069     Tcerr << "fail to open a file " << theFileName << Tendl;
00070     exit( theStatus = -EFAULT );
00071   }
00072   theNumberOfLine = getNumberOfLine();
00073   Update();
00074 }
00075 
00076 TSoftwareDataFileModule::TSoftwareDataFileModule( const Tstring& filename, const TstringList& comment, const Tstring& separater, Tint nchannel )
00077   : TSoftwareModule( nchannel ), 
00078     theFileName( filename ), theSeparater( separater ),
00079     theChannel( nchannel, 0.0 ), theBufferLength( tDefaultBufferLength ),
00080     theNumberOfLine( 0 ), theCommentStringList( comment )
00081 {
00082   theInputFileStream.open( theFileName.c_str() );
00083   if ( !( theInputFileStream.good() ) || !( theInputFileStream.is_open() ) ) {
00084     Tcerr << "TSoftwareDataFileModule::TSoftwareDataFileModule: ";
00085     Tcerr << "fail to open a file " << theFileName << Tendl;
00086     exit( theStatus = -EFAULT );
00087   }
00088   theNumberOfLine = getNumberOfLine();
00089   Update();
00090 }
00091 
00092 TSoftwareDataFileModule::TSoftwareDataFileModule( const TSoftwareDataFileModule& right )
00093   : TSoftwareModule( right ),
00094     theFileName( right.theFileName ),
00095     theSeparater( right.theSeparater ),
00096     theChannel( right.theChannel ),
00097     theBufferLength( right.theBufferLength ),
00098     theNumberOfLine( 0 ),
00099     theCommentStringList( right.theCommentStringList )
00100 {
00101   theInputFileStream.open( theFileName.c_str() );
00102   if ( !( theInputFileStream.good() ) || !( theInputFileStream.is_open() ) ) {
00103     Tcerr << "TSoftwareDataFileModule::TSoftwareDataFileModule: ";
00104     Tcerr << "fail to open a file " << theFileName << Tendl;
00105     exit( theStatus = -EFAULT );
00106   }
00107   theNumberOfLine = getNumberOfLine();
00108   Update();
00109 }
00110 
00111 TSoftwareDataFileModule::~TSoftwareDataFileModule()
00112 {
00113   if ( theInputFileStream.is_open() )
00114     theInputFileStream.close();
00115 }
00116 
00117 Tint TSoftwareDataFileModule::Clear()
00118 {
00119   for ( Tint i = 0; i < theNumberOfChannels; i ++ ) {
00120     theChannel[ i ] = 0.0;
00121   }
00122   return theStatus = tStatusSuccess;
00123 }
00124 
00125 Tint TSoftwareDataFileModule::Update()
00126 {
00127   Tchar* linebuf = new Tchar[ theBufferLength ];
00128   Tstring strbuf;
00129 
00130   do {
00131     if ( theInputFileStream.eof() ) {
00132       // back to top of file, use a data again.
00133       Initialize();
00134     }
00135     theInputFileStream.getline( linebuf, theBufferLength );
00136     strbuf = linebuf;
00137     if ( !( strbuf.empty() ) )  // for empty line
00138       eraseComment( strbuf );
00139   } while ( strbuf.empty() );
00140 
00141   delete [] linebuf;
00142 
00143   theChannel = divideLine( strbuf );
00144 
00145   return theStatus = tStatusSuccess;
00146 }
00147 
00148 Tint TSoftwareDataFileModule::Initialize()
00149 {
00150   Clear();
00151   if ( theInputFileStream.is_open() )
00152     theInputFileStream.close();
00153   theInputFileStream.open( theFileName.c_str() );
00154   if ( !( theInputFileStream.good() ) || !( theInputFileStream.is_open() ) ) {
00155     Tcerr << "TSoftwareDataFileModule::TSoftwareDataFileModule: ";
00156     Tcerr << "fail to open a file " << theFileName << Tendl;
00157   }
00158   theNumberOfLine = getNumberOfLine();
00159   return theStatus = tStatusSuccess;
00160 }
00161 
00162 Tvoid TSoftwareDataFileModule::FillData( TDataElement& element, Tint channel )
00163 {
00164   if ( channel < 0 || channel >= theNumberOfChannels ) {
00165     Tcerr << "TSoftwareDataFileModule::FillData: invalid ID " << channel << Tendl;
00166     theStatus = -EFAULT;
00167     element.FillData( &theStatus, tTypeInt, 1 );
00168   } else {
00169     element.FillData( &theChannel[ channel ], tTypeDouble, 1 );
00170   }
00171   return;
00172 }
00173 
00174 const TSoftwareDataFileModule& TSoftwareDataFileModule::operator=( const TSoftwareDataFileModule& right )
00175 {
00176   *( (TSoftwareModule*)this ) = *( (TSoftwareModule*)(&right) );
00177   theFileName = right.theFileName;
00178   theSeparater = right.theSeparater;
00179   theChannel = right.theChannel;
00180   theBufferLength = right.theBufferLength;
00181   theCommentStringList = right.theCommentStringList;
00182   theInputFileStream.open( theFileName.c_str() );
00183   if ( !( theInputFileStream.good() ) || !( theInputFileStream.is_open() ) ) {
00184     Tcerr << "TSoftwareDataFileModule::operator=: ";
00185     Tcerr << "fail to open a file " << theFileName << Tendl;
00186     exit( theStatus = -EFAULT );
00187   }
00188   theNumberOfLine = getNumberOfLine();
00189   Update();
00190   return *this;
00191 }
00192 
00193 Tbool TSoftwareDataFileModule::operator==( const TSoftwareDataFileModule& right ) const
00194 {
00195   Tbool ret = Ttrue;
00196   ret &= ( *( (TSoftwareModule*)this ) == *( (TSoftwareModule*)(&right) ) );
00197   ret &= ( theFileName == right.theFileName );
00198   ret &= ( theSeparater == right.theSeparater );
00199   ret &= ( theChannel == right.theChannel );
00200   ret &= ( theBufferLength == right.theBufferLength );
00201   ret &= ( theNumberOfLine == right.theNumberOfLine );
00202   ret &= ( theCommentStringList == right.theCommentStringList );
00203   return ret;
00204 }
00205 
00206 Tbool TSoftwareDataFileModule::operator!=( const TSoftwareDataFileModule& right ) const
00207 {
00208   Tbool ret = Tfalse;
00209   ret |= ( *( (TSoftwareModule*)this ) != *( (TSoftwareModule*)(&right) ) );
00210   ret |= ( theFileName != right.theFileName );
00211   ret |= ( theSeparater != right.theSeparater );
00212   ret |= ( theChannel != right.theChannel );
00213   ret |= ( theBufferLength != right.theBufferLength );
00214   ret |= ( theNumberOfLine != right.theNumberOfLine );
00215   ret |= ( theCommentStringList != right.theCommentStringList );
00216   return ret;
00217 }
00218 
00219 const Tstring& TSoftwareDataFileModule::eraseComment( Tstring& readline ) const
00220 {
00221   for ( Tsize_t i = 0; i < theCommentStringList.size(); i ++ ) {
00222     Tsize_t pos = readline.find( theCommentStringList[ i ] );
00223     Tsize_t bufsize = readline.size();
00224     if ( pos >= 0 && pos <= bufsize - theCommentStringList[ i ].size() ) {
00225       readline.erase( pos, bufsize - pos );
00226     }
00227   }
00228 
00229   while ( !( readline.empty() ) && readline.rfind( Tspace ) == readline.size() - 1 ) {
00230     readline.erase( readline.end() - 1 );
00231   }
00232 
00233   while ( !( readline.empty() ) && readline.find( Tspace ) == 0 ) {
00234     readline.erase( readline.begin() );
00235   }
00236 
00237   return readline;
00238 }
00239 
00240 TdoubleList TSoftwareDataFileModule::divideLine( Tstring readline ) const
00241 {
00242   Tsize_t begin = 0;
00243   Tsize_t end = 0;
00244   Tstring strbuf;
00245   TdoubleList dbllist;
00246   static const Tstring head = "TSoftwareDataFileModule::divideLine(): ";
00247 
00248   if ( !( readline.empty() ) ) {
00249     Tsize_t lastpos = readline.rfind( theSeparater ) + 1;
00250     do {
00251       begin = readline.find_first_not_of( theSeparater, end );
00252       end = readline.find( theSeparater, begin );
00253       if ( end > readline.size() ) {
00254         end = readline.size();
00255       }
00256       strbuf = readline.substr( begin, end - begin );
00257       dbllist.push_back( atof( strbuf.c_str() ) );
00258     } while ( begin != lastpos );
00259   }
00260 
00261   if ( dbllist.size() > (Tsize_t)theNumberOfChannels ) {
00262     Tcerr << head << "too much channels." << Tendl;
00263     Tcerr << head << "rows and channels are inconsistency." << Tendl;
00264     dbllist.erase( dbllist.begin() + theNumberOfChannels, dbllist.end() );
00265   } else if ( dbllist.size() < (Tsize_t)theNumberOfChannels ) {
00266     Tcerr << head << "less than required channels." << Tendl;
00267     Tcerr << head << "filled 0.0." << Tendl;
00268     for ( Tsize_t i = 0; i < theNumberOfChannels - dbllist.size(); i ++ ) {
00269       dbllist.push_back( 0.0 );
00270     }
00271   }
00272 
00273   return dbllist;
00274 }
00275 
00276 Tint TSoftwareDataFileModule::getNumberOfLine() const
00277 {
00278   static const Tstring head = "TSoftwareDataFileModule::getNumberOfLine() ";
00279 
00280   Tint nline = 0;
00281   Tifstream tifs;
00282   tifs.open( theFileName.c_str() );
00283   if ( !( tifs.good() ) || !( tifs.is_open() ) ) {
00284     Tcerr << head << "fail to open a file " << theFileName << Tendl;
00285   }
00286 
00287 
00288   Tchar* linebuf = new Tchar[ theBufferLength ];
00289   Tstring strbuf;
00290 
00291   while ( !( tifs.eof() ) ) {
00292     do {
00293       if ( tifs.eof() ) {
00294         break;
00295       }
00296       tifs.getline( linebuf, theBufferLength );
00297       strbuf = linebuf;
00298       if ( !( strbuf.empty() ) ) { // for empty line
00299         eraseComment( strbuf );
00300       }
00301     } while ( strbuf.empty() );
00302     if ( !( tifs.eof() ) ) {
00303       nline ++;
00304     }
00305   }
00306   delete [] linebuf;
00307   tifs.close();
00308 
00309   return nline;
00310 }
00311 
00312 #ifdef __CLDAQ_ROOT_DLL
00313     ClassImp(TSoftwareDataFileModule)
00314 #endif


CLDAQ - a Class Library for DataAcQuisition (Version 1.14.3)
Go IWAI -- goiwai at users.sourceforge.jp