sbuild  1.7.1
parse-value.h
1 /* Copyright © 2005-2013 Roger Leigh <rleigh@debian.org>
2  *
3  * schroot is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * schroot is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  *********************************************************************/
18 
19 #ifndef SBUILD_PARSE_VALUE_H
20 #define SBUILD_PARSE_VALUE_H
21 
22 #include <sbuild/parse-error.h>
23 #include <sbuild/log.h>
24 
25 #include <string>
26 #include <sstream>
27 
28 namespace sbuild
29 {
30 
33  {
35  };
36 
39 
46  void
47  parse_value (const std::string& value,
48  bool& parsed_value);
49 
56  void
57  parse_value (const std::string& value,
58  std::string& parsed_value);
59 
66  template <typename T>
67  void
68  parse_value (const std::string& value,
69  T& parsed_value)
70  {
71  std::istringstream is(value);
72  is.imbue(std::locale::classic());
73  T tmpval;
74  if (is >> tmpval)
75  {
76  parsed_value = tmpval;
77  log_debug(DEBUG_NOTICE) << "value=" << parsed_value << std::endl;
78  }
79  else
80  {
81  log_debug(DEBUG_NOTICE) << "parse error" << std::endl;
82  throw parse_value_error(value, BAD_VALUE);
83  }
84  }
85 
86 }
87 
88 #endif /* SBUILD_PARSE_VALUE_H */
89 
90 /*
91  * Local Variables:
92  * mode:C++
93  * End:
94  */