6 * xstring - bounded string builder with three valued comparator
8 * xstring handles string concatenation to a buffer, flagging truncation.
9 * Additions can be transactional, where the addition wholly succeeds or the buffer is reterminated at its prior length.
10 * Once flagged, no further additions are allowed.
11 * Clearing reuses the buffer from the beginning.
13 * When comparing two xstrings, the truncation state is taken into account.
14 * If either is truncated, an equality test returns unknown (-1).
15 * Testing if x contains y returns unknown (-1) if y is truncated.
16 * If x is whole, the test returns true (1) or false (0) appropriately.
17 * If x is truncated, the test returns true (1) if the known portion of x contains y, and unknown (-1) otherwise.
19 * The structure members are intended for direct access.
22 * // demo - break long lines
28 * #include <sys/types.h>
29 * #include <sys/stat.h>
30 * #include <sys/mman.h>
32 * #include "ccan/xstring/xstring.h"
36 * int main(int argc, char *argv[])
39 * xstring _x, *x = &_x;
41 * int fd, span, lnlen;
44 * xstrInit(x, buf, sizeof(buf), 0);
45 * assert(sizeof(buf) > MAXLEN);
47 * if (argc != 2) return 1;
49 * if ((fd = open(argv[1], O_RDONLY)) == -1)
51 * if (fstat(fd, &sb) == -1)
53 * if ((map = mmap(NULL, sb.st_size + 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0)) == MAP_FAILED)
55 * map[sb.st_size] = '\n';
57 * for (p = map, lnlen = 0; p < map + sb.st_size; ) {
58 * span = strcspn(p, " \t\n") + 1;
60 * if (lnlen + span > MAXLEN) {
61 * span = MAXLEN - lnlen;
62 * xstrAddSubsT(x, p, span, "\n", 1, NULL);
65 * xstrAddSubT(x, p, span);
68 * fputs(x->str, stdout);
70 * continue; // p unchanged
73 * q = x->str + x->len - 1;
74 * if (lnlen + MARGIN > MAXLEN)
77 * lnlen = *q == '\n' ? 0 : lnlen + span;
82 * fputs(x->str, stdout);
88 * Author: Dan Good <dan@dancancode.com>
90 int main(int argc, char *argv[])
92 /* Expect exactly one argument */
96 if (strcmp(argv[1], "depends") == 0)
98 if (strcmp(argv[1], "testdepends") == 0) {
99 printf("ccan/failtest\n");