From e45534f433664dc4d68e79b1779d6ff53339e73f Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 25 Aug 2015 02:51:31 +0200 Subject: libmds_compose_v: make it easy to exclude headers conditionally MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/libmdsclient/proto-util.c | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/src/libmdsclient/proto-util.c b/src/libmdsclient/proto-util.c index b4c580e..a2b24f6 100644 --- a/src/libmdsclient/proto-util.c +++ b/src/libmdsclient/proto-util.c @@ -596,11 +596,14 @@ void libmds_headers_sort(char** restrict headers, size_t header_count) * header line, that is, the header name, colon, blank space and then * the header's value. No LF should be included. The following * arguments should be the argument to format the header line. - * This may be be iterated any number of this. The last argument - * should be `NULL` to specify that there are no more headers. - * The `Length`-header should not be included, it is added automatically. - * A header may not have a length larger than 2¹⁵, otherwise - * the behaviour of this function is undefined. + * If a format for a line begins with a question mark, the remainder + * of the line used, but only if the next argument is non-zero + * (it should be of type `int`,) that argument will not be used + * for the formatting. This may be be iterated any number of this. + * The last argument should be `NULL` to specify that there are no + * more headers. The `Length`-header should not be included, it is + * added automatically. A header may not have a length larger than + * 2¹⁵, otherwise the behaviour of this function is undefined. * @return Zero on success, -1 on error, `errno` will have been set * accordingly on error. * @@ -649,11 +652,14 @@ int libmds_compose(char** restrict buffer, size_t* restrict buffer_size, size_t* * header line, that is, the header name, colon, blank space and then * the header's value. No LF should be included. The following * arguments should be the argument to format the header line. - * This may be be iterated any number of this. The last argument - * should be `NULL` to specify that there are no more headers. - * The `Length`-header should not be included, it is added automatically. - * A header may not have a length larger than 2¹⁵, otherwise - * the behaviour of this function is undefined. + * If a format for a line begins with a question mark, the remainder + * of the line used, but only if the next argument is non-zero + * (it should be of type `int`,) that argument will not be used + * for the formatting. This may be be iterated any number of this. + * The last argument should be `NULL` to specify that there are no + * more headers. The `Length`-header should not be included, it is + * added automatically. A header may not have a length larger than + * 2¹⁵, otherwise the behaviour of this function is undefined. * @return Zero on success, -1 on error, `errno` will have been set * accordingly on error. * @@ -670,6 +676,7 @@ int libmds_compose_v(char** restrict buffer, size_t* restrict buffer_size, size_ char* part_msg; size_t payload_len = 0; const char* format; + int include; int saved_errno; *length = 0; @@ -691,11 +698,26 @@ int libmds_compose_v(char** restrict buffer, size_t* restrict buffer_size, size_ if (format == NULL) break; + include = 1; + if (*format == '?') + { + include = va_arg(args, int); + format++; + } + # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wformat-nonliteral" # pragma GCC diagnostic ignored "-Wsuggest-attribute=format" part_len = vasprintf(&part_msg, format, args); # pragma GCC diagnostic pop + + if (include == 0) + { + if (part_len >= 0) + free(part_msg); + continue; + } + if (part_len < 0) return -1; -- cgit v1.2.3-70-g09d2