Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
LBNEKeyedInput Class Reference

#include <LBNEKeyedInput.hh>

Public Member Functions

 LBNEKeyedInput ()
 
void readFile (cstr_t filename)
 
void readString (cstr_t data)
 
void readStream (std::istream &in)
 
G4bool contains (G4String name) const
 
void get (cstr_t name, G4int &buffer, G4int length=-1)
 
void get (cstr_t name, G4double &buffer, G4int length=-1)
 
void get (cstr_t name, G4bool &buffer, G4int length=-1)
 
void get (cstr_t name, G4String &buffer, G4int length=-1)
 
void get (cstr_t name, std::vector< G4int > &buffer, G4int length=-1)
 
void get (cstr_t name, std::vector< G4double > &buffer, G4int length=-1)
 
void get (cstr_t name, std::vector< G4bool > &buffer, G4int length=-1)
 
void get (cstr_t name, std::vector< G4String > &buffer, G4int length=-1)
 
void printAll () const
 

Private Types

typedef std::map< G4String, G4String > ssmap_t
 
typedef const G4String & cstr_t
 

Private Member Functions

void handleLine (cstr_t line)
 
void readIndexSpec (std::istringstream &in, cstr_t name)
 
template<class T >
void getSingle (cstr_t name, T &buffer)
 
template<class T >
void getVector (G4String name, std::vector< T > &buffer, G4int length)
 

Static Private Member Functions

static G4bool isLineComment (cstr_t line)
 
static G4int countTerms (cstr_t value)
 
static void fail (cstr_t message, cstr_t key="", G4int expected=-1)
 
static void parse (std::istream &in, G4int &buffer, cstr_t name)
 
static void parse (std::istream &in, G4double &buffer, cstr_t name)
 
static void parse (std::istream &in, G4bool &buffer, cstr_t name)
 
static void parse (std::istream &in, G4String &buffer, cstr_t name)
 
static void checkGotTerm (std::istream &in, cstr_t message, cstr_t name)
 

Private Attributes

ssmap_t varMap
 

Detailed Description

Definition at line 44 of file LBNEKeyedInput.hh.

Member Typedef Documentation

typedef const G4String& LBNEKeyedInput::cstr_t
private

Definition at line 48 of file LBNEKeyedInput.hh.

typedef std::map<G4String,G4String> LBNEKeyedInput::ssmap_t
private

Definition at line 47 of file LBNEKeyedInput.hh.

Constructor & Destructor Documentation

LBNEKeyedInput::LBNEKeyedInput ( )
explicit

Definition at line 11 of file LBNEKeyedInput.cc.

11 {}

Member Function Documentation

void LBNEKeyedInput::checkGotTerm ( std::istream &  in,
cstr_t  message,
cstr_t  name 
)
staticprivate

Definition at line 247 of file LBNEKeyedInput.cc.

247  {
248  if(in.peek() != ' ' || in.fail()) fail(message, name);
249 }
static QCString name
Definition: declinfo.cpp:673
static void fail(cstr_t message, cstr_t key="", G4int expected=-1)
G4bool LBNEKeyedInput::contains ( G4String  name) const

Definition at line 44 of file LBNEKeyedInput.cc.

44  {
45  name.toUpper();
46  return (varMap.find(name) != varMap.end());
47 }
static QCString name
Definition: declinfo.cpp:673
G4int LBNEKeyedInput::countTerms ( cstr_t  value)
staticprivate

Definition at line 146 of file LBNEKeyedInput.cc.

146  {
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 }
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
void LBNEKeyedInput::fail ( cstr_t  message,
cstr_t  key = "",
G4int  expected = -1 
)
staticprivate

Definition at line 162 of file LBNEKeyedInput.cc.

162  {
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 }
const char expected[]
Definition: Exception_t.cc:22
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:45
void LBNEKeyedInput::get ( cstr_t  name,
G4int &  buffer,
G4int  length = -1 
)

Definition at line 57 of file LBNEKeyedInput.cc.

58  {getSingle(name, buffer);}
static QCString name
Definition: declinfo.cpp:673
void getSingle(cstr_t name, T &buffer)
void LBNEKeyedInput::get ( cstr_t  name,
G4double &  buffer,
G4int  length = -1 
)

Definition at line 59 of file LBNEKeyedInput.cc.

60  {getSingle(name, buffer);}
static QCString name
Definition: declinfo.cpp:673
void getSingle(cstr_t name, T &buffer)
void LBNEKeyedInput::get ( cstr_t  name,
G4bool &  buffer,
G4int  length = -1 
)

Definition at line 61 of file LBNEKeyedInput.cc.

62  {getSingle(name, buffer);}
static QCString name
Definition: declinfo.cpp:673
void getSingle(cstr_t name, T &buffer)
void LBNEKeyedInput::get ( cstr_t  name,
G4String &  buffer,
G4int  length = -1 
)

Definition at line 63 of file LBNEKeyedInput.cc.

64  {getSingle(name, buffer);}
static QCString name
Definition: declinfo.cpp:673
void getSingle(cstr_t name, T &buffer)
void LBNEKeyedInput::get ( cstr_t  name,
std::vector< G4int > &  buffer,
G4int  length = -1 
)

Definition at line 66 of file LBNEKeyedInput.cc.

67  {getVector(name, buffer, length);}
static QCString name
Definition: declinfo.cpp:673
void getVector(G4String name, std::vector< T > &buffer, G4int length)
void LBNEKeyedInput::get ( cstr_t  name,
std::vector< G4double > &  buffer,
G4int  length = -1 
)

