From 5bd544a6cb88b10d477dd24b956bc7ceddeb706b Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Thu, 5 Mar 2026 16:11:30 -0500 Subject: Add strlist_appendf function (future use) --- src/lib/core/include/strlist.h | 1 + src/lib/core/strlist.c | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) (limited to 'src/lib') diff --git a/src/lib/core/include/strlist.h b/src/lib/core/include/strlist.h index 18c60eb..1aaae3e 100644 --- a/src/lib/core/include/strlist.h +++ b/src/lib/core/include/strlist.h @@ -46,6 +46,7 @@ void strlist_append_strlist(struct StrList *pStrList1, struct StrList *pStrList2 void strlist_append(struct StrList **pStrList, char *str); void strlist_append_array(struct StrList *pStrList, char **arr); void strlist_append_tokenize(struct StrList *pStrList, char *str, char *delim); +int strlist_appendf(struct StrList **pStrList, const char *fmt, ...); struct StrList *strlist_copy(struct StrList *pStrList); int strlist_cmp(struct StrList *a, struct StrList *b); void strlist_free(struct StrList **pStrList); diff --git a/src/lib/core/strlist.c b/src/lib/core/strlist.c index b742d37..3479c44 100644 --- a/src/lib/core/strlist.c +++ b/src/lib/core/strlist.c @@ -229,6 +229,28 @@ void strlist_append_strlist(struct StrList *pStrList1, struct StrList *pStrList2 guard_free(tmp); } +/** + * Append a formatted string + * Behavior is identical to asprintf-family of functions + * @param pStrList `StrList` + * @param fmt printf format string + * @param ... format arguments + * @return same as vasnprintf + */ +int strlist_appendf(struct StrList **pStrList, const char *fmt, ...) { + char *s = NULL; + va_list ap; + va_start(ap, fmt); + const int len = vasprintf(&s, fmt, ap); + va_end(ap); + + if (pStrList && *pStrList && len >= 0) { + strlist_append(pStrList, s); + } + guard_free(s); + return len; +} + /** * Produce a new copy of a `StrList` * @param pStrList `StrList` -- cgit