gboolean mu_util_fputs_encoded (const char *str, FILE *stream) { int rv; char *conv; g_return_val_if_fail (stream, FALSE); /* g_get_charset return TRUE when the locale is UTF8 */ if (mu_util_locale_is_utf8()) return fputs (str, stream) == EOF ? FALSE : TRUE; /* charset is _not_ utf8, so we need to convert it */ conv = NULL; if (g_utf8_validate (str, -1, NULL)) conv = g_locale_from_utf8 (str, -1, NULL, NULL, NULL); /* conversion failed; this happens because is some cases GMime may gives * us non-UTF-8 strings from e.g. wrongly encoded message-subjects; if * so, we escape the string */ conv = conv ? conv : g_strescape (str, "\n\t"); rv = conv ? fputs (conv, stream) : EOF; g_free (conv); return (rv == EOF) ? FALSE : TRUE; }
gboolean mu_util_fputs_encoded (const char *str, FILE *stream) { int rv; g_return_val_if_fail (str, FALSE); g_return_val_if_fail (stream, FALSE); /* g_get_charset return TRUE when the locale is UTF8 */ if (mu_util_locale_is_utf8()) rv = fputs (str, stream); else { /* charset is _not_ utf8, so we actually have to * convert it..*/ GError *err; unsigned bytes; char *conv; err = NULL; conv = g_locale_from_utf8 (str, -1, (gsize*)&bytes, NULL, &err); if (!conv || err) { /* conversion failed; this happens because is * some cases GMime may gives us non-UTF-8 * strings from e.g. wrongly encoded * message-subjects; if so, we escape the * string */ g_warning ("%s: g_locale_from_utf8 failed: %s", __FUNCTION__, err ? err->message : "conversion failed"); g_free (conv); conv = g_strescape (str, NULL); } g_clear_error (&err); rv = fputs (conv, stream); g_free (conv); } return (rv == EOF) ? FALSE : TRUE; }
gboolean mu_util_fputs_encoded (const char *str, FILE *stream) { char *conv; int rv; g_return_val_if_fail (str, FALSE); g_return_val_if_fail (stream, FALSE); /* g_get_charset return TRUE when the locale is UTF8 */ if (mu_util_locale_is_utf8()) rv = fputs (str, stream); else { /* charset is _not_ utf8, so we actually have to * convert it..*/ GError *err; unsigned bytes; err = NULL; conv = g_locale_from_utf8 (str, -1, &bytes, NULL, &err); if (err) { /* conversion failed; this happens because is * some cases GMime may gives us non-UTF-8 * string from e.g. wrongly encoded * message-subjects; if so, we escape the * string */ g_free (conv); conv = g_strescape (str, NULL); 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; }
const char* mu_date_str_s (const char* frm, time_t t) { struct tm *tmbuf; static char buf[128]; static int is_utf8 = -1; size_t len; if (G_UNLIKELY(is_utf8 == -1)) is_utf8 = mu_util_locale_is_utf8 () ? 1 : 0; g_return_val_if_fail (frm, NULL); tmbuf = localtime(&t); len = strftime (buf, sizeof(buf) - 1, frm, tmbuf); if (len == 0) return ""; /* not necessarily an error... */ if (!is_utf8) { /* charset is _not_ utf8, so we need to convert it, so * the date could contain locale-specific characters*/ gchar *conv; GError *err; err = NULL; conv = g_locale_to_utf8 (buf, -1, NULL, NULL, &err); if (err) { g_warning ("conversion failed: %s", err->message); g_error_free (err); strcpy (buf, "<error>"); } else strncpy (buf, conv, sizeof(buf)); g_free (conv); } return buf; }
gboolean mu_util_fputs_encoded (const char *str, FILE *stream) { int rv; unsigned bytes; char *conv; g_return_val_if_fail (str, FALSE); g_return_val_if_fail (stream, FALSE); /* g_get_charset return TRUE when the locale is UTF8 */ if (mu_util_locale_is_utf8()) return fputs (str, stream) == EOF ? FALSE : TRUE; /* charset is _not_ utf8, so we actually have to convert * it */ conv = NULL; if (g_utf8_validate (str, -1, NULL)) /* it _seems_ that on the bsds, the final err param * may receive garbage... so we don't use it */ conv = g_locale_from_utf8 (str, -1, (gsize*)&bytes, NULL, NULL); /* conversion failed; this happens because is some cases GMime * may gives us non-UTF-8 strings from e.g. wrongly encoded * message-subjects; if so, we escape the string */ if (!conv) conv = g_strescape (str, "\n\t"); rv = conv ? fputs (conv, stream) : EOF; g_free (conv); return (rv == EOF) ? FALSE : TRUE; }