]> git.ozlabs.org Git - ccan/blob - ccan/str_talloc/str_talloc.h
licence->license: US English is the standard for code.
[ccan] / ccan / str_talloc / str_talloc.h
1 #ifndef CCAN_STR_TALLOC_H
2 #define CCAN_STR_TALLOC_H
3 #include <string.h>
4 #include <stdbool.h>
5
6 /**
7  * strsplit - Split string into an array of substrings
8  * @ctx: the context to tallocate from (often NULL)
9  * @string: the string to split
10  * @delims: delimiters where lines should be split.
11  * @nump: optional pointer to place resulting number of lines
12  *
13  * This function splits a single string into multiple strings.  The
14  * original string is untouched: an array is allocated (using talloc)
15  * pointing to copies of each substring.  Multiple delimiters result
16  * in empty substrings.  By definition, no delimiters will appear in
17  * the substrings.
18  *
19  * The final char * in the array will be NULL, so you can use this or
20  * @nump to find the array length.
21  *
22  * Example:
23  *      #include <ccan/talloc/talloc.h>
24  *      #include <ccan/str_talloc/str_talloc.h>
25  *      ...
26  *      static unsigned int count_long_lines(const char *string)
27  *      {
28  *              char **lines;
29  *              unsigned int i, long_lines = 0;
30  *
31  *              // Can only fail on out-of-memory.
32  *              lines = strsplit(NULL, string, "\n", NULL);
33  *              for (i = 0; lines[i] != NULL; i++)
34  *                      if (strlen(lines[i]) > 80)
35  *                              long_lines++;
36  *              talloc_free(lines);
37  *              return long_lines;
38  *      }
39  */
40 char **strsplit(const void *ctx, const char *string, const char *delims,
41                  unsigned int *nump);
42
43 /**
44  * strjoin - Join an array of substrings into one long string
45  * @ctx: the context to tallocate from (often NULL)
46  * @strings: the NULL-terminated array of strings to join
47  * @delim: the delimiter to insert between the strings
48  *
49  * This function joins an array of strings into a single string.  The
50  * return value is allocated using talloc.  Each string in @strings is
51  * followed by a copy of @delim.
52  *
53  * Example:
54  *      // Append the string "--EOL" to each line.
55  *      static char *append_to_all_lines(const char *string)
56  *      {
57  *              char **lines, *ret;
58  *
59  *              lines = strsplit(NULL, string, "\n", NULL);
60  *              ret = strjoin(NULL, lines, "-- EOL\n");
61  *              talloc_free(lines);
62  *              return ret;
63  *      }
64  */
65 char *strjoin(const void *ctx, char *strings[], const char *delim);
66 #endif /* CCAN_STR_TALLOC_H */