mirror of
https://github.com/ibm-s390-linux/s390-tools.git
synced 2026-08-05 02:14:52 +00:00
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:
committed by
Steffen Eiden
parent
3b26f79143
commit
d8e5bc07aa
@@ -40,6 +40,14 @@ int main(void)
|
||||
fprintf(stderr, "result = \"%s\"\n", str);
|
||||
free(str);
|
||||
|
||||
/* Use util_concatf() for string concatenation */
|
||||
fprintf(stderr, "Try to concatenate \"list\" plus comma-separated list of numbers 1 to 3: ");
|
||||
str = NULL;
|
||||
util_concatf(&str, "list:");
|
||||
for (int i = 1; i <= 3; i++)
|
||||
util_concatf(&str, "%s%d", (i > 1 ? "," : ""), i);
|
||||
fprintf(stderr, "result = %s\n", str); /* list:part1,part2,part3 */
|
||||
|
||||
/* One byte allocation should work */
|
||||
fprintf(stderr, "Try to allocate 1 byte: ");
|
||||
ptr = util_malloc(1);
|
||||
|
||||
Reference in New Issue
Block a user