shared: add nm_strquote_a() helper

This commit is contained in:
Thomas Haller 2016-07-05 12:23:26 +02:00
parent 8583791eb3
commit 4041bf966f

View file

@ -540,6 +540,35 @@ nm_decode_version (guint version, guint *major, guint *minor, guint *micro) {
}
/*****************************************************************************/
/* if @str is NULL, return "(null)". Otherwise, allocate a buffer using
* alloca() of size @bufsize and fill it with @str. @str will be quoted with
* single quote, and in case @str is too long, the final quote will be '^'. */
#define nm_strquote_a(bufsize, str) \
({ \
G_STATIC_ASSERT ((bufsize) >= 6); \
const gsize _BUFSIZE = (bufsize); \
const char *const _s = (str); \
char *_r; \
gsize _l; \
gboolean _truncated; \
\
nm_assert (_BUFSIZE >= 6); \
\
if (_s) { \
_l = strlen (_s) + 3; \
if ((_truncated = (_BUFSIZE < _l))) \
_l = _BUFSIZE; \
\
_r = g_alloca (_l); \
_r[0] = '\''; \
memcpy (&_r[1], _s, _l - 3); \
_r[_l - 2] = _truncated ? '^' : '\''; \
_r[_l - 1] = '\0'; \
} else \
_r = "(null)"; \
_r; \
})
#define nm_sprintf_buf(buf, format, ...) ({ \
char * _buf = (buf); \
\