Add some uri list tests.c.
authorMatthias Clasen <mclasen@redhat.com>
Fri, 22 Oct 2004 19:51:29 +0000 (19:51 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Fri, 22 Oct 2004 19:51:29 +0000 (19:51 +0000)
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.

ChangeLog
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-6
ChangeLog.pre-2-8
glib/gconvert.c
glib/gconvert.h
tests/uri-test.c

index ba58798..dd444db 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+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 
index ba58798..dd444db 100644 (file)
@@ -1,3 +1,12 @@
+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 
index ba58798..dd444db 100644 (file)
@@ -1,3 +1,12 @@
+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 
index ba58798..dd444db 100644 (file)
@@ -1,3 +1,12 @@
+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 
index ba58798..dd444db 100644 (file)
@@ -1,3 +1,12 @@
+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 
index c7d8880..346d0e6 100644 (file)
@@ -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;
+}
index e3989b4..7b203ec 100644 (file)
@@ -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
 
index 148b1c9..5545708 100644 (file)
@@ -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;
 }