From 132b0ceb8c30ef82a6c11635eff8e7b41feba357 Mon Sep 17 00:00:00 2001 From: Daniel Elstner Date: Sun, 12 Aug 2007 21:09:00 +0000 Subject: Add -I$(top_builddir) in order to allow to be included. * build_shared/Makefile_build.am_fragment (all_includes): Add -I$(top_builddir) in order to allow to be included. * glib/glibmm/ustring.{cc,h}: Include for the definition of SIZEOF_WCHAR_T. (ustring::FormatStream::stream): Replace accessor with template method that passes its argument onto the stream. Add overload for "const char*" to enable the use of UTF-8 string literals as arguments to ustring::format(). (ustring::FormatStream::FormatStream): Handle exceptions on failure to initialize the locale gracefully. (ustring::format): Adapt to the modified stream() method. (operator<<): Add missing call to ustring::raw() to get the number of bytes instead of code points. Oops. svn path=/trunk/; revision=433 --- ChangeLog | 17 ++++++++++ build_shared/Makefile_build.am_fragment | 2 +- glib/glibmm/ustring.cc | 26 ++++++++++++--- glib/glibmm/ustring.h | 58 +++++++++++++++++++++------------ 4 files changed, 78 insertions(+), 25 deletions(-) diff --git a/ChangeLog b/ChangeLog index 30345d40..b798d206 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +2007-08-12 Daniel Elstner + + * build_shared/Makefile_build.am_fragment (all_includes): Add + -I$(top_builddir) in order to allow to be included. + + * glib/glibmm/ustring.{cc,h}: Include for the + definition of SIZEOF_WCHAR_T. + (ustring::FormatStream::stream): Replace accessor with template + method that passes its argument onto the stream. Add overload + for "const char*" to enable the use of UTF-8 string literals as + arguments to ustring::format(). + (ustring::FormatStream::FormatStream): Handle exceptions on + failure to initialize the locale gracefully. + (ustring::format): Adapt to the modified stream() method. + (operator<<): Add missing call to ustring::raw() to get the + number of bytes instead of code points. Oops. + 2007-08-12 Daniel Elstner * glib/glibmm/ustring.{cc,h}: Add preliminary implementation of diff --git a/build_shared/Makefile_build.am_fragment b/build_shared/Makefile_build.am_fragment index 95a39412..7a8bef44 100644 --- a/build_shared/Makefile_build.am_fragment +++ b/build_shared/Makefile_build.am_fragment @@ -41,7 +41,7 @@ endif common_ldflags = -version-info $(LIBGLIBMM_SO_VERSION) $(no_undefined) -all_includes = -I$(top_builddir)/glib -I$(top_srcdir)/glib \ +all_includes = -I$(top_builddir)/glib -I$(top_srcdir)/glib -I$(top_builddir) \ $(sublib_cflags) $(GTHREAD_CFLAGS) extra_defines = -DG_LOG_DOMAIN=\"$(sublib_name)\" $(extra_win32_defines) \ diff --git a/glib/glibmm/ustring.cc b/glib/glibmm/ustring.cc index 5397659b..57e6ea21 100644 --- a/glib/glibmm/ustring.cc +++ b/glib/glibmm/ustring.cc @@ -28,10 +28,13 @@ #include #include +#include #include +#ifdef GLIBMM_EXCEPTIONS_ENABLED +# include +#endif GLIBMM_USING_STD(find) - namespace { @@ -1251,8 +1254,20 @@ ustring::FormatStream::FormatStream() : stream_ () { - // Use the default locale of the environment. + // Try to use the default locale of the environment, + // but don't abort if it cannot be initialized. +#ifdef GLIBMM_EXCEPTIONS_ENABLED + try + { + stream_.imbue(std::locale("")); + } + catch (const std::runtime_error& error) + { + g_warning("%s: %s", G_STRFUNC, error.what()); + } +#else stream_.imbue(std::locale("")); +#endif /* !GLIBMM_EXCEPTIONS_ENABLED */ } ustring::FormatStream::~FormatStream() @@ -1333,8 +1348,8 @@ std::istream& operator>>(std::istream& is, Glib::ustring& utf8_string) std::ostream& operator<<(std::ostream& os, const Glib::ustring& utf8_string) { GError* error = 0; - const ScopedPtr buf (g_locale_from_utf8(utf8_string.data(), - utf8_string.size(), 0, 0, &error)); + const ScopedPtr buf (g_locale_from_utf8(utf8_string.raw().data(), + utf8_string.raw().size(), 0, 0, &error)); if (error) { #ifdef GLIBMM_EXCEPTIONS_ENABLED @@ -1413,6 +1428,9 @@ std::wostream& operator<<(std::wostream& os, const ustring& utf8_string) const ScopedPtr buf (g_utf8_to_utf16(utf8_string.raw().data(), utf8_string.raw().size(), 0, 0, &error)); #else + // TODO: For some reason the conversion from UTF-8 to WCHAR_T doesn't work + // with g_convert(), while iconv on the command line handles it just fine. + // Maybe a bug in GLib? const ScopedPtr buf (g_convert(utf8_string.raw().data(), utf8_string.raw().size(), "WCHAR_T", "UTF-8", 0, 0, &error)); #endif /* !(__STDC_ISO_10646__ || G_OS_WIN32) */ diff --git a/glib/glibmm/ustring.h b/glib/glibmm/ustring.h index 5d5dbd89..230a349f 100644 --- a/glib/glibmm/ustring.h +++ b/glib/glibmm/ustring.h @@ -698,20 +698,9 @@ private: //The Tru64 compiler needs these partial specializations to be declared here, //as well as defined later. That's probably correct. murrayc. - template - struct SequenceToString; - - template - struct SequenceToString; + template struct SequenceToString; + template struct SequenceToString; - /* - template <> - struct ustring::SequenceToString; - - template <> - struct ustring::SequenceToString; - */ - class FormatStream; #endif /* DOXYGEN_SHOULD_SKIP_THIS */ @@ -770,7 +759,8 @@ public: FormatStream(); ~FormatStream(); - StreamType& stream() { return stream_; } + template inline void stream(const T& value); + inline void stream(const char* value); ustring to_string() const; }; @@ -941,6 +931,19 @@ ustring::SequenceToString::SequenceToString(In pbegin, In pend) } } +/**** Glib::ustring::FormatStream ******************************************/ + +template inline +void ustring::FormatStream::stream(const T& value) +{ + stream_ << value; +} + +inline +void ustring::FormatStream::stream(const char* value) +{ + stream_ << ustring(value); +} /**** Glib::ustring ********************************************************/ @@ -1086,7 +1089,7 @@ template inline // static ustring ustring::format(const T1& a1) { ustring::FormatStream buf; - buf.stream() << a1; + buf.stream(a1); return buf.to_string(); } @@ -1094,7 +1097,8 @@ template inline // static ustring ustring::format(const T1& a1, const T2& a2) { ustring::FormatStream buf; - buf.stream() << a1 << a2; + buf.stream(a1); + buf.stream(a2); return buf.to_string(); } @@ -1102,7 +1106,9 @@ template inline // static ustring ustring::format(const T1& a1, const T2& a2, const T3& a3) { ustring::FormatStream buf; - buf.stream() << a1 << a2 << a3; + buf.stream(a1); + buf.stream(a2); + buf.stream(a3); return buf.to_string(); } @@ -1110,7 +1116,10 @@ template inline // static ustring ustring::format(const T1& a1, const T2& a2, const T3& a3, const T1& a4) { ustring::FormatStream buf; - buf.stream() << a1 << a2 << a3 << a4; + buf.stream(a1); + buf.stream(a2); + buf.stream(a3); + buf.stream(a4); return buf.to_string(); } @@ -1118,7 +1127,11 @@ template inline // static ustring ustring::format(const T1& a1, const T2& a2, const T3& a3, const T1& a4, const T2& a5) { ustring::FormatStream buf; - buf.stream() << a1 << a2 << a3 << a4 << a5; + buf.stream(a1); + buf.stream(a2); + buf.stream(a3); + buf.stream(a4); + buf.stream(a5); return buf.to_string(); } @@ -1127,7 +1140,12 @@ ustring ustring::format(const T1& a1, const T2& a2, const T3& a3, const T1& a4, const T3& a6) { ustring::FormatStream buf; - buf.stream() << a1 << a2 << a3 << a4 << a5 << a6; + buf.stream(a1); + buf.stream(a2); + buf.stream(a3); + buf.stream(a4); + buf.stream(a5); + buf.stream(a6); return buf.to_string(); } -- cgit v1.2.1