|
From: Johnny O. <js...@us...> - 2010-10-28 20:47:16
|
Module: performous
Branch: opengl2
Commit: fe36c33058dc282c04cd67ee2b0ae767bbb68669
Author: Tapio Vierros <tap...@gm...>
Date: Tue Oct 26 18:58:46 2010 +0300
Implement convertToUTF8 with glib instead of glibmm.
---
game/unicode.cc | 31 ++++++++++++++++++++++---------
1 files changed, 22 insertions(+), 9 deletions(-)
diff --git a/game/unicode.cc b/game/unicode.cc
index b2a8852..c2129a5 100644
--- a/game/unicode.cc
+++ b/game/unicode.cc
@@ -1,28 +1,41 @@
#include "unicode.hh"
+#include <boost/scoped_ptr.hpp>
#include <glibmm/ustring.h>
-#include <glibmm/convert.h>
+#include <glib/gconvert.h>
#include <sstream>
+#include <stdexcept>
+
+namespace {
+ // Convert a string using Glib, throw exception on error.
+ // This is in fact (slightly modified) Glib::convert from glibmm.
+ std::string convert(const std::string& str, const std::string& to_codeset, const std::string& from_codeset) {
+ gsize bytes_written = 0;
+ GError* gerror = 0;
+
+ char *const buf = g_convert(
+ str.data(), str.size(), to_codeset.c_str(), from_codeset.c_str(),
+ 0, &bytes_written, &gerror);
+
+ if (gerror) throw std::runtime_error("Conversion error"); // Throw on error
+
+ return std::string(boost::scoped_ptr<char>(buf).get(), bytes_written);
+ }
+}
-// FIXME: Glib::convert may throw a Glib::ConvertError that doesn't
-// get caught in Windows builds with dynamic glibmm.
-#ifdef _WIN32
-void convertToUTF8( std::stringstream &, std::string ) {
-#else
void convertToUTF8( std::stringstream &_stream, std::string _filename ) {
try {
- Glib::convert(_stream.str(), "UTF-8", "UTF-8"); // Test if input is UTF-8
+ convert(_stream.str(), "UTF-8", "UTF-8"); // Test if input is UTF-8
} catch(...) {
if (!_filename.empty()) std::clog << "unicode/warning: " << _filename << " is not UTF-8.\n Assuming CP1252 for now. Use recode CP1252..UTF-8 */*.txt to convert your files." << std::endl;
try {
- _stream.str(Glib::convert(_stream.str(), "UTF-8", "CP1252")); // Convert from Microsoft CP1252
+ _stream.str(convert(_stream.str(), "UTF-8", "CP1252")); // Convert from Microsoft CP1252
} catch (...) {
// Filter out anything but ASCII
std::string tmp;
for (char ch; _stream.get(ch);) tmp += (ch >= 0x20 && ch < 0x7F) ? ch : '?';
}
}
-#endif
}
std::string unicodeCollate(std::string const& str) {
|