strlist.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "strlist.h"
  4. #define check_alloc(x) \
  5. if((x)->len + 1 >= (x)->alloc) { \
  6. (x)->alloc *= 2; \
  7. (x)->entries = realloc((x)->entries, (x)->alloc * sizeof(*(x)->entries)); \
  8. }
  9. void strlist_init(strlist* sl) {
  10. sl->len = 0;
  11. sl->alloc = 32;
  12. sl->entries = malloc(sl->alloc * sizeof(*sl->entries));
  13. sl->entries[0] = 0;
  14. }
  15. strlist* strlist_new() {
  16. strlist* sl = malloc(sizeof(*sl));
  17. strlist_init(sl);
  18. return sl;
  19. }
  20. void strlist_push(strlist* sl, char* e) {
  21. check_alloc(sl);
  22. sl->entries[sl->len++] = e;
  23. sl->entries[sl->len] = 0;
  24. }
  25. // remove and return the first entry
  26. char* strlist_shift(strlist* sl) {
  27. if(sl->len == 0) return NULL;
  28. char* ret = sl->entries[0];
  29. sl->len--;
  30. memmove(sl->entries, sl->entries + 1, sl->len * sizeof(*sl->entries));
  31. return ret;
  32. }
  33. void strlist_free(strlist* sl, char freeSelf) {
  34. for(int i = 0; i < sl->len; i++) {
  35. free(sl->entries[i]);
  36. }
  37. if(freeSelf) free(sl);
  38. }
  39. // full deep clone
  40. strlist* strlist_clone(strlist* old) {
  41. strlist* new = malloc(sizeof(*new));
  42. new->alloc = old->alloc;
  43. new->len = old->len;
  44. new->entries = malloc(new->alloc * sizeof(*new->entries));
  45. for(int i = 0; i < new->len; i++) {
  46. new->entries[i] = strdup(old->entries[i]);
  47. }
  48. new->entries[new->len] = NULL;
  49. return new;
  50. }
  51. // splits on whitespace
  52. char** str_split(char* in, char* splitters) {
  53. char* e;
  54. int alloc = 32;
  55. int len = 0;
  56. char** list = malloc(alloc * sizeof(*list));
  57. for(char* s = in; *s;) {
  58. e = strpbrk(s, splitters);
  59. if(!e) e = s + strlen(s);
  60. if(len >= alloc - 1) {
  61. alloc *= 2;
  62. list = realloc(list, alloc* sizeof(*list));
  63. }
  64. list[len++] = strndup(s, e - s);
  65. e += strspn(e, splitters);
  66. s = e;
  67. }
  68. list[len] = NULL;
  69. return list;
  70. }
  71. void free_strpp(char** l) {
  72. for(char** s = l; *s; s++) free(*s);
  73. free(l);
  74. }
  75. char* join_str_list(char* list[], char* joiner) {
  76. size_t list_len = 0;
  77. size_t total = 0;
  78. size_t jlen = strlen(joiner);
  79. if(list == NULL) return strdup("");
  80. // calculate total length
  81. for(int i = 0; list[i]; i++) {
  82. list_len++;
  83. total += strlen(list[i]);
  84. }
  85. if(total == 0) return strdup("");
  86. total += (list_len - 1) * jlen;
  87. char* out = malloc((total + 1) * sizeof(*out));
  88. char* end = out;
  89. for(int i = 0; list[i]; i++) {
  90. char* s = list[i];
  91. size_t l = strlen(s);
  92. if(i > 0) {
  93. memcpy(end, joiner, jlen);
  94. end += jlen;
  95. }
  96. if(s) {
  97. memcpy(end, s, l);
  98. end += l;
  99. }
  100. total += strlen(list[i]);
  101. }
  102. *end = 0;
  103. return out;
  104. }