]> git.lyx.org Git - lyx.git/blobdiff - src/support/docstring.C
Don't use a global variable for avoiding removal of the system temp dir
[lyx.git] / src / support / docstring.C
index b8a0c4c21f90de3893448fffbcb7945105197cd0..0b7f796628286eb5cd0b1c4daf99b60f83dcbd42 100644 (file)
@@ -11,6 +11,7 @@
 #include <config.h>
 
 #include "docstring.h"
+#include "qstring_helpers.h"
 #include "unicode.h"
 
 #include <locale>
@@ -91,6 +92,30 @@ std::string const to_utf8(docstring const & ucs4)
 }
 
 
+docstring const from_local8bit(std::string const & s)
+{
+       return qstring_to_ucs4(QString::fromLocal8Bit(s.data(), s.length()));
+}
+
+
+const char* to_local8bit_failure::what() const throw()
+{
+       return "A string could not be converted from unicode to the local 8 bit encoding.";
+}
+
+
+std::string const to_local8bit(docstring const & s)
+{
+       // This conversion can fail, depending on input.
+       if (s.empty())
+               return std::string();
+       QByteArray const local = toqstr(s).toLocal8Bit();
+       if (local.size() == 0)
+               throw to_local8bit_failure();
+       return std::string(local.begin(), local.end());
+}
+
+
 bool operator==(lyx::docstring const & l, char const * r)
 {
        int const len = l.length();
@@ -414,15 +439,69 @@ public:
        };
 
 protected:
+       iter_type
+       do_put(iter_type oit, std::ios_base & b, char_type fill, bool v) const
+       {
+               return do_put_helper(oit, b, fill, v);
+       }
+
        iter_type
        do_put(iter_type oit, std::ios_base & b, char_type fill, long v) const
+       {
+               return do_put_helper(oit, b, fill, v);
+       }
+
+       iter_type
+       do_put(iter_type oit, std::ios_base & b, char_type fill, unsigned long v) const
+       {
+               return do_put_helper(oit, b, fill, v);
+       }
+
+#ifdef _GLIBCXX_USE_LONG_LONG
+       iter_type
+       do_put(iter_type oit, std::ios_base & b, char_type fill, long long v) const
+       {
+               return do_put_helper(oit, b, fill, v);
+       }
+
+       iter_type
+       do_put(iter_type oit, std::ios_base & b, char_type fill, unsigned long long v) const
+       {
+               return do_put_helper(oit, b, fill, v);
+       }
+#endif
+
+       iter_type
+       do_put(iter_type oit, std::ios_base & b, char_type fill, double v) const
+       {
+               return do_put_helper(oit, b, fill, v);
+       }
+
+       iter_type
+       do_put(iter_type oit, std::ios_base & b, char_type fill, long double v) const
+       {
+               return do_put_helper(oit, b, fill, v);
+       }
+
+       iter_type
+       do_put(iter_type oit, std::ios_base & b, char_type fill, void const * v) const
+       {
+               return do_put_helper(oit, b, fill, v);
+       }
+
+private:
+       template <typename ValueType>
+       iter_type
+       do_put_helper(iter_type oit, std::ios_base & b, char_type fill, ValueType v) const
        {
                if (fill >= 0x80)
                        throw num_put_failure();
 
-               std::string s;
-               // 64 is large enough
-               s.resize(64);
+               std::streamsize const sz = b.width() > b.precision() ?
+                                          b.width() : b.precision();
+               // 64 is large enough, unless width or precision are bigger
+               std::streamsize const wd = (sz > 56 ? sz : 56) + 8;
+               std::string s(wd, '\0');
                string_num_put_facet f;
                std::string::const_iterator cit = s.begin();
                std::string::const_iterator end =
@@ -468,6 +547,11 @@ protected:
                s.reserve(64);
                for (; iit != eit && isNumpunct(*iit); ++iit)
                        s += static_cast<char>(*iit);
+               // We add another character, not part of the numpunct facet,
+               // in order to avoid setting the eofbit in the stream state,
+               // which would prevent any further read. The space seems a
+               // good choice here.
+               s += ' ';
                string_num_get_facet f;
                f.get(s.begin(), s.end(), b, err, v);