Skip to content
Snippets Groups Projects
Commit bc3c3784 authored by Pekka Paalanen's avatar Pekka Paalanen Committed by Daniel Stone
Browse files

shared: add str_printf()


asprintf() has the problem that it leaves *strp undefined when it
fails. Here is a simple wrapper that ensures NULL if asprintf() fails,
which is much more convenient to use.

This will be useful in future patches, where one needs to return error
messages from maybe failing functions, and more.

Signed-off-by: default avatarPekka Paalanen <pekka.paalanen@collabora.com>
parent 63fc73d1
No related branches found
No related tags found
2 merge requests!905color-lcms: Introduction and implementation of LCMS plugin,!658pixel-formats: Add human readable format modifier
......@@ -31,6 +31,8 @@
#include <stdint.h>
#include <errno.h>
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
/* Convert string to integer
*
......@@ -68,4 +70,29 @@ safe_strtoint(const char *str, int32_t *value)
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 */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment