DABC (Data Acquisition Backbone Core)  2.9.9
BinaryFileIO.cxx
Go to the documentation of this file.
1 // $Id: BinaryFileIO.cxx 4476 2020-04-15 14:12:38Z 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 #include "dabc/BinaryFileIO.h"
17 
19  FileInput(url),
20  fFile(),
21  fCurrentBufSize(0),
22  fCurrentBufType(0)
23 {
24 }
25 
27 {
28  CloseFile();
29 }
30 
32 {
33  if (!dabc::FileInput::Read_Init(wrk, cmd)) return false;
34 
35  return OpenNextFile();
36 }
37 
39 {
40  CloseFile();
41 
42  if (!TakeNextFileName()) return false;
43 
44  if (!fFile.OpenReading(CurrentFileName().c_str())) {
45  EOUT("Cannot open file %s for reading", CurrentFileName().c_str());
46  return false;
47  }
48 
49  DOUT1("Open bin file %s for reading", CurrentFileName().c_str());
50 
51  return true;
52 }
53 
54 
56 {
57  fFile.Close();
58  ClearCurrentFileName();
59  return true;
60 }
61 
63 {
64  if (!fFile.isReading()) return di_Error;
65 
66  fCurrentBufSize = 0;
67  fCurrentBufType = 0;
68 
69  if (fFile.eof()) return di_EndOfStream;
70 
71  if (!fFile.ReadBufHeader(&fCurrentBufSize, &fCurrentBufType)) {
72  DOUT1("Error reading file %s", CurrentFileName().c_str());
73  return di_Error;
74  }
75 
76  return fCurrentBufSize;
77 }
78 
80 {
81  if (!fFile.isReading()) return di_Error;
82 
83  if (buf.GetTotalSize()<fCurrentBufSize) {
84  EOUT("Not enough space in buffer, required is %u", (unsigned) fCurrentBufSize);
85  return di_Error;
86  }
87 
88  if (buf.NumSegments()!=1) {
89  EOUT(("Segmented buffer not supported - can be easily done"));
90  return di_Error;
91  }
92 
93  if (!fFile.ReadBufPayload(buf.SegmentPtr(), fCurrentBufSize)) return di_Error;
94 
95  buf.SetTotalSize(fCurrentBufSize);
96  buf.SetTypeId(fCurrentBufType);
97 
98  return di_Ok;
99 }
100 
101 // _________________________________________________________________
102 
104  FileOutput(url,".bin"),
105  fFile()
106 {
107 }
108 
110 {
111  CloseFile();
112 }
113 
114 
116 {
117  if (!dabc::FileOutput::Write_Init()) return false;
118 
119  fFile.SetIO(fIO, false);
120 
121  return StartNewFile();
122 }
123 
125 {
126  CloseFile();
127 
128  ProduceNewFileName();
129 
130  if (!fFile.OpenWriting(CurrentFileName().c_str())) {
131  ShowInfo(-1, dabc::format("%s cannot open file for writing", CurrentFileName().c_str()));
132  return false;
133  }
134 
135  ShowInfo(0, dabc::format("Open %s for writing", CurrentFileName().c_str()));
136 
137  return true;
138 }
139 
140 
142 {
143  if (fFile.isWriting()) {
144  ShowInfo(0, dabc::format("Close file %s", CurrentFileName().c_str()));
145  fFile.Close();
146  }
147  return true;
148 }
149 
150 
152 {
153  if (!fFile.isWriting() || buf.null()) return do_Error;
154 
155  BufferSize_t fullsz = buf.GetTotalSize();
156 
157  if (CheckBufferForNextFile(fullsz))
158  if (!StartNewFile()) {
159  EOUT("Cannot start new file for writing");
160  return do_Error;
161  }
162 
163  if (!fFile.WriteBufHeader(fullsz, buf.GetTypeId())) return do_Error;
164 
165  for (unsigned n=0;n<buf.NumSegments();n++)
166  if (!fFile.WriteBufPayload(buf.SegmentPtr(n), buf.SegmentSize(n))) return do_Error;
167 
168  AccountBuffer(fullsz);
169 
170  return do_Ok;
171 }
BinaryFileInput(const dabc::Url &url)
virtual bool Read_Init(const WorkerRef &wrk, const Command &cmd)
Initialize data input, using port and command.
virtual unsigned Read_Size()
Defines required buffer size for next operation.
virtual unsigned Read_Complete(Buffer &buf)
Complete reading of the buffer from source,.
BinaryFileOutput(const dabc::Url &url)
virtual bool Write_Init()
This is generic virtual method to initialize output before real work is started.
virtual unsigned Write_Buffer(Buffer &buf)
Start writing of buffer to output.
Reference on memory from memory pool.
Definition: Buffer.h:135
unsigned NumSegments() const
Returns number of segment in buffer.
Definition: Buffer.h:163
unsigned SegmentSize(unsigned n=0) const
Returns size on the segment, no any boundary checks.
Definition: Buffer.h:174
void SetTotalSize(BufferSize_t len)
Set total length of the buffer to specified value Size cannot be bigger than original size of the buf...
Definition: Buffer.cxx:99
unsigned GetTypeId() const
Definition: Buffer.h:152
BufferSize_t GetTotalSize() const
Return total size of all buffer segments.
Definition: Buffer.cxx:91
void SetTypeId(unsigned tid)
Definition: Buffer.h:151
void * SegmentPtr(unsigned n=0) const
Returns pointer on the segment, no any boundary checks.
Definition: Buffer.h:171
Represents command with its arguments.
Definition: Command.h:99
Interface for implementing file inputs.
Definition: DataIO.h:246
virtual bool Read_Init(const WorkerRef &wrk, const Command &cmd)
Initialize data input, using port and command.
Definition: DataIO.cxx:139
Interface for implementing file outputs.
Definition: DataIO.h:283
virtual bool Write_Init()
This is generic virtual method to initialize output before real work is started.
Definition: DataIO.cxx:211
bool null() const
Returns true if reference contains nullptr.
Definition: Reference.h:151
Uniform Resource Locator interpreter.
Definition: Url.h:33
Reference on dabc::Worker
Definition: Worker.h:466
#define EOUT(args ...)
Definition: logging.h:150
#define DOUT1(args ...)
Definition: logging.h:162
uint32_t BufferSize_t
Definition: Buffer.h:32
std::string format(const char *fmt,...)
Definition: string.cxx:49
@ do_Ok
Definition: DataIO.h:142
@ do_Error
Definition: DataIO.h:147
@ di_Ok
Definition: DataIO.h:38
@ di_Error
Definition: DataIO.h:40
@ di_EndOfStream
Definition: DataIO.h:37