Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

IoDataStreamItr.cxx

Go to the documentation of this file.
00001 
00002 // $Id: IoDataStreamItr.cxx,v 1.12 2009/02/28 21:46:13 gmieg Exp $
00003 //
00004 // A class for walking over data streams.
00005 //
00006 // messier@huhepl.harvard.edu
00008 #include <algorithm>
00009 
00010 #include "MessageService/MsgService.h"
00011 #include "MessageService/MsgFormat.h"
00012 #include "IoModules/IoDataStreamItr.h"
00013 
00014 CVSID("$Id: IoDataStreamItr.cxx,v 1.12 2009/02/28 21:46:13 gmieg Exp $");
00015 
00016 //......................................................................
00017 
00018 IoDataStreamItr::IoDataStreamItr():
00019                fSourceName(""),fFileListItr(fFileList.end()),fIsBOF(false) { }
00020 
00021 //......................................................................
00022 
00023 IoDataStreamItr::IoDataStreamItr(const char* sourceName):
00024        fSourceName(sourceName),fFileListItr(fFileList.end()),fIsBOF(false) { }
00025 
00026 //......................................................................
00027 
00028 IoDataStreamItr::~IoDataStreamItr() { }
00029 
00030 //......................................................................  
00031 
00032 int IoDataStreamItr::GoToEOF() 
00033 {
00034   static const int BIG_NUMBER = 1000000;
00035   while (this->Increment(BIG_NUMBER)) { continue; }
00036   return 1; // AOK
00037 }
00038 
00039 //......................................................................  
00040 
00041 int IoDataStreamItr::DefineStream(const char* /* streamname */,
00042                                   const char* /* treename */) { return 0; }
00043 
00044 //......................................................................  
00045 
00046 int IoDataStreamItr::Streams(const char* /* streamlist */) { return 0; }
00047 
00048 //......................................................................  
00049 
00050 int IoDataStreamItr::Select(const char* /* stream */,
00051                             const char* /* selection */,
00052                             bool /* isRequired */) { return 0; }
00053 
00054 //......................................................................  
00055 
00056 int IoDataStreamItr::SetSequenceMode(const char* /* stream */,
00057                               Per::ESequenceMode /* seqmode */) { return 0; }
00058 
00059 //......................................................................  
00060 
00061 int IoDataStreamItr::SetPerOwnedDisabled(const char* /* stream */,
00062                                      bool /* perowneddisabled */) { return 0; }
00063 
00064 //......................................................................  
00065 
00066 int IoDataStreamItr::SetTestMode(const char* /* stream */,
00067                                  bool /* testmode */) { return 0; }
00068 
00069 //......................................................................  
00070 
00071 int IoDataStreamItr::SetWindow(const char*, double, double) { return 0; }
00072 
00073 //......................................................................  
00074 
00075 void IoDataStreamItr::SetPort(unsigned int /* port */) { }
00076 
00077 //......................................................................  
00078 
00079 void IoDataStreamItr::SetTimeOut(unsigned int /* seconds */) { }
00080 
00081 //......................................................................  
00082 
00083 int IoDataStreamItr::SetMaxFileRepeat(const char*, int) { return 0; }
00084 
00085 //......................................................................  
00086 
00087 int IoDataStreamItr::SetMeanMom(const char*, double) { return 0; }
00088 
00089 //......................................................................  
00090 
00091 int IoDataStreamItr::SetPushRandom(const char*, bool) { return 0; }
00092 
00093 //......................................................................  
00094 
00095 void IoDataStreamItr::SetRandomSeed(int) {}
00096 
00097 //......................................................................  
00098 
00099 void IoDataStreamItr::AddFile(const char* fullfilepathname, int at,
00100                               const char* /* streamlist */) {
00101 //======================================================================
00102 // Add fullfilepathname to the list of input files at the position "at". 
00103 // -1 = end of list.  streamlist is ignored in this def implementation,
00104 // i.e. action is applied to all streams.
00105 //======================================================================
00106   // Remember the current file so we can come back here
00107   std::string curf;
00108   if ( !fIsBOF && fFileList.size()>0) curf = (*fFileListItr);
00109 
00110   // Figure out where to insert the file into the list
00111   std::string f(fullfilepathname);
00112   if (at<0)  {
00113     fFileList.push_back(f); 
00114   }
00115   else if (at==0) {
00116     fFileList.push_front(f);
00117   }
00118   else {
00119     std::list<std::string>::iterator itr(fFileList.begin());
00120     std::list<std::string>::iterator itrEnd(fFileList.end());
00121     for (int i=0; (i<at && itr!=itrEnd); ++i, ++itr) continue;
00122     fFileList.insert(itr,f);
00123   }
00124   
00125   // Put the position back to the orginal file (or start of list)
00126   if ( fIsBOF || fFileList.size()==1) {
00127     fIsBOF = true;  // NextFile will move to first file
00128   }
00129   else {
00130     // Find original file in new list
00131     fFileListItr = std::find(fFileList.begin(),fFileList.end(),curf);
00132     fIsBOF = false;
00133   }
00134 
00135   return;
00136 
00137 }
00138 
00139 //......................................................................
00140 
00141 void IoDataStreamItr::RemoveFile(const char* fullfilepathname, 
00142                                  const char* /* streamlist */) {
00143 //======================================================================
00144 // Remove the fullfilepathname from the list of input data files. If == "*", 
00145 // remove all files.  streamlist is ignored in this def implementation,
00146 // i.e. action is applied to all streams.
00147 //======================================================================
00148   
00149   std::string curf; bool isBegin = false;
00150   if ( !fIsBOF && fFileListItr != fFileList.end() ) 
00151                                                     curf = *fFileListItr;
00152   else if ( fIsBOF ) isBegin = true;
00153 
00154   std::string f(fullfilepathname);
00155 
00156   std::list<std::string>::iterator itr(fFileList.end());
00157   while ( !fFileList.empty() && itr != fFileList.begin() ) {
00158     itr--;  // Move backwards to undisturbed list as files erased off end
00159     if ( f == "*" || f == (*itr) ) {
00160       if ( fFileListItr == itr ) {
00161         MSG("Io",Msg::kWarning)
00162           << "Attempt to remove currently opened file\n" << (*itr) 
00163           << " ignored." << endl;
00164       }
00165       else {
00166         fFileList.erase(itr);
00167       }
00168     }
00169   }
00170 
00171   // Reset file iterator in revised list
00172   if ( isBegin && !fFileList.empty() ) fIsBOF = true;
00173   else {
00174     fFileListItr = std::find(fFileList.begin(),fFileList.end(),curf);
00175     fIsBOF = false;
00176   }
00177   
00178   return;
00179 
00180 }
00181 
00182 //......................................................................
00183 
00184 const char* IoDataStreamItr::GetCurrentFile(const char* /* streamname */)const{
00185 //======================================================================
00186 // Return current fullfilepathname.
00187 //======================================================================
00188   std::string currentfilename;
00189   if ( !fIsBOF && fFileListItr != fFileList.end() ) 
00190     currentfilename = *fFileListItr;
00191   return currentfilename.c_str();
00192 }
00193  
00194 //......................................................................
00195 
00196 JobCResult IoDataStreamItr::NextFile(int n, const char* /* streamlist */) {
00197 //======================================================================
00198 // Move to the next file in the list (move by n positions)
00199 // Return JobCResult::kAOK if advance successful and file opened okay, or
00200 //        JobCResult::kEndOfInputStream, if at end of file list, or
00201 //        JobCResult error code returned from OpenFile() if file open failed.
00202 //======================================================================
00203 
00204   // If we've hit the end of the input stream, don't allow the user to
00205   // step further
00206   if ( fFileList.empty() || (!fIsBOF && fFileListItr == fFileList.end()) ) {
00207     return JobCResult::kEndOfInputStream;
00208   }
00209 
00210   this -> CloseFile();
00211   for ( int i = 0; i < n; i++ ) {
00212     if ( fIsBOF ) {
00213       fFileListItr = fFileList.begin();
00214       fIsBOF = false;
00215     }
00216     else fFileListItr++;
00217     
00218     // Flag attempts to read past the end of the file list
00219     if (fFileListItr == fFileList.end()) {
00220       // Flag that we've reached the end of the input stream
00221       return JobCResult::kEndOfInputStream;
00222     }
00223   }
00224 
00225   return this -> OpenFile(); // returns JobCResult::kAOK or error code
00226  
00227 }
00228 
00229 //......................................................................
00230 
00231 JobCResult IoDataStreamItr::GoToFile(int n, const char* /* streamlist */) {
00232 //======================================================================
00233 // Move the stream to the nth file in the list ( n = 0 is first )
00234 // Return JobCResult::kAOK if advance successful and file opened okay, or
00235 //        JobCResult::kWarning, if requested file number out of range, or
00236 //        JobCResult error code returned from OpenFile() if file open failed.
00237 //======================================================================
00238 
00239   std::list<std::string>::iterator itr(fFileList.begin());
00240   std::list<std::string>::iterator itrEnd(fFileList.end());
00241   int i = 0;
00242   for ( ; i < n && itr != itrEnd; ++i,++itr) { continue; }
00243 
00244   if ( i != n || itr == itrEnd ) {
00245     // Did not find file -- warn and take no action
00246     MSG("Io",Msg::kWarning) << 
00247       "Request to goto file ["<< n <<"]. Only " << i <<" files loaded."<< endl;
00248     return JobCResult::kWarning;
00249   }
00250 
00251   this -> CloseFile(); // Close the current file
00252   fFileListItr = itr; // Reset the current file pointer
00253   fIsBOF = false;
00254   
00255   return this -> OpenFile(); // returns JobCResult::kAOK or error code
00256  
00257 }
00258 
00259 //......................................................................
00260 
00261 JobCResult IoDataStreamItr::GoToFile(const char* fullfilepathname, 
00262                                      const char* /* streamlist */) {
00263 //======================================================================
00264 // Move the stream to file fullfilepathname in the list. If "", go to
00265 // first file in the list.
00266 // Return JobCResult::kAOK if advance successful and file opened okay, or
00267 //        JobCResult::kWarning, if requested file name not in list or
00268 //        JobCResult error code returned from OpenFile() if file open failed.
00269 //======================================================================
00270 
00271   std::string f(fullfilepathname);
00272 
00273   if (f == "") return this->GoToFile(0); // w/o argument reset to start of list
00274 
00275   std::list<std::string>::iterator itr = 
00276           std::find(fFileList.begin(), fFileList.end(), f);
00277 
00278   if ( itr != fFileList.end() ) {
00279     MSG("Io",Msg::kDebug) << "Go to file '" << f << "'." << endl;
00280     this -> CloseFile();
00281     fFileListItr = itr;
00282     fIsBOF = false;
00283     return this -> OpenFile(); // returns JobCResult::kAOK or error code   
00284   }
00285 
00286   MSG("Io",Msg::kWarning) << "File '" << f << "' not found in list." << endl;
00287   return JobCResult::kWarning;  // requested file not found 
00288 
00289 }
00290 
00291 //......................................................................
00292 
00293 JobCResult IoDataStreamItr::PrevFile(int n, const char* /* streamlist */) {
00294 //======================================================================
00295 // Rewind to the previous file in the list (move by n positions). 
00296 // Return JobCResult::kAOK if rewind successful and file opened okay, or
00297 //        JobCResult::kBeginOfInputStream, if at begin of file list, or
00298 //        JobCResult error code returned from OpenFile() if file open failed.
00299 //======================================================================
00300 
00301   // If we've hit the beginning of the input stream, don't allow the user 
00302   // to step further
00303   if ( fIsBOF ) {
00304     return JobCResult::kBeginOfInputStream;
00305   }
00306 
00307   this -> CloseFile();
00308   for ( int i = 0; i < n; i++ ) {
00309     if ( fFileListItr == fFileList.begin() ) fIsBOF = true;
00310     else fFileListItr--;
00311   
00312     // Flag attempts to read past the begin of the file list
00313     if ( fIsBOF ) {
00314       // Flag that we've reached the end of the input stream
00315       return JobCResult::kBeginOfInputStream;
00316     }
00317   }
00318 
00319   return this -> OpenFile(); // returns JobCResult::kAOK or error code   
00320 
00321 }
00322 
00323 std::ostream& IoDataStreamItr::ListFile(std::ostream& os, 
00324                                         const char* /* streamlist */) const {
00325 //======================================================================
00326 // Print the file list on ostream
00327 //======================================================================
00328 
00329   int indx = 0;
00330   MsgFormat ifmt("%3i");
00331 
00332   std::string s;
00333   s = "index  file name";
00334   os << s << endl;
00335   int len = s.size();
00336   s = "=====  ";
00337   os << s;
00338   for ( int i = s.size()+1; i < len; i++ ) os << "=";
00339   os << endl;
00340   
00341   std::list<std::string>::const_iterator itr(fFileList.begin());
00342   std::list<std::string>::const_iterator itrEnd(fFileList.end());
00343   for (; itr!=itrEnd; itr++) {
00344     os << "[" << ifmt(indx++) << "] ";
00345     if (itr == fFileListItr) os << "*";
00346     else os << " ";
00347     os << (*itr) << endl;
00348   }
00349 
00350   return os;
00351 
00352 }
00353 
00354 
00355 //......................................................................
00356 
00357 JobCResult IoDataStreamItr::OpenFile() {
00358 //======================================================================
00359 // Open current file. User must override this dummy def implementation. 
00360 // Return JobCResult::kAOK if success, else error message
00361 //======================================================================
00362 
00363   return JobCResult::kWarning;
00364 
00365 }
00366 
00367 //......................................................................
00368 
00369 void IoDataStreamItr::CloseFile() {
00370 //======================================================================
00371 // Close current file 
00372 // Return: none.
00373 //======================================================================
00374 
00375   return;
00376 
00377 }
00378 
00379 
00380 
00381 

Generated on Mon Feb 15 11:06:48 2010 for loon by  doxygen 1.3.9.1