Slightly better patch.

Georg
Index: src/support/docstring.C
===================================================================
--- src/support/docstring.C	(Revision 14975)
+++ src/support/docstring.C	(Arbeitskopie)
@@ -15,9 +15,80 @@
 
 #include <boost/assert.hpp>
 
+#include <locale>
+
+
+#ifdef __GCC__
+namespace {
+
+/// ctype facet for UCS4 streams. Widening and narrowing is restricted to
+/// ASCII, since we do not need anything else.
+class ascii_ctype_facet : public std::ctype<boost::uint32_t>
+{
+public:
+	typedef boost::uint32_t char_type;
+	explicit ascii_ctype_facet(size_t refs = 0)
+		: std::ctype<char_type>(refs) {}
+protected:
+	virtual ~ascii_ctype_facet() {}
+	virtual char_type do_widen(char c) const
+	{
+		if (static_cast<unsigned char>(c) < 128)
+			return c;
+		throw std::bad_cast();
+	}
+	virtual char const * do_widen(char const * lo, char const * hi,
+			char_type * dest) const
+	{
+		while (lo < hi) {
+			if (static_cast<unsigned char>(*lo) >= 128)
+				throw std::bad_cast();
+			*dest = *lo;
+			++lo;
+			++dest;
+		}
+		return hi;
+	}
+	virtual char do_narrow(char_type wc, char) const
+	{
+		if (wc < 128)
+			return static_cast<char>(wc);
+		throw std::bad_cast();
+	}
+	virtual char_type const * do_narrow(char_type const * lo,
+			char_type const * hi, char, char * dest) const
+	{
+		while (lo < hi) {
+			if (*lo < 128)
+				*dest = static_cast<char>(*lo);
+			else
+				throw std::bad_cast();
+			++lo;
+			++dest;
+		}
+		return hi;
+	}
+};
+
+}
+#endif
+
 
 namespace lyx {
 
+void init_docstring()
+{
+#ifdef __GCC__
+	// Add our own ctype facet to the global locale, since
+	// std::ctype<boost::uint32_t> of gcc throws bad casts in
+	// do_widen/do_narrow even for ASCII characters.
+	std::locale global;
+	std::locale locale(global, new ascii_ctype_facet);
+	std::locale::global(locale);
+#endif
+}
+
+
 docstring const from_ascii(char const * ascii)
 {
 	docstring s;
Index: src/support/docstring.h
===================================================================
--- src/support/docstring.h	(Revision 14975)
+++ src/support/docstring.h	(Arbeitskopie)
@@ -18,6 +18,9 @@
 
 namespace lyx {
 
+/// Installs locale facets that are needed for dealing with docstrings
+void init_docstring();
+
 /// String type for storing the main text in UCS4 encoding
 typedef std::basic_string<boost::uint32_t> docstring;
 
Index: src/main.C
===================================================================
--- src/main.C	(Revision 14975)
+++ src/main.C	(Arbeitskopie)
@@ -15,6 +15,7 @@
 #include "lyx_main.h"
 #include "gettext.h"
 
+#include "support/docstring.h"
 #include "support/os.h"
 
 #include <boost/filesystem/path.hpp>
@@ -44,5 +45,7 @@ int main(int argc, char * argv[])
 	// initialize for internationalized version *EK*
 	locale_init();
 
+	lyx::init_docstring();
+
 	return LyX::exec(argc, argv);
 }

Reply via email to