Added functions to convert GError messages (which are in UTF-8 charset)

to terminal charset.

src/strutil.h: added conv_gerror_message member to str_class structure.
Added str_conv_gerror_message function declaration.

src/strutil.c (str_conv_gerror_message): new function to convert GError
message to terminal charset.

src/strutil8bit.c (str_8bit_conv_gerror_message): new function to convert GError
message to terminal charset for 8-bit locales.

src/strutilascii.c (str_ascii_conv_gerror_message): new function to convert GError
message to terminal charset for 7-bit locales.

src/strutilutf8.c (str_utf8_conv_gerror_message): new function to convert GError
message to terminal charset for utf-8 locales.
This commit is contained in:
Andrew Borodin 2009-04-29 20:08:33 +04:00
parent 45210ad625
commit 15e1db5157
5 changed files with 67 additions and 1 deletions

View File

@ -252,6 +252,12 @@ str_nconvert (GIConv coder, const char *string, int size, GString * buffer)
return _str_convert (coder, string, size, buffer);
}
gchar *
str_conv_gerror_message (GError *error, const char *def_msg)
{
return used_class.conv_gerror_message (error, def_msg);
}
estr_t
str_vfs_convert_from (GIConv coder, const char *string, GString * buffer)
{

View File

@ -82,6 +82,7 @@ extern GIConv str_cnv_not_convert;
/* all functions in str_class must be defined for every encoding */
struct str_class {
gchar *(*conv_gerror_message) (GError *error, const char *def_msg); /*I*/
estr_t (*vfs_convert_to) (GIConv coder, const char *string,
int size, GString *buffer); /*I*/
void (*insert_replace_char) (GString *buffer);
@ -160,9 +161,15 @@ void str_close_conv (GIConv);
* otherwise return ESTR_PROBLEM or ESTR_FAILURE
*/
estr_t str_convert (GIConv, const char *, GString *);
estr_t str_nconvert (GIConv, const char *, int, GString *);
/* convert GError message (which in UTF-8) to terminal charset
* def_char is used if result of error->str conversion if ESTR_FAILURE
* return new allocated null-terminated string, which is need to be freed
* I
*/
gchar *str_conv_gerror_message (GError *error, const char *def_msg);
/* return only ESTR_SUCCESS or ESTR_FAILURE, because vfs must be able to convert
* result to original string. (so no replace with questionmark)
* if coder is str_cnv_from_term or str_cnv_not_convert, string is only copied,

View File

@ -165,6 +165,36 @@ str_8bit_length2 (const char *text, int size)
return (size >= 0) ? min (strlen (text), (gsize)size) : strlen (text);
}
static gchar *
str_8bit_conv_gerror_message (GError *error, const char *def_msg)
{
GIConv conv;
gchar *ret;
/* glib messages are in UTF-8 charset */
conv = str_crt_conv_from ("UTF-8");
if (conv == INVALID_CONV)
ret = g_strdup (def_msg != NULL ? def_msg : "");
else {
GString *buf;
buf = g_string_new ("");
if (str_convert (conv, error->message, buf) != ESTR_FAILURE) {
ret = buf->str;
g_string_free (buf, FALSE);
} else {
ret = g_strdup (def_msg != NULL ? def_msg : "");
g_string_free (buf, TRUE);
}
str_close_conv (conv);
}
return ret;
}
static estr_t
str_8bit_vfs_convert_to (GIConv coder, const char *string,
int size, GString * buffer)
@ -644,6 +674,7 @@ str_8bit_init ()
{
struct str_class result;
result.conv_gerror_message = str_8bit_conv_gerror_message;
result.vfs_convert_to = str_8bit_vfs_convert_to;
result.insert_replace_char = str_8bit_insert_replace_char;
result.is_valid_string = str_8bit_is_valid_string;

View File

@ -164,6 +164,16 @@ str_ascii_length2 (const char *text, int size)
return (size >= 0) ? min (strlen (text), (gsize) size) : strlen (text);
}
static gchar *
str_ascii_conv_gerror_message (GError *error, const char *def_msg)
{
/* the same as str_utf8_conv_gerror_message() */
if ((error != NULL) && (error->message != NULL))
return g_strdup (error->message);
return g_strdup (def_msg != NULL ? def_msg : "");
}
static estr_t
str_ascii_vfs_convert_to (GIConv coder, const char *string,
int size, GString * buffer)
@ -671,6 +681,7 @@ str_ascii_init ()
{
struct str_class result;
result.conv_gerror_message = str_ascii_conv_gerror_message;
result.vfs_convert_to = str_ascii_vfs_convert_to;
result.insert_replace_char = str_ascii_insert_replace_char;
result.is_valid_string = str_ascii_is_valid_string;

View File

@ -329,6 +329,16 @@ str_utf8_questmark_sustb (char **string, size_t * left, GString * buffer)
g_string_append_c (buffer, '?');
}
*/
static gchar *
str_utf8_conv_gerror_message (GError *error, const char *def_msg)
{
if ((error != NULL) && (error->message != NULL))
return g_strdup (error->message);
return g_strdup (def_msg != NULL ? def_msg : "");
}
static estr_t
str_utf8_vfs_convert_to (GIConv coder, const char *string,
int size, GString * buffer)
@ -1291,6 +1301,7 @@ str_utf8_init ()
{
struct str_class result;
result.conv_gerror_message = str_utf8_conv_gerror_message;
result.vfs_convert_to = str_utf8_vfs_convert_to;
result.insert_replace_char = str_utf8_insert_replace_char;
result.is_valid_string = str_utf8_is_valid_string;