From d0fe1087db77421a135ff4560206c061cdde4453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 19 Mar 2023 11:00:50 +0100 Subject: [PATCH] Add `str_format_v` with `va_list` argument So the string formatting can be reused in other places. Passing variadic arguments directly to another function is not possible. The function must have a different name, as overloading `str_format` would cause the wrong function to be called when passing the `va_list`. The same naming with suffix `_v` is used for the logging functions. --- src/base/system.cpp | 23 ++++++++++++----------- src/base/system.h | 26 +++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/base/system.cpp b/src/base/system.cpp index 1cf644ea5..057176f8a 100644 --- a/src/base/system.cpp +++ b/src/base/system.cpp @@ -2690,26 +2690,27 @@ int str_length(const char *str) return (int)strlen(str); } -int str_format(char *buffer, int buffer_size, const char *format, ...) +int str_format_v(char *buffer, int buffer_size, const char *format, va_list args) { #if defined(CONF_FAMILY_WINDOWS) - va_list ap; - va_start(ap, format); - _vsprintf_p(buffer, buffer_size, format, ap); - va_end(ap); - + _vsprintf_p(buffer, buffer_size, format, args); buffer[buffer_size - 1] = 0; /* assure null termination */ #else - va_list ap; - va_start(ap, format); - vsnprintf(buffer, buffer_size, format, ap); - va_end(ap); - + vsnprintf(buffer, buffer_size, format, args); /* null termination is assured by definition of vsnprintf */ #endif return str_utf8_fix_truncation(buffer); } +int str_format(char *buffer, int buffer_size, const char *format, ...) +{ + va_list args; + va_start(args, format); + int length = str_format_v(buffer, buffer_size, format, args); + va_end(args); + return length; +} + const char *str_trim_words(const char *str, int words) { while(*str && str_isspace(*str)) diff --git a/src/base/system.h b/src/base/system.h index 7247092e7..b6599f448 100644 --- a/src/base/system.h +++ b/src/base/system.h @@ -15,6 +15,7 @@ #endif #include +#include #include #include @@ -1248,13 +1249,32 @@ int str_length(const char *str); * @param buffer Pointer to the buffer to receive the formatted string. * @param buffer_size Size of the buffer. * @param format printf formatting string. - * @param ... Parameters for the formatting. + * @param args The variable argument list. * - * @return Length of written string, even if it has been truncated + * @return Length of written string, even if it has been truncated. * * @remark See the C manual for syntax for the printf formatting string. * @remark The strings are treated as zero-terminated strings. - * @remark Guarantees that dst string will contain zero-termination. + * @remark Guarantees that buffer string will contain zero-termination. + */ +int str_format_v(char *buffer, int buffer_size, const char *format, va_list args) + GNUC_ATTRIBUTE((format(printf, 3, 0))); + +/** + * Performs printf formatting into a buffer. + * + * @ingroup Strings + * + * @param buffer Pointer to the buffer to receive the formatted string. + * @param buffer_size Size of the buffer. + * @param format printf formatting string. + * @param ... Parameters for the formatting. + * + * @return Length of written string, even if it has been truncated. + * + * @remark See the C manual for syntax for the printf formatting string. + * @remark The strings are treated as zero-terminated strings. + * @remark Guarantees that buffer string will contain zero-termination. */ int str_format(char *buffer, int buffer_size, const char *format, ...) GNUC_ATTRIBUTE((format(printf, 3, 4)));