X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=shared%2Fstring-helpers.h;h=3c2f07f887749300eb1bffaf1d641e90da023893;hb=d8e72099ecbcadc6e8b242686cf012763a5f1a3a;hp=c8ce4498cdbdf95eddff370fb1337bbadf4a0e62;hpb=aa4ba5d283d641da25722af7a966c31d61a9686b;p=src%2Fagl-compositor.git diff --git a/shared/string-helpers.h b/shared/string-helpers.h index c8ce449..3c2f07f 100644 --- a/shared/string-helpers.h +++ b/shared/string-helpers.h @@ -27,6 +27,7 @@ #define WESTON_STRING_HELPERS_H #include +#include #include #include #include @@ -68,4 +69,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 */