stringchicken.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "stringchicken.h"
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. /*
  7. parser that can read K=V, maybe also K:V? although would just be configurable seperator
  8. I know python uses dictionary which uses a hash table so key -> hash -> value table
  9. for this assignment, I could use 2 arrays? though that's... a bit dangerous? or at least tricky, in terms of reassignments.
  10. let's just go with a struct with 2 dynamic arrays where the indices are aligned
  11. ik neem dus eerst 1 die het omzet naar een struct van strings, dan een struct van de KV values.
  12. */
  13. struct stringchicken *create_string_chicken(){
  14. struct stringchicken *mystruct = malloc(sizeof(struct stringchicken));
  15. mystruct->length = 0;
  16. mystruct->capacity = 2;
  17. mystruct->data = malloc(sizeof(char*) * mystruct->capacity);
  18. return mystruct;
  19. }
  20. void push_SC(char *string_input, struct stringchicken *struct_input){
  21. if (struct_input->length == struct_input->capacity) {
  22. struct_input->capacity*=2;
  23. char **tempptr = realloc(struct_input->data, sizeof(char*) * struct_input->capacity);
  24. if (tempptr == NULL) {
  25. exit(1);
  26. }
  27. struct_input->data = tempptr;
  28. }
  29. char *newstring = malloc(strnlen(string_input, 101)+1);
  30. strcpy(newstring,string_input);
  31. struct_input->data[struct_input->length] = newstring;
  32. struct_input->length++;
  33. }
  34. void free_SC(struct stringchicken *struct_input){
  35. for (int i = 0; i < struct_input->length; i++) {
  36. free(struct_input->data[i]);
  37. }
  38. free(struct_input->data);
  39. free(struct_input);
  40. }
  41. void print_SC(struct stringchicken *input_struct){
  42. for (size_t i = 0; i < input_struct->length; i++) {
  43. printf("line:%zu equals %s", i, input_struct->data[i]);
  44. }
  45. }