Diana Software
QGTextFileHandler.cc
Go to the documentation of this file.
1 #include "QGTextFileHandler.hh"
2 #include <fstream>
3 #include <sstream>
4 
6 
7 using std::endl;
8 using std::flush;
9 using std::ifstream;
10 using std::map;
11 using std::ofstream;
12 using std::pair;
13 using std::string;
14 using std::stringstream;
15 using std::vector;
16 
18 {
19 }
20 
22 {
23 }
24 
25 bool QGTextFileHandler::ReadFile(string filename)
26 {
27  fFileMap.clear();
28  fFileLines.clear();
29  ifstream file(filename.c_str());
30  bool success = false;
31  if (file) {
32  success = true;
33  string line;
34  while (getline(file, line)) {
35  pair<string, string> parsedLine = ParseLine(line);
36  fFileMap.insert(parsedLine);
37  fFileLines.push_back(parsedLine);
38  }
39  }
40  file.close();
41  return success;
42 }
43 
44 bool QGTextFileHandler::WriteFile(string filename)
45 {
46  ofstream file(filename.c_str());
47  bool success = false;
48  if (file) {
49  success = true;
50  vector<pair<string, string> >::const_iterator lineIter;
51  for (lineIter = fFileLines.begin(); lineIter != fFileLines.end(); ++lineIter) {
52  file << BuildLine(*lineIter) << endl;
53  }
54  }
55  file.close();
56  return success;
57 }
58 
60 {
61  return (fFileMap.count(key) != 0);
62 }
63 
64 string QGTextFileHandler::GetValue(string key)
65 {
66  string value = "";
67  if (DoesKeyExist(key)) {
68  value = fFileMap[key];
69  }
70  return value;
71 }
72 
73 void QGTextFileHandler::AppendLine(string key, string value)
74 {
75  fFileLines.push_back(make_pair(key, value));
76  fFileMap[key] = value;
77 }
78 
79 void QGTextFileHandler::AppendLine(string key, Double_t value)
80 {
81  stringstream valueStream;
82  valueStream.precision(16);
83  valueStream << value << flush;
84  AppendLine(key, valueStream.str());
85 }
86 
87 void QGTextFileHandler::AppendLine(string key, Int_t value)
88 {
89  stringstream valueStream;
90  valueStream << value << flush;
91  AppendLine(key, valueStream.str());
92 }
93 
94 pair<string, string> QGTextFileHandler::ParseLine(const string& line)
95 {
96  string key, value;
97  string::size_type delimiter = line.find_first_of(" "); // could add other delimiters
98  if (delimiter != string::npos) {
99  key = line.substr(0, delimiter);
100  value = line.substr(delimiter + 1);
101  } else {
102  key = line;
103  value = "";
104  }
105  return make_pair(key, value);
106 }
107 
108 string QGTextFileHandler::BuildLine(const pair<string, string>& parsedLine)
109 {
110  string line = parsedLine.first + " " + parsedLine.second;
111  return line;
112 }
113 
success
Definition: CheckOF.C:112
ClassImp(QObject)
Class to handle input and output for text files.
std::string GetValue(std::string key)
Retrieve value associated with a key.
std::pair< std::string, std::string > ParseLine(const std::string &line)
Parse a line of a text file, splitting at a delimiter.
bool ReadFile(std::string filename)
Read file, returns true if successful.
std::vector< std::pair< std::string, std::string > > fFileLines
Contents of text file, line by line.
std::string BuildLine(const std::pair< std::string, std::string > &parsedLine)
Build a line from a key and value, inserting a delimiter.
std::map< std::string, std::string > fFileMap
Contents of text file, useful for unique keys.
bool WriteFile(std::string filename)
Write file, returns true if successful.
bool DoesKeyExist(std::string key)
Return true if key exists.
void AppendLine(std::string key="", std::string value="")
Append a key and value to the file.
QGTextFileHandler()
Constructor.
virtual ~QGTextFileHandler()
Destructor.