]> git.meshlink.io Git - catta/commitdiff
follow Bonjour's hostname number incrementing algorithm for handling conflicts. On...
authorLennart Poettering <lennart@poettering.net>
Mon, 21 Aug 2006 20:53:24 +0000 (20:53 +0000)
committerLennart Poettering <lennart@poettering.net>
Mon, 21 Aug 2006 20:53:24 +0000 (20:53 +0000)
git-svn-id: file:///home/lennart/svn/public/avahi/trunk@1250 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe

avahi-common/alternative-test.c
avahi-common/alternative.c

index 010478c21206619ec21cca8b54064aa52faa55c4..b3ee4e9eb020db3ab4dbf5f3c6caecf3805fc2d0 100644 (file)
 #include "malloc.h"
 
 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
+    const char* const test_strings[] = {
+        "gurke",
+        "",
+        "-",
+        " #",
+        "1",
+        "#0",
+        " #0",
+        " #1",
+        "#-1",
+        " #-1",
+        "-0",
+        "--0",
+        "-1",
+        "--1",
+        "-2",
+        "gurke1",
+        "gurke0",
+        "gurke-2",
+        "gurke #0",
+        "gurke #1",
+        "gurke #",
+        "gurke#1",
+        "gurke-",
+        "gurke---",
+        "gurke #",
+        "gurke ###",
+        NULL
+    };
+        
     char *r = NULL;
-    int i, k;
+    int i, j, k;
+
+    for (k = 0; test_strings[k]; k++) {
 
-    for (k = 0; k < 2; k++) {
+        printf(">>>>>%s<<<<\n", test_strings[k]);
+        
+        for (j = 0; j < 2; j++) {
         
-        for (i = 0; i < 20; i++) {
-            char *n;
-            
-            n = i == 0 ? avahi_strdup("gurke") : (k ? avahi_alternative_service_name(r) : avahi_alternative_host_name(r));
-            avahi_free(r);
-            r = n;
-            
-            printf("%s\n", r);
+            for (i = 0; i < 20; i++) {
+                char *n;
+                
+                n = i == 0 ? avahi_strdup(test_strings[k]) : (j ? avahi_alternative_service_name(r) : avahi_alternative_host_name(r));
+                avahi_free(r);
+                r = n;
+                
+                printf("%s\n", r);
+            }
         }
     }
 
index 436a0d47b2f1e3a3899afa308299c2cf6b33a47e..77470baa405009105be92b0cb9a0db89b17f575f 100644 (file)
 #include "malloc.h"
 
 char * avahi_alternative_host_name(const char *s) {
-    const char *p, *e;
+    const char *e;
     char *r;
 
     assert(s);
 
-    e = s;
-    
-    for (p = s; *p; p++)
-        if (!isdigit(*p))
-            e = p+1;
+    e = strrchr(s, '-');
+
+    if (e) {
+        const char *p;
+        
+        for (p = e+1; *p; p++)
+            if (!isdigit(*p)) {
+                e = NULL;
+                break;
+            }
+
+        if (e && (*(e+1) == '0' || (*(e+1) == 0)))
+            e = NULL;
+    }
 
-    if (*e) {
+    if (e) {
         char *c;
 
+        e++;
+        
         if (!(c = avahi_strndup(s, e-s)))
             return NULL;
 
@@ -53,7 +64,7 @@ char * avahi_alternative_host_name(const char *s) {
         avahi_free(c);
         
     } else
-        r = avahi_strdup_printf("%s2", s);
+        r = avahi_strdup_printf("%s-2", s);
     
     return r;
 }
@@ -76,6 +87,9 @@ char *avahi_alternative_service_name(const char *s) {
                 e = NULL;
                 break;
             }
+
+        if (e && (*e == '0' || *e == 0))
+            e = NULL;
     }
     
     if (e) {