[avahi] about the scanf like function

Lennart Poettering lennart at poettering.de
Mon Aug 22 07:26:09 PDT 2005


On Mon, 22.08.05 11:36, Sebastien Estienne (sebastien.estienne at gmail.com) wrote:

> hello
> 
> as we started to talking with lennart on the irc, when you resolve a
> service you get a avahistrlst for TXT record, which is a string list
> looking like this:
> "foo=bar","abc=123","comment=ok"
> so most of the time (if not always) people are interested in knowing
> the values of this list in a key/value pair.
> 
> So i think they need some kind of avahistrlst iterator, a bit in the
> same fashion than this:
> http://www.porchdogsoft.com/products/howl/docs/txt_rec_iter.html#sw_text_record_iterator_next
> 
> this function would be similar to avahi_string_list_add_printf but for
> doing the reverse operation

Iterarting over the list is easily possible:

<snip>
AvahiStringList list, p;

list = avahi_string_list_new("foo", "bar", "waldo", NULL);

for (p = list; p; p = p->next) 
    printf("%s\n", p->text);
</snip>

(This will print the list in reversed order, though. Avahi stores TXT
lists in reversed order due to some reasons. You may use
avahi_string_list_reverse() to reverse a list in case you care about
the order)

(Please note that TXT record entries may contain NUL bytes. It is OK to
store such strings in AvahiStringList. AvahiStringList will
(implicitly) append a NUL byte on every string to make sure that
printf() works without hassles. Note that strlen(p->text) and p->size
might differ. (Though strlen(p->text) <= p->size always holds)) 

> Another possiblility is to have a set of functions especially designed
> to handle TXT record, like:
> char * avahi_text_record_get_value_from_key(AvahiStringList *l, const
> char *key); //return value
> 
> to be coherent we may add avahi_text_text_record_set

I you like I can add four functions:

AvahiStringList *avahi_string_list_find(AvahiStringList *l, const char *key);

   Iterates through the string list returns the entry with the
   specified key.

int avahi_string_list_get_pair(AvahiStringList *l, char **key, char **value, size_t *size);
 
   Split the given string list entry data at the "=" and allocate a
   new string for both parts, return it in *key and *value. If size is
   not NULL, return the length of value in it.

AvahiStringList *avahi_string_list_add_pair(AvahiStringList *l, const char *key, const char *value)

   Identical to "avahi_string_list_add_printf(l, "%s=%s", key, value);

AvahiStringList *avahi_string_list_add_pair_arbitrary(AvahiStringList *l, const char *key, const uint8_t *value, size_t size);

   Same as the previous but allow value strings with NUL bytes.

I don't like to add the function you proposed, because this will lead
users to write ineffecient code. An AvahiStringList is not  a
hash table. The complexity of searching a specific key is O(n). So if
the user parses the TXT records with calling your function for each
key this might total to O(n*n). Instead, the user should iterate
through the TXT list and process each entry as it comes. That way the
complexity is O(n).

Does this fit your requirements?

Lennart

-- 
Lennart Poettering; lennart [at] poettering [dot] de
ICQ# 11060553; GPG 0x1A015CC4; http://0pointer.de/lennart/


More information about the avahi mailing list