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
28 #include <avahi-common/strlst.h>
29 #include <avahi-common/malloc.h>
30 #include <avahi-common/domain.h>
35 struct _sw_text_record {
36 AvahiStringList *strlst;
42 sw_result sw_text_record_init(sw_text_record *self) {
47 if (!(*self = avahi_new(struct _sw_text_record, 1))) {
52 (*self)->strlst = NULL;
53 (*self)->buffer = NULL;
54 (*self)->buffer_size = 0;
55 (*self)->buffer_valid = 0;
60 sw_result sw_text_record_fina(sw_text_record self) {
65 avahi_string_list_free(self->strlst);
66 avahi_free(self->buffer);
71 sw_result sw_text_record_add_string(
73 sw_const_string string) {
82 if (!(n = avahi_string_list_add(self->strlst, string)))
86 self->buffer_valid = 0;
90 sw_result sw_text_record_add_key_and_string_value(
93 sw_const_string val) {
102 if (!(n = avahi_string_list_add_pair(self->strlst, key, val)))
106 self->buffer_valid = 0;
110 sw_result sw_text_record_add_key_and_binary_value(
124 if (!(n = avahi_string_list_add_pair_arbitrary(self->strlst, key, val, len)))
128 self->buffer_valid = 0;
132 static int rebuild(sw_text_record self) {
135 if (self->buffer_valid)
138 self->buffer_size = avahi_string_list_serialize(self->strlst, NULL, 0);
140 if (!(self->buffer = avahi_realloc(self->buffer, self->buffer_size + 1)))
143 avahi_string_list_serialize(self->strlst, self->buffer, self->buffer_size);
144 self->buffer_valid = 1;
149 sw_octets sw_text_record_bytes(sw_text_record self) {
154 if (rebuild(self) < 0)
160 sw_uint32 sw_text_record_len(sw_text_record self) {
165 if (rebuild(self) < 0)
166 return (uint32_t) -1;
168 return self->buffer_size;
171 struct _sw_text_record_iterator {
172 AvahiStringList *strlst, *index;
176 sw_result sw_text_record_iterator_init(
177 sw_text_record_iterator * self,
178 sw_octets text_record,
179 sw_uint32 text_record_len) {
185 if (!(*self = avahi_new(struct _sw_text_record_iterator, 1))) {
190 (*self)->index = (*self)->strlst = avahi_string_list_reverse(avahi_string_list_parse(text_record, text_record_len));
195 sw_result sw_text_record_iterator_fina(sw_text_record_iterator self) {
200 avahi_string_list_free(self->strlst);
206 sw_result sw_text_record_iterator_next(
207 sw_text_record_iterator self,
208 char key[SW_TEXT_RECORD_MAX_LEN],
209 sw_uint8 val[SW_TEXT_RECORD_MAX_LEN],
210 sw_uint32 * val_len) {
212 char *mkey = NULL, *mvalue = NULL;
223 if (avahi_string_list_get_pair(self->index, &mkey, &mvalue, &msize) < 0)
226 avahi_strlcpy(key, mkey, SW_TEXT_RECORD_MAX_LEN);
227 memset(val, 0, SW_TEXT_RECORD_MAX_LEN);
228 memcpy(val, mvalue, msize);
234 self->index = self->index->next;