use glib wrappers where possible for safe memory freeing

This commit is contained in:
Evgeniy Dushistov
2013-07-07 17:07:02 +00:00
parent 9034e792b6
commit c3d733b1fc
2 changed files with 18 additions and 24 deletions

View File

@@ -1,4 +1,4 @@
/* /*
* This file part of sdcv - console version of Stardict program * This file part of sdcv - console version of Stardict program
* http://sdcv.sourceforge.net * http://sdcv.sourceforge.net
* Copyright (C) 2005-2006 Evgeniy <dushistov@mail.ru> * Copyright (C) 2005-2006 Evgeniy <dushistov@mail.ru>
@@ -31,24 +31,22 @@
std::string utf8_to_locale_ign_err(const std::string& utf8_str) std::string utf8_to_locale_ign_err(const std::string& utf8_str)
{ {
gsize bytes_read, bytes_written;
GError *err=nullptr;
std::string res; std::string res;
const char * charset; const char *charset;
if (g_get_charset(&charset)) if (g_get_charset(&charset))
res=utf8_str; res = utf8_str;
else { else {
gchar *tmp=g_convert_with_fallback(utf8_str.c_str(), -1, charset, "UTF-8", nullptr, gsize bytes_read, bytes_written;
&bytes_read, &bytes_written, &err); glib::Error err;
if (nullptr==tmp){ 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, _("Can not convert %s to current locale.\n"), utf8_str.c_str());
fprintf(stderr, "%s\n", err->message); fprintf(stderr, "%s\n", err->message);
g_error_free(err);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
res=tmp; res = get_impl(tmp);
g_free(tmp);
} }
return res; 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) char *locale_to_utf8(const char *loc_str)
{ {
if(nullptr==loc_str) if (nullptr == loc_str)
return nullptr; return nullptr;
gsize bytes_read; gsize bytes_read, bytes_written;
gsize bytes_written; glib::Error err;
GError *err=nullptr; gchar *str = g_locale_to_utf8(loc_str, -1, &bytes_read, &bytes_written, get_addr(err));
gchar *str=nullptr; if (nullptr == str){
str=g_locale_to_utf8(loc_str, -1, &bytes_read, &bytes_written, &err);
if(nullptr==str){
fprintf(stderr, _("Can not convert %s to utf8.\n"), loc_str); fprintf(stderr, _("Can not convert %s to utf8.\n"), loc_str);
fprintf(stderr, "%s\n", err->message); fprintf(stderr, "%s\n", err->message);
g_error_free(err);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return str; return str;
} }

View File

@@ -21,12 +21,13 @@ public:
} }
friend inline T *get_impl(const ResourceWrapper& rw) { friend inline T *get_impl(const ResourceWrapper& rw) {
return rw.p_; return rw.p_;
} }
friend inline T **get_addr(ResourceWrapper& rw) { friend inline T **get_addr(ResourceWrapper& rw) {
return &rw.p_; return &rw.p_;
} }
private: private:
T *p_; T *p_;
@@ -56,4 +57,3 @@ namespace glib {
extern char *locale_to_utf8(const char *locstr); extern char *locale_to_utf8(const char *locstr);
extern std::string utf8_to_locale_ign_err(const std::string& utf8_str); extern std::string utf8_to_locale_ign_err(const std::string& utf8_str);