33 if (isOpened())
return false;
35 if (fname==0 || *fname==0) {
36 fprintf(stderr,
"file name not specified\n");
42 fd = io->fopen(fname,
"w", opt);
44 fprintf(stderr,
"File open failed %s for writing\n", fname);
53 if(!WriteBuffer(&evnt,
sizeof(evnt))) {
65 if (isOpened())
return false;
67 if (fname==0 || *fname==0) {
68 fprintf(stderr,
"file name not specified\n");
74 fd = io->fopen(fname,
"r", opt);
76 fprintf(stderr,
"File open failed %s for reading\n", fname);
88 if (!ReadBuffer(&evnt, &size,
true)) {
89 fprintf(stderr,
"Cannot read starting event from file\n");
95 fprintf(stderr,
"Did not found start event at the file beginning\n");
110 DOUT3(
"hadaq::HldFile::Close()... ");
115 WriteBuffer(&evnt,
sizeof(evnt));
128 if (!isWriting() || (buf==0) || (bufsize==0))
return false;
130 if (io->fwrite(buf, bufsize, 1, fd)!=1) {
131 fprintf(stderr,
"fail to write buffer payload of size %u\n", (
unsigned) bufsize);
141 if (!isReading() || (ptr==0) || (sz==0) || (*sz <
sizeof(
hadaq::HadTu)))
return false;
143 uint64_t maxsz = *sz; *sz = 0;
145 size_t readsz = io->fread(ptr, 1, (onlyevent ?
sizeof(
hadaq::HadTu) : maxsz), fd);
150 if (!io->feof(fd)) fprintf(stderr,
"Fail to read next portion while no EOF detected\n");
161 fprintf(stderr,
"Buffer %u too small to read next event %u from hld file\n", (
unsigned) maxsz, (
unsigned)hdr->
GetPaddedSize());
173 fprintf(stderr,
"Reading problem\n");
191 size_t checkedsz = 0;
193 while (checkedsz < readsz) {
197 size_t restsize = readsz - checkedsz;
210 bool not_enough_place_for_next_event = (checkedsz + restsize) > readsz;
212 if (not_enough_place_for_next_event || (onlyevent && (checkedsz>0))) {
214 if (not_enough_place_for_next_event && (readsz<maxsz)) fEOF =
true;
217 io->fseek(fd, -(readsz - checkedsz),
true);
226 if ((readsz<maxsz) && (checkedsz == readsz) && !fEOF) fEOF =
true;
bool ReadBuffer(void *ptr, uint32_t *bufsize, bool onlyevent=false)
Read one or several elements to provided user buffer When called, bufsize should has available buffer...
bool WriteBuffer(void *buf, uint32_t bufsize)
Write user buffer to file without reformatting User must be aware about correct formatting of data.
bool OpenRead(const char *fname, const char *opt=0)
Opened file for reading.
HldFile()
flag indicate that end-of-file was reached
bool OpenWrite(const char *fname, uint32_t rid=0, const char *opt=0)
Open file with specified name for writing.
HADES transport unit header.
uint32_t GetPaddedSize() const
void Init(uint32_t evnt, uint32_t run=0, uint32_t id=EvtId_DABC)