msbGrid  1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
parametertreeparser.hh
Go to the documentation of this file.
1 /*****************************************************************************
2 * This program is part of the msbGrid software *
3 * *
4 * msbGrid stands for multi-structured block Grid generator *
5 * *
6 * msbGrid is a free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * msbGrid is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * See the file COPYING for full copying permissions. *
17 *****************************************************************************/
23 #ifndef _PARAMETER_TREE_PARSER_HH_
24 #define _PARAMETER_TREE_PARSER_HH_
25 
26 #include <set>
27 
28 namespace msbGrid
29 {
30 class ParameterTreeParser;
31 } /* namespace msbGrid */
32 
33 class msbGrid :: ParameterTreeParser
34 {
35  static std :: string ltrim(const std :: string &s);
36  static std :: string rtrim(const std :: string &s);
37 
38 public:
39  static void readINITree(std :: string file,
40  ParameterTree &pt,
41  bool overwrite = true);
42 
43  static void readINITree(std :: istream &in,
44  ParameterTree &pt,
45  const std :: string srcname,
46  bool overwrite);
47 };
48 
50  ParameterTree &pt,
51  bool overwrite)
52 {
53  std :: ifstream in( file.c_str() );
54 
55  if ( !in ) {
56  std :: cerr << "IOError : Could not open configuration file " << file << "\n\nCalling exit(1)\n";
57  exit(1);
58  }
59 
60  readINITree(in, pt, "file '" + file + "'", overwrite);
61 }
62 
64  ParameterTree &pt,
65  const std :: string srcname,
66  bool overwrite)
67 {
68  std :: set< std :: string > keysInFile;
69  int lineIdx = 1;
70  while ( !in.eof() ) {
71  std :: string line;
72  getline(in, line);
73  line = ltrim(line);
74  switch ( line [ 0 ] ) {
75  case '#':
76  break;
77 
78  default:
79  std :: string :: size_type comment = line.find("#");
80  line = line.substr(0, comment);
81  std :: string :: size_type mid = line.find("=");
82  if ( mid != std :: string :: npos ) {
83  std :: string key = rtrim( ltrim( line.substr(0, mid) ) );
84  std :: string value = ltrim( line.substr(mid + 1) );
85 
86  if ( value.length() > 0 ) {
87  // handle quoted strings
88  if ( ( value [ 0 ] == '\'' )or(value [ 0 ] == '"') ) {
89  char quote = value [ 0 ];
90  value = value.substr(1);
91  while ( * ( rtrim(value).rbegin() ) != quote ) {
92  if ( !in.eof() ) {
93  std :: string l;
94  getline(in, l);
95  value = value + "\n" + l;
96  } else {
97  value = value + quote;
98  }
99  }
100 
101  value = rtrim(value);
102  value = value.substr(0, value.length() - 1);
103  } else {
104  value = rtrim(value);
105  }
106  }
107 
108  if ( keysInFile.count(key) != 0 ) {
109  std :: cerr << "Exception, " << "Key '" << key << "' appears twice in " << srcname << " !" << "\n\nCalling exit(1)\n";
110  exit(1);
111  } else {
112  if ( overwrite || !pt.hasKey(key) ) {
113  pt [ key ] = value;
114  }
115 
116  keysInFile.insert(key);
117  }
118  }
119 
120  break;
121  }
122  }
123 }
124 
125 std :: string msbGrid :: ParameterTreeParser :: ltrim(const std :: string &s)
126 {
127  std :: size_t firstNonWS = s.find_first_not_of(" \t\n\r");
128 
129  if ( firstNonWS != std :: string :: npos ) {
130  return s.substr(firstNonWS);
131  }
132 
133  return std :: string();
134 }
135 
136 std :: string msbGrid :: ParameterTreeParser :: rtrim(const std :: string &s)
137 {
138  std :: size_t lastNonWS = s.find_last_not_of(" \t\n\r");
139 
140  if ( lastNonWS != std :: string :: npos ) {
141  return s.substr(0, lastNonWS + 1);
142  }
143 
144  return std :: string();
145 }
146 
147 #endif /* #ifndef _PARAMETER_TREE_PARSER_HH_ */