diff --git a/src/utils.cpp b/src/utils.cpp index 98580bd..5c78ef9 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,4 +1,4 @@ -/* +/* * This file part of sdcv - console version of Stardict program * http://sdcv.sourceforge.net * Copyright (C) 2005-2006 Evgeniy @@ -31,24 +31,22 @@ std::string utf8_to_locale_ign_err(const std::string& utf8_str) { - gsize bytes_read, bytes_written; - GError *err=nullptr; std::string res; - - const char * charset; + + const char *charset; if (g_get_charset(&charset)) - res=utf8_str; + res = utf8_str; else { - gchar *tmp=g_convert_with_fallback(utf8_str.c_str(), -1, charset, "UTF-8", nullptr, - &bytes_read, &bytes_written, &err); - if (nullptr==tmp){ + gsize bytes_read, bytes_written; + glib::Error err; + glib::CharStr tmp(g_convert_with_fallback(utf8_str.c_str(), -1, charset, "UTF-8", nullptr, + &bytes_read, &bytes_written, get_addr(err))); + if (nullptr == get_impl(tmp)){ fprintf(stderr, _("Can not convert %s to current locale.\n"), utf8_str.c_str()); fprintf(stderr, "%s\n", err->message); - g_error_free(err); exit(EXIT_FAILURE); } - res=tmp; - g_free(tmp); + res = get_impl(tmp); } return res; @@ -56,20 +54,16 @@ std::string utf8_to_locale_ign_err(const std::string& utf8_str) char *locale_to_utf8(const char *loc_str) { - if(nullptr==loc_str) + if (nullptr == loc_str) return nullptr; - gsize bytes_read; - gsize bytes_written; - GError *err=nullptr; - gchar *str=nullptr; - str=g_locale_to_utf8(loc_str, -1, &bytes_read, &bytes_written, &err); - if(nullptr==str){ + gsize bytes_read, bytes_written; + glib::Error err; + gchar *str = g_locale_to_utf8(loc_str, -1, &bytes_read, &bytes_written, get_addr(err)); + if (nullptr == str){ fprintf(stderr, _("Can not convert %s to utf8.\n"), loc_str); fprintf(stderr, "%s\n", err->message); - g_error_free(err); exit(EXIT_FAILURE); } return str; } - diff --git a/src/utils.hpp b/src/utils.hpp index 87ac1fe..2fa41dd 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -21,12 +21,13 @@ public: } friend inline T *get_impl(const ResourceWrapper& rw) { - return rw.p_; + return rw.p_; } friend inline T **get_addr(ResourceWrapper& rw) { - return &rw.p_; + return &rw.p_; } + private: T *p_; @@ -56,4 +57,3 @@ namespace glib { extern char *locale_to_utf8(const char *locstr); extern std::string utf8_to_locale_ign_err(const std::string& utf8_str); -