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
30 #include <avahi-common/strlst.h>
31 #include <avahi-common/malloc.h>
32 #include <avahi-common/domain.h>
36 struct _sw_text_record {
37 AvahiStringList *strlst;
43 sw_result sw_text_record_init(sw_text_record *self) {
48 if (!(*self = avahi_new(struct _sw_text_record, 1))) {
53 (*self)->strlst = NULL;
54 (*self)->buffer = NULL;
55 (*self)->buffer_size = 0;
56 (*self)->buffer_valid = 0;
61 sw_result sw_text_record_fina(sw_text_record self) {
66 avahi_string_list_free(self->strlst);
67 avahi_free(self->buffer);
72 sw_result sw_text_record_add_string(
74 sw_const_string string) {
83 if (!(n = avahi_string_list_add(self->strlst, string)))
87 self->buffer_valid = 0;
91 sw_result sw_text_record_add_key_and_string_value(
94 sw_const_string val) {
103 if (!(n = avahi_string_list_add_pair(self->strlst, key, val)))
107 self->buffer_valid = 0;
111 sw_result sw_text_record_add_key_and_binary_value(
125 if (!(n = avahi_string_list_add_pair_arbitrary(self->strlst, key, val, len)))
129 self->buffer_valid = 0;
133 static int rebuild(sw_text_record self) {
136 if (self->buffer_valid)
139 self->buffer_size = avahi_string_list_serialize(self->strlst, NULL, 0);
141 if (!(self->buffer = avahi_realloc(self->buffer, self->buffer_size + 1)))
144 avahi_string_list_serialize(self->strlst, self->buffer, self->buffer_size);
145 self->buffer_valid = 1;
150 sw_octets sw_text_record_bytes(sw_text_record self) {
155 if (rebuild(self) < 0)
161 sw_uint32 sw_text_record_len(sw_text_record self) {
166 if (rebuild(self) < 0)
167 return (uint32_t) -1;
169 return self->buffer_size;
172 struct _sw_text_record_iterator {
173 AvahiStringList *strlst, *index;
177 sw_result sw_text_record_iterator_init(
178 sw_text_record_iterator * self,
179 sw_octets text_record,
180 sw_uint32 text_record_len) {
186 if (!(*self = avahi_new(struct _sw_text_record_iterator, 1))) {
191 (*self)->index = (*self)->strlst = avahi_string_list_reverse(avahi_string_list_parse(text_record, text_record_len));
196 sw_result sw_text_record_iterator_fina(sw_text_record_iterator self) {
201 avahi_string_list_free(self->strlst);
207 sw_result sw_text_record_iterator_next(
208 sw_text_record_iterator self,
211 sw_uint32 * val_len) {
213 char *mkey = NULL, *mvalue = NULL;
224 if (avahi_string_list_get_pair(self->index, &mkey, &mvalue, &msize) < 0)
227 avahi_strlcpy(key, mkey, 255);
228 memcpy(val, mvalue, msize);
234 self->index = self->index->next;