/** * \file convert.cpp * This file is part of LyX, the document processor. * Licence details can be found in the file COPYING. * * \author André Pönitz * \author Lars Gullik Bjønnes * * Full author contact details are available in file CREDITS. */ #include #include "support/convert.h" #include "support/docstring.h" #include #include #include //needed for Mac OSX 10.5.2 Leopard #include using namespace std; namespace { // A version of lexical cast that does not throw. Useful for when we convert to string template To lexical_cast(From const & value, To const & defaultResult = To()) { try { return boost::lexical_cast(value); } catch(...) { // Ignore all exceptions and use default. return defaultResult; } } } // namespace namespace lyx { template<> string convert(bool b) { return (b ? "true" : "false"); } template<> string convert(char c) { return string(1, c); } template<> string convert(short unsigned int sui) { return lexical_cast(sui); } template<> string convert(int i) { return lexical_cast(i); } template<> docstring convert(int i) { return from_ascii(lexical_cast(i)); } template<> string convert(unsigned int ui) { return lexical_cast(ui); } template<> docstring convert(unsigned int ui) { return from_ascii(lexical_cast(ui)); } template<> string convert(unsigned long ul) { return lexical_cast(ul); } template<> docstring convert(unsigned long ul) { return from_ascii(lexical_cast(ul)); } #ifdef HAVE_LONG_LONG_INT template<> string convert(unsigned long long ull) { return lexical_cast(ull); } template<> docstring convert(unsigned long long ull) { return from_ascii(lexical_cast(ull)); } #endif template<> string convert(long l) { return lexical_cast(l); } template<> docstring convert(long l) { return from_ascii(lexical_cast(l)); } #ifdef HAVE_LONG_LONG_INT template<> string convert(long long ll) { return lexical_cast(ll); } template<> docstring convert(long long ll) { return from_ascii(lexical_cast(ll)); } #endif template<> string convert(float f) { std::ostringstream val; val << f; return val.str(); } template<> string convert(double d) { std::ostringstream val; val << d; return val.str(); } template<> docstring convert(double d) { return from_ascii(convert(d)); } template<> int convert(string const s) { return strtol(s.c_str(), 0, 10); } template<> int convert(docstring const s) { return strtol(to_ascii(s).c_str(), 0, 10); } template<> unsigned int convert(string const s) { return strtoul(s.c_str(), 0, 10); } template<> unsigned long convert(string const s) { return strtoul(s.c_str(), 0, 10); } template<> double convert(string const s) { return strtod(s.c_str(), 0); } template<> int convert(char const * cptr) { return strtol(cptr, 0, 10); } template<> double convert(char const * cptr) { return strtod(cptr, 0); } } // namespace lyx