7 int ksprintf(kstring_t *s, const char *fmt, ...)
12 l = vsnprintf(s->s + s->l, s->m - s->l, fmt, ap); // This line does not work with glibc 2.0. See `man snprintf'.
14 if (l + 1 > s->m - s->l) {
17 s->s = (char*)realloc(s->s, s->m);
19 l = vsnprintf(s->s + s->l, s->m - s->l, fmt, ap);
26 // s MUST BE a null terminated string; l = strlen(s)
27 int ksplit_core(char *s, int delimiter, int *_max, int **_offsets)
29 int i, n, max, last_char, last_start, *offsets, l;
30 n = 0; max = *_max; offsets = *_offsets;
33 #define __ksplit_aux do { \
37 max = max? max<<1 : 2; \
38 offsets = (int*)realloc(offsets, sizeof(int) * max); \
40 offsets[n++] = last_start; \
44 for (i = 0, last_char = last_start = 0; i <= l; ++i) {
46 if (isspace(s[i]) || s[i] == 0) {
47 if (isgraph(last_char)) __ksplit_aux; // the end of a field
49 if (isspace(last_char) || last_char == 0) last_start = i;
52 if (s[i] == delimiter || s[i] == 0) {
53 if (last_char != 0 && last_char != delimiter) __ksplit_aux; // the end of a field
55 if (last_char == delimiter || last_char == 0) last_start = i;
60 *_max = max; *_offsets = offsets;
70 s = (kstring_t*)calloc(1, sizeof(kstring_t));
72 ksprintf(s, " abcdefg: %d ", 100);
73 printf("'%s'\n", s->s);
75 fields = ksplit(s, 0, &n);
76 for (i = 0; i < n; ++i)
77 printf("field[%d] = '%s'\n", i, s->s + fields[i]);