* 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)
This commit is contained in:
Dirk-Jan C. Binnema 2011-05-31 23:17:09 +03:00
parent c81f13cbdd
commit 096c92e5c2
2 changed files with 111 additions and 0 deletions

View File

@ -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;
}

View File

@ -23,6 +23,7 @@
#define __MU_UTIL_H__
#include <glib.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h> /* 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