Files
sdcv/src/utils.cpp
Evgeniy Dushistov d2327e2a0f Import patch from Roman Imankulov:
"sdcv" does not set up `rl_readline_name' variable which can be extremely useful for the writing the .inputrc file. Additionally sdcv does not use readline in the "Your choice [-1 to abort]" dialog. This patch fix both these issues, readline_name is set up to "sdcv".
This trick allows me to add into the .inputrc
$if sdcv
"\e\e": "-1\n"
$endif
and type double-escape in the "Your choice" dialog instead of pretty annoying "-1".
2013-07-06 13:40:11 +00:00

76 lines
2.0 KiB
C++

/*
* This file part of sdcv - console version of Stardict program
* http://sdcv.sourceforge.net
* Copyright (C) 2005-2006 Evgeniy <dushistov@mail.ru>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <glib.h>
#include <glib/gi18n.h>
#include <cstdlib>
#include <cstdio>
#include "utils.hpp"
std::string utf8_to_locale_ign_err(const std::string& utf8_str)
{
gsize bytes_read, bytes_written;
GError *err=NULL;
std::string res;
const char * charset;
if (g_get_charset(&charset))
res=utf8_str;
else {
gchar *tmp=g_convert_with_fallback(utf8_str.c_str(), -1, charset, "UTF-8", NULL,
&bytes_read, &bytes_written, &err);
if (NULL==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);
}
return res;
}
char *locale_to_utf8(const char *loc_str)
{
if(NULL==loc_str)
return NULL;
gsize bytes_read;
gsize bytes_written;
GError *err=NULL;
gchar *str=NULL;
str=g_locale_to_utf8(loc_str, -1, &bytes_read, &bytes_written, &err);
if(NULL==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;
}