KaliVeda  1.13/01
Heavy-Ion Analysis Toolkit
KVFileReader.h
Go to the documentation of this file.
1 #ifndef __KVFILEREADER_H
2 #define __KVFILEREADER_H
3 
4 #include <string>
5 #include "KVBase.h"
6 #include "TSystem.h"
7 #include "KVString.h"
8 #include "TString.h"
9 
119 class KVFileReader : public KVBase {
120 private:
121  void init()
122  {
123  reading_line = "";
124  nline = 0;
126  }
127  void StoreParameters(const KVString& pattern)
128  {
130  items.clear();
131  reading_line.Begin(pattern);
132  while(!reading_line.End()) items.emplace_back(reading_line.Next(kTRUE).Data());
133  }
134 
135  void AddParameters(const KVString& pattern)
136  {
139  reading_line.Begin(pattern);
140  while(!reading_line.End()) items.emplace_back(reading_line.Next(kTRUE).Data());
141  }
142 
143 
144 protected:
145  std::vector<std::string> items;
150 
151 public:
152  std::ifstream f_in;
153 
157  enum class ReadStatus {
158  EmptyLine,
159  OK,
161  CommentLine,
162  EndOfFile
163  };
164 
166  {
167  switch(s)
168  {
170  return "EmptyLine";
172  return "CommentLine";
174  return "EndOfFile";
175  case ReadStatus::OK:
176  return "OK";
178  return "ParamMismatch";
179  }
180  return "???";
181  }
182  KVFileReader(const KVString& comments = "")
183  : comment_string(comments)
184  {
186  init();
187  }
188 
190  {
191  return file_name;
192  }
193 
194  void Clear(Option_t* /*opt*/ = "")
195  {
196  init();
197  }
198 
200  {
204  CloseFile();
205  Clear();
206  return OpenFileToRead(GetFileName());
207  }
208 
209  Bool_t OpenFileToRead(const KVString& filename)
210  {
216 
217  TString _filename = filename;
218  gSystem->ExpandPathName(_filename);
219  file_name = _filename;
220 
221  f_in.open(_filename.Data());
222  status = f_in.good();
223 
224  if (!status)
225  Error("OpenFileToRead", "Failed to open file %s", _filename.Data());
226 
227  return status;
228  }
229 
231  {
233  return f_in.good();
234  }
235 
236  void CloseFile()
237  {
239  if (f_in.is_open()) f_in.close();
240  }
241 
242  ReadStatus ReadLine(const KVString& pattern = "")
243  {
250  if (!IsOK()) return ReadStatus::EndOfFile;
251 
252  nline++;
254  if (!pattern.IsNull())
255  StoreParameters(pattern);
256 
257  return ReadStatus::OK;
258  }
259 
260  ReadStatus ReadLineAndAdd(const KVString& pattern = "")
261  {
268  if (!IsOK()) return ReadStatus::EndOfFile;
269 
270  nline++;
272  if (!pattern.IsNull())
273  AddParameters(pattern);
274 
275  return ReadStatus::OK;
276  }
277 
278  ReadStatus ReadLineAndCheck(Int_t nexpect, const KVString& pattern)
279  {
287  if (!IsOK()) return ReadStatus::EndOfFile;
288 
289  nline++;
291 
293 
294  if (GetCurrentLine().IsNull()) {
295  return ReadStatus::EmptyLine;
296  }
297  StoreParameters(pattern);
298  if (GetNparRead() != nexpect) {
300  }
301  return ReadStatus::OK;
302  }
303  ReadStatus ReuseLineAndCheck(Int_t nexpect, const KVString& pattern)
304  {
311 
312  StoreParameters(pattern);
313  if (GetNparRead() != nexpect) {
315  }
316  return ReadStatus::OK;
317  }
318 
320  {
321  return reading_line;
322  }
323 
325  {
326  return items.size();
327  }
329  {
330  return nline;
331  }
332 
334  {
335  return GetReadPar(pos).Atof();
336  }
338  {
339  return GetReadPar(pos).Atoi();
340  }
342  {
343  return items[pos].c_str();
344  }
345 
346  ClassDef(KVFileReader, 2) //Manage the reading of file
347 };
348 
349 #endif
int Int_t
bool Bool_t
double Double_t
const Bool_t kTRUE
const char Option_t
#define ClassDef(name, id)
R__EXTERN TSystem * gSystem
Base class for KaliVeda framework.
Definition: KVBase.h:141
Handle reading columns of numeric data in text files.
Definition: KVFileReader.h:119
KVString GetFileName()
Definition: KVFileReader.h:189
std::ifstream f_in
Definition: KVFileReader.h:152
KVString GetCurrentLine()
Definition: KVFileReader.h:319
ReadStatus
status returned by each method used to read a line in the file
Definition: KVFileReader.h:157
@ ParamMismatch
the number of parameters read from line does not correspond to expectations
@ CommentLine
last line read was a comment line
@ EndOfFile
end of file reached
@ EmptyLine
last line read was empty (only whitespace)
@ OK
successful read and import of parameters from line
ReadStatus ReadLineAndCheck(Int_t nexpect, const KVString &pattern)
Definition: KVFileReader.h:278
Bool_t PreparForReadingAgain()
Definition: KVFileReader.h:199
Int_t GetNlineRead() const
Definition: KVFileReader.h:328
KVString file_name
Definition: KVFileReader.h:146
void CloseFile()
Definition: KVFileReader.h:236
ReadStatus ReadLine(const KVString &pattern="")
Definition: KVFileReader.h:242
KVFileReader(const KVString &comments="")
Definition: KVFileReader.h:182
Double_t GetDoubleReadPar(Int_t pos) const
Definition: KVFileReader.h:333
void Clear(Option_t *="")
Clear object properties : name, type/title, number, label.
Definition: KVFileReader.h:194
ReadStatus ReadLineAndAdd(const KVString &pattern="")
Definition: KVFileReader.h:260
Int_t GetIntReadPar(Int_t pos) const
Definition: KVFileReader.h:337
Int_t GetNparRead() const
Definition: KVFileReader.h:324
void StoreParameters(const KVString &pattern)
Definition: KVFileReader.h:127
KVString reading_line
Definition: KVFileReader.h:146
Bool_t IsOK()
Definition: KVFileReader.h:230
Bool_t skip_comments
Definition: KVFileReader.h:149
KVString GetReadStatus(ReadStatus s)
Definition: KVFileReader.h:165
KVString comment_string
Definition: KVFileReader.h:146
KVString GetReadPar(Int_t pos) const
Definition: KVFileReader.h:341
void AddParameters(const KVString &pattern)
Definition: KVFileReader.h:135
ReadStatus ReuseLineAndCheck(Int_t nexpect, const KVString &pattern)
Definition: KVFileReader.h:303
std::vector< std::string > items
Definition: KVFileReader.h:145
Bool_t OpenFileToRead(const KVString &filename)
Definition: KVFileReader.h:209
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:72
void Begin(TString delim) const
Definition: KVString.cpp:565
void RemoveAllExtraWhiteSpace()
Definition: KVString.cpp:1253
Bool_t End() const
Definition: KVString.cpp:634
KVString Next(Bool_t strip_whitespace=kFALSE) const
Definition: KVString.cpp:695
virtual void Error(const char *method, const char *msgfmt,...) const
Int_t Atoi() const
Double_t Atof() const
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
const char * Data() const
Bool_t IsNull() const
std::istream & ReadLine(std::istream &str, Bool_t skipWhite=kTRUE)
virtual char * ExpandPathName(const char *path)
const long double s
Definition: KVUnits.h:94