]> git.ozlabs.org Git - ccan/blob - ccan/str_talloc/str_talloc.h
Add author and maintainer fields.
[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  *      unsigned int count_long_lines(const char *text)
24  *      {
25  *              char **lines;
26  *              unsigned int i, long_lines = 0;
27  *
28  *              // Can only fail on out-of-memory.
29  *              lines = strsplit(NULL, string, "\n", NULL);
30  *              for (i = 0; lines[i] != NULL; i++)
31  *                      if (strlen(lines[i]) > 80)
32  *                              long_lines++;
33  *              talloc_free(lines);
34  *              return long_lines;
35  *      }
36  */
37 char **strsplit(const void *ctx, const char *string, const char *delims,
38                  unsigned int *nump);
39
40 /**
41  * strjoin - Join an array of substrings into one long string
42  * @ctx: the context to tallocate from (often NULL)
43  * @strings: the NULL-terminated array of strings to join
44  * @delim: the delimiter to insert between the strings
45  *
46  * This function joins an array of strings into a single string.  The
47  * return value is allocated using talloc.  Each string in @strings is
48  * followed by a copy of @delim.
49  *
50  * Example:
51  *      // Append the string "--EOL" to each line.
52  *      char *append_to_all_lines(const char *string)
53  *      {
54  *              char **lines, *ret;
55  *              unsigned int i, num, newnum;
56  *
57  *              lines = strsplit(NULL, string, "\n", NULL);
58  *              ret = strjoin(NULL, lines, "-- EOL\n");
59  *              talloc_free(lines);
60  *              return ret;
61  *      }
62  */
63 char *strjoin(const void *ctx, char *strings[], const char *delim);
64 #endif /* CCAN_STR_TALLOC_H */