1 /* CC0 (Public domain) - see LICENSE file for details */
2 #ifndef CCAN_STRINGBUILDER_H
3 #define CCAN_STRINGBUILDER_H
9 * stringbuilder - Join strings from a varadic list. The list of arguments
10 * are all assumed to be of type const char*. If the first argument is str,
11 * then the contents of str are preserved and appended to.
13 * @str: A pointer to a string buffer that will receive the result.
14 * @str_sz: Size of the buffer pointed to by str.
15 * @delim: A delimiter to separate the strings with, or NULL.
17 * Returns: 0 on success
18 * EMSGSIZE if the resulting string would overflow the buffer.
19 * If an overflow condition is detected, the buffer content is
25 * res = stringbuilder(file_name, sizeof(file_name), "/",
26 * "/var/lib/foo", "bar", "baz");
28 * printf("Failed to determine file name: %s",
31 * printf("File is at %s", file_name);
33 #define stringbuilder(str, str_sz, delim, ...) \
34 stringbuilder_args(str, str_sz, delim, __VA_ARGS__, NULL)
36 * stringbuilder_args - Join strings from a varadic list. The list of
37 * arguments are all assumed to be of type const char* and must end with a NULL.
38 * If the first argument is str, then the contents of str are preserved and
41 * @str: A pointer to a string buffer that will receive the result.
42 * @str_sz: Size of the buffer pointed to by str.
43 * @delim: A delimiter to separate the strings with, or NULL.
45 * Returns: 0 on success
46 * EMSGSIZE if the resulting string would overflow the buffer.
47 * If an overflow condition is detected, the buffer content is
53 * res = stringbuilder_args(file_name, sizeof(file_name), "/",
54 * "/var/lib/foo", "bar", "baz",
57 * printf("Failed to determine file name: %s",
60 * printf("File is at %s", file_name);
62 int stringbuilder_args(char* str, size_t str_sz, const char* delim, ...);
65 * stringbuilder_va - Join strings from a varadic list. The list of arguments
66 * are all assumed to be of type const char* and must end with a NULL. If the
67 * first argument is str, then the contents of str are preserved and appended
70 * @str: A pointer to a string buffer that will receive the result.
71 * @str_sz: Size of the buffer pointed to by str.
72 * @delim: A delimiter to separate the strings with, or NULL.
74 * Returns: 0 on success
75 * EMSGSIZE if the resulting string would overflow the buffer.
76 * If an overflow condition is detected, the buffer content is
80 * #include <ccan/stringbuilder/stringbuilder.h>
86 * int my_stringbuilder(char* str, size_t str_sz,
87 * const char* delim, ...);
89 * int my_stringbuilder(char* str, size_t str_sz,
90 * const char* delim, ...)
94 * va_start(ap, delim);
95 * res = stringbuilder_va(str, str_sz, delim, ap);
101 * char my_string[80];
102 * int res = my_stringbuilder(my_string,
103 * sizeof(my_string), " ", "foo", "bar", NULL);
105 * printf("%s\n", my_string);
106 * return res ? 1 : 0;
109 int stringbuilder_va(char* str, size_t str_sz, const char* delim, va_list ap);
112 * stringbuilder_array - Join strings from an array of const char* pointers.
114 * @str: A pointer to a string buffer that will receive the result.
115 * @str_sz: Size of the buffer pointed to by str.
116 * @delim: A delimiter to separate the strings with, or NULL.
117 * @n_strings: The number of strings to join.
118 * @strings: The array of strings to join.
120 * Returns: 0 on success
121 * EMSGSIZE if the resulting string would overflow the buffer.
122 * If an overflow condition is detected, the buffer content is
127 * int res = stringbuilder_array(my_args, sizeof(my_args), ", ",
128 * argc, (const char**)argv);
130 * printf("Failed to list arguments: %s",
133 * printf("My arguments were %s", my_args);
135 int stringbuilder_array(char* str, size_t str_sz, const char* delim,
136 size_t n_strings, const char** strings);
138 #endif /* CCAN_STRINGBUILDER_H */