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)
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)
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)
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)
return NULL;
}
- num_keys = g_list_length (group->key_value_pairs);
+ num_keys = 0;
+ for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
+ {
+ GKeyFileKeyValuePair *pair;
+
+ pair = (GKeyFileKeyValuePair *) tmp->data;
+
+ if (pair->key)
+ num_keys++;
+ }
keys = (gchar **) g_new0 (gchar **, num_keys + 1);
- tmp = group->key_value_pairs;
- for (i = 1; i <= num_keys; i++)
+ i = num_keys - 1;
+ for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
{
GKeyFileKeyValuePair *pair;
pair = (GKeyFileKeyValuePair *) tmp->data;
- keys[num_keys - i] = g_strdup (pair->key);
- tmp = tmp->next;
+ if (pair->key)
+ {
+ keys[i] = g_strdup (pair->key);
+ i--;
+ }
}
+
keys[num_keys] = NULL;
if (length)
GError **error)
{
GKeyFileGroup *group;
+ GKeyFileKeyValuePair *pair;
GList *key_node, *tmp;
GString *string;
gchar *comment;
* key and concatentate them.
*/
tmp = key_node->next;
- while (tmp != NULL)
- {
- GKeyFileKeyValuePair *pair;
+ if (!key_node->next)
+ return NULL;
- pair = (GKeyFileKeyValuePair *) tmp->data;
+ 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;
+
+ pair = (GKeyFileKeyValuePair *) tmp->data;
if (string == NULL)
- string = g_string_sized_new (512);
-
+ 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;
+
+ tmp = tmp->prev;
}
if (string != NULL)
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 *
g_key_file_get_group_comment (GKeyFile *key_file,
const gchar *group_name,
GError **error)
{
+ GList *group_node;
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)
{
g_set_error (error, G_KEY_FILE_ERROR,
if (group->comment)
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 *
g_key_file_get_top_comment (GKeyFile *key_file,
GError **error)
{
- GList *group_node, *tmp;
+ GList *group_node;
GKeyFileGroup *group;
- GString *string;
- gchar *comment;
/* The last group in the list should be the top (comments only)
* group in the file
group = (GKeyFileGroup *) group_node->data;
g_assert (group->name == NULL);
- string = NULL;
-
- /* 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;
+ return get_group_comment (key_file, group, error);
}
/**
* @key: a key
* @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
* be read from above @group_name. If both @key
* and @group_name are NULL, then @comment will
* be read from above the first group in the file.
*
- * Since: 2.6
* Returns: a comment that should be freed with g_free()
+ *
+ * Since: 2.6
**/
gchar *
g_key_file_get_comment (GKeyFile *key_file,
const gchar *expected,
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);
exit (1);
}
gchar **names;
gsize len;
GError *error = NULL;
+ gchar *comment;
const gchar *data =
- "# comment 1\n"
- "# second line\n"
+ "# top comment\n"
+ "# top comment, continued\n"
"[group1]\n"
"key1 = value1\n"
- "#comment 2\n"
+ "# key comment\n"
+ "# key comment, continued\n"
"key2 = value2\n"
- "# comment 3\r\n"
+ "# line end check\r\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);
g_strfreev (names);
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);
}