diff --git a/include/sxmp/sxmp.h b/include/sxmp/sxmp.h index 10974c9..b45738b 100644 --- a/include/sxmp/sxmp.h +++ b/include/sxmp/sxmp.h @@ -311,6 +311,12 @@ struct _stream_entry_node { list_node_t node; }; +struct _stream_named_order_node { + int order; + char *name; + list_node_t node; +}; + struct _stream_list_node { list_head_t *list; list_node_t node; @@ -423,10 +429,19 @@ int sxmp_rpclist_filter(usrtc_t *source, usrtc_t **dest, int flag, int *filter); void sxstream_generic_slist_free(list_head_t *list); int sxstream_generic_slist_additem(list_head_t *list, const char *value); + void sxstream_generic_named_free(list_head_t *list); + int sxstream_generic_nmlist_additem(list_head_t *list, const char *name, + int order); + const char *sxstream_generic_named_lookupname(list_head_t *list, int order); + int sxstream_generic_named_lookuporder(list_head_t *list, const char *name); + #ifdef __cplusplus } #endif +#define sxstream_generic_named_alloc() \ + sxstream_generic_slist_alloc() + #define blub(txt) fprintf(stderr, "%s:%d in %s > %s\n", __FILE__, __LINE__, __FUNCTION__, txt) #define dumphead(head) fprintf(stderr, "id: %d, opcode: %d, attr: %d, len = %d, reserve: %d\n", (head)->msgid, (head)->opcode, (head)->attr, (head)->payload_length, (head)->reserve) diff --git a/lib/stream_generic_listops.c b/lib/stream_generic_listops.c index 19a0529..e96ef64 100644 --- a/lib/stream_generic_listops.c +++ b/lib/stream_generic_listops.c @@ -87,3 +87,78 @@ int sxstream_generic_slist_additem(list_head_t *list, const char *value) return 0; } +int sxstream_generic_nmlist_additem(list_head_t *list, const char *name, int order) +{ + struct _stream_named_order_node *entry = malloc(sizeof(struct _stream_named_order_node)); + size_t name_len = 0; + + if(!entry || !list) return -1; + if(!name) { + __enomem: + free(entry); + return -1; + } + + /* check up */ + if(strchr(name, '"')) goto __enomem; + else name_len = strlen(name) + 2*sizeof(char); + + if(!(entry->name = malloc(name_len))) goto __enomem; + else snprintf(entry->name, name_len, ":%s", name); + entry->order = order; + + /* add to list */ + list_init_node(&entry->node); + list_add2tail(list, &entry->node); + + return 0; +} + +void sxstream_generic_named_free(list_head_t *list) +{ + struct _stream_named_order_node *entry; + list_node_t *iter, *siter; + + if(!list) return; + + list_for_each_safe(list, iter, siter) { + entry = list_entry(iter, struct _stream_named_order_node, node); + list_del(iter); + free(entry->name); + free(entry); + } + + free(list); + + return; +} + +const char *sxstream_generic_named_lookupname(list_head_t *list, int order) +{ + struct _stream_named_order_node *entry; + list_node_t *iter, *siter; + + if(!list) return NULL; + + list_for_each_safe(list, iter, siter) { + entry = list_entry(iter, struct _stream_named_order_node, node); + if(entry->order == order) return entry->name + sizeof(char); + } + + return NULL; +} + +int sxstream_generic_named_lookuporder(list_head_t *list, const char *name) +{ + struct _stream_named_order_node *entry; + list_node_t *iter, *siter; + + if(!list || !name) return -1; + + list_for_each_safe(list, iter, siter) { + entry = list_entry(iter, struct _stream_named_order_node, node); + if(!strcmp(entry->name + sizeof(char), name)) return entry->order; + } + + return -1; +}