File indexing completed on 2025-08-03 08:20:45
0001
0002
0003
0004
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
0016
0017
0018 #include "fileEventiterator.h"
0019
0020
0021 #include "lzobuffer.h"
0022
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
0104 return "fileEventiterator";
0105 };
0106
0107
0108
0109
0110
0111
0112 Event * fileEventiterator::getNextEvent()
0113 {
0114 if ( _defunct ) return 0;
0115 Event *evt = 0;
0116
0117
0118 if (last_read_status) return NULL;
0119
0120
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
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
0147
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
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172 char *cp = (char *) initialbuffer;
0173
0174 unsigned int xc;
0175
0176
0177
0178
0179
0180 while (buffer_size == 0 )
0181 {
0182
0183 xc = read ( fd, cp, BUFFERBLOCKSIZE);
0184
0185
0186 if ( xc < BUFFERBLOCKSIZE )
0187 {
0188
0189 return -1;
0190 }
0191
0192
0193
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
0224 if (buffer_size > allocatedsize*4)
0225 {
0226 delete [] bp;
0227 i = (buffer_size +BUFFERBLOCKSIZE-1) /BUFFERBLOCKSIZE;
0228 allocatedsize = i * BUFFERBLOCKSIZE/4;
0229 bp = new PHDWORD[allocatedsize];
0230
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
0242 memcpy ( bp, initialbuffer, BUFFERBLOCKSIZE);
0243
0244 cp = (char *) bp;
0245
0246
0247 cp += BUFFERBLOCKSIZE;
0248
0249 PHDWORD read_so_far = BUFFERBLOCKSIZE;
0250
0251 int errorinread=0;
0252
0253
0254
0255
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
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