File indexing completed on 2025-08-03 08:20:35
0001
0002
0003
0004
0005
0006
0007
0008 #include "oncsEventiterator.h"
0009 #include <stdio.h>
0010 #include "oncsEvent.h"
0011 #include <stddef.h>
0012 #include <string.h>
0013 #include <sys/types.h>
0014 #include <sys/stat.h>
0015 #include <fcntl.h>
0016 #include <unistd.h>
0017
0018
0019
0020
0021
0022
0023
0024 oncsEventiterator::~oncsEventiterator()
0025 {
0026 if (fd) close (fd);
0027 if (thefilename != NULL) delete [] thefilename;
0028 if (bp != NULL ) delete [] bp;
0029 if (bptr != NULL ) delete bptr;
0030 }
0031
0032
0033 oncsEventiterator::oncsEventiterator(const char *filename)
0034 {
0035 fd = open (filename, O_RDONLY | O_LARGEFILE);
0036 bptr = 0;
0037 bp = 0;
0038 allocatedsize = 0;
0039 if (fd > 0)
0040 {
0041 thefilename = new char[strlen(filename)+1];
0042 strcpy (thefilename, filename);
0043 last_read_status = 0;
0044 current_index = 0;
0045 }
0046 else
0047 last_read_status = 1;
0048
0049 }
0050
0051 oncsEventiterator::oncsEventiterator(const char *filename, int &status)
0052 {
0053 fd = open (filename, O_RDONLY | O_LARGEFILE);
0054 bptr = 0;
0055 bp = 0;
0056 allocatedsize = 0;
0057 if (fd >0 )
0058 {
0059 thefilename = new char[strlen(filename)+1];
0060 strcpy (thefilename, filename);
0061 status = 0;
0062 last_read_status = 0;
0063 current_index = 0;
0064 }
0065 else
0066 {
0067 status = 1;
0068 last_read_status = 1;
0069 }
0070 }
0071
0072 void
0073 oncsEventiterator::identify (OSTREAM &os) const
0074 {
0075 os << getIdTag() << std::endl;
0076
0077 };
0078
0079 const char * oncsEventiterator::getIdTag () const
0080 {
0081 static char line[180];
0082 strcpy (line, " -- oncsEventiterator reading from ");
0083 strcat (line, thefilename);
0084 return line;
0085 };
0086
0087
0088
0089
0090
0091 Event * oncsEventiterator::getNextEvent()
0092 {
0093 Event *evt = 0;
0094
0095
0096
0097 if (last_read_status) return NULL;
0098
0099
0100 if (bptr == 0)
0101 {
0102 if ( (last_read_status = read_next_buffer()) !=0 )
0103 {
0104 return NULL;
0105 }
0106 }
0107
0108 while (last_read_status == 0)
0109 {
0110 if (bptr) evt = bptr->getEvent();
0111 if (evt) return evt;
0112
0113 last_read_status = read_next_buffer();
0114 }
0115
0116 return NULL;
0117
0118 }
0119
0120
0121
0122
0123
0124 int oncsEventiterator::read_next_buffer()
0125 {
0126 if (bptr)
0127 {
0128 delete bptr;
0129 bptr = 0;
0130 }
0131
0132
0133
0134
0135 char *cp = (char *) initialbuffer;
0136
0137 unsigned int xc;
0138
0139
0140 xc = read ( fd, cp, 8192);
0141
0142
0143 if ( xc < 8192 ) return -1;
0144
0145
0146 if (initialbuffer[1] == ONCSBUFFERID || initialbuffer[1] == PRDFBUFFERID )
0147 {
0148 buffer_size = initialbuffer[0];
0149 }
0150 else
0151 {
0152 unsigned int id = oncsBuffer::i4swap(initialbuffer[1]);
0153 if (id == ONCSBUFFERID || id == PRDFBUFFERID )
0154 {
0155 buffer_size = oncsBuffer::i4swap(initialbuffer[0]);
0156 }
0157 else
0158 {
0159 return 1;
0160 }
0161 }
0162 int i;
0163 if (bp)
0164 {
0165 if (buffer_size > allocatedsize*4)
0166 {
0167 delete [] bp;
0168 i = (buffer_size + BUFFERBLOCKSIZE -1) /BUFFERBLOCKSIZE;
0169 allocatedsize = i * 2048;
0170 bp = new int[allocatedsize];
0171 }
0172 }
0173 else
0174 {
0175 i = (buffer_size + BUFFERBLOCKSIZE) /BUFFERBLOCKSIZE;
0176 allocatedsize = i * BUFFERBLOCKSIZE/4;
0177 bp = new int[allocatedsize];
0178 }
0179
0180
0181 memcpy ( bp, initialbuffer, BUFFERBLOCKSIZE);
0182
0183 unsigned int read_so_far = BUFFERBLOCKSIZE;
0184
0185 cp = (char *) bp;
0186
0187
0188 cp += BUFFERBLOCKSIZE;
0189
0190
0191
0192
0193 int records_to_read = (buffer_size -1) /BUFFERBLOCKSIZE;
0194 unsigned int bytes_to_read = records_to_read * BUFFERBLOCKSIZE;
0195
0196 xc = read ( fd, cp, bytes_to_read);
0197 if ( xc < bytes_to_read )
0198 {
0199 COUT << "error in buffer, salvaging" << std::endl;
0200 bp[0] = read_so_far + xc;
0201 }
0202
0203
0204 bptr = new oncsBuffer ( (PHDWORD *) bp, (PHDWORD) allocatedsize );
0205 return 0;
0206 }
0207