From: Matthias Clasen Date: Fri, 22 Oct 2004 19:51:29 +0000 (+0000) Subject: Add some uri list tests.c. X-Git-Tag: GTK_2_5_4~27 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8df27c8dd5cf7ca192d64373fb15b5c63dc510b6;p=platform%2Fupstream%2Fglib.git Add some uri list tests.c. 2004-10-22 Matthias Clasen * tests/uri-test.c (run_uri_list_tests): Add some uri list tests.c. * glib/gconvert.h: * glib/gconvert.c (g_uri_list_extract_uris): New function to split a text/uri-list data into individual uris and strip comments. --- diff --git a/ChangeLog b/ChangeLog index ba58798..dd444db 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2004-10-22 Matthias Clasen + + * tests/uri-test.c (run_uri_list_tests): Add some + uri list tests.c. + + * glib/gconvert.h: + * glib/gconvert.c (g_uri_list_extract_uris): New function to + split a text/uri-list data into individual uris and strip comments. + 2004-10-20 Matthias Clasen * glib/goption.c (get_change): Don't return the wrong diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index ba58798..dd444db 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,12 @@ +2004-10-22 Matthias Clasen + + * tests/uri-test.c (run_uri_list_tests): Add some + uri list tests.c. + + * glib/gconvert.h: + * glib/gconvert.c (g_uri_list_extract_uris): New function to + split a text/uri-list data into individual uris and strip comments. + 2004-10-20 Matthias Clasen * glib/goption.c (get_change): Don't return the wrong diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index ba58798..dd444db 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,12 @@ +2004-10-22 Matthias Clasen + + * tests/uri-test.c (run_uri_list_tests): Add some + uri list tests.c. + + * glib/gconvert.h: + * glib/gconvert.c (g_uri_list_extract_uris): New function to + split a text/uri-list data into individual uris and strip comments. + 2004-10-20 Matthias Clasen * glib/goption.c (get_change): Don't return the wrong diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index ba58798..dd444db 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,12 @@ +2004-10-22 Matthias Clasen + + * tests/uri-test.c (run_uri_list_tests): Add some + uri list tests.c. + + * glib/gconvert.h: + * glib/gconvert.c (g_uri_list_extract_uris): New function to + split a text/uri-list data into individual uris and strip comments. + 2004-10-20 Matthias Clasen * glib/goption.c (get_change): Don't return the wrong diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index ba58798..dd444db 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,12 @@ +2004-10-22 Matthias Clasen + + * tests/uri-test.c (run_uri_list_tests): Add some + uri list tests.c. + + * glib/gconvert.h: + * glib/gconvert.c (g_uri_list_extract_uris): New function to + split a text/uri-list data into individual uris and strip comments. + 2004-10-20 Matthias Clasen * glib/goption.c (get_change): Don't return the wrong diff --git a/glib/gconvert.c b/glib/gconvert.c index c7d8880..346d0e6 100644 --- a/glib/gconvert.c +++ b/glib/gconvert.c @@ -1644,3 +1644,76 @@ g_filename_to_uri (const gchar *filename, return escaped_uri; } + +/** + * g_uri_list_extract_uris: + * @uri_list: an URI list + * + * Splits an URI list conforming to the text/uri-list + * mime type defined in RFC 2483 into individual URIs, + * discarding any comments. The URIs are not validated. + * + * Returns: a newly allocated %NULL-terminated list of + * strings holding the individual URIs. The array should + * be freed with g_strfreev(). + * + * Since: 2.6 + */ +gchar ** +g_uri_list_extract_uris (const gchar *uri_list) +{ + GSList *uris, *u; + const gchar *p, *q; + gchar **result; + gint n_uris = 0; + + uris = NULL; + + p = uri_list; + + /* We don't actually try to validate the URI according to RFC + * 2396, or even check for allowed characters - we just ignore + * comments and trim whitespace off the ends. We also + * allow LF delimination as well as the specified CRLF. + * + * We do allow comments like specified in RFC 2483. + */ + while (p) + { + if (*p != '#') + { + while (g_ascii_isspace (*p)) + p++; + + q = p; + while (*q && (*q != '\n') && (*q != '\r')) + q++; + + if (q > p) + { + q--; + while (q > p && g_ascii_isspace (*q)) + q--; + + if (q > p) + { + uris = g_slist_prepend (uris, g_strndup (p, q - p + 1)); + n_uris++; + } + } + } + p = strchr (p, '\n'); + if (p) + p++; + } + + result = g_new (gchar *, n_uris + 1); + + result[n_uris--] = 0; + for (u = uris; u; u = u->next) + result[n_uris--] = u->data; + + g_slist_free (uris); + + return result; +} diff --git a/glib/gconvert.h b/glib/gconvert.h index e3989b4..7b203ec 100644 --- a/glib/gconvert.h +++ b/glib/gconvert.h @@ -117,6 +117,7 @@ gchar *g_filename_to_uri (const gchar *filename, const gchar *hostname, GError **error); +gchar **g_uri_list_extract_uris (const gchar *uri_list); G_END_DECLS diff --git a/tests/uri-test.c b/tests/uri-test.c index 148b1c9..5545708 100644 --- a/tests/uri-test.c +++ b/tests/uri-test.c @@ -352,9 +352,9 @@ run_roundtrip_tests (void) if (safe_strcmp (to_uri_tests[i].filename, res)) { - g_message ("roundtrip test %d failed, filename modified: " - " expected \"%s\", but got \"%s\"\n", - i, to_uri_tests[i].filename, res); + g_print ("roundtrip test %d failed, filename modified: " + " expected \"%s\", but got \"%s\"\n", + i, to_uri_tests[i].filename, res); any_failed = TRUE; } @@ -372,6 +372,58 @@ run_roundtrip_tests (void) g_print ("\n"); } +static void +run_uri_list_tests (void) +{ + /* straight from the RFC */ + gchar *list = + "# urn:isbn:0-201-08372-8\r\n" + "http://www.huh.org/books/foo.html\r\n" + "http://www.huh.org/books/foo.pdf \r\n" + " ftp://ftp.foo.org/books/foo.txt\r\n"; + gchar *expected_uris[] = { + "http://www.huh.org/books/foo.html", + "http://www.huh.org/books/foo.pdf", + "ftp://ftp.foo.org/books/foo.txt" + }; + + gchar **uris; + gint j; + + uris = g_uri_list_extract_uris (list); + + if (g_strv_length (uris) != 3) + { + g_print ("uri list test failed: " + " expected %d uris, but got %d\n", + 3, g_strv_length (uris)); + any_failed = TRUE; + } + + for (j = 0; j < 3; j++) + { + if (safe_strcmp (uris[j], expected_uris[j])) + { + g_print ("uri list test failed: " + " expected \"%s\", but got \"%s\"\n", + expected_uris[j], uris[j]); + any_failed = TRUE; + } + } + + g_strfreev (uris); + + uris = g_uri_list_extract_uris ("# just hot air\r\n# more hot air"); + if (g_strv_length (uris) != 0) + { + g_print ("uri list test 2 failed: " + " expected %d uris, but got %d (first is \"%s\")\n", + 0, g_strv_length (uris), uris[0]); + any_failed = TRUE; + } + +} + int main (int argc, char *argv[]) @@ -390,6 +442,7 @@ main (int argc, run_to_uri_tests (); run_from_uri_tests (); run_roundtrip_tests (); + run_uri_list_tests (); return any_failed ? 1 : 0; }