Accept \r\n as line end. (#163030, Bastian Nocera)

2005-01-19  Matthias Clasen  <mclasen@redhat.com>

	* glib/gkeyfile.c (g_key_file_parse_data): Accept \r\n as
	line end.  (#163030, Bastian Nocera)
	(g_key_file_load_from_data): Make -1 work as a size.

	* tests/keyfile-test.c: Some unit tests for the keyfile
	parser.

	* tests/Makefile.am (test_programs): Add keyfile-test.
This commit is contained in:
Matthias Clasen 2005-01-19 14:14:27 +00:00 committed by Matthias Clasen
parent b45420bca8
commit 92a710ba28
7 changed files with 155 additions and 2 deletions

View File

@ -1,3 +1,14 @@
2005-01-19 Matthias Clasen <mclasen@redhat.com>
* glib/gkeyfile.c (g_key_file_parse_data): Accept \r\n as
line end. (#163030, Bastian Nocera)
(g_key_file_load_from_data): Make -1 work as a size.
* tests/keyfile-test.c: Some unit tests for the keyfile
parser.
* tests/Makefile.am (test_programs): Add keyfile-test.
2005-01-19 Tor Lillqvist <tml@novell.com>
* glib/gbacktrace.c (g_on_error_stack_trace): On Win32 call

View File

@ -1,3 +1,14 @@
2005-01-19 Matthias Clasen <mclasen@redhat.com>
* glib/gkeyfile.c (g_key_file_parse_data): Accept \r\n as
line end. (#163030, Bastian Nocera)
(g_key_file_load_from_data): Make -1 work as a size.
* tests/keyfile-test.c: Some unit tests for the keyfile
parser.
* tests/Makefile.am (test_programs): Add keyfile-test.
2005-01-19 Tor Lillqvist <tml@novell.com>
* glib/gbacktrace.c (g_on_error_stack_trace): On Win32 call

View File

@ -1,3 +1,14 @@
2005-01-19 Matthias Clasen <mclasen@redhat.com>
* glib/gkeyfile.c (g_key_file_parse_data): Accept \r\n as
line end. (#163030, Bastian Nocera)
(g_key_file_load_from_data): Make -1 work as a size.
* tests/keyfile-test.c: Some unit tests for the keyfile
parser.
* tests/Makefile.am (test_programs): Add keyfile-test.
2005-01-19 Tor Lillqvist <tml@novell.com>
* glib/gbacktrace.c (g_on_error_stack_trace): On Win32 call

View File

@ -1,3 +1,14 @@
2005-01-19 Matthias Clasen <mclasen@redhat.com>
* glib/gkeyfile.c (g_key_file_parse_data): Accept \r\n as
line end. (#163030, Bastian Nocera)
(g_key_file_load_from_data): Make -1 work as a size.
* tests/keyfile-test.c: Some unit tests for the keyfile
parser.
* tests/Makefile.am (test_programs): Add keyfile-test.
2005-01-19 Tor Lillqvist <tml@novell.com>
* glib/gbacktrace.c (g_on_error_stack_trace): On Win32 call

View File

@ -485,8 +485,9 @@ g_key_file_load_from_file (GKeyFile *key_file,
* @flags: flags from #GKeyFileFlags
* @error: return location for a #GError, or %NULL
*
* Loads a key file from memory into an empty #GKeyFile structure.
* If the object cannot be created then %error is set to a #GKeyFileError.
* Loads a key file from memory into an empty #GKeyFile structure. If
* the object cannot be created then %error is set to a
* #GKeyFileError.
*
* Return value: %TRUE if a key file could be loaded, %FALSE othewise
* Since: 2.6
@ -504,6 +505,9 @@ g_key_file_load_from_data (GKeyFile *key_file,
g_return_val_if_fail (data != NULL, FALSE);
g_return_val_if_fail (length != 0, FALSE);
if (length == (gsize)-1)
length = strlen (data);
if (key_file->approximate_size > 0)
{
g_key_file_clear (key_file);
@ -847,6 +851,11 @@ g_key_file_parse_data (GKeyFile *key_file,
{
if (data[i] == '\n')
{
if (i > 0 && data[i - 1] == '\r')
g_string_erase (key_file->parse_buffer,
key_file->parse_buffer->len - 1,
1);
/* When a newline is encountered flush the parse buffer so that the
* line can be parsed. Note that completely blank lines won't show
* up in the parse buffer, so they get parsed directly.

View File

@ -74,6 +74,7 @@ test_programs = \
gio-test \
hash-test \
iochannel-test \
keyfile-test \
list-test \
mainloop-test \
markup-escape-test \
@ -127,6 +128,7 @@ env_test_LDADD = $(progs_ldadd)
gio_test_LDADD = $(progs_ldadd)
hash_test_LDADD = $(progs_ldadd)
iochannel_test_LDADD = $(progs_ldadd)
keyfile_test_LDADD = $(progs_ldadd)
list_test_LDADD = $(progs_ldadd)
mainloop_test_LDADD = $(thread_ldadd)
markup_test_LDADD = $(progs_ldadd)

98
tests/keyfile-test.c Normal file
View File

@ -0,0 +1,98 @@
#include <glib.h>
#include <string.h>
#include <stdlib.h>
static GKeyFile *
load_data (const gchar *data)
{
GKeyFile *keyfile;
GError *error = NULL;
keyfile = g_key_file_new ();
g_key_file_load_from_data (keyfile, data, -1, 0, &error);
if (error)
{
g_print ("Could not load data: %s\n", error->message);
exit (1);
}
return keyfile;
}
static void
check_string_value (GKeyFile *keyfile,
const gchar *group,
const gchar *key,
const gchar *expected)
{
GError *error = NULL;
gchar *value;
value = g_key_file_get_string (keyfile, group, key, &error);
if (error)
{
g_print ("Group %s key %s: %s\n", group, key, error->message);
exit (1);
}
g_assert (value != NULL);
if (strcmp (value, expected) != 0)
{
g_print ("Group %s key %s: "
"expected value '%s', actual value '%s'\n",
group, key, expected, value);
exit (1);
}
}
static void
test_line_ends (void)
{
GKeyFile *keyfile;
const gchar *data =
"[group1]\n"
"key1=value1\n"
"key2=value2\r\n"
"[group2]\r\n"
"key3=value3\r\r\n"
"key4=value4\n";
keyfile = load_data (data);
check_string_value (keyfile, "group1", "key1", "value1");
check_string_value (keyfile, "group1", "key2", "value2");
check_string_value (keyfile, "group2", "key3", "value3\r");
check_string_value (keyfile, "group2", "key4", "value4");
}
static void
test_whitespace (void)
{
GKeyFile *keyfile;
const gchar *data =
"[group1]\n"
"key1 = value1\n"
"key2\t=\tvalue2\n"
" [ group2 ] \n"
"key3 = value3 \n"
"key4 = value \t4\n";
keyfile = load_data (data);
check_string_value (keyfile, "group1", "key1", "value1");
check_string_value (keyfile, "group1", "key2", "value2");
check_string_value (keyfile, " group2 ", "key3", "value3 ");
check_string_value (keyfile, " group2 ", "key4", "value \t4");
}
int
main (int argc, char *argv[])
{
test_line_ends ();
test_whitespace ();
return 0;
}