Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 //
0002 // fileEventIterator   mlp 4/19/1997
0003 //
0004 // this iterator reads events froma data file. 
0005 
0006 
0007 #include <stddef.h>
0008 #include <string.h>
0009 
0010 #include <unistd.h>
0011 #include <sys/types.h>
0012 #include <sys/stat.h>
0013 #include <fcntl.h>
0014 
0015 // there are two similar constructors, one with just the
0016 // filename, the other with an additional status value
0017 // which is non-zero on return if anything goes wrong. 
0018 #include "fileEventiterator.h"
0019 
0020 //#ifndef LVL2_WINNT
0021 #include "lzobuffer.h"
0022 //#endif
0023 
0024 
0025 fileEventiterator::~fileEventiterator()
0026 {
0027      if (fd) close (fd);
0028      if (thefilename != NULL) delete [] thefilename;
0029      if (bp != NULL ) delete [] bp;
0030      if (bptr != NULL ) delete bptr;
0031 }  
0032 
0033 
0034 fileEventiterator::fileEventiterator(const char *filename)
0035 {
0036   open_file ( filename);
0037 }  
0038 
0039 fileEventiterator::fileEventiterator(const char *filename, int &status)
0040 {
0041   status =  open_file ( filename);
0042 }
0043 
0044 
0045 int fileEventiterator::open_file(const char *filename)
0046 {
0047   fd  = open (filename, O_RDONLY | O_LARGEFILE);
0048   bptr = 0;
0049   bp = 0;
0050   allocatedsize = 0;
0051   thefilename = NULL;
0052   events_so_far = 0;
0053   verbosity=0;
0054   _defunct = 0;
0055 
0056   if (fd > 0) 
0057     {
0058       thefilename = new char[strlen(filename)+1];
0059       strcpy (thefilename, filename);
0060       last_read_status = 0;
0061       current_index = 0;
0062       return 0;
0063     }
0064   else
0065     {
0066       last_read_status = 1;
0067       _defunct = 1;
0068     }
0069   return 1;
0070 
0071 }
0072 
0073 
0074 
0075 void fileEventiterator::identify (OSTREAM &os) const
0076 { 
0077   os << "fileEventiterator reading from " << thefilename;
0078   if ( _defunct ) os << " *** defunct";
0079   os<< std::endl;
0080 
0081 };
0082 
0083 
0084 const char * fileEventiterator::getCurrentFileName() const
0085 { 
0086   static char namestr[512];
0087   if ( thefilename == NULL)
0088     {
0089       return " ";
0090     }
0091   else
0092     {
0093       strcpy (namestr, thefilename);
0094       return namestr;
0095     }
0096 };
0097 
0098 
0099 
0100 
0101 const char *  fileEventiterator::getIdTag () const
0102 { 
0103   //  sprintf (idline, " -- fileEventiterator reading from %s", thefilename);
0104   return "fileEventiterator";
0105 };
0106 
0107 
0108 
0109 // and, finally, the only non-constructor member function to
0110 // retrieve events from the iterator.
0111 
0112 Event * fileEventiterator::getNextEvent()
0113 {
0114   if ( _defunct ) return 0;
0115   Event *evt = 0;
0116 
0117   // if we had a read error before, we just return
0118   if (last_read_status) return NULL;
0119 
0120   // see if we have a buffer to read
0121   if (bptr == 0) 
0122     {
0123       if ( (last_read_status = read_next_buffer()) !=0 )
0124     {
0125       return NULL;
0126     }
0127     }
0128 
0129   while (last_read_status == 0)
0130     {
0131       if (bptr) evt =  bptr->getEvent();
0132       if (evt) 
0133     {
0134       //evt->setOriginBuffer(bptr->getBufferSequence());
0135       events_so_far++;
0136       return evt;
0137     }
0138       last_read_status = read_next_buffer();
0139     }
0140 
0141   return NULL;
0142 
0143 }
0144 
0145 // -----------------------------------------------------
0146 // this is a private function to read the next buffer
0147 // if needed. 
0148 
0149 int fileEventiterator::read_next_buffer()
0150 {
0151   PHDWORD initialbuffer[BUFFERBLOCKSIZE/4];
0152 
0153   buffer_size = 0;
0154   
0155   if (bptr) 
0156     {
0157       delete bptr;
0158       bptr = 0;
0159     }
0160   events_so_far = 0;
0161     
0162   // we are now reading the first block (8192 bytes) into
0163   // initialbuffer. The buffer is at least that long. We
0164   // look up the buffersize etc from that, and 
0165   // start filling the actual buffer. We do not read 
0166   // the data into the eventual destination since we may 
0167   // have to delete it to get more room (we might find that
0168   // the buffer is larger than what we have allocated).
0169 
0170 
0171   // set the pointer to char to the destination buffer
0172   char *cp = (char *) initialbuffer;
0173 
0174   unsigned int xc;
0175 
0176 
0177   // this while loop implements the skipping of 8k records until
0178   // we find a valid buffer marker. (We usually find it right away). 
0179  
0180   while (buffer_size == 0 )
0181     {  
0182       // read the first record
0183       xc = read ( fd, cp, BUFFERBLOCKSIZE);
0184       
0185       // error of EoF?
0186       if ( xc < BUFFERBLOCKSIZE  ) 
0187     {
0188       //      COUT << "ferror" << std::endl;
0189       return -1;
0190     }
0191 
0192 
0193       // get the buffer length into a dedicated variable
0194       if (initialbuffer[1] == BUFFERMARKER || initialbuffer[1]== GZBUFFERMARKER 
0195       ||  initialbuffer[1]== LZO1XBUFFERMARKER 
0196       || initialbuffer[1]== LZO1CBUFFERMARKER 
0197       || initialbuffer[1]== LZO2ABUFFERMARKER 
0198       || initialbuffer[1]== BZ2BUFFERMARKER 
0199       || initialbuffer[1]== ONCSBUFFERMARKER) 
0200     {
0201       buffer_size = initialbuffer[0];
0202     }
0203       else
0204     {
0205       unsigned int  marker = buffer::u4swap(initialbuffer[1]);
0206       if (marker == BUFFERMARKER 
0207           || marker == GZBUFFERMARKER 
0208           || marker ==  LZO1XBUFFERMARKER 
0209           || marker ==  LZO1CBUFFERMARKER 
0210           || marker ==  LZO2ABUFFERMARKER 
0211           || marker ==  BZ2BUFFERMARKER 
0212           || marker == ONCSBUFFERMARKER)
0213         {
0214           buffer_size = buffer::u4swap(initialbuffer[0]);
0215         }
0216     }
0217     }
0218 
0219 
0220   int i;
0221   if (bp) 
0222     {
0223       // this tests if we have enough space in the existing buffer
0224       if  (buffer_size > allocatedsize*4)   // no, we delete and make a bigger one
0225     {
0226       delete [] bp;
0227       i = (buffer_size +BUFFERBLOCKSIZE-1) /BUFFERBLOCKSIZE;
0228       allocatedsize = i * BUFFERBLOCKSIZE/4;
0229       bp = new PHDWORD[allocatedsize];
0230       //  std::cout << __FILE__ << "  " << __LINE__ << " new bp pointer is " << bp << "  length value "  << bp[-1]<< std::endl;
0231     }
0232     }
0233   else
0234     {
0235       i = (buffer_size +BUFFERBLOCKSIZE-1) /BUFFERBLOCKSIZE;
0236       allocatedsize = i * BUFFERBLOCKSIZE/4;
0237       bp = new PHDWORD[allocatedsize];
0238 
0239     }
0240 
0241   //  for (i = 0; i<BUFFERBLOCKSIZE/4; i++ ) bp[i] = initialbuffer[i];
0242   memcpy ( bp, initialbuffer, BUFFERBLOCKSIZE);
0243  
0244   cp = (char *) bp;
0245 
0246   // and update the destination buffer pointer
0247   cp += BUFFERBLOCKSIZE;
0248 
0249   PHDWORD read_so_far =  BUFFERBLOCKSIZE;
0250 
0251   int errorinread=0;
0252 
0253   // we calculate how many BUFFERBLOCKSIZE-sized records we need to read
0254   // we have already one, so this is the number of records -1.
0255   // normally we would round up  (buffer_size + BUFFERBLOCKSIZE -1) /BUFFERBLOCKSIZE
0256   int records_to_read =  (buffer_size -1) /BUFFERBLOCKSIZE;
0257   unsigned int bytes_to_read   =  records_to_read * BUFFERBLOCKSIZE;
0258   
0259   xc = read ( fd, cp, bytes_to_read);
0260   if ( xc < bytes_to_read ) 
0261     {
0262       COUT << "error in buffer, salvaging" << std::endl;
0263       bp[0] = read_so_far + xc; 
0264       errorinread =1;
0265     }
0266 
0267   // and initialize the current_index to be the first event
0268 
0269   if ( ( initialbuffer[1]== GZBUFFERMARKER 
0270      || buffer::u4swap(initialbuffer[1])== GZBUFFERMARKER 
0271      || initialbuffer[1]== LZO1XBUFFERMARKER 
0272      || buffer::u4swap(initialbuffer[1])== LZO1XBUFFERMARKER 
0273      || initialbuffer[1]== LZO1CBUFFERMARKER 
0274      || buffer::u4swap(initialbuffer[1])== LZO1CBUFFERMARKER
0275      || initialbuffer[1]== LZO2ABUFFERMARKER 
0276      || buffer::u4swap(initialbuffer[1])== LZO2ABUFFERMARKER )
0277        && errorinread  )
0278     {
0279       bptr = 0;
0280       return -3;
0281     }
0282 
0283   return buffer::makeBuffer( bp, allocatedsize, &bptr);
0284 
0285 
0286 }
0287