From 096c92e5c261b3a17d04c614079cae12c3cf4340 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Tue, 31 May 2011 23:17:09 +0300 Subject: [PATCH] * add mu_util_print_encoded, mu_util_printerr_encoded, mu_util_fputs_encoded, for printing where the input (in utf8) is converted to the current locale before printing (WIP) --- src/mu-util.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/mu-util.h | 35 ++++++++++++++++++++++++ 2 files changed, 111 insertions(+) diff --git a/src/mu-util.c b/src/mu-util.c index 6081bb04..ec4f6b25 100644 --- a/src/mu-util.c +++ b/src/mu-util.c @@ -370,3 +370,79 @@ mu_util_get_dtype_with_lstat (const char *path) } + +gboolean +mu_util_fputs_encoded (const char *str, FILE *stream) +{ + char *conv; + GError *err; + int rv; + + g_return_val_if_fail (str, FALSE); + g_return_val_if_fail (stream, FALSE); + + err = NULL; + conv = g_locale_from_utf8 (str, -1, NULL, NULL, &err); + if (err) { + g_printerr ("conversion failed: %s", err->message); + g_error_free (err); + return FALSE; + } + + rv = fputs (conv, stream); + g_free (conv); + + if (rv == EOF) { /* note, apparently, does not set errno */ + g_printerr ("fputs failed"); + return FALSE; + } + + return TRUE; +} + + +static gboolean +print_args (FILE *stream, const char *frm, va_list args) +{ + gchar *str; + gboolean rv; + + str = g_strdup_vprintf (frm, args); + + rv = mu_util_fputs_encoded (str, stream); + + g_free (str); + + return rv; +} + + +gboolean +mu_util_print_encoded (const char *frm, ...) +{ + va_list args; + gboolean rv; + + g_return_val_if_fail (frm, FALSE); + + va_start (args, frm); + rv = print_args (stdout, frm, args); + va_end (args); + + return rv; +} + +gboolean +mu_util_printerr_encoded (const char *frm, ...) +{ + va_list args; + gboolean rv; + + g_return_val_if_fail (frm, FALSE); + + va_start (args, frm); + rv = print_args (stderr, frm, args); + va_end (args); + + return rv; +} diff --git a/src/mu-util.h b/src/mu-util.h index 0ebcba0f..f332ab9f 100644 --- a/src/mu-util.h +++ b/src/mu-util.h @@ -23,6 +23,7 @@ #define __MU_UTIL_H__ #include +#include #include #include /* for mode_t */ @@ -133,6 +134,40 @@ int mu_util_create_writeable_fd (const char* path, mode_t mode, */ gboolean mu_util_is_local_file (const char* path); + +/** + * write a string (assumed to be in utf8-format) to a stream, + * converted to the current locale + * + * @param str a string + * @param stream a stream + * + * @return TRUE if printing worked, FALSE otherwise + */ +gboolean mu_util_fputs_encoded (const char *str, FILE *stream); + +/** + * print a formatted string (assumed to be in utf8-format) to stdout, + * converted to the current locale + * + * @param a standard printf() format string, followed by a parameter list + * + * @return TRUE if printing worked, FALSE otherwise + */ +gboolean mu_util_print_encoded (const char *frm, ...) G_GNUC_PRINTF(1,2); + +/** + * print a formatted string (assumed to be in utf8-format) to stderr, + * converted to the current locale + * + * @param a standard printf() format string, followed by a parameter list + * + * @return TRUE if printing worked, FALSE otherwise + */ +gboolean mu_util_printerr_encoded (const char *frm, ...) G_GNUC_PRINTF(1,2); + + + /** * try to 'play' (ie., open with it's associated program) a * file. depends on xdg-open to do the actual opening