sbuild-parse-value.h

Go to the documentation of this file.
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 
00031   enum parse_value_error_code
00032     {
00033       BAD_VALUE 
00034     };
00035 
00036   typedef parse_error<parse_value_error_code> parse_value_error;
00037 
00044   void
00045   parse_value (std::string const& value,
00046                bool&              parsed_value);
00047 
00054   void
00055   parse_value (std::string const& value,
00056                std::string&       parsed_value);
00057 
00064   template <typename T>
00065   void
00066   parse_value (std::string const& value,
00067                T& parsed_value)
00068   {
00069     std::istringstream is(value);
00070     is.imbue(std::locale::classic());
00071     T tmpval;
00072     if (is >> tmpval)
00073       {
00074         parsed_value = tmpval;
00075         log_debug(DEBUG_NOTICE) << "value=" << parsed_value << std::endl;
00076       }
00077     else
00078       {
00079         log_debug(DEBUG_NOTICE) << "parse error" << std::endl;
00080         throw parse_value_error(value, BAD_VALUE);
00081       }
00082   }
00083 
00084 }
00085 
00086 #endif /* SBUILD_PARSE_VALUE_H */
00087 
00088 /*
00089  * Local Variables:
00090  * mode:C++
00091  * End:
00092  */

Generated on Sat Jul 5 11:58:47 2008 for sbuild by  doxygen 1.5.6