libutil: add function to concatenate a format string in place

Add function util_concatf() that appends the result of a format string
expansion to the end of an existing string while taking care of the
required memory allocations.

Usage example:

  char *str = NULL;

  util_concatf(&str, "list:");
  for (int i = 1; i <= 3; i++)
    util_concatf(&str, "%spart%d", (i > 1 ? "," : ""), i);
  printf("%s\n", str); /* list:part1,part2,part3 */

Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Peter Oberparleiter <oberpar@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
This commit is contained in:
Peter Oberparleiter
2024-02-05 14:40:25 +01:00
committed by Steffen Eiden
parent 3b26f79143
commit d8e5bc07aa
3 changed files with 29 additions and 0 deletions
+20
View File
@@ -139,6 +139,26 @@ char *util_strcat_realloc(char *str1, const char *str2)
return buf;
}
/**
* Concatenate a string with the result of a format string expansion
*
* @param[in, out] str1 Pointer to pointer to first string
* @param[in] fmt Format string for generation of the second string
* @param[in] ... Parameters for format string
*/
void util_concatf(char **str1, const char *fmt, ...)
{
va_list args;
char *str2;
va_start(args, fmt);
util_vasprintf(&str2, fmt, args);
va_end(args);
*str1 = util_strcat_realloc(*str1, str2);
free(str2);
}
/**
* Convert string to uppercase
*