1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
/* CC0 (Public domain) - see LICENSE file for details */
#ifndef CCAN_STRINGBUILDER_H
#define CCAN_STRINGBUILDER_H
#include "config.h"
#include <stdarg.h>
#include <sys/types.h>
/**
* stringbuilder - Join strings from a varadic list. The list of arguments
* are all assumed to be of type const char*. If the first argument is str,
* then the contents of str are preserved and appended to.
*
* @str: A pointer to a string buffer that will receive the result.
* @str_sz: Size of the buffer pointed to by str.
* @delim: A delimiter to separate the strings with, or NULL.
*
* Returns: 0 on success
* EMSGSIZE if the resulting string would overflow the buffer.
* If an overflow condition is detected, the buffer content is
* NOT defined.
*
* Example:
* int res;
* char file_name[80];
* res = stringbuilder(file_name, sizeof(file_name), "/",
* "/var/lib/foo", "bar", "baz");
* if (res)
* printf("Failed to determine file name: %s",
* strerror(res));
* else
* printf("File is at %s", file_name);
*/
#define stringbuilder(str, str_sz, delim, ...) \
stringbuilder_args(str, str_sz, delim, __VA_ARGS__, NULL)
/**
* stringbuilder_args - Join strings from a varadic list. The list of
* arguments are all assumed to be of type const char* and must end with a NULL.
* If the first argument is str, then the contents of str are preserved and
* appended to.
*
* @str: A pointer to a string buffer that will receive the result.
* @str_sz: Size of the buffer pointed to by str.
* @delim: A delimiter to separate the strings with, or NULL.
*
* Returns: 0 on success
* EMSGSIZE if the resulting string would overflow the buffer.
* If an overflow condition is detected, the buffer content is
* NOT defined.
*
* Example:
* int res;
* char file_name[80];
* res = stringbuilder_args(file_name, sizeof(file_name), "/",
* "/var/lib/foo", "bar", "baz",
* NULL);
* if (res)
* printf("Failed to determine file name: %s",
* strerror(res));
* else
* printf("File is at %s", file_name);
*/
int stringbuilder_args(char* str, size_t str_sz, const char* delim, ...);
/**
* stringbuilder_va - Join strings from a varadic list. The list of arguments
* are all assumed to be of type const char* and must end with a NULL. If the
* first argument is str, then the contents of str are preserved and appended
* to.
*
* @str: A pointer to a string buffer that will receive the result.
* @str_sz: Size of the buffer pointed to by str.
* @delim: A delimiter to separate the strings with, or NULL.
*
* Returns: 0 on success
* EMSGSIZE if the resulting string would overflow the buffer.
* If an overflow condition is detected, the buffer content is
* NOT defined.
*
* Example:
* #include <ccan/stringbuilder/stringbuilder.h>
* #include <stdarg.h>
* #include <stdio.h>
* #include <string.h>
* #include <errno.h>
*
* int my_stringbuilder(char* str, size_t str_sz,
* const char* delim, ...);
*
* int my_stringbuilder(char* str, size_t str_sz,
* const char* delim, ...)
* {
* int res;
* va_list ap;
* va_start(ap, delim);
* res = stringbuilder_va(str, str_sz, delim, ap);
* va_end(ap);
* return res;
* }
*
* int main(void) {
* char my_string[80];
* int res = my_stringbuilder(my_string,
* sizeof(my_string), " ", "foo", "bar", NULL);
* if (!res)
* printf("%s\n", my_string);
* return res ? 1 : 0;
* }
*/
int stringbuilder_va(char* str, size_t str_sz, const char* delim, va_list ap);
/**
* stringbuilder_array - Join strings from an array of const char* pointers.
*
* @str: A pointer to a string buffer that will receive the result.
* @str_sz: Size of the buffer pointed to by str.
* @delim: A delimiter to separate the strings with, or NULL.
* @n_strings: The number of strings to join.
* @strings: The array of strings to join.
*
* Returns: 0 on success
* EMSGSIZE if the resulting string would overflow the buffer.
* If an overflow condition is detected, the buffer content is
* NOT defined.
*
* Example:
* char my_args[128];
* int res = stringbuilder_array(my_args, sizeof(my_args), ", ",
* argc, (const char**)argv);
* if (res)
* printf("Failed to list arguments: %s",
* strerror(res));
* else
* printf("My arguments were %s", my_args);
*/
int stringbuilder_array(char* str, size_t str_sz, const char* delim,
size_t n_strings, const char** strings);
#endif /* CCAN_STRINGBUILDER_H */
|