DABC (Data Acquisition Backbone Core)  2.9.9
TextInput.cxx
Go to the documentation of this file.
1 // $Id: TextInput.cxx 4482 2020-04-15 14:47:18Z 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 "mbs/TextInput.h"
17 
18 #include <cstring>
19 #include <cstdlib>
20 #include <sstream>
21 
22 #include "mbs/Iterator.h"
23 
25  dabc::FileInput(url),
26  fDataFormat("uint32_t"),
27  fNumData(8),
28  fNumHeaderLines(1),
29  fCharBufferLength(1024),
30  fFile(),
31  fCharBuffer(nullptr),
32  fEventCounter(0),
33  fFormatId(0),
34  fDataUnitSize(4)
35 {
36  fDataFormat = url.GetOptionStr("format", fDataFormat);
37  fNumData = url.GetOptionInt("numdata", fNumData);
40 }
41 
43 {
44  CloseFile();
45 
46  delete [] fCharBuffer; fCharBuffer = nullptr;
47 }
48 
50 {
51  if (!dabc::FileInput::Read_Init(wrk, cmd)) return false;
52 
53  fDataFormat = wrk.Cfg(mbs::xmlTextDataFormat, cmd).AsStr(fDataFormat);
54  fNumData = wrk.Cfg(mbs::xmlTextNumData, cmd).AsInt(fNumData);
55  fNumHeaderLines = wrk.Cfg(mbs::xmlTextHeaderLines, cmd).AsInt(fNumHeaderLines);
56  fCharBufferLength = wrk.Cfg(mbs::xmlTextCharBuffer, cmd).AsInt(fCharBufferLength);
57 
58  if (fDataFormat=="float") { fFormatId = 0; fDataUnitSize = sizeof(float); } else
59  if (fDataFormat=="int32_t") { fFormatId = 1; fDataUnitSize = 4; } else
60  if (fDataFormat=="int") { fFormatId = 1; fDataUnitSize = 4; } else
61  if (fDataFormat=="uint32_t") { fFormatId = 2; fDataUnitSize = 4; } else
62  if (fDataFormat=="unsigned") { fFormatId = 2; fDataUnitSize = 4; } else {
63  EOUT("Unsupported data format %s", fDataFormat.c_str());
64  return false;
65  }
66 
67  if (fCharBufferLength < 1024) fCharBufferLength = 1024;
68  fCharBuffer = new char[fCharBufferLength];
69 
70  return OpenNextFile();
71 }
72 
74 {
75  CloseFile();
76 
77  if (!TakeNextFileName()) return false;
78 
79  fFile.open(CurrentFileName().c_str());
80 
81  int cnt = fNumHeaderLines;
82  while (cnt-- > 0) fFile.getline(fCharBuffer, fCharBufferLength);
83 
84  if (!fFile.good()) {
85  EOUT("Cannot open file %s for reading", CurrentFileName().c_str());
86  fFile.close();
87  return false;
88  }
89 
90  return true;
91 }
92 
94 {
95  if (fFile.is_open()) fFile.close();
96  ClearCurrentFileName();
97  return true;
98 }
99 
101 {
102  // get size of the buffer which should be read from the file
103 
104  if (!fFile.good())
105  if (!OpenNextFile()) return dabc::di_EndOfStream;
106 
107  return dabc::di_DfltBufSize;
108 }
109 
111 {
112  unsigned rawdatasize = RawDataSize();
113 
114  mbs::WriteIterator iter(buf);
115  while (iter.NewEvent(fEventCounter)) {
116  // check if we have enough place for next subevent,
117  if (!iter.NewSubevent(rawdatasize, 1, 0)) break;
118 
119  // fill raw data iter.rawdata() with 0
120  memset(iter.rawdata(), 0, rawdatasize);
121 
122  // read next nonempty line into buffer
123 
124  const char* sbuf = nullptr;
125  do {
126  if (fFile.eof())
127  if (!OpenNextFile()) return dabc::di_EndOfStream;
128 
129  *fCharBuffer = 0;
130 
131  fFile.getline(fCharBuffer, fCharBufferLength);
132 
133  if ((strlen(fCharBuffer) == 0) && fFile.eof()) {
134  DOUT1("empty line in end of file");
135  continue;
136  }
137 
138  if (fFile.bad()) {
139  EOUT("File: %s reading error", CurrentFileName().c_str());
140  return dabc::di_Error;
141  }
142 
143  sbuf = fCharBuffer;
144  while ((*sbuf!=0) && ((*sbuf==' ') || (*sbuf=='\t'))) sbuf++;
145 
146  if (strlen(sbuf)==0) DOUT1("Empty string eof fail = %d %d", fFile.eof(), fFile.fail());
147 
148  } while (strlen(sbuf)==0);
149 
150  unsigned filledsize = FillRawData(fCharBuffer, iter.rawdata(), rawdatasize);
151  if (filledsize==0) return dabc::di_Error;
152 
153  iter.FinishSubEvent(filledsize);
154 
155  if (!iter.FinishEvent()) {
156  EOUT("Problem with iterator");
157  return dabc::di_Error;
158  }
159 
160  fEventCounter++;
161 
162  if (fFile.eof())
163  if (!OpenNextFile()) return dabc::di_EndOfStream;
164  }
165 
166  buf = iter.Close();
167 
168  return dabc::di_Ok;
169 }
170 
172 {
175  return fNumData * fDataUnitSize;
176 }
177 
178 unsigned mbs::TextInput::FillRawData(const char* str, void* rawdata, unsigned maxsize)
179 {
183  std::istringstream src(str);
184 
185  switch (fFormatId) {
186  case 0: {
187  float* tgt = (float*) rawdata;
188  for(int n=0;n<fNumData;n++) {
189  src >> *tgt;
190  if (src.fail()) {
191  EOUT("Fail to decode stream into float, cnt = %d", n);
192  EOUT("Error Line %s", str);
193  return 0;
194  }
195  tgt++;
196  }
197  break;
198  }
199 
200  case 1: {
201  int32_t* tgt = (int32_t*) rawdata;
202  for(int n=0;n<fNumData;n++) {
203  src >> *tgt;
204  if (src.fail()) {
205  EOUT("Fail to decode stream into int32_t, cnt = %d", n);
206  EOUT("Error Line %s", str);
207  return 0;
208  }
209  tgt++;
210  }
211  break;
212  }
213 
214  case 2: {
215  uint32_t* tgt = (uint32_t*) rawdata;
216  for(int n=0;n<fNumData;n++) {
217  src >> *tgt;
218  if (src.fail()) {
219  EOUT("Fail to decode stream into uint32_t, cnt = %d", n);
220  EOUT("Error Line %s", str);
221  return 0;
222  }
223  tgt++;
224  }
225  break;
226  }
227 
228  default:
229  return 0;
230  }
231 
232  return maxsize;
233 }
Reference on memory from memory pool.
Definition: Buffer.h:135
Represents command with its arguments.
Definition: Command.h:99
virtual bool Read_Init(const WorkerRef &wrk, const Command &cmd)
Initialize data input, using port and command.
Definition: DataIO.cxx:139
std::string AsStr(const std::string &dflt="") const
Definition: Record.cxx:749
int64_t AsInt(int64_t dflt=0) const
Definition: Record.cxx:501
Uniform Resource Locator interpreter.
Definition: Url.h:33
std::string GetOptionStr(const std::string &optname, const std::string &dflt="") const
Definition: Url.cxx:281
int GetOptionInt(const std::string &optname, int dflt=0) const
Definition: Url.cxx:290
Reference on dabc::Worker
Definition: Worker.h:466
RecordField Cfg(const std::string &name, Command cmd=nullptr) const
Returns configuration record of specified name.
Definition: Worker.h:482
bool OpenNextFile()
Definition: TextInput.cxx:73
int fCharBufferLength
Definition: TextInput.h:38
virtual unsigned Read_Complete(dabc::Buffer &buf)
Complete reading of the buffer from source,.
Definition: TextInput.cxx:110
virtual unsigned RawDataSize()
Definition: TextInput.cxx:171
virtual bool Read_Init(const dabc::WorkerRef &wrk, const dabc::Command &cmd)
Initialize data input, using port and command.
Definition: TextInput.cxx:49
virtual unsigned FillRawData(const char *str, void *rawdata, unsigned maxsize)
Definition: TextInput.cxx:178
std::string fDataFormat
Definition: TextInput.h:35
bool CloseFile()
Definition: TextInput.cxx:93
virtual ~TextInput()
Definition: TextInput.cxx:42
int fNumHeaderLines
Definition: TextInput.h:37
virtual unsigned Read_Size()
Defines required buffer size for next operation.
Definition: TextInput.cxx:100
TextInput(const dabc::Url &url)
Definition: TextInput.cxx:24
Write iterator for MBS events/subevents.
Definition: Iterator.h:97
bool FinishSubEvent(uint32_t rawdatasz=0)
Definition: Iterator.cxx:378
bool NewSubevent(uint32_t minrawsize=0, uint8_t crate=0, uint16_t procid=0, uint8_t control=0)
Definition: Iterator.cxx:329
void * rawdata() const
Definition: Iterator.h:158
bool NewEvent(EventNumType event_number=0, uint32_t subeventsize=0)
Definition: Iterator.cxx:304
dabc::Buffer Close()
Definition: Iterator.cxx:237
#define EOUT(args ...)
Definition: logging.h:150
#define DOUT1(args ...)
Definition: logging.h:162
Event manipulation API.
Definition: api.h:23
@ di_Ok
Definition: DataIO.h:38
@ di_DfltBufSize
Definition: DataIO.h:42
@ di_Error
Definition: DataIO.h:40
@ di_EndOfStream
Definition: DataIO.h:37
const char * xmlTextCharBuffer
Definition: MbsTypeDefs.cxx:52
const char * xmlTextNumData
Definition: MbsTypeDefs.cxx:50
const char * xmlTextHeaderLines
Definition: MbsTypeDefs.cxx:51
const char * xmlTextDataFormat
Definition: MbsTypeDefs.cxx:49