]> git.meshlink.io Git - catta/blob - avahi-common/alternative.c
* Split out a fair bit of avahi-core into avahi-common for use by the client library
[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
56 gchar *avahi_alternative_service_name(const gchar *s) {
57     const gchar *e;
58     g_assert(s);
59
60     if ((e = strstr(s, " #"))) {
61         const gchar *n, *p;
62         e += 2;
63     
64         while ((n = strstr(e, " #")))
65             e = n + 2;
66
67         for (p = e; *p; p++)
68             if (!isdigit(*p)) {
69                 e = NULL;
70                 break;
71             }
72     }
73     
74     if (e) {
75         gchar *r, *c = g_strndup(s, e-s);
76         r = g_strdup_printf("%s%i", c, atoi(e)+1);
77         g_free(c);
78         return r;
79     } else
80         return g_strdup_printf("%s #2", s);
81 }