strlist.c 683 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* ----- strlist.c ----- */
  2. #include <string.h>
  3. #include <stdlib.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. typedef struct strlist {
  10. int len;
  11. int alloc;
  12. char** entries;
  13. } strlist;
  14. void strlist_init(strlist* sl) {
  15. sl->len = 0;
  16. sl->alloc = 32;
  17. sl->entries = malloc(sl->alloc * sizeof(*sl->entries));
  18. }
  19. strlist* strlist_new() {
  20. strlist* sl = malloc(sizeof(*sl));
  21. strlist_init(sl);
  22. return sl;
  23. }
  24. void strlist_push(strlist* sl, char* e) {
  25. check_alloc(sl);
  26. sl->entries[sl->len++] = e;
  27. sl->entries[sl->len] = 0;
  28. }
  29. /* -END- strlist.c ----- */