23 printf(
"TMReaderInterface::ctor!\n");
31 snprintf(buf,
sizeof(buf),
"%d", v);
35static std::string
Errno(
const char* s)
39 r +=
" failed: errno: ";
47static int ReadPipe(FILE *fp,
char* buf,
int length)
51 int rd = fread(buf, 1, length, fp);
71 printf(
"ErrorReader::ctor!\n");
73 fErrorString =
"The ErrorReader always returns an error";
79 printf(
"ErrorReader::dtor!\n");
82 int Read(
void* buf,
int count)
100 printf(
"FileReader::ctor!\n");
102 fFp = fopen(filename,
"r");
112 printf(
"FileReader::dtor!\n");
133 printf(
"FileReader::Close!\n");
151 printf(
"PipeReader::ctor!\n");
153 fPipe = popen(pipename,
"r");
163 printf(
"PipeReader::dtor!\n");
172 assert(
fPipe != NULL);
184 printf(
"PipeReader::Close!\n");
204 printf(
"ZlibReader::ctor!\n");
206 fGzFile = gzopen(filename,
"rb");
216 printf(
"PipeReader::dtor!\n");
226 int rd = gzread(
fGzFile, buf, count);
237 printf(
"ZlibReader::Close!\n");
277 printf(
"Lz4Reader::ctor!\n");
282 fErrorString =
"MLZ4F_createDecompressionContext() error ";
296 printf(
"Lz4Reader::dtor!\n");
306 fErrorString =
"MLZ4F_freeDecompressionContext() error ";
326 char* cptr = (
char*)buf;
329 while (clen < count) {
330 int more = count - clen;
344 }
else if (rd == 0) {
356 size_t dst_size = more;
370 assert(dst_size!=0 || src_size!=0);
389 printf(
"Lz4Reader::Close!\n");
416 fFp = fopen(filename,
"w");
420 int Write(
const void* buf,
int count)
423 return fwrite(buf, 1, count,
fFp);
440 const char* s = strstr(name,suffix);
444 return (s-name)+strlen(suffix) == strlen(name);
450 signal(SIGPIPE, SIG_IGN);
453 signal(SIGXFSZ, SIG_IGN);
459 else if (strncmp(
source,
"ssh://", 6) == 0)
461 const char* name =
source + 6;
462 const char* s = strstr(name,
"/");
467 e->
fErrorString =
"TMidasFile::Open: Invalid ssh:// URI. Should be: ssh://user@host/file/path/...";
471 const char* remoteFile = s + 1;
473 std::string remoteHost;
474 for (s=name; *s !=
'/'; s++)
479 pipe =
"ssh -e none -T -x -n ";
486 pipe +=
" | gzip -dc";
488 pipe +=
" | bzip2 -dc";
494 else if (strncmp(
source,
"dccp://", 7) == 0)
496 const char* name =
source + 7;
502 pipe +=
" /dev/fd/1";
505 pipe +=
" | gzip -dc";
507 pipe +=
" | bzip2 -dc";
513 else if (strncmp(
source,
"pipein://", 9) == 0)
515 std::string pipe =
source + 9;
561 return *(uint16_t*)ptr;
566 return *(uint32_t*)ptr;
582 return (size + 7) & ~7;
590 assert(
sizeof(
char)==1);
591 assert(
sizeof(uint16_t)==2);
592 assert(
sizeof(uint32_t)==4);
599 const int event_header_size = 4*4;
600 char event_header[event_header_size];
602 int rd = reader->
Read(event_header, event_header_size);
607 }
else if (rd == 0) {
610 }
else if (rd != event_header_size) {
611 fprintf(stderr,
"TMReadEvent: error: read %d shorter than event header size %d\n", (
int)rd, (
int)event_header_size);
633 e->
data.resize(event_header_size + to_read);
635 memcpy(&e->
data[0], event_header, event_header_size);
637 rd = reader->
Read(&e->
data[event_header_size], to_read);
642 }
else if (rd != (
int)to_read) {
643 fprintf(stderr,
"TMReadEvent: error: short read %d instead of %d\n", (
int)rd, (
int)to_read);
659 snprintf(buf,
sizeof(buf),
"event: id %d, mask 0x%04x, serial %d, time %d, size %d, error %d, banks %d",
667 for (
unsigned i=0; i<
banks.size(); i++) {
678 snprintf(buf,
sizeof(buf),
"name \"%s\", type %d, size %d, offset %d\n",
713 fprintf(stderr,
"TMEvent::ParseEvent: error: vector size %d mismatch against event size in event header: data_size %d, event_size %d\n", (
int)
data.size(), (
int)
data_size, (
int)(
event_header_size +
data_size));
722 const char* event_header = (
const char*)buf;
725 fprintf(stderr,
"TMEvent::ctor: error: buffer size %d is smaller than event header size %d\n", (
int)buf_size, (
int)
event_header_size);
749 assert(
sizeof(
char)==1);
750 assert(
sizeof(uint16_t)==2);
751 assert(
sizeof(uint32_t)==4);
764 if (zevent_size != buf_size) {
765 fprintf(stderr,
"TMEvent::ctor: error: buffer size %d mismatch against event size in event header: data_size %d, event_size %d\n", (
int)buf_size, (
int)zdata_size, (
int)zevent_size);
773 const char* cptr = (
const char*)buf;
775 data.assign(cptr, cptr + zevent_size);
777 assert(
data.size() == zevent_size);
780void TMEvent::Init(uint16_t xevent_id, uint16_t xtrigger_mask, uint32_t xserial_number, uint32_t xtime_stamp,
size_t capacity)
808 data.reserve(capacity);
814 char* event_header =
data.data();
821 PutU32(event_header+16, 0);
827 assert(
data.size() > 0);
831 size_t end_of_event_offset =
data.size();
833 data.resize(end_of_event_offset + bank_size);
835 char* pbank =
data.data() + end_of_event_offset;
839 pbank[0] = bank_name[0];
840 pbank[1] = bank_name[1];
841 pbank[2] = bank_name[2];
842 pbank[3] = bank_name[3];
847 memcpy(pbank+4*4, buf, size);
853 char* event_header =
data.data();
876 if (e->
data.size() < off + 8) {
877 fprintf(stderr,
"TMEvent::FindFirstBank: error: data size %d is too small\n", (
int)e->
data.size());
882 uint32_t bank_header_data_size =
GetU32(&e->
data[off]);
883 uint32_t bank_header_flags =
GetU32(&e->
data[off+4]);
887 if (bank_header_data_size + 8 != e->
data_size) {
888 fprintf(stderr,
"TMEvent::FindFirstBank: error: bank header size %d mismatch against data size %d\n", (
int)bank_header_data_size, (
int)e->
data_size);
899static char xchar(
char c)
901 if (c>=
'0' && c<=
'9')
903 if (c>=
'a' && c<=
'z')
905 if (c>=
'A' && c<=
'Z')
916 size_t remaining = e->
data.size() - pos;
920 if (remaining == 0) {
927 fprintf(stderr,
"TMEvent::FindNextBank: error: too few bytes %d remaining at the end of event\n", (
int)remaining);
933 size_t ibank = e->
banks.size();
934 e->
banks.resize(ibank+1);
945 size_t data_offset = 0;
953 data_offset = pos+4+4+4+4;
958 data_offset = pos+4+4+4;
963 data_offset = pos+4+2+2;
971 fprintf(stderr,
"TMEvent::FindNextBank: error: invalid tid %d\n", b->
type);
980 size_t npos = data_offset + aligned_data_size;
984 if (npos > e->
data.size()) {
985 fprintf(stderr,
"TMEvent::FindNextBank: error: invalid bank data size %d: aligned %d, npos %d, end of event %d\n", b->
data_size, (
int)aligned_data_size, (
int)npos, (
int)e->
data.size());
1056 for (
unsigned i=0; i<
banks.size(); i++) {
1057 if (
banks[i].name == bank_name)
1078 if (pos>0 && b && bank_name) {
1079 if (b->
name == bank_name)
1101 printf(
"Event: id 0x%04x, mask 0x%04x, serial 0x%08x, time 0x%08x, data size %d, vector size %d, capacity %d\n",
1108 (
int)
data.capacity()
1116 printf(
"%d banks:", (
int)
banks.size());
1117 for (
size_t i=0; i<
banks.size(); i++) {
1118 printf(
" %s",
banks[i].name.c_str());
1123 for (
size_t i=0; i<
banks.size(); i++) {
1124 printf(
"bank %3d: name \"%s\", tid %2d, data_size %8d\n",
1126 banks[i].name.c_str(),
1128 banks[i].data_size);
1132 for (
size_t j=0; j<
banks[i].data_size; j+=4) {
1133 printf(
"%11d: 0x%08x\n", (
int)j, *(uint32_t*)(p+j));
1143 const char* p =
data.data();
1146 printf(
" 0: 0x%08x\n",
GetU32(p+0*4));
1147 printf(
" 1: 0x%08x\n",
GetU32(p+1*4));
1148 printf(
" 2: 0x%08x\n",
GetU32(p+2*4));
1149 printf(
" 3: 0x%08x (0x%08x and 0x%08x-0x10)\n",
GetU32(p+3*4),
data_size, (
int)
data.size());
1152 printf(
" 4: 0x%08x\n",
GetU32(p+4*4));
ErrorReader(const char *filename)
int Read(void *buf, int count)
FileReader(const char *filename)
int Read(void *buf, int count)
int Write(const void *buf, int count)
FileWriter(const char *filename)
Lz4Reader(TMReaderInterface *reader)
MLZ4F_decompressionContext_t fContext
TMReaderInterface * fReader
void AllocSrcBuf(int size)
int Read(void *buf, int count)
int Read(void *buf, int count)
PipeReader(const char *pipename)
uint32_t type
type of bank data, enum of TID_xxx
uint32_t data_size
size of bank data in bytes
size_t data_offset
offset of data for this bank in the event data[] container
std::string name
bank name, 4 characters max
std::string BankListToString() const
print the list of MIDAS banks
void FindAllBanks()
scan the MIDAS event, find all data banks
std::string HeaderToString() const
print the MIDAS event header
TMBank * FindBank(const char *bank_name)
scan the MIDAS event
bool found_all_banks
all the banks in the event data have been discovered
std::vector< TMBank > banks
list of MIDAS banks, fill using FindAllBanks()
void Reset()
reset everything
void PrintBanks(int level=0)
void AddBank(const char *bank_name, int tid, const char *buf, size_t size)
add new MIDAS bank
char * GetEventData()
get pointer to MIDAS event data
void Init(uint16_t event_id, uint16_t trigger_mask=0, uint32_t serial_number=0, uint32_t time_stamp=0, size_t capacity=0)
uint32_t time_stamp
MIDAS event time stamp (unix time in sec)
size_t bank_scan_position
location where scan for MIDAS banks was last stopped
uint32_t bank_header_flags
flags from the MIDAS event bank header
void ParseHeader(const void *buf, size_t buf_size)
parse event header
void ParseEvent()
parse event data
std::vector< char > data
MIDAS event bytes.
size_t event_header_size
size of MIDAS event header
uint32_t serial_number
MIDAS event serial number.
uint32_t data_size
MIDAS event data size.
uint16_t trigger_mask
MIDAS trigger mask.
uint16_t event_id
MIDAS event ID.
char * GetBankData(const TMBank *)
get pointer to MIDAS data bank
bool error
event has an error - incomplete, truncated, inconsistent or corrupted
std::string BankToString(const TMBank *) const
print definition of one MIDAS bank
virtual int Read(void *buf, int count)=0
virtual int Write(const void *buf, int count)=0
int Read(void *buf, int count)
ZlibReader(const char *filename)
MLZ4F_errorCode_t MLZ4F_freeDecompressionContext(MLZ4F_decompressionContext_t dctx)
const char * MLZ4F_getErrorName(MLZ4F_errorCode_t code)
size_t MLZ4F_decompress(MLZ4F_decompressionContext_t dctx, void *dstBuffer, size_t *dstSizePtr, const void *srcBuffer, size_t *srcSizePtr, const MLZ4F_decompressOptions_t *dOptPtr)
unsigned MLZ4F_isError(MLZ4F_errorCode_t code)
MLZ4F_errorCode_t MLZ4F_createDecompressionContext(MLZ4F_decompressionContext_t *dctxPtr, unsigned version)
static size_t FindFirstBank(TMEvent *e)
static int ReadPipe(FILE *fp, char *buf, int length)
void TMWriteEvent(TMWriterInterface *writer, const TMEvent *event)
static std::string to_string(int v)
static std::string Lz4Error(int errorCode)
static void PutU32(char *ptr, uint32_t v)
static uint32_t GetU32(const void *ptr)
static size_t FindNextBank(TMEvent *e, size_t pos, TMBank **pb)
static void PutU16(char *ptr, uint16_t v)
TMWriterInterface * TMNewWriter(const char *destination)
static std::string Errno(const char *s)
static int hasSuffix(const char *name, const char *suffix)
TMEvent * TMReadEvent(TMReaderInterface *reader)
static uint16_t GetU16(const void *ptr)
TMReaderInterface * TMNewReader(const char *source)
static size_t Align8(size_t size)