KaliVeda
Toolkit for HIC analysis
KVFileReader.h
1 #ifndef __KVFILEREADER_H
2 #define __KVFILEREADER_H
3 
4 #include <string>
5 #include <fstream>
6 #include "KVBase.h"
7 #include "TSystem.h"
8 #include "KVString.h"
9 #include "TString.h"
10 
121 class KVFileReader : public KVBase {
122 private:
123  void init()
124  {
125  reading_line = "";
126  nline = 0;
128  }
129  void StoreParameters(const KVString& pattern)
130  {
132  items.clear();
133  reading_line.Begin(pattern);
134  while (!reading_line.End()) items.emplace_back(reading_line.Next(kTRUE).Data());
135  }
136 
137  void AddParameters(const KVString& pattern)
138  {
141  reading_line.Begin(pattern);
142  while (!reading_line.End()) items.emplace_back(reading_line.Next(kTRUE).Data());
143  }
144 
145 
146 protected:
147  std::vector<std::string> items;
152 
153 public:
154  std::ifstream f_in;
155 
159  enum class ReadStatus {
160  EmptyLine,
161  OK,
163  CommentLine,
164  EndOfFile
165  };
166 
168  {
169  switch (s) {
171  return "EmptyLine";
173  return "CommentLine";
175  return "EndOfFile";
176  case ReadStatus::OK:
177  return "OK";
179  return "ParamMismatch";
180  }
181  return "???";
182  }
183  KVFileReader(const KVString& comments = "")
184  : comment_string(comments)
185  {
187  init();
188  }
189 
191  {
192  return file_name;
193  }
194 
195  void Clear(Option_t* /*opt*/ = "") override
196  {
197  init();
198  }
199 
201  {
205  CloseFile();
206  Clear();
207  return OpenFileToRead(GetFileName());
208  }
209 
210  Bool_t OpenFileToRead(const KVString& filename)
211  {
217 
218  TString _filename = filename;
219  gSystem->ExpandPathName(_filename);
220  file_name = _filename;
221 
222  f_in.open(_filename.Data());
223  status = f_in.good();
224 
225  if (!status)
226  Error("OpenFileToRead", "Failed to open file %s", _filename.Data());
227 
228  return status;
229  }
230 
232  {
234  return f_in.good();
235  }
236 
237  void CloseFile()
238  {
240  if (f_in.is_open()) f_in.close();
241  }
242 
243  ReadStatus ReadLine(const KVString& pattern = "")
244  {
251  if (!IsOK()) return ReadStatus::EndOfFile;
252 
253  nline++;
255  if (!pattern.IsNull())
256  StoreParameters(pattern);
257 
258  return ReadStatus::OK;
259  }
260 
261  ReadStatus ReadLineAndAdd(const KVString& pattern = "")
262  {
269  if (!IsOK()) return ReadStatus::EndOfFile;
270 
271  nline++;
273  if (!pattern.IsNull())
274  AddParameters(pattern);
275 
276  return ReadStatus::OK;
277  }
278 
279  ReadStatus ReadLineAndCheck(Int_t nexpect, const KVString& pattern)
280  {
288  if (!IsOK()) return ReadStatus::EndOfFile;
289 
290  nline++;
292 
294 
295  if (GetCurrentLine().IsNull()) {
296  return ReadStatus::EmptyLine;
297  }
298  StoreParameters(pattern);
299  if (GetNparRead() != nexpect) {
301  }
302  return ReadStatus::OK;
303  }
304  ReadStatus ReuseLineAndCheck(Int_t nexpect, const KVString& pattern)
305  {
312 
313  StoreParameters(pattern);
314  if (GetNparRead() != nexpect) {
316  }
317  return ReadStatus::OK;
318  }
319 
321  {
322  return reading_line;
323  }
324 
326  {
327  return items.size();
328  }
330  {
331  return nline;
332  }
333 
335  {
336  return GetReadPar(pos).Atof();
337  }
339  {
340  return GetReadPar(pos).Atoi();
341  }
343  {
344  return items[pos].c_str();
345  }
346 
347  ClassDefOverride(KVFileReader, 2) //Manage the reading of file
348 };
349 
350 #endif
int Int_t
bool Bool_t
double Double_t
constexpr Bool_t kTRUE
const char Option_t
#define ClassDefOverride(name, id)
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
R__EXTERN TSystem * gSystem
Base class for KaliVeda framework.
Definition: KVBase.h:139
Handle reading columns of numeric data in text files.
Definition: KVFileReader.h:121
KVString GetFileName()
Definition: KVFileReader.h:190
std::ifstream f_in
Definition: KVFileReader.h:154
KVString GetCurrentLine()
Definition: KVFileReader.h:320
ReadStatus
status returned by each method used to read a line in the file
Definition: KVFileReader.h:159
@ 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:279
Bool_t PreparForReadingAgain()
Definition: KVFileReader.h:200
Int_t GetNlineRead() const
Definition: KVFileReader.h:329
KVString file_name
Definition: KVFileReader.h:148
void CloseFile()
Definition: KVFileReader.h:237
ReadStatus ReadLine(const KVString &pattern="")
Definition: KVFileReader.h:243
KVFileReader(const KVString &comments="")
Definition: KVFileReader.h:183
void Clear(Option_t *="") override
Definition: KVFileReader.h:195
Double_t GetDoubleReadPar(Int_t pos) const
Definition: KVFileReader.h:334
ReadStatus ReadLineAndAdd(const KVString &pattern="")
Definition: KVFileReader.h:261
Int_t GetIntReadPar(Int_t pos) const
Definition: KVFileReader.h:338
Int_t GetNparRead() const
Definition: KVFileReader.h:325
void StoreParameters(const KVString &pattern)
Definition: KVFileReader.h:129
KVString reading_line
Definition: KVFileReader.h:148
Bool_t IsOK()
Definition: KVFileReader.h:231
Bool_t skip_comments
Definition: KVFileReader.h:151
KVString GetReadStatus(ReadStatus s)
Definition: KVFileReader.h:167
KVString comment_string
Definition: KVFileReader.h:148
KVString GetReadPar(Int_t pos) const
Definition: KVFileReader.h:342
void AddParameters(const KVString &pattern)
Definition: KVFileReader.h:137
ReadStatus ReuseLineAndCheck(Int_t nexpect, const KVString &pattern)
Definition: KVFileReader.h:304
std::vector< std::string > items
Definition: KVFileReader.h:147
Bool_t OpenFileToRead(const KVString &filename)
Definition: KVFileReader.h:210
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:73
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
const char * Data() const
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Bool_t IsNull() const
std::istream & ReadLine(std::istream &str, Bool_t skipWhite=kTRUE)
virtual char * ExpandPathName(const char *path)