Definition at line 68 of file LBNEKeyedInput.cc.

69  {getVector(name, buffer, length);}
static QCString name
Definition: declinfo.cpp:673
void getVector(G4String name, std::vector< T > &buffer, G4int length)
void LBNEKeyedInput::get ( cstr_t  name,
std::vector< G4bool > &  buffer,
G4int  length = -1 
)

Definition at line 70 of file LBNEKeyedInput.cc.

71  {getVector(name, buffer, length);}
static QCString name
Definition: declinfo.cpp:673
void getVector(G4String name, std::vector< T > &buffer, G4int length)
void LBNEKeyedInput::get ( cstr_t  name,
std::vector< G4String > &  buffer,
G4int  length = -1 
)

Definition at line 72 of file LBNEKeyedInput.cc.

73  {getVector(name, buffer, length);}
static QCString name
Definition: declinfo.cpp:673
void getVector(G4String name, std::vector< T > &buffer, G4int length)
template<class T >
void LBNEKeyedInput::getSingle ( cstr_t  name,
T &  buffer 
)
private

Definition at line 173 of file LBNEKeyedInput.cc.

173  {
174  vector<T> myVector;
175  getVector(name, myVector, 1);
176  buffer = myVector[0];
177 }
static QCString name
Definition: declinfo.cpp:673
void getVector(G4String name, std::vector< T > &buffer, G4int length)
template<class T >
void LBNEKeyedInput::getVector ( G4String  name,
std::vector< T > &  buffer,
G4int  length 
)
private

Definition at line 185 of file LBNEKeyedInput.cc.

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 }
static QCString name
Definition: declinfo.cpp:673
static void parse(std::istream &in, G4int &buffer, cstr_t name)
static void fail(cstr_t message, cstr_t key="", G4int expected=-1)
void LBNEKeyedInput::handleLine ( cstr_t  line)
private

Definition at line 92 of file LBNEKeyedInput.cc.

92  {
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 }
static G4bool isLineComment(cstr_t line)
void readIndexSpec(std::istringstream &in, cstr_t name)
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
G4bool LBNEKeyedInput::isLineComment ( cstr_t  line)
staticprivate

Definition at line 116 of file LBNEKeyedInput.cc.

116  {
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 }
void LBNEKeyedInput::parse ( std::istream &  in,
G4int &  buffer,
cstr_t  name 
)
staticprivate

Definition at line 210 of file LBNEKeyedInput.cc.

210  {
211  in >> buffer;
212  checkGotTerm(in, "expected integer(s)", name);
213 }
static QCString name
Definition: declinfo.cpp:673
static void checkGotTerm(std::istream &in, cstr_t message, cstr_t name)
void LBNEKeyedInput::parse ( std::istream &  in,
G4double &  buffer,
cstr_t  name 
)
staticprivate

Definition at line 215 of file LBNEKeyedInput.cc.

215  {
216  in >> buffer;
217  checkGotTerm(in, "expected floating point number(s)", name);
218 }
static QCString name
Definition: declinfo.cpp:673
static void checkGotTerm(std::istream &in, cstr_t message, cstr_t name)
void LBNEKeyedInput::parse ( std::istream &  in,
G4bool &  buffer,
cstr_t  name 
)
staticprivate

Definition at line 220 of file LBNEKeyedInput.cc.

220  {
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 }
static QCString name
Definition: declinfo.cpp:673
static void checkGotTerm(std::istream &in, cstr_t message, cstr_t name)
void LBNEKeyedInput::parse ( std::istream &  in,
G4String &  buffer,
cstr_t  name 
)
staticprivate

Definition at line 229 of file LBNEKeyedInput.cc.

229  {
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 }
static QCString name
Definition: declinfo.cpp:673
static void checkGotTerm(std::istream &in, cstr_t message, cstr_t name)
void LBNEKeyedInput::printAll ( ) const

Definition at line 77 of file LBNEKeyedInput.cc.

77  {
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 }
intermediate_table::const_iterator const_iterator
void LBNEKeyedInput::readFile ( cstr_t  filename)

Definition at line 19 of file LBNEKeyedInput.cc.

19  {
20  ifstream in(filename.c_str());
21  if(!in.is_open()) fail("unable to open file");
22  readStream(in);
23  in.close();
24 }
void readStream(std::istream &in)
string filename
Definition: train.py:213
static void fail(cstr_t message, cstr_t key="", G4int expected=-1)
void LBNEKeyedInput::readIndexSpec ( std::istringstream &  in,
cstr_t  name 
)
private

Definition at line 130 of file LBNEKeyedInput.cc.

130  {
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 }
static QCString name
Definition: declinfo.cpp:673
G4bool contains(G4String name) const
static G4int countTerms(cstr_t value)
static void fail(cstr_t message, cstr_t key="", G4int expected=-1)
start
Definition: test.py:4
void LBNEKeyedInput::readStream ( std::istream &  in)

Definition at line 31 of file LBNEKeyedInput.cc.

31  {
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 }
void handleLine(cstr_t line)
static void fail(cstr_t message, cstr_t key="", G4int expected=-1)
void LBNEKeyedInput::readString ( cstr_t  data)

Definition at line 26 of file LBNEKeyedInput.cc.

26  {
27  istringstream in(data);
28  readStream(in);
29 }
void readStream(std::istream &in)

Member Data Documentation

ssmap_t LBNEKeyedInput::varMap
private

Definition at line 92 of file LBNEKeyedInput.hh.


The documentation for this class was generated from the following files: