PlasCom2  1.0
XPACC Multi-physics simluation application
Configuration.C
Go to the documentation of this file.
1 #include "Configuration.H"
7 namespace ix {
8  namespace util {
9 
14  if(section_name == Name())
15  return(*this);
16  std::vector<util::ConfigParameters>::iterator pi = _parameters.begin();
17  while(pi != _parameters.end()){
18  if(pi->Name() == section_name)
19  return(_parameters[pi-_parameters.begin()]);
20  pi++;
21  }
22  std::cerr << "util::ConfigurationObject::GetSectionParameters::Fatal Error - Section "
23  << section_name << " does not exist. Exiting." << std::endl;
24  util::Error err = 1;
25  exit(1);
26  return(_parameters[0]);
27  };
28 
29  // returns index = _parameters.size() if not found
30  unsigned int util::ConfigurationObject::SectionIndex(const std::string &section_name) const {
31  std::vector<util::ConfigParameters>::const_iterator pi = _parameters.begin();
32  unsigned int index = 0;
33  if(section_name != Name()){
34  while(pi != _parameters.end()){
35  if(pi->Name() == section_name)
36  return (index+1);
37  pi++;
38  index++;
39  }
40  }
41  return(index);
42  };
43 
44  std::string &util::ConfigurationObject::NavigationSection(const std::string &section_name)
45  {
46  return(_sections[SectionIndex(section_name)]);
47  };
48  std::string util::ConfigurationObject::NavigationSection(const std::string &section_name) const
49  {
50  return(_sections[SectionIndex(section_name)]);
51  };
52  std::string util::ConfigurationObject::ExtractSection(const std::string &section_name,std::istream &Inf)
53  {
54  while(Inf)
55  {
56  std::string line;
57  std::getline(Inf,line);
58  if(!line.empty()){
59  if(line[0] == '#'){
60  std::vector<std::string> tokens;
61  util::TokenizeString(tokens,line);
62  if(tokens[0] == "#Section"){
63  if(tokens.size() > 1){
64  if(tokens[1] == section_name){
65  return (this->ReadSection(Inf));
66  }
67 
68  }
69  }
70  }
71  }
72  }
73  return(std::string(""));
74  }
75 
76  std::string util::ConfigurationObject::AdvanceToNextSection(std::istream &Inf)
77  {
78  std::string empty_retval;
79  while(Inf)
80  {
81  std::string line;
82  std::getline(Inf,line);
83  if(!line.empty()){
84  if(line[0] == '#'){
85  std::vector<std::string> tokens;
86  util::TokenizeString(tokens,line);
87  if(tokens[0] == "#Section")
88  if(tokens.size() > 1)
89  return(tokens[1]);
90  }
91  }
92  }
93  return(empty_retval);
94  }
95 
96  std::string util::ConfigurationObject::ReadSection(std::istream &Inf)
97  {
98  std::ostringstream Ostr;
99  while(Inf)
100  {
101  int open_sections = 1;
102  std::string line;
103  std::getline(Inf,line);
104  if(line[0] == '#'){
105  std::vector<std::string> tokens;
106  util::TokenizeString(tokens,line);
107  if(tokens[0] == "#EndSection") {
108  open_sections = open_sections - 1;
109  if(open_sections == 0)
110  return(Ostr.str());
111  }
112  else if(tokens[0] == "#Section") {
113  open_sections++;
114  Ostr << line;
115  }
116  }
117  Ostr << line << std::endl;
118  }
119  return(Ostr.str());
120  }
124  std::ostream &operator<<(std::ostream &Ostr,const util::ConfigurationObject &cob){
125  // Output the configuration of the configuration object itself
126  Ostr << "#" << std::endl
127  << "#Section Configuration" << std::endl
128  << "#" << std::endl
129  << static_cast<util::Parameters>(cob) << std::endl
130  << "#Sections " << cob._sections[0] << std::endl
131  << "#EndSection" << std::endl;
132  // Now output the parameters that configure the application to which
133  // this configuration object applies.
134  std::vector<util::ConfigParameters>::const_iterator pi = cob._parameters.begin();
135  while(pi != cob._parameters.end()){
136  Ostr << "#" << std::endl
137  << "#Section " << pi->Name() << std::endl
138  << "#" << std::endl
139  << static_cast<util::Parameters>(*pi) << std::endl
140  << "#Sections " << cob._sections[pi-cob._parameters.begin()+1] << std::endl
141  << "#EndSection" << std::endl;
142  pi++;
143  }
144  return(Ostr);
145  }
146 
150  std::istream &operator>>(std::istream &Istr,util::ConfigurationObject &cob){
151  // Read in the configuration for the configuration object itself
152  std::string configsection = cob.ExtractSection("Configuration",Istr);
153  cob.Name("Configuration");
154  std::istringstream ConfigIstr(configsection);
155  ConfigIstr >> static_cast<util::Parameters &>(cob);
156  // Now read in the one token name for the application which this
157  // object configures and stuff it into the _sections vector.
158  ConfigIstr.clear();
159  ConfigIstr.str(configsection);
160  std::string line;
161  cob._sections.resize(0);
162  while(std::getline(ConfigIstr,line) && cob._sections.empty()){
163  if(!line.empty()){
164  std::vector<std::string> tokens;
165  util::TokenizeString(tokens,line);
166  std::vector<std::string>::iterator ti = tokens.begin();
167  if(*ti++ == "#Sections"){
168  std::ostringstream Ostr;
169  while(ti != tokens.end())
170  Ostr << *ti++ << " ";
171  cob._sections.push_back(Ostr.str());
172  }
173  }
174  }
175  // Advance to the next section - if found, then read the
176  // section from the input stream and stuff it into the
177  // _parameters vector.
178  std::string sectionname = cob.AdvanceToNextSection(Istr);
179  while(!sectionname.empty()){
180  util::ConfigParameters params(sectionname);
181  std::string section = cob.ReadSection(Istr);
182  std::istringstream SectionIstr(section);
183  SectionIstr >> static_cast<util::Parameters &>(params);
184  cob._parameters.push_back(params);
185  // Re-read the section and extract the sections to
186  // which the currently read section is linked. This
187  // is how we track the relationships between sections
188  // as an aid for automatically generating user interfaces
189  // to the configuration object.
190  SectionIstr.clear();
191  SectionIstr.str(section);
192  std::string line;
193  bool done = false;
194  while(std::getline(SectionIstr,line) && !done){
195  if(!line.empty()){
196  std::vector<std::string> tokens;
197  util::TokenizeString(tokens,line);
198  std::vector<std::string>::iterator ti = tokens.begin();
199  if(*ti++ == "#Sections"){
200  std::ostringstream Ostr;
201  while(ti != tokens.end())
202  Ostr << *ti++ << " ";
203  cob._sections.push_back(Ostr.str());
204  done = true;
205  }
206  }
207  }
208  sectionname = cob.AdvanceToNextSection(Istr);
209  }
210  return(Istr);
211  }
212  };
213 };
virtual std::string ReadSection(std::istream &Inf)
Definition: Configuration.C:96
unsigned int SectionIndex(const std::string &section_name) const
Definition: Configuration.C:30
util::ConfigParameters & Section(const std::string &section_name)
Extract parameters for particular section.
Definition: Configuration.C:13
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.
const std::string & Name() const
Definition: Configuration.H:24
std::istream & operator>>(std::istream &Istr, util::ConfigurationObject &cob)
Stream input operator for util::ConfigurationObject.
virtual std::string AdvanceToNextSection(std::istream &Inf)
Definition: Configuration.C:76
std::vector< util::ConfigParameters > _parameters
Definition: Configuration.H:33
int Error
Error type.
std::vector< std::string > _sections
Definition: Configuration.H:34
Configuration object interface (for config files, etc).
std::string & NavigationSection(const std::string &section_name)
Definition: Configuration.C:44
virtual std::string ExtractSection(const std::string &section_name, std::istream &Inf)
Definition: Configuration.C:52