From: Matthias Clasen Date: Fri, 1 Jul 2005 18:54:25 +0000 (+0000) Subject: Don't reverse the order of multiline comments. X-Git-Tag: GLIB_2_7_2~16 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9d6e10125b9dbf416550f86e435b577ab2a2a135;p=platform%2Fupstream%2Fglib.git Don't reverse the order of multiline comments. 2005-07-01 Matthias Clasen * 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) --- diff --git a/ChangeLog b/ChangeLog index d8be0594d..d7f021e41 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2005-07-01 Matthias Clasen + * 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) diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index d8be0594d..d7f021e41 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,5 +1,10 @@ 2005-07-01 Matthias Clasen + * 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) diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index d8be0594d..d7f021e41 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,5 +1,10 @@ 2005-07-01 Matthias Clasen + * 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) diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index d8be0594d..d7f021e41 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,5 +1,10 @@ 2005-07-01 Matthias Clasen + * 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) diff --git a/glib/gkeyfile.c b/glib/gkeyfile.c index 0c80dfe13..86df7f13f 100644 --- a/glib/gkeyfile.c +++ b/glib/gkeyfile.c @@ -1019,20 +1019,33 @@ g_key_file_get_keys (GKeyFile *key_file, 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) @@ -2335,6 +2348,7 @@ g_key_file_get_key_comment (GKeyFile *key_file, GError **error) { GKeyFileGroup *group; + GKeyFileKeyValuePair *pair; GList *key_node, *tmp; GString *string; gchar *comment; @@ -2370,23 +2384,37 @@ g_key_file_get_key_comment (GKeyFile *key_file, * 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) @@ -2400,14 +2428,68 @@ g_key_file_get_key_comment (GKeyFile *key_file, 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, @@ -2420,18 +2502,18 @@ g_key_file_get_group_comment (GKeyFile *key_file, 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 @@ -2441,40 +2523,7 @@ g_key_file_get_top_comment (GKeyFile *key_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); } /** @@ -2484,14 +2533,15 @@ g_key_file_get_top_comment (GKeyFile *key_file, * @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, diff --git a/tests/keyfile-test.c b/tests/keyfile-test.c index 730bba43d..475688e6d 100644 --- a/tests/keyfile-test.c +++ b/tests/keyfile-test.c @@ -291,9 +291,9 @@ check_name (const gchar *what, 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); } @@ -376,17 +376,26 @@ test_comments (void) 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); @@ -407,6 +416,36 @@ test_comments (void) 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); }