Diana Software
LASCIIGlobalReader.cc
Go to the documentation of this file.
1 #include "LASCIIGlobalReader.hh"
2 #include "QObject.hh"
3 #include "QMatrix.hh"
4 #include "QString.hh"
5 #include "QBool.hh"
6 #include "QBaseType.hh"
7 #include <sstream>
8 
9 // MV FIXME: invalidity if QInt, QString are not found
11 
12 using namespace Diana;
13 using std::istringstream;
14 using std::string;
15 using std::stringstream;
16 
18 {
19  fQObjectCache = new std::map<std::string, QObject*>;
20 }
21 
23 {
24  Close();
25  delete fQObjectCache;
26 }
27 
28 QError LASCIIGlobalReader::Open(const std::string& filename, const std::string& opt)
29 {
31  fFile = new QASCII(filename);
32  if(!fFile->Exists())
33  {
35  err.SetDescription(GetName(),__LINE__,std::string("Cannot open file: ") + filename);
36  delete fFile;
37  fFile = NULL;
38  }
39  return err;
40 }
41 
42 
44 {
46  delete fFile;
47  fFile = NULL;
48  return err;
49 }
50 
51 const QObject* LASCIIGlobalReader::GetQObject(const std::string& name,const std::string& className, QError& err) const
52 {
53  if(className == "Diana::QBaseType<int>") {
54  return new QInt(GetInt(name,err));
55  } else if(className == "Diana::QBaseType<float>") {
56  return new QFloat(GetFloat(name,err));
57  } else if(className == "Diana::QBaseType<double>") {
58  return new QDouble(GetDouble(name,err));
59  } else if(className == "Diana::QBool") {
60  return new QBool(GetBool(name,err));
61  } else if(className == "Diana::QString") {
62  return new QString(GetString(name,err));
63  } else {
64  return GetQObject(name, err);
65  }
66  return 0;
67 }
68 
69 const QObject* LASCIIGlobalReader::GetQObject(const std::string& name, QError& err) const
70 {
71  err = QERR_SUCCESS;
72  // check for already existing object
73  std::map<std::string, QObject*>::const_iterator object = fQObjectCache->find(name);
74  if(object != fQObjectCache->end()) return object->second;
75 
76  // If not in cache, look in file
77  QObject* obj;
78  int startline, endline;
79  string startstring, endstring;
80 
81  string readstring;
82 
83  // does file exist?
84 
85  if(!fFile) {
87  err.SetDescription(GetName(),__LINE__,"no file is open");
88  return NULL;
89  }
90 
91  startstring = "# begin " + name;
92  startline = 1 + fFile->FindLine(startstring);
93 
94  if(startline < 0) {
96  err.SetDescription(GetName(),__LINE__,std::string("Failed to find line: \"") + startstring + "\"");
97  return NULL;
98  }
99 
100  endstring = "# end " + name;
101  endline = -1 + fFile->FindLine(endstring, startline);
102 
103  readstring = fFile->ReadString(startline, startline);
104 
105  if(readstring == "# QVector")
106  {
107  obj = fFile->ReadQVector(0, startline, endline);
108  }
109  else if(readstring == "# QVectorC")
110  {
111  QVector* real = fFile->ReadQVector(0, startline, endline);
112  QVector* imag = fFile->ReadQVector(1, startline, endline);
113 
114  obj = new QVectorC(*real, *imag);
115  }
116  else if(readstring == "# QTime")
117  {
118  obj = fFile->ReadQTime(startline);
119  }
120  else if(readstring == "# QMatrix")
121  {
122  readstring = fFile->ReadString(startline+1, startline+1);
123  istringstream iss(readstring);
124  int nrow, ncol;
125  char c;
126  if(!(iss >> c >> nrow >> ncol)) {
128  err.SetDescription(GetName(),__LINE__,std::string("Cannot read row and column numbers of object ") + name);
129  return 0;
130  }
131 
132  QMatrix* qmat = new QMatrix(nrow, ncol);
133  QVector* dummy;
134  for(int i=0; i<ncol; i++)
135  {
136  dummy = fFile->ReadQVector(i, startline, endline);
137  qmat->SetCol(i, *dummy);
138  }
139  obj = qmat;
140  }
141  else
142  {
143 
144  istringstream iss(readstring);
145  char c; string objtype;
146  if(!(iss>>c>>objtype)) {
148  err.SetDescription(GetName(),__LINE__,std::string("Cannot read type of object ") + name);
149  return 0;
150  }
151  obj = QObject::New(objtype.c_str());
152  if(!obj) {
153  stringstream msg;
154  msg<<"Object \""<<name<<"\" has invalid type \""<<objtype<<"\"";
156  err.SetDescription(GetName(),__LINE__,msg.str());
157  } else {
158  stringstream data;
159  int line = startline+1;
160  while(line <= endline) {
161  readstring = fFile->ReadString(line,line);
162  if(!readstring.empty() && readstring.at(0) != '#') data<<readstring<<std::endl;;
163  line++;
164  }
165  err = obj->ReadFromStream(data);
166  if(err != QERR_SUCCESS) {
167  delete obj;
168  obj = 0;
169  stringstream msg;
171  msg<<"Error while reading object \""<< name <<"\": "<<err.GetDescription();
172  err.SetDescription(GetName(),__LINE__,msg.str());
173 
174  }
175  }
176  }
177 
178  if(obj) {
179  // MV: We do not store the validity flag in ascii files so we assume that it is valid...
180  obj->Validate();
181  (*fQObjectCache)[name] = obj;
182  }
183  return obj;
184 }
185 
186 std::string LASCIIGlobalReader::GetString(const std::string& name, QError& err) const
187 {
188 
189  err = QERR_SUCCESS;
190  string readstring = Q_STRING_DEFAULT;
191  int startline, endline;
192  string startstring, endstring;
193 
194  // does file exist?
195  if(!fFile) {
197  err.SetDescription(GetName(),__LINE__,"no file is open");
198  return readstring;
199  }
200 
201 
202  startstring = "# begin " + name;
203  startline = 1 + fFile->FindLine(startstring);
204 
205  if(startline < 0) {
207  err.SetDescription(GetName(),__LINE__,std::string("Failed to find line: \"") + startstring + "\"");
208  return readstring;
209  }
210 
211  endstring = "# end " + name;
212  endline = -1 + fFile->FindLine(endstring, startline);
213 
214  readstring = fFile->ReadString(startline, endline);
215 
216  return readstring;
217 }
218 
219 int LASCIIGlobalReader::GetInt(const std::string& name,QError& err) const
220 {
221 
222  err = QERR_SUCCESS;
223  int obj = Q_INT_DEFAULT;
224  int startline;
225  string startstring;
226 
227  string readstring;
228 
229  // does file exist?
230  if(!fFile) {
232  err.SetDescription(GetName(),__LINE__,"no file is open");
233  return obj;
234  }
235 
236  startstring = "# begin " + name;
237  startline = 1 + fFile->FindLine(startstring);
238 
239  if(startline < 0) {
241  err.SetDescription(GetName(),__LINE__,std::string("Failed to find line: \"") + startstring + "\"");
242  return obj;
243  }
244 
245  readstring = fFile->ReadString(startline, startline);
246  obj = atoi(readstring.c_str());
247 
248  return obj;
249 }
250 
251 double LASCIIGlobalReader::GetDouble(const std::string& name, QError& err) const
252 {
253  err = QERR_SUCCESS;
254  double obj = Q_DOUBLE_DEFAULT;
255  int startline;
256  string startstring;
257 
258  string readstring;
259 
260  // does file exist?
261  if(!fFile) {
263  err.SetDescription(GetName(),__LINE__,"no file is open");
264  return obj;
265  }
266 
267  startstring = "# begin " + name;
268  startline = 1 + fFile->FindLine(startstring);
269 
270  if(startline < 0) {
272  err.SetDescription(GetName(),__LINE__,std::string("Failed to find line: \"") + startstring + "\"");
273  return obj;
274  }
275 
276  readstring = fFile->ReadString(startline, startline);
277  obj = atof(readstring.c_str());
278 
279  return obj;
280 }
281 
282 float LASCIIGlobalReader::GetFloat(const std::string& name, QError& err) const
283 {
284 
285  err = QERR_SUCCESS;
286  float obj = Q_FLOAT_DEFAULT;
287  int startline;
288  string startstring;
289 
290  string readstring;
291 
292  // does file exist?
293  if(!fFile) {
295  err.SetDescription(GetName(),__LINE__,"no file is open");
296  return obj;
297  }
298 
299  startstring = "# begin " + name;
300  startline = 1 + fFile->FindLine(startstring);
301 
302  if(startline < 0) {
304  err.SetDescription(GetName(),__LINE__,std::string("Failed to find line: \"") + startstring + "\"");
305  return obj;
306  }
307 
308  readstring = fFile->ReadString(startline, startline);
309  istringstream ss(readstring);
310  ss >> obj;
311 
312  return obj;
313 }
314 
315 bool LASCIIGlobalReader::GetBool(const std::string& name, QError& err) const
316 {
317 
318  err = QERR_SUCCESS;
319  bool obj = Q_BOOL_DEFAULT;
320  int startline;
321  string startstring;
322 
323  string readstring;
324 
325  // does file exist?
326  if(!fFile) {
328  err.SetDescription(GetName(),__LINE__,"no file is open");
329  return obj;
330  }
331  startstring = "# begin " + name;
332  startline = 1 + fFile->FindLine(startstring);
333 
334 
335  if(startline < 0) {
337  err.SetDescription(GetName(),__LINE__,std::string("Failed to find line: \"") + startstring + "\"");
338  return obj;
339  }
340 
341  readstring = fFile->ReadString(startline, startline);
342  istringstream ss(readstring);
343  ss >> obj;
344 
345  return obj;
346 }
347 
348 /*
349 QVector* LASCIIGlobalReader::GetQVector(const std::string& name) const
350 {
351  QVector* obj;
352 
353  int startline, endline;
354  string startstring, endstring;
355 
356  string readstring;
357 
358  startstring = "# begin " + name;
359  startline = 1 + fFile->FindLine(startstring);
360 
361  if(startline < 0) return NULL;
362 
363  endstring = "# end " + name;
364  endline = -1 + fFile->FindLine(endstring, startline);
365 
366  obj = fFile->ReadQVector(0, startline, endline);
367 
368  return obj;
369 }
370 
371 QVectorC* LASCIIGlobalReader::GetQVectorC(const std::string& name) const
372 {
373  QVectorC* obj;
374 
375  QVector* real;
376  QVector* imag;
377 
378  int startline, endline;
379  string startstring, endstring;
380 
381  string readstring;
382 
383  startstring = "# begin " + name;
384  startline = 1 + fFile->FindLine(startstring);
385 
386  if(startline < 0) return NULL;
387 
388  endstring = "# end " + name;
389  endline = -1 + fFile->FindLine(endstring, startline);
390 
391  real = fFile->ReadQVector(0, startline, endline);
392  imag = fFile->ReadQVector(1, startline, endline);
393 
394  obj = new QVectorC(*real, *imag);
395 
396  return obj;
397 }
398 
399 */
400 
err
Definition: CheckOF.C:114
QBaseType< double > QDouble
double wrapped in a QObject
Definition: QBaseType.hh:184
QBaseType< int > QInt
int wrapped in a QObject
Definition: QBaseType.hh:174
QBaseType< float > QFloat
float wrapped in a QObject
Definition: QBaseType.hh:179
#define Q_DOUBLE_DEFAULT
Definition: QDiana.hh:24
#define Q_BOOL_DEFAULT
Definition: QDiana.hh:36
#define Q_STRING_DEFAULT
Definition: QDiana.hh:38
#define Q_FLOAT_DEFAULT
Definition: QDiana.hh:32
#define Q_INT_DEFAULT
Definition: QDiana.hh:26
@ QERR_FILE_NOT_FOUND
Definition: QError.hh:32
@ QERR_UNKNOWN_ERR
Definition: QError.hh:108
@ QERR_CANNOT_OPEN_FILE
Definition: QError.hh:33
@ QERR_SUCCESS
Definition: QError.hh:27
#define REGISTER_GLOBAL_READER(clazz, ext)
global reader for txt files
bool GetBool(const std::string &name, QError &err) const
float GetFloat(const std::string &name, QError &err) const
QError Open(const std::string &filename, const std::string &opt="")
Open file, called by QGlobalReaderDispatcher.
QError Close()
Close file, called by QGlobalReaderDispatcher.
const Diana::QObject * GetQObject(const std::string &name, const std::string &className, QError &err) const
int GetInt(const std::string &name, QError &err) const
std::string GetString(const std::string &name, QError &err) const
std::map< std::string, Diana::QObject * > * fQObjectCache
double GetDouble(const std::string &name, QError &err) const
Class for reading and writing ascii text files.
Definition: QASCII.hh:22
bool Exists()
Returns true if the file exists and can be opened.
Definition: QASCII.cc:38
std::string ReadString(int startline=0, int endline=-1)
Reads entire file or subset of file into a string.
Definition: QASCII.cc:260
int FindLine(std::string pattern, int startline=0)
Finds a line matching the string pattern.
Definition: QASCII.cc:441
Diana::QVector * ReadQVector()
Reads a text file into a QVector. Text file should contain a list of numbers.
Definition: QASCII.cc:64
Diana::QTime * ReadQTime(int startline=0)
Reads an object of type QTime.
Definition: QASCII.cc:488
bool wrapped into a QObject
Definition: QBool.hh:17
error class with error type and description
Definition: QError.hh:115
Abstract class for global readers.
Interface for matrices in Diana analysis.
Definition: QMatrix.hh:24
void SetCol(UInt_t ncol, const QVector &vec)
set column to specific vector
Definition: QMatrix.cc:179
const std::string & GetName() const
Definition: QNamed.hh:19
static QObject * New(const char *object)
Definition: QObject.cc:14
string wrapped into a QObject
Definition: QString.hh:18
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...