Diana Software
QASCII.cc
Go to the documentation of this file.
1 /*
2 *
3 * Class QASCII
4 *
5 */
6 
7 #include "QASCII.hh"
8 
9 #include <sstream>
10 #include <fstream>
11 #include <iomanip>
12 #include <limits>
13 
14 using namespace std;
15 using namespace Diana;
16 
17 
18 
20 {
21  fFilename = "";
22 }
23 
24 
25 QASCII::QASCII(const QASCII& orig)
26 {
27  fFilename = orig.fFilename;
28 }
29 
30 QASCII::QASCII(string filename)
31 
32 {
33  fFilename = filename;
34 }
35 
37 
39 {
40  fstream filestream;
41  filestream.open(fFilename.c_str(), fstream::in);
42  bool exists = filestream.good();
43  filestream.close();
44  filestream.clear();
45  return exists;
46 }
47 
48 int QASCII::NumberOfLines(bool ExcludeComments)
49 {
50  int numLines = 0;
51  string line;
52  fstream filestream;
53  filestream.open(fFilename.c_str(), fstream::in);
54  while(getline(filestream, line))
55  {
56  if(line[0]=='#' && ExcludeComments) continue;
57  else numLines++;
58  }
59  filestream.close();
60  filestream.clear();
61  return numLines;
62 }
63 
65 {
66  fstream filestream;
67  filestream.open(fFilename.c_str(), fstream::in);
68  if(!filestream.good()) return 0;
69 
70  vector<double> vec;
71 
72  string line;
73  double dummy;
74 
75  while(getline(filestream, line))
76  {
77  if(line[0] == '#') continue;
78  istringstream stream(line);
79  while (stream >> dummy)
80  vec.push_back(dummy); // use vector instead of QVector for speed
81  }
82  QVector* qvec = new QVector(vec.size());
83  for(unsigned int i=0; i<vec.size(); i++)
84  {
85  (*qvec)[i] = vec[i];
86  }
87  filestream.close();
88  filestream.clear();
89  return qvec;
90 }
91 
92 QVector* QASCII::ReadQVector(int ColumnNumber, int startline, int endline)
93 {
94  if(endline < startline) endline = this->NumberOfLines(false);
95 
96  fstream filestream;
97  filestream.open(fFilename.c_str(), fstream::in);
98  if(!filestream.good()) return 0;
99 
100  vector<double> vec;
101  string line;
102  double dummy; // temporarally store values here
103  int badrow=0; // this row does not contain correct number of columns
104  int LineNumber=-1;
105 
106  while(getline(filestream, line))
107  {
108  LineNumber++;
109  if(line[0] == '#') continue;
110  if(LineNumber < startline) continue;
111  if(LineNumber > endline) break;
112 
113  istringstream stream(line);
114  for(int i=0; i<=ColumnNumber; i++)
115  {
116  if(stream.good())
117  stream >> dummy;
118  else
119  badrow=1;
120  }
121  if(badrow) {
122  badrow=0; // reset badrow flag
123  vec.push_back(std::numeric_limits<double>::quiet_NaN());
124  }
125  else vec.push_back(dummy); // only append value if correct column found
126  }
127 
128  QVector* qvec = new QVector(vec.size());
129  for(unsigned int i=0; i<vec.size(); i++)
130  {
131  (*qvec)[i] = vec[i];
132  }
133  filestream.close();
134  filestream.clear();
135  return qvec;
136 }
137 
138 int QASCII::WriteQVector(QVector outputVector, bool append)
139 {
140  ofstream filestream;
141  if(append) filestream.open(fFilename.c_str(), ofstream::app);
142  else filestream.open(fFilename.c_str(), ofstream::out);
143 
144  if(!filestream.good()) return 0;
145 
146  int NumberElements = outputVector.Size();
147  int NumberWritten = 0;
148 
149  for(int i=0; i<NumberElements; i++)
150  {
151  filestream << outputVector[i] << endl;
152  if(!filestream.fail()) NumberWritten++;
153  }
154  filestream.close();
155  filestream.clear();
156  return NumberWritten;
157 }
158 
159 int QASCII::WriteQVectors(vector<QVector> outputVectors, bool append)
160 {
161  ofstream filestream;
162  if(append) filestream.open(fFilename.c_str(), ofstream::app);
163  else filestream.open(fFilename.c_str(), ofstream::out);
164 
165  if(!filestream.good()) return 0;
166 
167  int NumberOfQVectors = outputVectors.size();
168  QVector NumberOfElements(NumberOfQVectors);
169  for(int i=0; i<NumberOfQVectors; i++)
170  {
171  NumberOfElements[i] = outputVectors[i].Size();
172  }
173  int MaxSize = int(NumberOfElements.GetMax());
174  int NumberWritten = 0;
175 
176  for(int i=0; i<MaxSize; i++)
177  {
178  for(int j=0; j<NumberOfQVectors; j++)
179  {
180  if(i < NumberOfElements[j])
181  {
182  filestream << outputVectors[j][i] << '\t';
183  if(!filestream.fail()) NumberWritten++;
184  }
185  else
186  {
187  filestream << '\t';
188  }
189  }
190  filestream << endl;
191  }
192  filestream.close();
193  filestream.clear();
194  return NumberWritten;
195 }
196 
197 bool QASCII::WriteString(string outputString, bool append)
198 {
199  ofstream filestream;
200  if(append) filestream.open(fFilename.c_str(), ofstream::app);
201  else filestream.open(fFilename.c_str(), ofstream::out);
202 
203  filestream << outputString << flush;
204  bool fail = filestream.fail();
205  filestream.close();
206  filestream.clear();
207  return fail;
208 }
209 bool QASCII::WriteLine(string line, bool append)
210 {
211  ofstream filestream;
212  if(append) filestream.open(fFilename.c_str(), ofstream::app);
213  else filestream.open(fFilename.c_str(), ofstream::out);
214 
215  filestream << line << endl;
216  bool fail = filestream.fail();
217  filestream.close();
218  filestream.clear();
219  return fail;
220 }
221 bool QASCII::Comment(string comment)
222 {
223  ofstream filestream;
224  filestream.open(fFilename.c_str(), ofstream::app);
225 
226  filestream << "# " << comment << endl;
227  bool fail = filestream.fail();
228  filestream.close();
229  filestream.clear();
230  return fail;
231 }
232 string QASCII::ReadComment(int commentNumber)
233 {
234  fstream filestream;
235  filestream.open(fFilename.c_str(), fstream::in);
236  if(!filestream.good()) return 0;
237 
238  int commentsRead = 0;
239  string line;
240 
241  while(getline(filestream, line))
242  {
243  if(line[0]=='#')
244  {
245  if(commentsRead==commentNumber)
246  {
247  filestream.close();
248  filestream.clear();
249  int commentStartIndex = line.find_first_not_of("# ");
250  return line.substr(commentStartIndex);
251  }
252  commentsRead++;
253  }
254  }
255  filestream.close();
256  filestream.clear();
257  return 0;
258 }
259 
260 string QASCII::ReadString(int startline, int endline)
261 {
262  if(endline < startline) endline = this->NumberOfLines(false);
263 
264  fstream filestream;
265  filestream.open(fFilename.c_str(), fstream::in);
266  if(!filestream.good()) return 0;
267 
268  string dummy;
269  int LineNumber = 0;
270  while(startline > LineNumber)
271  {
272  getline(filestream, dummy);
273  LineNumber++;
274  }
275 
276  char c;
277  ostringstream oss;
278  string readString;
279 
280  while(filestream.good())
281  {
282  c = filestream.get();
283  if(c=='\n') LineNumber++;
284  if(LineNumber > endline) break;
285  oss << c;
286  if(oss.fail()) break;
287  }
288 
289  readString = oss.str();
290 
291  return readString;
292 }
293 
294 vector<double> QASCII::ReadVector()
295 {
296  fstream filestream;
297  filestream.open(fFilename.c_str(), fstream::in);
298 
299  vector<double> vec;
300 
301  if(!filestream.good()) return vec;
302 
303  string line;
304  double dummy;
305 
306  while(getline(filestream, line))
307  {
308  if(line[0] == '#') continue;
309  istringstream stream(line);
310  while (stream >> dummy)
311  vec.push_back(dummy);
312  }
313 
314  filestream.close();
315  filestream.clear();
316  return vec;
317 }
318 
319 vector<double> QASCII::ReadVector(int ColumnNumber, int startline, int endline)
320 {
321  if(endline < startline) endline = this->NumberOfLines(false);
322 
323  fstream filestream;
324  filestream.open(fFilename.c_str(), fstream::in);
325 
326  vector<double> vec;
327 
328  if(!filestream.good()) return vec;
329 
330  string line;
331  double dummy; // temporarally store values here
332  int badrow=0; // this row does not contain correct number of columns
333  int LineNumber=-1;
334 
335  while(getline(filestream, line))
336  {
337  LineNumber++;
338  if(line[0] == '#') continue;
339  if(LineNumber < startline) continue;
340  if(LineNumber > endline) break;
341 
342  istringstream stream(line);
343  for(int i=0; i<=ColumnNumber; i++)
344  {
345  if(stream.good())
346  stream >> dummy;
347  else
348  badrow=1;
349  }
350 
351  if(badrow) badrow=0; // reset badrow flag
352  else vec.push_back(dummy); // only append value if correct column found
353  }
354 
355  filestream.close();
356  filestream.clear();
357  return vec;
358 }
359 
360 int QASCII::WriteVector(vector<double> outputVector, bool append)
361 {
362  ofstream filestream;
363  if(append) filestream.open(fFilename.c_str(), ofstream::app);
364  else filestream.open(fFilename.c_str(), ofstream::out);
365 
366  if(!filestream.good()) return 0;
367 
368  int NumberElements = outputVector.size();
369  int NumberWritten = 0;
370 
371  for(int i=0; i<NumberElements; i++)
372  {
373  filestream << outputVector[i] << endl;
374  if(!filestream.fail()) NumberWritten++;
375  }
376  filestream.close();
377  filestream.clear();
378  return NumberWritten;
379 }
380 
381 int QASCII::WriteVector(vector<int> outputVector, bool append)
382 {
383  ofstream filestream;
384  if(append) filestream.open(fFilename.c_str(), ofstream::app);
385  else filestream.open(fFilename.c_str(), ofstream::out);
386 
387  if(!filestream.good()) return 0;
388 
389  int NumberElements = outputVector.size();
390  int NumberWritten = 0;
391 
392  for(int i=0; i<NumberElements; i++)
393  {
394  filestream << outputVector[i] << endl;
395  if(!filestream.fail()) NumberWritten++;
396  }
397  filestream.close();
398  filestream.clear();
399  return NumberWritten;
400 }
401 
402 int QASCII::WriteVectors(vector<vector<double> > outputVectors, bool append)
403 {
404  ofstream filestream;
405  if(append) filestream.open(fFilename.c_str(), ofstream::app);
406  else filestream.open(fFilename.c_str(), ofstream::out);
407 
408  if(!filestream.good()) return 0;
409 
410  int NumberOfVectors = outputVectors.size();
411  vector<int> NumberOfElements(NumberOfVectors, 0);
412  int MaxSize = 0;
413  for(int i=0; i<NumberOfVectors; i++)
414  {
415  NumberOfElements[i] = outputVectors[i].size();
416  if(NumberOfElements[i] > MaxSize) MaxSize = NumberOfElements[i];
417  }
418  int NumberWritten = 0;
419 
420  for(int i=0; i<MaxSize; i++)
421  {
422  for(int j=0; j<NumberOfVectors; j++)
423  {
424  if(i < NumberOfElements[j])
425  {
426  filestream << outputVectors[j][i] << '\t';
427  if(!filestream.fail()) NumberWritten++;
428  }
429  else
430  {
431  filestream << '\t';
432  }
433  }
434  filestream << endl;
435  }
436  filestream.close();
437  filestream.clear();
438  return NumberWritten;
439 }
440 
441 int QASCII::FindLine(string pattern, int startline)
442 {
443  fstream filestream;
444  filestream.open(fFilename.c_str(), fstream::in);
445  if(!filestream.good()) return -1;
446 
447  string line;
448  int LineNumber = 0;
449  if(startline > 0)
450  {
451  for(int i=0; i<startline; i++)
452  {
453  getline(filestream, line);
454  LineNumber++;
455  }
456  }
457 
458  while(getline(filestream, line))
459  {
460  if(line == pattern)
461  return LineNumber;
462  LineNumber++;
463  }
464  filestream.close();
465  filestream.clear();
466  return -2;
467 }
468 
469 bool QASCII::WriteQTime(QTime* qt, bool append)
470 {
471  bool ret = false;
472  ofstream filestream;
473  if(append) filestream.open(fFilename.c_str(), ofstream::app);
474  else filestream.open(fFilename.c_str(), ofstream::out);
475 
476  if(!filestream.good()) return true;
477 
478  filestream << qt->GetFromStartRunNs() << endl;
479  filestream << qt->GetStartRunUnix();
480 
481  if(filestream.fail()) ret=true;
482 
483  filestream.close();
484  filestream.clear();
485  return ret;
486 }
487 
488 QTime* QASCII::ReadQTime(int startline)
489 {
490  if(startline<0) return 0;
491 
492  fstream filestream;
493  filestream.open(fFilename.c_str(), fstream::in);
494  if(!filestream.good()) return 0;
495 
496  bool found = false;
497  unsigned long long NsTime;
498  unsigned int StartRunUnix;
499  string line;
500  int LineNumber=-1;
501 
502  while(getline(filestream, line))
503  {
504  LineNumber++;
505  if(line[0] == '#') continue;
506  if(LineNumber < startline) continue;
507 
508  if(!found)
509  {
510  istringstream stream(line);
511  if(stream.good())
512  {
513  stream >> NsTime;
514  found=true;
515  }
516  else
517  {
518  break;
519  }
520  }
521  else
522  {
523  istringstream stream(line);
524  if(stream.good()) stream >> StartRunUnix;
525  else found=false;
526  break;
527  }
528  }
529 
530  QTime* qt = 0;
531  if(found)
532  {
533  qt = new QTime();
534  qt->SetFromStartRunNs(NsTime);
535  qt->SetStartRunUnix(StartRunUnix);
536  }
537  filestream.close();
538  filestream.clear();
539  return qt;
540 }
541 
QVector vec(3)
Class for reading and writing ascii text files.
Definition: QASCII.hh:22
std::vector< double > ReadVector()
Reads a text file into a vector. Text file should contain a list of numbers.
Definition: QASCII.cc:294
int NumberOfLines(bool ExcludeComments=true)
Returns the number of lines in the file, excluding comments by default.
Definition: QASCII.cc:48
bool Exists()
Returns true if the file exists and can be opened.
Definition: QASCII.cc:38
bool WriteLine(std::string line, bool append=true)
Writes a string to a file, terminated with newline.
Definition: QASCII.cc:209
int WriteQVectors(std::vector< Diana::QVector > outputVector, bool append=false)
Writes a vector of QVectors, each QVector in a new column. Warning: QVectors of different lengths can...
Definition: QASCII.cc:159
std::string ReadString(int startline=0, int endline=-1)
Reads entire file or subset of file into a string.
Definition: QASCII.cc:260
bool WriteQTime(Diana::QTime *qt, bool append=true)
Writes an object of type QTime.
Definition: QASCII.cc:469
bool Comment(std::string comment)
Writes a comment to a file; begins with '#', terminates with newline.
Definition: QASCII.cc:221
QASCII()
default constructor
Definition: QASCII.cc:19
std::string fFilename
Name of the file on disk.
Definition: QASCII.hh:193
virtual ~QASCII()
destructor
Definition: QASCII.cc:36
bool WriteString(std::string outputString, bool append=false)
Writes a string to a file.
Definition: QASCII.cc:197
int WriteQVector(Diana::QVector outputVector, bool append=false)
Writes data from a QVector to a file.
Definition: QASCII.cc:138
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
int WriteVectors(std::vector< std::vector< double > > outputVector, bool append=false)
Writes a vector of vectors, each vector in a new column. Warning: vectors of different lengths can be...
Definition: QASCII.cc:402
std::string ReadComment(int commentNumber=0)
Reads the nth comment from a file. Comments are lines beginning with '#'.
Definition: QASCII.cc:232
Diana::QTime * ReadQTime(int startline=0)
Reads an object of type QTime.
Definition: QASCII.cc:488
int WriteVector(std::vector< double > outputVector, bool append=false)
Writes data from a vector to a file.
Diana time.
Definition: QTime.hh:17
unsigned long long GetFromStartRunNs() const
retrieve time from start of run in ns
Definition: QTime.hh:38
void SetStartRunUnix(unsigned int time)
set time of start of run in seconds from 1/1/1970
Definition: QTime.hh:32
time_t GetStartRunUnix() const
retrieve time of start of run in seconds from 1/1/1970
Definition: QTime.hh:41
void SetFromStartRunNs(unsigned long long time)
set time from start of run in ns
Definition: QTime.hh:29
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...