sbuild-parse-value.h

00001 /* Copyright © 2005-2007  Roger Leigh <rleigh@debian.org>
00002  *
00003  * schroot is free software: you can redistribute it and/or modify it
00004  * under the terms of the GNU General Public License as published by
00005  * the Free Software Foundation, either version 3 of the License, or
00006  * (at your option) any later version.
00007  *
00008  * schroot is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see
00015  * <http://www.gnu.org/licenses/>.
00016  *
00017  *********************************************************************/
00018 
00019 #ifndef SBUILD_PARSE_VALUE_H
00020 #define SBUILD_PARSE_VALUE_H
00021 
00022 #include <sbuild/sbuild-parse-error.h>
00023 #include <sbuild/sbuild-log.h>
00024 
00025 #include <string>
00026 #include <sstream>
00027 
00028 namespace sbuild
00029 {
00030 
00032   enum parse_value_error_code
00033     {
00034       BAD_VALUE 
00035     };
00036 
00038   typedef parse_error<parse_value_error_code> parse_value_error;
00039 
00046   void
00047   parse_value (std::string const& value,
00048                bool&              parsed_value);
00049 
00056   void
00057   parse_value (std::string const& value,
00058                std::string&       parsed_value);
00059 
00066   template <typename T>
00067   void
00068   parse_value (std::string const& value,
00069                T& parsed_value)
00070   {
00071     std::istringstream is(value);
00072     is.imbue(std::locale::classic());
00073     T tmpval;
00074     if (is >> tmpval)
00075       {
00076         parsed_value = tmpval;
00077         log_debug(DEBUG_NOTICE) << "value=" << parsed_value << std::endl;
00078       }
00079     else
00080       {
00081         log_debug(DEBUG_NOTICE) << "parse error" << std::endl;
00082         throw parse_value_error(value, BAD_VALUE);
00083       }
00084   }
00085 
00086 }
00087 
00088 #endif /* SBUILD_PARSE_VALUE_H */
00089 
00090 /*
00091  * Local Variables:
00092  * mode:C++
00093  * End:
00094  */