1 | /* Copyright © 2005-2007 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/sbuild-parse-error.h> |
---|
23 | #include <sbuild/sbuild-log.h> |
---|
24 | |
---|
25 | #include <string> |
---|
26 | #include <sstream> |
---|
27 | |
---|
28 | namespace sbuild |
---|
29 | { |
---|
30 | |
---|
31 | enum parse_value_error_code |
---|
32 | { |
---|
33 | BAD_VALUE ///< The value could not be parsed. |
---|
34 | }; |
---|
35 | |
---|
36 | typedef parse_error<parse_value_error_code> parse_value_error; |
---|
37 | |
---|
38 | /** |
---|
39 | * Parse a boolean value. |
---|
40 | * @param value the value to parse. |
---|
41 | * @param parsed_value the variable to store the parsed value. |
---|
42 | * @returns true on success, false on failure. |
---|
43 | */ |
---|
44 | void |
---|
45 | parse_value (std::string const& value, |
---|
46 | bool& parsed_value); |
---|
47 | |
---|
48 | /** |
---|
49 | * Parse a string value. |
---|
50 | * @param value the value to parse. |
---|
51 | * @param parsed_value the variable to store the parsed value. |
---|
52 | * @returns true on success, false on failure. |
---|
53 | */ |
---|
54 | void |
---|
55 | parse_value (std::string const& value, |
---|
56 | std::string& parsed_value); |
---|
57 | |
---|
58 | /** |
---|
59 | * Parse a value of type T. |
---|
60 | * @param value the value to parse. |
---|
61 | * @param parsed_value the variable to store the parsed value. |
---|
62 | * @returns true on success, false on failure. |
---|
63 | */ |
---|
64 | template <typename T> |
---|
65 | void |
---|
66 | parse_value (std::string const& value, |
---|
67 | T& parsed_value) |
---|
68 | { |
---|
69 | std::istringstream is(value); |
---|
70 | is.imbue(std::locale::classic()); |
---|
71 | T tmpval; |
---|
72 | if (is >> tmpval) |
---|
73 | { |
---|
74 | parsed_value = tmpval; |
---|
75 | log_debug(DEBUG_NOTICE) << "value=" << parsed_value << std::endl; |
---|
76 | } |
---|
77 | else |
---|
78 | { |
---|
79 | log_debug(DEBUG_NOTICE) << "parse error" << std::endl; |
---|
80 | throw parse_value_error(value, BAD_VALUE); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | } |
---|
85 | |
---|
86 | #endif /* SBUILD_PARSE_VALUE_H */ |
---|
87 | |
---|
88 | /* |
---|
89 | * Local Variables: |
---|
90 | * mode:C++ |
---|
91 | * End: |
---|
92 | */ |
---|