Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:20:34

0001 //
0002 // listEventIterator   mlp 4/19/1997
0003 //
0004 // this iterator reads events froma data file. 
0005 
0006 
0007 #include "listEventiterator.h"
0008 #include <stddef.h>
0009 #include <string.h>
0010 
0011 // there are two similar constructors, one with just the
0012 // filename, the other with an additional status value
0013 // which is non-zero on return if anything goes wrong. 
0014 
0015 
0016 listEventiterator::~listEventiterator()
0017 {
0018      if (it) delete it;
0019      if (liststream) delete liststream;
0020      if ( listfilename ) delete [] listfilename;
0021 }  
0022 
0023 
0024 listEventiterator::listEventiterator(const char *filename)
0025 {
0026   listfilename = new char[strlen(filename)+1];
0027   strcpy (listfilename, filename);
0028 
0029   liststream = new std::ifstream(filename);
0030   finished = 0;
0031   defunct = 0;
0032 #ifdef OSF1
0033   if (liststream) 
0034 #else
0035   if (!liststream->is_open() ) 
0036 #endif
0037     {
0038       defunct = 1;
0039       finished = 1;
0040     }
0041   it = 0;
0042   strcpy(thefilename, " ");
0043 }
0044 
0045 
0046 listEventiterator::listEventiterator(const char *filename, int &status)
0047 {
0048   listfilename = new char[strlen(filename)+1];
0049   strcpy (listfilename, filename);
0050 
0051   liststream = new std::ifstream(filename);
0052   finished = 0;
0053   defunct = 0;
0054   status = 0;
0055 #ifdef OSF1
0056   if (liststream) 
0057 #else
0058   if (!liststream->is_open() ) 
0059 #endif
0060     {
0061       status = 1;
0062       defunct = 1;
0063       finished = 1;
0064     }
0065   it = 0;
0066   strcpy(thefilename, " ");
0067 }
0068 
0069 
0070 
0071 void listEventiterator::identify (OSTREAM &os) const
0072 { 
0073   os << "listEventiterator from list " << listfilename << ", current from " << thefilename << std::endl;
0074 
0075 };
0076 
0077 
0078 const char * listEventiterator::getCurrentFileName() const
0079 { 
0080   static char namestr[512];
0081 
0082   strcpy (namestr, thefilename);
0083   return namestr;
0084  
0085 };
0086 
0087 
0088 const char *  listEventiterator::getIdTag () const
0089 { 
0090   //  sprintf (idline, " -- listEventiterator reading from %s", thefilename);
0091   return "listEventiterator";
0092 };
0093 
0094 
0095 
0096 // and, finally, the only non-constructor member function to
0097 // retrieve events from the iterator.
0098 
0099 Eventiterator * listEventiterator::getNextIterator()
0100 {
0101 
0102   Eventiterator *fit;
0103   int status = 0;
0104 
0105   if (finished) return 0;
0106 
0107   if (it) return it; //protect against overwriting our own.
0108 
0109   while ( liststream->getline(thefilename,FEMAXFILENAMELENGTH) )
0110     {
0111       fit = new fileEventiterator(thefilename,status);
0112       if (! status)
0113     {
0114       std::cout << "opened file " << thefilename << std::endl;
0115       return fit;
0116     }
0117     }
0118   finished = 1;
0119   delete liststream;
0120   liststream = 0;
0121   return 0;
0122 }
0123         
0124 
0125 Event * listEventiterator::getNextEvent()
0126 {
0127   while ( ! finished)   // we still have more file to process
0128     {
0129       if (it ==0 )      // if we don't have one open...
0130     {
0131       it = getNextIterator();     // try to open it
0132       if (! it)                   // no more there, done
0133         {
0134           return 0;
0135         }
0136     }
0137       Event *e = it->getNextEvent();  // get the nexct event
0138       if (e) return e;                // and if we got it, we're done
0139       delete it;                      // if that prdf was exhausted, try the next one
0140       it = 0;
0141     }
0142   return 0;
0143 }