]> git.ozlabs.org Git - ccan/blob - ccan/str_talloc/str_talloc.c
tdb: enforce hashing, via example hash in old rwlocks entry in header.
[ccan] / ccan / str_talloc / str_talloc.c
1 #include <unistd.h>
2 #include <stdint.h>
3 #include <string.h>
4 #include <limits.h>
5 #include <stdlib.h>
6 #include "str_talloc.h"
7 #include <ccan/talloc/talloc.h>
8
9 char **strsplit(const void *ctx, const char *string, const char *delims,
10                  unsigned int *nump)
11 {
12         char **lines = NULL;
13         unsigned int max = 64, num = 0;
14
15         lines = talloc_array(ctx, char *, max+1);
16
17         while (*string != '\0') {
18                 unsigned int len = strcspn(string, delims);
19                 lines[num] = talloc_array(lines, char, len + 1);
20                 memcpy(lines[num], string, len);
21                 lines[num][len] = '\0';
22                 string += len;
23                 string += strspn(string, delims) ? 1 : 0;
24                 if (++num == max)
25                         lines = talloc_realloc(ctx, lines, char *, max*=2 + 1);
26         }
27         lines[num] = NULL;
28         if (nump)
29                 *nump = num;
30         return lines;
31 }
32
33 char *strjoin(const void *ctx, char *strings[], const char *delim)
34 {
35         unsigned int i;
36         char *ret = talloc_strdup(ctx, "");
37
38         for (i = 0; strings[i]; i++) {
39                 ret = talloc_append_string(ret, strings[i]);
40                 ret = talloc_append_string(ret, delim);
41         }
42         return ret;
43 }