]> git.meshlink.io Git - catta/blob - avahi-common/alternative.c
* add avahi-publish
[catta] / avahi-common / alternative.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <stdlib.h>
28 #include <ctype.h>
29
30 #include "alternative.h"
31
32 gchar * avahi_alternative_host_name(const gchar *s) {
33     const gchar *p, *e = NULL;
34     gchar *c, *r;
35     gint n;
36
37     g_assert(s);
38     
39     for (p = s; *p; p++)
40         if (!isdigit(*p))
41             e = p+1;
42
43     if (e && *e)
44         n = atoi(e)+1;
45     else
46         n = 2;
47
48     c = e ? g_strndup(s, e-s) : g_strdup(s);
49     r = g_strdup_printf("%s%i", c, n);
50     g_free(c);
51     
52     return r;
53 }
54
55 gchar *avahi_alternative_service_name(const gchar *s) {
56     const gchar *e;
57     g_assert(s);
58
59     if ((e = strstr(s, " #"))) {
60         const gchar *n, *p;
61         e += 2;
62     
63         while ((n = strstr(e, " #")))
64             e = n + 2;
65
66         for (p = e; *p; p++)
67             if (!isdigit(*p)) {
68                 e = NULL;
69                 break;
70             }
71     }
72     
73     if (e) {
74         gchar *r, *c = g_strndup(s, e-s);
75         r = g_strdup_printf("%s%i", c, atoi(e)+1);
76         g_free(c);
77         return r;
78     } else
79         return g_strdup_printf("%s #2", s);
80 }