]> git.ipfire.org Git - pakfire.git/commitdiff
strings: Add function to add formatted strings to a buffer
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 22 Feb 2025 16:00:36 +0000 (16:00 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 22 Feb 2025 16:00:36 +0000 (16:00 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/string.c
src/pakfire/string.h

index 31aea9562e807380674f18f49d1b2db778cbb234..6db0cf63f5b548679b3b458b72c5f15000fdb8e6 100644 (file)
@@ -381,6 +381,26 @@ int __pakfire_string_append(char* buffer, size_t length, const char* appendix) {
        return __pakfire_string_set(buffer + l, length - l, appendix);
 }
 
+int __pakfire_string_appendf(char* buffer, size_t length, const char* format, ...) {
+       char* appendix = NULL;
+       va_list args;
+       int r;
+
+       va_start(args, format);
+       r = vasprintf(&appendix, format, args);
+       va_end(args);
+
+       // Return on error
+       if (r < 0)
+               return r;
+
+       // Append the string
+       r = __pakfire_string_append(buffer, length, appendix);
+       free(appendix);
+
+       return r;
+}
+
 int pakfire_string_search(const char* haystack, ssize_t lhaystack,
                const char* needle, ssize_t lneedle) {
        // Check inputs
index 33a6ce6b7d76add43bb45619bda700d7711e2a26..7f96dd9f8eeca11ba60e7582195c08b2a4d3b928 100644 (file)
@@ -91,6 +91,11 @@ static inline int pakfire_string_contains_whitespace(const char* s) {
        __pakfire_string_append(s, sizeof(s), appendix)
 int __pakfire_string_append(char* buffer, size_t length, const char* appendix);
 
+#define pakfire_string_appendf(s, format, ...) \
+       __pakfire_string_appendf(s, sizeof(s), format, __VA_ARGS__)
+int __pakfire_string_appendf(char* buffer, size_t length, const char* format, ...)
+       __attribute__((format(printf, 3, 4)));
+
 int pakfire_string_search(const char* haystack, ssize_t lhaystack,
        const char* needle, ssize_t lneedle);