Don't reverse the order of multiline comments.

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

	* glib/gkeyfile.c (g_key_file_get_key_comment):
	(g_key_file_get_top_comment): Don't reverse the order of multiline
	comments.
	(g_key_file_get_group_comment): Actually get the group comment.

	* tests/keyfile-test.c (test_comments): Test that comments are
	handled properly.  (#309263, Mikael Magnusson)
This commit is contained in:
Matthias Clasen
2005-07-01 18:54:25 +00:00
committed by Matthias Clasen
parent 61b7337092
commit 9d6e10125b
6 changed files with 175 additions and 66 deletions

View File

@@ -1,5 +1,10 @@
2005-07-01 Matthias Clasen <mclasen@redhat.com> 2005-07-01 Matthias Clasen <mclasen@redhat.com>
* glib/gkeyfile.c (g_key_file_get_key_comment):
(g_key_file_get_top_comment): Don't reverse the order of multiline
comments.
(g_key_file_get_group_comment): Actually get the group comment.
* tests/keyfile-test.c (test_comments): Test that comments are * tests/keyfile-test.c (test_comments): Test that comments are
handled properly. (#309263, Mikael Magnusson) handled properly. (#309263, Mikael Magnusson)

View File

@@ -1,5 +1,10 @@
2005-07-01 Matthias Clasen <mclasen@redhat.com> 2005-07-01 Matthias Clasen <mclasen@redhat.com>
* glib/gkeyfile.c (g_key_file_get_key_comment):
(g_key_file_get_top_comment): Don't reverse the order of multiline
comments.
(g_key_file_get_group_comment): Actually get the group comment.
* tests/keyfile-test.c (test_comments): Test that comments are * tests/keyfile-test.c (test_comments): Test that comments are
handled properly. (#309263, Mikael Magnusson) handled properly. (#309263, Mikael Magnusson)

View File

@@ -1,5 +1,10 @@
2005-07-01 Matthias Clasen <mclasen@redhat.com> 2005-07-01 Matthias Clasen <mclasen@redhat.com>
* glib/gkeyfile.c (g_key_file_get_key_comment):
(g_key_file_get_top_comment): Don't reverse the order of multiline
comments.
(g_key_file_get_group_comment): Actually get the group comment.
* tests/keyfile-test.c (test_comments): Test that comments are * tests/keyfile-test.c (test_comments): Test that comments are
handled properly. (#309263, Mikael Magnusson) handled properly. (#309263, Mikael Magnusson)

View File

@@ -1,5 +1,10 @@
2005-07-01 Matthias Clasen <mclasen@redhat.com> 2005-07-01 Matthias Clasen <mclasen@redhat.com>
* glib/gkeyfile.c (g_key_file_get_key_comment):
(g_key_file_get_top_comment): Don't reverse the order of multiline
comments.
(g_key_file_get_group_comment): Actually get the group comment.
* tests/keyfile-test.c (test_comments): Test that comments are * tests/keyfile-test.c (test_comments): Test that comments are
handled properly. (#309263, Mikael Magnusson) handled properly. (#309263, Mikael Magnusson)

View File

@@ -1019,20 +1019,33 @@ g_key_file_get_keys (GKeyFile *key_file,
return NULL; return NULL;
} }
num_keys = g_list_length (group->key_value_pairs); num_keys = 0;
for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
keys = (gchar **) g_new0 (gchar **, num_keys + 1);
tmp = group->key_value_pairs;
for (i = 1; i <= num_keys; i++)
{ {
GKeyFileKeyValuePair *pair; GKeyFileKeyValuePair *pair;
pair = (GKeyFileKeyValuePair *) tmp->data; pair = (GKeyFileKeyValuePair *) tmp->data;
keys[num_keys - i] = g_strdup (pair->key);
tmp = tmp->next; if (pair->key)
num_keys++;
} }
keys = (gchar **) g_new0 (gchar **, num_keys + 1);
i = num_keys - 1;
for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
{
GKeyFileKeyValuePair *pair;
pair = (GKeyFileKeyValuePair *) tmp->data;
if (pair->key)
{
keys[i] = g_strdup (pair->key);
i--;
}
}
keys[num_keys] = NULL; keys[num_keys] = NULL;
if (length) if (length)
@@ -2335,6 +2348,7 @@ g_key_file_get_key_comment (GKeyFile *key_file,
GError **error) GError **error)
{ {
GKeyFileGroup *group; GKeyFileGroup *group;
GKeyFileKeyValuePair *pair;
GList *key_node, *tmp; GList *key_node, *tmp;
GString *string; GString *string;
gchar *comment; gchar *comment;
@@ -2370,15 +2384,29 @@ g_key_file_get_key_comment (GKeyFile *key_file,
* key and concatentate them. * key and concatentate them.
*/ */
tmp = key_node->next; tmp = key_node->next;
while (tmp != NULL) if (!key_node->next)
return NULL;
pair = (GKeyFileKeyValuePair *) tmp->data;
if (pair->key != NULL)
return NULL;
while (tmp->next)
{
pair = (GKeyFileKeyValuePair *) tmp->next->data;
if (pair->key != NULL)
break;
tmp = tmp->next;
}
while (tmp != key_node)
{ {
GKeyFileKeyValuePair *pair; GKeyFileKeyValuePair *pair;
pair = (GKeyFileKeyValuePair *) tmp->data; pair = (GKeyFileKeyValuePair *) tmp->data;
if (pair->key != NULL)
break;
if (string == NULL) if (string == NULL)
string = g_string_sized_new (512); string = g_string_sized_new (512);
@@ -2386,7 +2414,7 @@ g_key_file_get_key_comment (GKeyFile *key_file,
g_string_append (string, comment); g_string_append (string, comment);
g_free (comment); g_free (comment);
tmp = tmp->next; tmp = tmp->prev;
} }
if (string != NULL) if (string != NULL)
@@ -2400,14 +2428,68 @@ g_key_file_get_key_comment (GKeyFile *key_file,
return comment; return comment;
} }
static gchar *
get_group_comment (GKeyFile *key_file,
GKeyFileGroup *group,
GError **error)
{
GString *string;
GList *tmp;
gchar *comment;
string = NULL;
tmp = group->key_value_pairs;
while (tmp)
{
GKeyFileKeyValuePair *pair;
pair = (GKeyFileKeyValuePair *) tmp->data;
if (pair->key != NULL)
{
tmp = tmp->prev;
break;
}
if (tmp->next == NULL)
break;
tmp = tmp->next;
}
while (tmp != NULL)
{
GKeyFileKeyValuePair *pair;
pair = (GKeyFileKeyValuePair *) tmp->data;
if (string == NULL)
string = g_string_sized_new (512);
comment = g_key_file_parse_value_as_comment (key_file, pair->value);
g_string_append (string, comment);
g_free (comment);
tmp = tmp->prev;
}
if (string != NULL)
return g_string_free (string, FALSE);
return NULL;
}
static gchar * static gchar *
g_key_file_get_group_comment (GKeyFile *key_file, g_key_file_get_group_comment (GKeyFile *key_file,
const gchar *group_name, const gchar *group_name,
GError **error) GError **error)
{ {
GList *group_node;
GKeyFileGroup *group; GKeyFileGroup *group;
group = g_key_file_lookup_group (key_file, group_name); group_node = g_key_file_lookup_group_node (key_file, group_name);
group = (GKeyFileGroup *)group_node->data;
if (!group) if (!group)
{ {
g_set_error (error, G_KEY_FILE_ERROR, g_set_error (error, G_KEY_FILE_ERROR,
@@ -2421,17 +2503,17 @@ g_key_file_get_group_comment (GKeyFile *key_file,
if (group->comment) if (group->comment)
return g_strdup (group->comment->value); return g_strdup (group->comment->value);
return NULL; group_node = group_node->next;
group = (GKeyFileGroup *)group_node->data;
return get_group_comment (key_file, group, error);
} }
static gchar * static gchar *
g_key_file_get_top_comment (GKeyFile *key_file, g_key_file_get_top_comment (GKeyFile *key_file,
GError **error) GError **error)
{ {
GList *group_node, *tmp; GList *group_node;
GKeyFileGroup *group; GKeyFileGroup *group;
GString *string;
gchar *comment;
/* The last group in the list should be the top (comments only) /* The last group in the list should be the top (comments only)
* group in the file * group in the file
@@ -2441,40 +2523,7 @@ g_key_file_get_top_comment (GKeyFile *key_file,
group = (GKeyFileGroup *) group_node->data; group = (GKeyFileGroup *) group_node->data;
g_assert (group->name == NULL); g_assert (group->name == NULL);
string = NULL; return get_group_comment (key_file, group, error);
/* Then find all the comments already associated with the
* key and concatentate them.
*/
tmp = group->key_value_pairs;
while (tmp != NULL)
{
GKeyFileKeyValuePair *pair;
pair = (GKeyFileKeyValuePair *) tmp->data;
if (pair->key != NULL)
break;
if (string == NULL)
string = g_string_sized_new (512);
comment = g_key_file_parse_value_as_comment (key_file, pair->value);
g_string_append (string, comment);
g_free (comment);
tmp = tmp->next;
}
if (string != NULL)
{
comment = string->str;
g_string_free (string, FALSE);
}
else
comment = NULL;
return comment;
} }
/** /**
@@ -2484,14 +2533,15 @@ g_key_file_get_top_comment (GKeyFile *key_file,
* @key: a key * @key: a key
* @error: return location for a #GError * @error: return location for a #GError
* *
* Retreives a comment above @key from @group_name. * Retrieves a comment above @key from @group_name.
* @group_name. If @key is %NULL then @comment will * @group_name. If @key is %NULL then @comment will
* be read from above @group_name. If both @key * be read from above @group_name. If both @key
* and @group_name are NULL, then @comment will * and @group_name are NULL, then @comment will
* be read from above the first group in the file. * be read from above the first group in the file.
* *
* Since: 2.6
* Returns: a comment that should be freed with g_free() * Returns: a comment that should be freed with g_free()
*
* Since: 2.6
**/ **/
gchar * gchar *
g_key_file_get_comment (GKeyFile *key_file, g_key_file_get_comment (GKeyFile *key_file,

View File

@@ -291,9 +291,9 @@ check_name (const gchar *what,
const gchar *expected, const gchar *expected,
gint position) gint position)
{ {
if (strcmp (expected, value) != 0) if (!value || strcmp (expected, value) != 0)
{ {
g_print ("Wrong %s returned: got %s at %d, expected %s\n", g_print ("Wrong %s returned: got '%s' at %d, expected '%s'\n",
what, value, position, expected); what, value, position, expected);
exit (1); exit (1);
} }
@@ -376,17 +376,26 @@ test_comments (void)
gchar **names; gchar **names;
gsize len; gsize len;
GError *error = NULL; GError *error = NULL;
gchar *comment;
const gchar *data = const gchar *data =
"# comment 1\n" "# top comment\n"
"# second line\n" "# top comment, continued\n"
"[group1]\n" "[group1]\n"
"key1 = value1\n" "key1 = value1\n"
"#comment 2\n" "# key comment\n"
"# key comment, continued\n"
"key2 = value2\n" "key2 = value2\n"
"# comment 3\r\n" "# line end check\r\n"
"key3 = value3\n" "key3 = value3\n"
"key4 = value4\n"; "key4 = value4\n"
"# group comment\n"
"# group comment, continued\n"
"[group2]\n";
const gchar *top_comment= " top comment\n top comment, continued\n";
const gchar *group_comment= " group comment\n group comment, continued\n";
const gchar *key_comment= " key comment\n key comment, continued\n";
keyfile = load_data (data, 0); keyfile = load_data (data, 0);
@@ -407,6 +416,36 @@ test_comments (void)
g_strfreev (names); g_strfreev (names);
g_key_file_free (keyfile); g_key_file_free (keyfile);
keyfile = load_data (data, G_KEY_FILE_KEEP_COMMENTS);
names = g_key_file_get_keys (keyfile, "group1", &len, &error);
check_no_error (&error);
check_length ("keys", g_strv_length (names), len, 4);
check_name ("key", names[0], "key1", 0);
check_name ("key", names[1], "key2", 1);
check_name ("key", names[2], "key3", 2);
check_name ("key", names[3], "key4", 3);
g_strfreev (names);
comment = g_key_file_get_comment (keyfile, NULL, NULL, &error);
check_no_error (&error);
check_name ("top comment", comment, top_comment, 0);
g_free (comment);
comment = g_key_file_get_comment (keyfile, "group1", "key2", &error);
check_no_error (&error);
check_name ("key comment", comment, key_comment, 0);
g_free (comment);
comment = g_key_file_get_comment (keyfile, "group2", NULL, &error);
check_no_error (&error);
check_name ("group comment", comment, group_comment, 0);
g_free (comment);
g_key_file_free (keyfile);
} }