GDataInputStream: Add _utf8() variants of _read_line

These will validate the resulting line, and throw a conversion error.
In practice these will likely be used by bindings, but it's good
for even C apps too that don't want to explode if that text file
they're reading into Pango actually has invalid UTF-8.

https://bugzilla.gnome.org/show_bug.cgi?id=652758
This commit is contained in:
Colin Walters
2011-06-16 14:00:36 -04:00
parent ff2f46a7f4
commit 28254a38a7
4 changed files with 166 additions and 0 deletions

View File

@@ -150,6 +150,78 @@ test_read_lines_any (void)
test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_ANY);
}
static void
test_read_lines_LF_valid_utf8 (void)
{
GInputStream *stream;
GInputStream *base_stream;
GError *error = NULL;
char *line;
guint n_lines = 0;
base_stream = g_memory_input_stream_new ();
stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream),
"foo\nthis is valid UTF-8 ☺!\nbar\n", -1, NULL);
/* Test read line */
error = NULL;
while (TRUE)
{
gsize length = -1;
line = g_data_input_stream_read_line_utf8 (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
g_assert_no_error (error);
if (line == NULL)
break;
n_lines++;
g_free (line);
}
g_assert_cmpint (n_lines, ==, 3);
g_object_unref (base_stream);
g_object_unref (stream);
}
static void
test_read_lines_LF_invalid_utf8 (void)
{
GInputStream *stream;
GInputStream *base_stream;
GError *error = NULL;
char *line;
guint n_lines = 0;
base_stream = g_memory_input_stream_new ();
stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream),
"foo\nthis is not valid UTF-8 \xE5 =(\nbar\n", -1, NULL);
/* Test read line */
error = NULL;
while (TRUE)
{
gsize length = -1;
line = g_data_input_stream_read_line_utf8 (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
if (n_lines == 0)
g_assert_no_error (error);
else
{
g_assert (error != NULL);
g_clear_error (&error);
g_free (line);
break;
}
n_lines++;
g_free (line);
}
g_assert_cmpint (n_lines, ==, 1);
g_object_unref (base_stream);
g_object_unref (stream);
}
static void
test_read_until (void)
{
@@ -417,6 +489,8 @@ main (int argc,
g_test_add_func ("/data-input-stream/basic", test_basic);
g_test_add_func ("/data-input-stream/read-lines-LF", test_read_lines_LF);
g_test_add_func ("/data-input-stream/read-lines-LF-valid-utf8", test_read_lines_LF_valid_utf8);
g_test_add_func ("/data-input-stream/read-lines-LF-invalid-utf8", test_read_lines_LF_invalid_utf8);
g_test_add_func ("/data-input-stream/read-lines-CR", test_read_lines_CR);
g_test_add_func ("/data-input-stream/read-lines-CR-LF", test_read_lines_CR_LF);
g_test_add_func ("/data-input-stream/read-lines-any", test_read_lines_any);