LBNEKeyedInput.cc
Go to the documentation of this file.
1 //author: Matthew Michelotti
2 
3 #include "LBNEKeyedInput.hh"
4 #include <fstream>
5 #include "G4ExceptionSeverity.hh"
6 
7 using namespace std;
8 
9 //Constructs object with an empty varMap. (The varMap maps variable names to
10 //their contents.)
12 
13 
14 //readFile, readString, and readStream behave similarly in that they read
15 //text input and add the appropriate variables to the varMap.
16 //See the class description for details on the format.
17 
18 //-------------------------------------------------------------------------------
20  ifstream in(filename.c_str());
21  if(!in.is_open()) fail("unable to open file");
22  readStream(in);
23  in.close();
24 }
25 //-------------------------------------------------------------------------------
27  istringstream in(data);
28  readStream(in);
29 }
30 //-------------------------------------------------------------------------------
31 void LBNEKeyedInput::readStream(istream& in) {
32  G4String line;
33  while(!in.eof()) {
34  getline(in, line);
35  if(in.bad()) fail("error reading input stream");
36  if(in.fail()) break;
37  line.toUpper();
38  handleLine(line);
39  }
40 }
41 
42 //-------------------------------------------------------------------------------
43 //return true iff a variable with the given name was read from input
44 G4bool LBNEKeyedInput::contains(G4String name) const {
45  name.toUpper();
46  return (varMap.find(name) != varMap.end());
47 }
48 
49 
50 //get(name, buffer, length) is a function overloaded many times. The type of
51 //buffer will determine which function is called. The get function will
52 //look up a variable with the given name, parse it, and write the resulting
53 //value into buffer. If buffer is a vector, then the optional length
54 //parameter may be used to require the input to be a certain length.
55 //The default length=-1 means that any length is allowed.
56 
57 void LBNEKeyedInput::get(cstr_t name, G4int& buffer, G4int /*length*/)
58  {getSingle(name, buffer);}
59 void LBNEKeyedInput::get(cstr_t name, G4double& buffer, G4int /*length*/)
60  {getSingle(name, buffer);}
61 void LBNEKeyedInput::get(cstr_t name, G4bool& buffer, G4int /*length*/)
62  {getSingle(name, buffer);}
63 void LBNEKeyedInput::get(cstr_t name, G4String& buffer, G4int /*length*/)
64  {getSingle(name, buffer);}
65 
66 void LBNEKeyedInput::get(cstr_t name, vector<G4int>& buffer, G4int length)
67  {getVector(name, buffer, length);}
68 void LBNEKeyedInput::get(cstr_t name, vector<G4double>& buffer, G4int length)
69  {getVector(name, buffer, length);}
70 void LBNEKeyedInput::get(cstr_t name, vector<G4bool>& buffer, G4int length)
71  {getVector(name, buffer, length);}
72 void LBNEKeyedInput::get(cstr_t name, vector<G4String>& buffer, G4int length)
73  {getVector(name, buffer, length);}
74 
75 //-------------------------------------------------------------------------------
76 //Prints contents of the varMap to standard output.
78  G4cout << G4endl;
79  G4cout << "printing LBNEKeyedInput..." << G4endl;
80  G4cout << "--------------------------" << G4endl << G4endl;
81  for(ssmap_t::const_iterator it = varMap.begin(); it != varMap.end(); it++) {
82  G4cout << it->first << ": " << it->second << G4endl << G4endl;
83  }
84  G4cout << "--------------------------" << G4endl << G4endl;
85 }
86 
87 //-------------------------------------------------------------------------------
88 //Processes a single line of input data. If line is empty or a comment,
89 //this will do nothing. If line is a continuation of an array, it will
90 //append the new contents to the pre-existing contents. Otherwise, it will
91 //make a new varMap entry with the appropriate contents.
93  if(isLineComment(line)) return;
94 
95  istringstream in(line);
96  G4String key;
97  in >> key;
98  if(in.fail()) return;
99 
100  readIndexSpec(in, key);
101 
102  G4String term;
103  ostringstream value;
104  value << varMap[key];
105  while(!in.fail()) {
106  in >> term;
107  if(!in.fail()) value << term << ' ';
108  }
109 
110  varMap[key] = value.str();
111 }
112 
113 //-------------------------------------------------------------------------------
114 //Return true iff line is a comment or "". A comment starts with a series
115 //of C's, then a non-alphanumeric character like ' '.
117  for(size_t i = 0; i < line.size(); i++) {
118  if(line[i] == 'C') continue;
119  if(isalnum(line[i])) return false;
120  return (i != 0);
121  }
122  return true;
123 }
124 
125 //-------------------------------------------------------------------------------
126 //Checks if there is a "#=" term (what I call the "index specifier") as the
127 //next thing in the input stream "in". After this function, the input stream
128 //will be positioned right after the index specifier, if it exists. Also
129 //checks that this is the correct index for the variable with the given name.
130 void LBNEKeyedInput::readIndexSpec(istringstream& in, cstr_t name) {
131  streampos start = in.tellg();
132  G4int index;
133  in >> index;
134  if(in.get() != '=' || in.fail()) {
135  if(contains(name)) fail("duplicate variable", name);
136  in.clear();
137  in.seekg(start);
138  }
139  else if(index != countTerms(varMap[name]) + 1) {
140  fail("illegal array indexing", name);
141  }
142 }
143 
144 //-------------------------------------------------------------------------------
145 //counts how many words (separated by spaces) are in value
147  istringstream in(value);
148  G4String term;
149  G4int count = 0;
150  in >> ws;
151  while(!in.eof()) {
152  in >> term >> ws;
153  count++;
154  }
155  return count;
156 }
157 //-------------------------------------------------------------------------------
158 //calls G4Exception with a constructed message
159 //-message: description of the nature of the error
160 //-key: optional variable name relating to the error (default: "")
161 //-expected: optional integer that was expected but not actual (default: -1)
163  ostringstream exc;
164  exc << "LBNEKeyedInput error:\n";
165  if(key != "") exc << " variable: " << key << '\n';
166  exc << " message: " << message << '\n';
167  if(expected >= 0) exc << " expected: " << expected << '\n';
168  G4Exception(exc.str().c_str(), " " , FatalException, " " );
169 }
170 //-------------------------------------------------------------------------------
171 //Similar to getVector, but only reads in one value instead of a vector.
172 template<class T>
174  vector<T> myVector;
175  getVector(name, myVector, 1);
176  buffer = myVector[0];
177 }
178 //-------------------------------------------------------------------------------
179 //A template function to parse a variable with the given name, and write
180 //the result to buffer. The general form of this variable is a sequence
181 //of terms, separated by single spaces. Each term will be parsed using one
182 //of the four parse() functions, depending on the type T. length is the
183 //required length of the resulting vector, or -1 if any length is allowed.
184 template<class T>
185 void LBNEKeyedInput::getVector(G4String name, vector<T>& buffer, G4int length)
186 {
187  name.toUpper();
188  if(buffer.size() != 0) fail("non-empty buffer", name);
189  istringstream in(varMap[name]);
190  in >> ws;
191  if(in.eof()) fail("missing or empty variable", name);
192  do {
193  T term;
194  parse(in, term, name);
195  buffer.push_back(term);
196  in >> ws;
197  }while(!in.eof());
198  if(length >= 0 && (size_t)length != buffer.size()) {
199  fail("unexpected array length", name, length);
200  }
201 }
202 
203 
204 //-------------------------------------------------------------------------------
205 //The parse(in, buffer, name) functions will read a single term from the input
206 //stream "in" and write it to buffer. The exact function that is called
207 //depends on the type of buffer. name is the name of the variable, used
208 //for displaying a message in case of failure.
209 
210 void LBNEKeyedInput::parse(istream& in, G4int& buffer, cstr_t name) {
211  in >> buffer;
212  checkGotTerm(in, "expected integer(s)", name);
213 }
214 
215 void LBNEKeyedInput::parse(istream& in, G4double& buffer, cstr_t name) {
216  in >> buffer;
217  checkGotTerm(in, "expected floating point number(s)", name);
218 }
219 
220 void LBNEKeyedInput::parse(istream& in, G4bool& buffer, cstr_t name) {
221  G4String term;
222  in >> term;
223  if(term == "TRUE" || term == "1") buffer = true;
224  else if(term == "FALSE" || term == "0") buffer = false;
225  else in.setstate(ios::failbit);
226  checkGotTerm(in, "expected boolean(s)", name);
227 }
228 
229 void LBNEKeyedInput::parse(istream& in, G4String& buffer, cstr_t name) {
230  in >> buffer;
231  size_t size = buffer.size();
232  if(size >= 2 && buffer[0u] == '\'' && buffer[size-1u] == '\'') {
233  buffer.erase(size-1,1);
234  buffer.erase(0,1);
235  }
236  else {
237  in.setstate(ios::failbit);
238  }
239  checkGotTerm(in,
240  "expected string(s) in single quotes, no spaces within string", name);
241 }
242 
243 //-------------------------------------------------------------------------------
244 //Called by parse functions to make sure the input stream "in" was
245 //successful in reading the data. If not, fail is called with the given
246 //message and variable name.
248  if(in.peek() != ' ' || in.fail()) fail(message, name);
249 }
static QCString name
Definition: declinfo.cpp:673
void readFile(cstr_t filename)
void readStream(std::istream &in)
void getVector(G4String name, std::vector< T > &buffer, G4int length)
const char expected[]
Definition: Exception_t.cc:22
static G4bool isLineComment(cstr_t line)
void handleLine(cstr_t line)
G4bool contains(G4String name) const
void printAll() const
STL namespace.
intermediate_table::const_iterator const_iterator
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:45
string filename
Definition: train.py:213
static void checkGotTerm(std::istream &in, cstr_t message, cstr_t name)
void readIndexSpec(std::istringstream &in, cstr_t name)
const HLTPathStatus fail
const G4String & cstr_t
static void parse(std::istream &in, G4int &buffer, cstr_t name)
void getSingle(cstr_t name, T &buffer)
static G4int countTerms(cstr_t value)
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
static void fail(cstr_t message, cstr_t key="", G4int expected=-1)
start
Definition: test.py:4
void readString(cstr_t data)
static QMap< QCString, MemberDef * > varMap
Definition: vhdldocgen.cpp:713
void get(cstr_t name, G4int &buffer, G4int length=-1)