util: allow conversion from strtab to htsmsg (suitable for idnode enum)

This commit is contained in:
Adam Sutton 2014-06-05 22:18:28 +01:00
parent b5f9979852
commit c26ccaba3d

View file

@ -19,6 +19,8 @@
#ifndef STRTAB_H_
#define STRTAB_H_
#include "htsmsg.h"
#include <strings.h>
struct strtab {
@ -77,4 +79,20 @@ val2str0(int val, const struct strtab tab[], int l)
#define val2str(val, tab) val2str0(val, tab, sizeof(tab) / sizeof(tab[0]))
static inline htsmsg_t *
strtab2htsmsg0(const struct strtab tab[], int n)
{
int i;
htsmsg_t *e, *l = htsmsg_create_list();
for (i = 0; i < n; i++) {
e = htsmsg_create_map();
htsmsg_add_s32(e, "key", tab[i].val);
htsmsg_add_str(e, "val", tab[i].str);
htsmsg_add_msg(l, NULL, e);
}
return l;
}
#define strtab2htsmsg(tab) strtab2htsmsg0(tab, sizeof(tab) / sizeof(tab[0]))
#endif /* STRTAB_H_ */