PlasCom2  1.0
XPACC Multi-physics simluation application
Parameters.C
Go to the documentation of this file.
1 #include "Parameters.H"
7 
8 
9 namespace ix {
10  namespace util {
11 
13  {
14  util::Parameters::const_iterator pi = this->begin();
15  while(pi != this->end()){
16  unsigned int index = pi - this->begin();
17  if(pi->first == key)
18  return(&((*this)[index]));
19  pi++;
20  }
21  return(NULL);
22  }
23 
24  std::string util::Parameters::GetValue(const std::string &key) const
25  {
26  return(this->Param(key));
27  // std::string retval(this->Param(key));
28  // std::string value(this->Param(key));
29  // if(!value.empty()){
30  // std::istringstream Istr(value);
31  // Istr >> retval;
32  // }
33  // return(retval);
34  }
35 
36  std::vector<std::string> util::Parameters::GetValueVector(const std::string &key) const
37  {
38  std::vector<std::string> retval;
39  std::string value(this->Param(key));
40  if(!value.empty()){
41  std::istringstream Istr(value);
42  std::string tmpval;
43  while(Istr >> tmpval)
44  retval.push_back(tmpval);
45  }
46  return(retval);
47  }
48 
49  std::istream &util::Parameters::ReadFromStream(std::istream &Is)
50  {
51  std::string streamLine;
52  int n = 0;
53  while(Is){
54  streamLine.erase();
55  std::getline(Is,streamLine);
56  n++;
57  if(streamLine.empty())
58  continue;
59  std::string lineToken;
60  std::istringstream skipWhiteSpace(streamLine);
61  std::ostringstream lineOut;
62  while(skipWhiteSpace >> std::skipws >> lineToken)
63  lineOut << lineToken << " ";
64  std::string line(lineOut.str());
65 
66  std::string::size_type x = line.find('#');
67  line = line.substr(0,x);
68  if(!line.empty()){
69  x = line.find('=');
70  if(x == std::string::npos)
71  return(Is);
72  util::ParamType param;
73  std::istringstream Instr(line.substr(0,x));
74  Instr >> param.Key();
75  std::vector<std::string> tokens;
76  line = line.substr(x+1,line.size());
77  TokenizeString(tokens,line);
78  std::ostringstream Ostr;
79  std::vector<std::string>::iterator ti = tokens.begin();
80  if(ti != tokens.end())
81  Ostr << *ti++;
82  while(ti != tokens.end())
83  Ostr << " " << *ti++;
84  param.Value() = Ostr.str();
85  this->push_back(param);
86  }
87  }
88  return(Is);
89  }
90 
91  std::string util::Parameters::Param(const std::string &key) const
92  {
93  std::string empty;
94  util::Parameters::const_iterator pi = this->begin();
95  while(pi != this->end()){
96  if(pi->first == key){
97  // if((key.find("Fields") != std::string::npos) &&
98  // (pi->first.find("Fields") != std::string::npos)){
99  // std::cout << "\"" << key << "\" =MATCHES= \""
100  // << pi->first << "\"" << std::endl;
101  // }
102  return(pi->second);
103  } else {
104  // if((key.find("Fields") != std::string::npos) &&
105  // (pi->first.find("Fields") != std::string::npos)){
106  // std::cout << "\"" << key << "\" =NOT= \""
107  // << pi->first << "\"" << std::endl;
108  // }
109  }
110  pi++;
111  }
112  return(empty);
113  }
114 
115  bool util::Parameters::IsSet(const std::string &key) const
116  {
117  return(!Param(key).empty());
118  };
119 
120 
121  std::ostream &util::Parameters::WriteToStream(std::ostream &oSt) const
122  {
123  util::Parameters::const_iterator pi = this->begin();
124  while(pi != this->end()){
125  oSt << *pi++;
126  if(pi != this->end())
127  oSt << std::endl;
128  }
129  return(oSt);
130  };
131 
132  std::ostream &operator<<(std::ostream &oSt,
133  const util::Parameters &pv)
134  {
135  return(pv.WriteToStream(oSt));
136  }
137 
138  std::istream &operator>>(std::istream &iSt,
139  util::Parameters &pv)
140  {
141  return(pv.ReadFromStream(iSt));
142  }
143 
144 
145  std::ostream &operator<<(std::ostream &Ostr,const util::ParamType &param)
146  {
147  Ostr << param.Key() << " = " << param.Value();
148  return(Ostr);
149  }
150 
151  };
152 };
153 
154 
util::ParamType * ParamPtr(const std::string &key)
Definition: Parameters.C:12
void TokenizeString(std::vector< std::string > &tokens, const std::string &source)
Tokenize string.
Defines MPI-specific parallel global and program classes.
std::ostream & operator<<(std::ostream &Ostr, const util::ConfigurationObject &cob)
Stream output operator for util::ConfigurationObject.
std::istream & operator>>(std::istream &Istr, util::ConfigurationObject &cob)
Stream input operator for util::ConfigurationObject.
std::string Param(const std::string &Key) const
Definition: Parameters.C:91
void const size_t const size_t const size_t const double const double * x
std::string GetValue(const std::string &key) const
Definition: Parameters.C:24
std::vector< std::string > GetValueVector(const std::string &key) const
Definition: Parameters.C:36
virtual std::ostream & WriteToStream(std::ostream &Os) const
Definition: Parameters.C:121
Parameters object interface (for config files, etc)
Simple key-value pair.
bool IsSet(const std::string &Key) const
Definition: Parameters.C:115
virtual std::istream & ReadFromStream(std::istream &Is)
Definition: Parameters.C:49