#include "glib.h"
#include "gprintfint.h"
#include "gthreadprivate.h"
+#include "gunicode.h"
#ifdef G_PLATFORM_WIN32
#define STRICT
return result;
}
-static gchar *
-make_valid_utf8 (const gchar *name)
-{
- GString *string;
- const gchar *remainder, *invalid;
- gint remaining_bytes, valid_bytes;
-
- string = NULL;
- remainder = name;
- remaining_bytes = strlen (name);
-
- while (remaining_bytes != 0)
- {
- if (g_utf8_validate (remainder, remaining_bytes, &invalid))
- break;
- valid_bytes = invalid - remainder;
-
- if (string == NULL)
- string = g_string_sized_new (remaining_bytes);
-
- g_string_append_len (string, remainder, valid_bytes);
- /* append U+FFFD REPLACEMENT CHARACTER */
- g_string_append (string, "\357\277\275");
-
- remaining_bytes -= valid_bytes + 1;
- remainder = invalid + 1;
- }
-
- if (string == NULL)
- return g_strdup (name);
-
- g_string_append (string, remainder);
-
- g_assert (g_utf8_validate (string->str, -1, NULL));
-
- return g_string_free (string, FALSE);
-}
-
/**
* g_filename_display_basename:
* @filename: an absolute pathname in the GLib file name encoding
* by a question mark
*/
if (!display_name)
- display_name = make_valid_utf8 (filename);
+ display_name = _g_utf8_make_valid (filename);
return display_name;
}
&parse_error);
else
{
+ gchar *line_utf8 = _g_utf8_make_valid (line);
g_set_error (error, G_KEY_FILE_ERROR,
G_KEY_FILE_ERROR_PARSE,
_("Key file contains line '%s' which is not "
- "a key-value pair, group, or comment"), line);
+ "a key-value pair, group, or comment"),
+ line_utf8);
+ g_free (line_utf8);
+
return;
}
{
if (g_ascii_strcasecmp (value, "UTF-8") != 0)
{
+ gchar *value_utf8 = _g_utf8_make_valid (value);
g_set_error (error, G_KEY_FILE_ERROR,
G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
- _("Key file contains unsupported encoding '%s'"), value);
+ _("Key file contains unsupported "
+ "encoding '%s'"), value_utf8);
+ g_free (value_utf8);
g_free (key);
g_free (value);
if (!g_utf8_validate (value, -1, NULL))
{
+ gchar *value_utf8 = _g_utf8_make_valid (value);
g_set_error (error, G_KEY_FILE_ERROR,
G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
_("Key file contains key '%s' with value '%s' "
- "which is not UTF-8"), key, value);
+ "which is not UTF-8"), key, value_utf8);
+ g_free (value_utf8);
g_free (value);
+
return NULL;
}
if (!g_utf8_validate (value, -1, NULL))
{
+ gchar *value_utf8 = _g_utf8_make_valid (value);
g_set_error (error, G_KEY_FILE_ERROR,
G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
_("Key file contains key '%s' with value '%s' "
- "which is not UTF-8"), key, value);
+ "which is not UTF-8"), key, value_utf8);
+ g_free (value_utf8);
g_free (value);
+
return NULL;
}
candidate_key, NULL);
g_free (candidate_key);
- if (translated_value && g_utf8_validate (translated_value, -1, NULL))
+ if (translated_value)
break;
g_free (translated_value);
if (*value == '\0' || *end_of_valid_int != '\0')
{
+ gchar *value_utf8 = _g_utf8_make_valid (value);
g_set_error (error, G_KEY_FILE_ERROR,
G_KEY_FILE_ERROR_INVALID_VALUE,
- _("Value '%s' cannot be interpreted as a number."), value);
+ _("Value '%s' cannot be interpreted "
+ "as a number."), value_utf8);
+ g_free (value_utf8);
+
return 0;
}
int_value = long_value;
if (int_value != long_value || errno == ERANGE)
{
+ gchar *value_utf8 = _g_utf8_make_valid (value);
g_set_error (error,
G_KEY_FILE_ERROR,
G_KEY_FILE_ERROR_INVALID_VALUE,
- _("Integer value '%s' out of range"), value);
+ _("Integer value '%s' out of range"),
+ value_utf8);
+ g_free (value_utf8);
+
return 0;
}
double_value = g_ascii_strtod (value, &end_of_valid_d);
if (*end_of_valid_d != '\0' || end_of_valid_d == value)
- g_set_error (error, G_KEY_FILE_ERROR,
- G_KEY_FILE_ERROR_INVALID_VALUE,
- _("Value '%s' cannot be interpreted as a float number."), value);
+ {
+ gchar *value_utf8 = _g_utf8_make_valid (value);
+ g_set_error (error, G_KEY_FILE_ERROR,
+ G_KEY_FILE_ERROR_INVALID_VALUE,
+ _("Value '%s' cannot be interpreted "
+ "as a float number."),
+ value_utf8);
+ g_free (value_utf8);
+ }
return double_value;
}
const gchar *value,
GError **error)
{
+ gchar *value_utf8;
+
if (value)
{
if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
return FALSE;
}
+ value_utf8 = _g_utf8_make_valid (value);
g_set_error (error, G_KEY_FILE_ERROR,
G_KEY_FILE_ERROR_INVALID_VALUE,
- _("Value '%s' cannot be interpreted as a boolean."), value);
+ _("Value '%s' cannot be interpreted "
+ "as a boolean."), value_utf8);
+ g_free (value_utf8);
return FALSE;
}
return result;
}
+
+gchar *
+_g_utf8_make_valid (const gchar *name)
+{
+ GString *string;
+ const gchar *remainder, *invalid;
+ gint remaining_bytes, valid_bytes;
+
+ string = NULL;
+ remainder = name;
+ remaining_bytes = strlen (name);
+
+ while (remaining_bytes != 0)
+ {
+ if (g_utf8_validate (remainder, remaining_bytes, &invalid))
+ break;
+ valid_bytes = invalid - remainder;
+
+ if (string == NULL)
+ string = g_string_sized_new (remaining_bytes);
+
+ g_string_append_len (string, remainder, valid_bytes);
+ /* append U+FFFD REPLACEMENT CHARACTER */
+ g_string_append (string, "\357\277\275");
+
+ remaining_bytes -= valid_bytes + 1;
+ remainder = invalid + 1;
+ }
+
+ if (string == NULL)
+ return g_strdup (name);
+
+ g_string_append (string, remainder);
+
+ g_assert (g_utf8_validate (string->str, -1, NULL));
+
+ return g_string_free (string, FALSE);
+}
+
+
#define __G_UTF8_C__
#include "galiasdef.c"