+2004-10-22 Matthias Clasen <mclasen@redhat.com>
+
+ * 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 <mclasen@redhat.com>
* glib/goption.c (get_change): Don't return the wrong
+2004-10-22 Matthias Clasen <mclasen@redhat.com>
+
+ * 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 <mclasen@redhat.com>
* glib/goption.c (get_change): Don't return the wrong
+2004-10-22 Matthias Clasen <mclasen@redhat.com>
+
+ * 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 <mclasen@redhat.com>
* glib/goption.c (get_change): Don't return the wrong
+2004-10-22 Matthias Clasen <mclasen@redhat.com>
+
+ * 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 <mclasen@redhat.com>
* glib/goption.c (get_change): Don't return the wrong
+2004-10-22 Matthias Clasen <mclasen@redhat.com>
+
+ * 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 <mclasen@redhat.com>
* glib/goption.c (get_change): Don't return the wrong
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;
+}
const gchar *hostname,
GError **error);
+gchar **g_uri_list_extract_uris (const gchar *uri_list);
G_END_DECLS
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;
}
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[])
run_to_uri_tests ();
run_from_uri_tests ();
run_roundtrip_tests ();
+ run_uri_list_tests ();
return any_failed ? 1 : 0;
}