stringbuilder: Functions for joining strings.
[ccan] / ccan / stringbuilder / _info
1 #include "config.h"
2 #include <stdio.h>
3 #include <string.h>
4
5 /**
6  * stringbuilder - join lists of strings
7  *
8  * This is a small set of functions for building up strings from a list.
9  * The destination buffer is bounds-checked, the functions return failure
10  * if the concatenated strings overflow the buffer.
11  *
12  * Example:
13  *      #include <stdio.h>
14  *      #include <string.h>
15  *      #include <errno.h>
16  *      #include <ccan/stringbuilder/stringbuilder.h>
17  *
18  *      int main(int argc, char *argv[])
19  *      {
20  *              char mystring[128];
21  *              int res;
22  *
23  *              res = stringbuilder_array(mystring, 128, "', '",
24  *                      argc, (const char**)argv);
25  *              if (!res)
26  *                      printf("My arguments: '%s'\n", mystring);
27  *              else
28  *                      printf("Failed to join arguments: %s\n",
29  *                              strerror(res));
30  *              if (!res) {
31  *                      res = stringbuilder(mystring, 128, ", ",
32  *                              "This", "Is", "A", "Test");
33  *                      if (!res)
34  *                              printf("My string: '%s'\n", mystring);
35  *                      else
36  *                              printf("Failed to join strings: %s\n",
37  *                                      strerror(res));
38  *              }
39  *              return 0;
40  *      }
41  *
42  * License: CC0 (Public domain)
43  * Author: Stuart Longland <stuartl@longlandclan.yi.org>
44  */
45 int main(int argc, char *argv[])
46 {
47         if (argc != 2)
48                 return 1;
49
50         if (strcmp(argv[1], "depends") == 0) {
51                 /*
52                  * This triggers a circular dependency!
53                  * printf("ccan/str\n");
54                  */
55                 return 0;
56         }
57
58         return 1;
59 }