DABC (Data Acquisition Backbone Core)  2.9.9
BinaryFile.h
Go to the documentation of this file.
1 // $Id: BinaryFile.h 4464 2020-04-15 12:14:20Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #ifndef DABC_BinaryFile
17 #define DABC_BinaryFile
18 
19 #include <cstdint>
20 #include <cstdio>
21 
22 namespace dabc {
23 
24  class Object;
25 
33  class FileInterface {
34  public:
35 
37  typedef void* Handle;
38 
39  virtual ~FileInterface() {}
40 
41  virtual Handle fopen(const char* fname, const char* mode, const char* = 0) { return (Handle) ::fopen(fname, mode); }
42 
43  virtual void fclose(Handle f) { if (f!=0) ::fclose((FILE*)f); }
44 
45  virtual size_t fwrite(const void* ptr, size_t sz, size_t nmemb, Handle f)
46  { return ((f==0) || (ptr==0)) ? 0 : ::fwrite(ptr, sz, nmemb, (FILE*) f); }
47 
48  virtual size_t fread(void* ptr, size_t sz, size_t nmemb, Handle f)
49  { return ((f==0) || (ptr==0)) ? 0 : ::fread(ptr, sz, nmemb, (FILE*) f); }
50 
51  virtual bool feof(Handle f)
52  { return f==0 ? false : ::feof((FILE*)f)>0; }
53 
54  virtual bool fflush(Handle f)
55  { return f==0 ? false : ::fflush((FILE*)f)==0; }
56 
57  virtual bool fseek(Handle f, long int offset, bool relative = true)
58  { return f==0 ? false : ::fseek((FILE*)f, offset, relative ? SEEK_CUR : SEEK_SET) == 0; }
59 
62  virtual Object* fmatch(const char* fmask, bool select_files = true);
63 
64  virtual bool mkdir(const char* path);
65 
67  virtual int GetFileIntPar(Handle h, const char* parname) { return 0; }
68 
70  virtual bool GetFileStrPar(Handle h, const char* parname, char* sbuf, int sbuflen) { if (sbuf) *sbuf = 0; return false; }
71 
72  };
73 
74  // ==============================================================================
75 
84  class BasicFile {
85  protected:
87  bool iowoner;
89  bool fReadingMode;
90 
92  {
93  if (fd && io) io->fclose(fd);
94  fd = 0;
95  fReadingMode = true;
96  return true;
97  }
98 
99  void CheckIO()
100  {
101  if (io==0) { io = new FileInterface; iowoner = true; }
102  }
103 
104  public:
105 
107  io(0),
108  iowoner(false),
109  fd(0),
110  fReadingMode(false)
111  {
112  }
113 
114  void SetIO(FileInterface* _io, bool _ioowner = false)
115  {
116  if (iowoner && io) delete io;
117  io = _io;
118  iowoner = _ioowner;
119  }
120 
122  {
123  CloseBasicFile();
124  if (iowoner && io) delete io;
125  io = 0; iowoner = false;
126  }
127 
128  // returns true if file is opened
129  inline bool isOpened() const { return (io!=0) && (fd!=0); }
130 
131  inline bool isReading() const { return isOpened() && fReadingMode; }
132 
133  inline bool isWriting() const { return isOpened() && !fReadingMode; }
134 
135  bool eof() const { return isReading() ? io->feof(fd) : true; }
136 
138  int GetIntPar(const char* parname) { return io ? io->GetFileIntPar(fd, parname) : 0; }
139 
141  bool GetStrPar(const char* parname, char* sbuf, int sbuflen) { return io ? io->GetFileStrPar(fd, parname, sbuf, sbuflen) : false; }
142 
144  bool IsRFIO() { return GetIntPar("RFIO") > 0; }
145 
146  };
147 
148 
149 
150  // ===============================================================================
151 
152 
155  uint64_t magic;
156  uint64_t version;
157 
160  };
161 
162  // ===============================================================================
163 
166  uint64_t datalength;
167  uint64_t buftype;
168 
171  };
172 
173  enum { BinaryFileMagicValue = 1237 };
174 
175 
181  class BinaryFile : public BasicFile {
182  protected:
185 
186  public:
187 
189  {
190  }
191 
193  {
194  Close();
195  }
196 
197  bool OpenReading(const char* fname)
198  {
199  if (isOpened()) return false;
200 
201  if (fname==0 || *fname==0) {
202  fprintf(stderr, "file name not specified\n");
203  return false;
204  }
205 
206  CheckIO();
207 
208  fd = io->fopen(fname, "r");
209  if (fd==0) {
210  fprintf(stderr, "File open failed %s for reading\n", fname);
211  return false;
212  }
213 
214  size_t res = io->fread(&fFileHdr, sizeof(fFileHdr), 1, fd);
215  if ((res!=1) || (fFileHdr.magic != BinaryFileMagicValue)) {
216  fprintf(stderr, "Failure reading file %s header\n", fname);
217  Close();
218  return false;
219  }
220 
221  fReadingMode = true;
222  fBufHdr.datalength = 0;
223  fBufHdr.buftype = 0;
224  return true;
225  }
226 
227  bool OpenWriting(const char* fname)
228  {
229  if (isOpened()) return false;
230 
231  if (fname==0 || *fname==0) {
232  fprintf(stderr, "file name not specified\n");
233  return false;
234  }
235 
236  CheckIO();
237 
238  fd = io->fopen(fname, "w");
239  if (fd==0) {
240  fprintf(stderr, "File open failed %s for writing\n", fname);
241  return false;
242  }
243 
245  fFileHdr.version = 2;
246 
247  if (io->fwrite(&fFileHdr, sizeof(fFileHdr), 1, fd) != 1) {
248  fprintf(stderr, "Failure writing file %s header", fname);
249  Close();
250  return false;
251  }
252 
253  fReadingMode = false;
254  fBufHdr.datalength = 0;
255  fBufHdr.buftype = 0;
256  return true;
257  }
258 
259  bool Close()
260  {
261  return CloseBasicFile();
262  }
263 
264  const BinaryFileHeader& hdr() const { return fFileHdr; }
265 
266  bool WriteBufHeader(uint64_t size, uint64_t typ = 0)
267  {
268  if (!isWriting() || (size==0)) return false;
269 
270  if (fBufHdr.datalength != 0) {
271  fprintf(stderr, "writing of previous buffer was not completed, remained %u bytes\n", (unsigned) fBufHdr.datalength);
272  Close();
273  return false;
274  }
275 
276  fBufHdr.datalength = size;
277  fBufHdr.buftype = typ;
278 
279  if (io->fwrite(&fBufHdr, sizeof(fBufHdr), 1, fd) != 1) {
280  fprintf(stderr, "fail to write buffer header\n");
281  Close();
282  return false;
283  }
284 
285  return true;
286  }
287 
288  bool WriteBufPayload(const void* ptr, uint64_t sz)
289  {
290  if (!isWriting() || (ptr==0) || (sz==0)) return false;
291 
292  if (fBufHdr.datalength < sz) {
293  fprintf(stderr, "Appropriate header was not written for buffer %u\n", (unsigned) sz);
294  Close();
295  return false;
296  }
297 
298  fBufHdr.datalength -= sz;
299 
300  if (io->fwrite(ptr, sz, 1, fd)!=1) {
301  fprintf(stderr, "fail to write buffer payload of size %u\n", (unsigned) sz);
302  Close();
303  return false;
304  }
305 
306  return true;
307  }
308 
309  bool WriteBuffer(const void* ptr, uint64_t sz, uint64_t typ = 0)
310  {
311  if (!WriteBufHeader(sz, typ)) return false;
312 
313  return WriteBufPayload(ptr, sz);
314  }
315 
316 
317  bool ReadBufHeader(uint64_t* size, uint64_t* typ = 0)
318  {
319  if (!isReading()) return false;
320 
321  if (fBufHdr.datalength != 0) {
322  fprintf(stderr, "reading of previous buffer was not completed, remained %u bytes\n", (unsigned) fBufHdr.datalength);
323  Close();
324  return false;
325  }
326  if (io->fread(&fBufHdr, sizeof(fBufHdr), 1, fd) != 1) {
327  fprintf(stderr, "fail to read buffer header\n");
328  Close();
329  return false;
330  }
331  if (size) *size = fBufHdr.datalength;
332  if (typ) *typ = fBufHdr.buftype;
333 
334  return true;
335  }
336 
337  bool ReadBufPayload(void* ptr, uint64_t sz)
338  {
339  if (!isReading() || (ptr==0) || (sz==0)) return false;
340 
341  if (fBufHdr.datalength < sz) {
342  fprintf(stderr, "Appropriate header was not read from buffer %u\n", (unsigned) sz);
343  Close();
344  return false;
345  }
346 
347  fBufHdr.datalength -= sz;
348 
349  if (io->fread(ptr, sz, 1, fd) != 1) {
350  fprintf(stderr, "fail to write buffer payload of size %u\n", (unsigned) sz);
351  Close();
352  return false;
353  }
354 
355  return true;
356  }
357 
358  bool ReadBuffer(void* ptr, uint64_t* sz, uint64_t* typ = 0)
359  {
360  if ((ptr==0) || (sz==0) || (*sz == 0)) return false;
361 
362  uint64_t maxsz = *sz; *sz = 0;
363 
364  if (!ReadBufHeader(sz, typ)) return false;
365 
366  if (*sz > maxsz) {
367  fprintf(stderr, "Provided buffer %u is smaller than stored in the file %u\n", (unsigned) maxsz, (unsigned) *sz);
368  Close();
369  return false;
370  }
371 
372  return ReadBufPayload(ptr, *sz);
373  }
374 
375  };
376 }
377 
378 #endif
Base class for file writing/reading in DABC.
Definition: BinaryFile.h:84
bool isReading() const
Definition: BinaryFile.h:131
FileInterface::Handle fd
if true, io object owned by file
Definition: BinaryFile.h:88
void CheckIO()
Definition: BinaryFile.h:99
bool isOpened() const
Definition: BinaryFile.h:129
bool eof() const
Definition: BinaryFile.h:135
bool GetStrPar(const char *parname, char *sbuf, int sbuflen)
Return string file parameter.
Definition: BinaryFile.h:141
int GetIntPar(const char *parname)
Return integer file parameter.
Definition: BinaryFile.h:138
bool IsRFIO()
Returns true when RFIO is used.
Definition: BinaryFile.h:144
FileInterface * io
Definition: BinaryFile.h:86
void SetIO(FileInterface *_io, bool _ioowner=false)
Definition: BinaryFile.h:114
bool CloseBasicFile()
reading/writing mode
Definition: BinaryFile.h:91
bool iowoner
interface to the file system
Definition: BinaryFile.h:87
bool isWriting() const
Definition: BinaryFile.h:133
bool fReadingMode
file descriptor
Definition: BinaryFile.h:89
Generic file storage for DABC buffers.
Definition: BinaryFile.h:181
const BinaryFileHeader & hdr() const
Definition: BinaryFile.h:264
bool WriteBufPayload(const void *ptr, uint64_t sz)
Definition: BinaryFile.h:288
bool ReadBufPayload(void *ptr, uint64_t sz)
Definition: BinaryFile.h:337
bool OpenReading(const char *fname)
Definition: BinaryFile.h:197
BinaryFileHeader fFileHdr
Definition: BinaryFile.h:183
bool OpenWriting(const char *fname)
Definition: BinaryFile.h:227
bool ReadBufHeader(uint64_t *size, uint64_t *typ=0)
Definition: BinaryFile.h:317
bool WriteBufHeader(uint64_t size, uint64_t typ=0)
Definition: BinaryFile.h:266
bool ReadBuffer(void *ptr, uint64_t *sz, uint64_t *typ=0)
Definition: BinaryFile.h:358
BinaryFile()
buffer header
Definition: BinaryFile.h:188
BinaryFileBufHeader fBufHdr
file header
Definition: BinaryFile.h:184
bool WriteBuffer(const void *ptr, uint64_t sz, uint64_t typ=0)
Definition: BinaryFile.h:309
Defines and implements basic POSIX file interface.
Definition: BinaryFile.h:33
virtual bool GetFileStrPar(Handle h, const char *parname, char *sbuf, int sbuflen)
Method returns file-specific string parameter.
Definition: BinaryFile.h:70
void * Handle
File handle descriptor.
Definition: BinaryFile.h:37
virtual bool mkdir(const char *path)
Definition: BinaryFile.cxx:29
virtual bool fseek(Handle f, long int offset, bool relative=true)
Definition: BinaryFile.h:57
virtual int GetFileIntPar(Handle h, const char *parname)
Method returns file-specific int parameter.
Definition: BinaryFile.h:67
virtual size_t fwrite(const void *ptr, size_t sz, size_t nmemb, Handle f)
Definition: BinaryFile.h:45
virtual bool feof(Handle f)
Definition: BinaryFile.h:51
virtual void fclose(Handle f)
Definition: BinaryFile.h:43
virtual bool fflush(Handle f)
Definition: BinaryFile.h:54
virtual size_t fread(void *ptr, size_t sz, size_t nmemb, Handle f)
Definition: BinaryFile.h:48
virtual ~FileInterface()
Definition: BinaryFile.h:39
virtual Handle fopen(const char *fname, const char *mode, const char *=0)
Definition: BinaryFile.h:41
virtual Object * fmatch(const char *fmask, bool select_files=true)
Produce list of files, object must be explicitly destroyed with ref.Destroy call One could decide if ...
Definition: BinaryFile.cxx:60
Base class for most of the DABC classes.
Definition: Object.h:116
Event manipulation API.
Definition: api.h:23
@ BinaryFileMagicValue
Definition: BinaryFile.h:173
Binary file buffer header structure.
Definition: BinaryFile.h:165
BinaryFileBufHeader()
Default constructor.
Definition: BinaryFile.h:170
uint64_t datalength
overall length of buffer
Definition: BinaryFile.h:166
uint64_t buftype
type id of the buffer
Definition: BinaryFile.h:167
Binary file header structure.
Definition: BinaryFile.h:154
uint64_t magic
special word, expected in the beginning of the file
Definition: BinaryFile.h:155
BinaryFileHeader()
Default constructor.
Definition: BinaryFile.h:159
uint64_t version
version number of the binary file
Definition: BinaryFile.h:156