more tests and tweaks
authorTero Marttila <terom@fixme.fi>
Thu, 09 Oct 2008 00:49:32 +0300
changeset 17 0a024b29b16d
parent 16 74fb62022fb3
child 18 b12e78767248
more tests and tweaks
src/lib/url.c
src/url_test.c
--- a/src/lib/url.c	Thu Oct 09 00:33:37 2008 +0300
+++ b/src/lib/url.c	Thu Oct 09 00:49:32 2008 +0300
@@ -62,8 +62,29 @@
     
 };
 
-static int _url_append_scheme (struct url *url, const char *data) {
+static int _url_append_scheme (struct url *url, const char *data, int copy) {
+    if (!url->schema) {
+        if ((url->schema = malloc(sizeof(struct url_schema) + (1 * sizeof(const char *)))) == NULL)
+            ERROR("malloc");
+
+        url->schema->count = 1;
+
+    } else {
+        url->schema->count++;
+        
+        // I'm starting to hate flexible array members...
+        if ((url->schema = realloc(url->schema, sizeof(struct url_schema) + url->schema->count * sizeof(const char *))) == NULL)
+            ERROR("realloc");
+    }
+    
+    if ((url->schema->list[url->schema->count - 1] = copy ? strdup(data) : data) == NULL)
+        ERROR("strdup");
+
+    // k
     return 0;
+
+error:
+    return -1;
 }
 
 static int _url_append_opt_key (struct url *url, const char *key) {
@@ -274,7 +295,7 @@
             switch (next_token) {
                 case URL_SCHEME_SEP:
                     // store the scheme
-                    if (_url_append_scheme(state->url, token_data))
+                    if (_url_append_scheme(state->url, token_data, 1))
                         goto error;
                     
                     break;
@@ -304,8 +325,10 @@
             switch (next_token) {
                 case URL_SCHEME_END_SLASH1:
                     // store the schema
-                    if (_url_append_scheme(state->url, token_data))
+                    if (_url_append_scheme(state->url, state->alnum, 0))
                         goto error;
+                    
+                    state->alnum = NULL;
 
                     break;
                 
@@ -321,7 +344,7 @@
 
         case URL_SCHEME:
             // store the scheme
-            if (_url_append_scheme(state->url, token_data))
+            if (_url_append_scheme(state->url, token_data, 1))
                 goto error;
 
             break;
@@ -489,16 +512,16 @@
     int i;
 
     if (url->schema) {
-        fprintf(stream, "schema=");
+        fprintf(stream, "schema=(");
 
         for (i = 0; i < url->schema->count; i++) {
             if (i > 0)
-                fprintf(stream, "+");
+                fprintf(stream, ",");
 
             fprintf(stream, "%s", url->schema->list[i]);
         }
 
-        fprintf(stream, " ");
+        fprintf(stream, ") ");
     }
 
     _url_dump_part("username", url->username, stream);
--- a/src/url_test.c	Thu Oct 09 00:33:37 2008 +0300
+++ b/src/url_test.c	Thu Oct 09 00:49:32 2008 +0300
@@ -7,7 +7,9 @@
 
 #define FAIL(...) do { printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); return -1; } while (0)
 
-struct url_schema basic_http = { 1, { "http" } };
+struct url_schema
+    basic_http = { 1, { "http" } },
+    svn_ssh = { 2, { "svn", "ssh" } };
 
 struct url_test {
     const char *url;
@@ -20,6 +22,14 @@
     {   "http://example.com/path",  {
         &basic_http, NULL, NULL, "example.com", NULL, "path", NULL 
     } },
+
+    {   "svn+ssh://user:passwd@someplace:someport/something",   {
+        &svn_ssh, "user", "passwd", "someplace", "someport", "something"
+    } },
+
+    {   "user@:service/",   {
+        NULL, "user", NULL, NULL, "service", NULL
+    } },
     
     {   NULL,               {   } },
 };