#define WESTON_STRING_HELPERS_H
#include <stdbool.h>
+#include <stdarg.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
return true;
}
+/**
+ * Exactly like asprintf(), but sets *str_out to NULL if it fails.
+ *
+ * If str_out is NULL, does nothing.
+ */
+static inline void __attribute__ ((format (printf, 2, 3)))
+str_printf(char **str_out, const char *fmt, ...)
+{
+ char *msg;
+ va_list ap;
+ int ret;
+
+ if (!str_out)
+ return;
+
+ va_start(ap, fmt);
+ ret = vasprintf(&msg, fmt, ap);
+ va_end(ap);
+
+ if (ret >= 0)
+ *str_out = msg;
+ else
+ *str_out = NULL;
+}
+
#endif /* WESTON_STRING_HELPERS_H */