From dc598f85213f89475e33b794b91c78f5415c592f Mon Sep 17 00:00:00 2001 From: Adam Sutton Date: Mon, 16 Sep 2013 21:39:12 +0100 Subject: [PATCH] htsmsg: add routine to convert a list to a CSV string Note: this will not work with all lists, only lists of basic types. --- src/htsmsg.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/htsmsg.h | 1 + 2 files changed, 44 insertions(+) diff --git a/src/htsmsg.c b/src/htsmsg.c index 2fb97a45..4e19857a 100644 --- a/src/htsmsg.c +++ b/src/htsmsg.c @@ -875,5 +875,48 @@ htsmsg_get_cdata(htsmsg_t *m, const char *field) return htsmsg_get_str_multi(m, field, "cdata", NULL); } +/** + * Convert list to CSV string + * + * Note: this will NOT work for lists of complex types + */ +char * +htsmsg_list_2_csv(htsmsg_t *m) +{ + int alloc, used, first = 1; + char *ret; + htsmsg_field_t *f; + const char *sep = ", "; + if (!m->hm_islist) return NULL; + +#define MAX(a,b) ((a) < (b)) ? (a) : (b) +#define REALLOC(l)\ + if ((alloc - used) < l) {\ + alloc = MAX((l)*2, alloc*2);\ + ret = realloc(ret, alloc);\ + }\ + + ret = malloc(alloc = 512); + *ret = 0; + used = 0; + HTSMSG_FOREACH(f, m) { + if (f->hmf_type == HMF_STR) { + REALLOC(2 + strlen(f->hmf_str)); + used += sprintf(ret+used, "%s%s", !first ? sep : "", f->hmf_str); + } else if (f->hmf_type == HMF_S64) { + REALLOC(34); // max length is actually 20 chars + 2 + used += sprintf(ret+used, "%s%"PRId64, !first ? sep : "", f->hmf_s64); + } else if (f->hmf_type == HMF_BOOL) { + REALLOC(2); // max length is actually 20 chars + 2 + used += sprintf(ret+used, "%s%d", !first ? sep : "", f->hmf_bool); + } else { + // TODO: handle doubles + free(ret); + return NULL; + } + } + + return ret; +} diff --git a/src/htsmsg.h b/src/htsmsg.h index 732ec3de..fb0e0d60 100644 --- a/src/htsmsg.h +++ b/src/htsmsg.h @@ -351,3 +351,4 @@ htsmsg_t *htsmsg_get_map_by_field_if_name(htsmsg_field_t *f, const char *name); const char *htsmsg_get_cdata(htsmsg_t *m, const char *field); +char *htsmsg_list_2_csv(htsmsg_t *m);