KaliVeda
Toolkit for HIC analysis
Loading...
Searching...
No Matches
KVFileReader.h
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
120class KVFileReader : public KVBase {
121private:
122 void init()
123 {
124 reading_line = "";
125 nline = 0;
127 }
128 void StoreParameters(const KVString& pattern)
129 {
131 items.clear();
132 reading_line.Begin(pattern);
133 while (!reading_line.End()) items.emplace_back(reading_line.Next(kTRUE).Data());
134 }
135
136 void AddParameters(const KVString& pattern)
137 {
140 reading_line.Begin(pattern);
141 while (!reading_line.End()) items.emplace_back(reading_line.Next(kTRUE).Data());
142 }
143
144
145protected:
146 std::vector<std::string> items;
151
152public:
153 std::ifstream f_in;
154
158 enum class ReadStatus {
159 EmptyLine,
160 OK,
163 EndOfFile
164 };
165
167 {
168 switch (s) {
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
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
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()) {
296 }
297 StoreParameters(pattern);
298 if (GetNparRead() != nexpect) {
300 }
301 return ReadStatus::OK;
302 }
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
constexpr Bool_t kTRUE
const char Option_t
#define ClassDef(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:142
Handle reading columns of numeric data in text files.
KVString GetFileName()
std::ifstream f_in
KVString GetCurrentLine()
ReadStatus
status returned by each method used to read a line in the file
@ 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)
Bool_t PreparForReadingAgain()
Int_t GetNlineRead() const
KVString file_name
ReadStatus ReadLine(const KVString &pattern="")
KVFileReader(const KVString &comments="")
Double_t GetDoubleReadPar(Int_t pos) const
void Clear(Option_t *="")
ReadStatus ReadLineAndAdd(const KVString &pattern="")
Int_t GetIntReadPar(Int_t pos) const
Int_t GetNparRead() const
void StoreParameters(const KVString &pattern)
KVString reading_line
Bool_t IsOK()
Bool_t skip_comments
KVString GetReadStatus(ReadStatus s)
KVString comment_string
KVString GetReadPar(Int_t pos) const
void AddParameters(const KVString &pattern)
ReadStatus ReuseLineAndCheck(Int_t nexpect, const KVString &pattern)
std::vector< std::string > items
Bool_t OpenFileToRead(const KVString &filename)
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()
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)