]> git.ozlabs.org Git - ccan/blob - ccan/str_talloc/str_talloc.h
tdb2: don't cancel transactions on lock failures in tdb1 backend.
[ccan] / ccan / str_talloc / str_talloc.h
1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #ifndef CCAN_STR_TALLOC_H
3 #define CCAN_STR_TALLOC_H
4 #include <string.h>
5 #include <stdbool.h>
6
7 /**
8  * strsplit - Split string into an array of substrings
9  * @ctx: the context to tallocate from (often NULL)
10  * @string: the string to split
11  * @delims: delimiters where lines should be split.
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, talloc_array_length() of the
20  * returned value is 1 greater than the number of valid elements in
21  * the array.
22  *
23  * Example:
24  *      #include <ccan/talloc/talloc.h>
25  *      #include <ccan/str_talloc/str_talloc.h>
26  *      ...
27  *      static unsigned int count_long_lines(const char *string)
28  *      {
29  *              char **lines;
30  *              unsigned int i, long_lines = 0;
31  *
32  *              // Can only fail on out-of-memory.
33  *              lines = strsplit(NULL, string, "\n");
34  *              for (i = 0; lines[i] != NULL; i++)
35  *                      if (strlen(lines[i]) > 80)
36  *                              long_lines++;
37  *              talloc_free(lines);
38  *              return long_lines;
39  *      }
40  */
41 char **strsplit(const void *ctx, const char *string, const char *delims);
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");
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
67 /**
68  * strreg - match and extract from a string via (extended) regular expressions.
69  * @ctx: the context to tallocate from (often NULL)
70  * @string: the string to try to match.
71  * @regex: the regular expression to match.
72  * ...: pointers to strings to allocate for subexpressions.
73  *
74  * Returns true if we matched, in which case any parenthesized
75  * expressions in @regex are allocated and placed in the char **
76  * arguments following @regex.  NULL arguments mean the match is not
77  * saved.  The order of the strings is the order
78  * of opening braces in the expression: in the case of repeated
79  * expressions (eg "([a-z])*") the last one is saved, in the case of
80  * non-existent matches (eg "([a-z]*)?") the pointer is set to NULL.
81  *
82  * Allocation failures or malformed regular expressions return false.
83  *
84  * See Also:
85  *      regcomp(3), regex(3).
86  *
87  * Example:
88  *      // Given 'My name is Rusty' outputs 'Hello Rusty!'
89  *      // Given 'my first name is Rusty Russell' outputs 'Hello Rusty Russell!'
90  *      // Given 'My name isnt Rusty Russell' outputs 'Hello there!'
91  *      int main(int argc, char *argv[])
92  *      {
93  *              char *person, *input;
94  *
95  *              // Join args and trim trailing space.
96  *              input = strjoin(NULL, argv+1, " ");
97  *              if (strlen(input) != 0)
98  *                      input[strlen(input)-1] = '\0';
99  *
100  *              if (strreg(NULL, input, "[Mm]y (first )?name is ([A-Za-z ]+)",
101  *                         NULL, &person))
102  *                      printf("Hello %s!\n", person);
103  *              else
104  *                      printf("Hello there!\n");
105  *              return 0;
106  *      }
107  */
108 bool strreg(const void *ctx, const char *string, const char *regex, ...);
109 #endif /* CCAN_STR_TALLOC_H */