4 This file is part of avahi.
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.
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.
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
31 #include "alternative.h"
36 static void drop_incomplete_utf8(char *c) {
43 if (avahi_utf8_valid(c))
53 char *avahi_alternative_host_name(const char *s) {
59 if (!avahi_is_valid_host_name(s))
62 if ((e = strrchr(s, '-'))) {
73 if (e && (*e == '0' || *e == 0))
83 if (!(m = avahi_strdup_printf("%i", n)))
88 if (l >= AVAHI_LABEL_MAX-1-strlen(m)-1)
89 l = AVAHI_LABEL_MAX-1-strlen(m)-1;
91 if (!(c = avahi_strndup(s, l))) {
96 drop_incomplete_utf8(c);
98 r = avahi_strdup_printf("%s-%s", c, m);
105 if (!(c = avahi_strndup(s, AVAHI_LABEL_MAX-1-2)))
108 drop_incomplete_utf8(c);
110 r = avahi_strdup_printf("%s-2", c);
114 assert(avahi_is_valid_host_name(r));
119 char *avahi_alternative_service_name(const char *s) {
125 if (!avahi_is_valid_service_name(s))
128 if ((e = strstr(s, " #"))) {
132 while ((n = strstr(e, " #")))
141 if (e && (*e == '0' || *e == 0))
151 if (!(m = avahi_strdup_printf("%i", n)))
156 if (l >= AVAHI_LABEL_MAX-1-strlen(m)-2)
157 l = AVAHI_LABEL_MAX-1-strlen(m)-2;
159 if (!(c = avahi_strndup(s, l))) {
164 drop_incomplete_utf8(c);
166 r = avahi_strdup_printf("%s #%s", c, m);
172 if (!(c = avahi_strndup(s, AVAHI_LABEL_MAX-1-3)))
175 drop_incomplete_utf8(c);
177 r = avahi_strdup_printf("%s #2", c);
181 assert(avahi_is_valid_service_name(r));