split.c 700 B

1234567891011121314151617181920212223242526272829
  1. #include "tools.h"
  2. #include "talloc/talloc.h"
  3. #include <string.h>
  4. /* This is a dumb one which copies. We could mangle instead. */
  5. char **split(const void *ctx, const char *text, const char *delims,
  6. unsigned int *nump)
  7. {
  8. char **lines = NULL;
  9. unsigned int max = 64, num = 0;
  10. lines = talloc_array(ctx, char *, max+1);
  11. while (*text != '\0') {
  12. unsigned int len = strcspn(text, delims);
  13. lines[num] = talloc_array(lines, char, len + 1);
  14. memcpy(lines[num], text, len);
  15. lines[num][len] = '\0';
  16. text += len;
  17. text += strspn(text, delims);
  18. if (++num == max)
  19. lines = talloc_realloc(ctx, lines, char *, max*=2 + 1);
  20. }
  21. lines[num] = NULL;
  22. if (nump)
  23. *nump = num;
  24. return lines;
  25. }