KaliVeda  1.12/06
Heavy-Ion Analysis Toolkit
KVFileReader.h
Go to the documentation of this file.
1 
4 #ifndef __KVFILEREADER_H
5 #define __KVFILEREADER_H
6 
7 #include "KVBase.h"
8 #include "Riostream.h"
9 #include "TObjArray.h"
10 #include "KVString.h"
11 #include "TString.h"
12 #include "TObjString.h"
13 
19 class KVFileReader : public KVBase {
20 private:
21  void init()
22  {
23  reading_line = "";
24  nline = 0;
25  }
26 
27 protected:
28  unique_ptr<TObjArray> toks;
32 
33 public:
34  std::ifstream f_in;
35 
36  KVFileReader();
37  KVFileReader(const KVFileReader&);
38  virtual void Copy(TObject&) const;
39 
40  virtual ~KVFileReader() {}
41 
43  {
44  return file_name;
45  }
46 
47  void Clear(Option_t* /*opt*/ = "")
48  {
49  init();
50  }
51 
53  {
54  CloseFile();
55  Clear();
56  return OpenFileToRead(GetFileName());
57  }
58 
60  {
61 
62  file_name = filename;
63 
64  f_in.open(filename.Data());
65  status = f_in.good();
66 
67  if (!status)
68  Error("OpenFileToRead", "Echec dans l ouverture du fichier %s", filename.Data());
69 
70  return status;
71 
72  }
73 
75  {
76  return f_in.good();
77  }
78 
79  void CloseFile()
80  {
81  if (f_in.is_open()) f_in.close();
82  }
83 
84  void ReadLine(const Char_t* pattern)
85  {
87  nline++;
88  if (pattern)
89  StoreParameters(pattern);
90  }
91 
92  void ReadLineAndAdd(const Char_t* pattern)
93  {
95  nline++;
96  if (pattern)
97  AddParameters(pattern);
98  }
99 
100  Int_t ReadLineAndCheck(Int_t nexpect, const Char_t* pattern)
101  {
102  ReadLine(0);
104 
105  if (GetCurrentLine().IsNull()) {
106  return 0;
107  }
108  StoreParameters(pattern);
109  if (GetNparRead() == nexpect) {
110  return 1;
111  }
112  else {
113  return 2;
114  }
115 
116  }
117  Int_t ReuseLineAndCheck(Int_t nexpect, const Char_t* pattern)
118  {
121  StoreParameters(pattern);
122  if (GetNparRead() == nexpect) {
123  return 1;
124  }
125  else {
126  return 2;
127  }
128 
129  }
130 
132  {
133  return reading_line;
134  }
135 
136  void StoreParameters(const Char_t* pattern)
137  {
138  toks.reset(GetCurrentLine().Tokenize(pattern));
139  }
140 
141  void AddParameters(const Char_t* pattern)
142  {
143  unique_ptr<TObjArray> tamp(GetCurrentLine().Tokenize(pattern));
144  Int_t ne = tamp->GetEntries();
146  if (toks.get() == nullptr) toks.reset(new TObjArray);
147  for (Int_t kk = 0; kk < ne; kk += 1) toks->Add(tamp->RemoveAt(kk));
148  }
149 
151  {
152  return toks->GetEntries();
153  }
155  {
156  return nline;
157  }
158 
160  {
161  return GetReadPar(pos).Atof();
162  }
164  {
165  return GetReadPar(pos).Atoi();
166  }
168  {
169  return ((TObjString*)toks->At(pos))->GetString();
170  }
171 
172 
173  ClassDef(KVFileReader, 2) //Manage the reading of file
174 };
175 
176 #endif
int Int_t
char Char_t
bool Bool_t
double Double_t
const char Option_t
#define ClassDef(name, id)
Base class for KaliVeda framework.
Definition: KVBase.h:135
Handle reading text files.
Definition: KVFileReader.h:19
KVString GetFileName()
Definition: KVFileReader.h:42
Int_t ReadLineAndCheck(Int_t nexpect, const Char_t *pattern)
Definition: KVFileReader.h:100
std::ifstream f_in
Definition: KVFileReader.h:34
KVString GetCurrentLine()
Definition: KVFileReader.h:131
void ReadLineAndAdd(const Char_t *pattern)
Definition: KVFileReader.h:92
Int_t ReuseLineAndCheck(Int_t nexpect, const Char_t *pattern)
Definition: KVFileReader.h:117
Bool_t PreparForReadingAgain()
Definition: KVFileReader.h:52
TString GetReadPar(Int_t pos)
Definition: KVFileReader.h:167
KVString file_name
Definition: KVFileReader.h:29
unique_ptr< TObjArray > toks
Definition: KVFileReader.h:28
void CloseFile()
Definition: KVFileReader.h:79
Bool_t OpenFileToRead(KVString filename)
Definition: KVFileReader.h:59
KVFileReader()
Default constructor.
virtual void Copy(TObject &) const
void StoreParameters(const Char_t *pattern)
Definition: KVFileReader.h:136
void Clear(Option_t *="")
Clear object properties : name, type/title, number, label.
Definition: KVFileReader.h:47
virtual ~KVFileReader()
Definition: KVFileReader.h:40
void ReadLine(const Char_t *pattern)
Definition: KVFileReader.h:84
Bool_t status
Definition: KVFileReader.h:31
Double_t GetDoubleReadPar(Int_t pos)
Definition: KVFileReader.h:159
Int_t GetNlineRead()
Definition: KVFileReader.h:154
KVString reading_line
Definition: KVFileReader.h:29
Bool_t IsOK()
Definition: KVFileReader.h:74
Int_t GetIntReadPar(Int_t pos)
Definition: KVFileReader.h:163
void AddParameters(const Char_t *pattern)
Definition: KVFileReader.h:141
Int_t GetNparRead()
Definition: KVFileReader.h:150
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:72
void RemoveAllExtraWhiteSpace()
Definition: KVString.cpp:1226
virtual void Error(const char *method, const char *msgfmt,...) const
Int_t Atoi() const
Double_t Atof() const
const char * Data() const
std::istream & ReadLine(std::istream &str, Bool_t skipWhite=kTRUE)