Structs.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "utils/Structs.h"
  4. void stAddVariable(Struct* st, const char* name, DataType type) {
  5. int index = st->amount;
  6. st->amount++;
  7. st->vars = realloc(st->vars, sizeof(StructVariable) * st->amount);
  8. st->vars[index].name = name;
  9. st->vars[index].type = type;
  10. }
  11. void stsInit(Structs* sts) {
  12. sts->capacity = 4;
  13. sts->entries = 0;
  14. sts->data = malloc(sizeof(Struct) * sts->capacity);
  15. }
  16. void stsDelete(Structs* sts) {
  17. for(int i = 0; i < sts->entries; i++) {
  18. free(sts->data[i].vars);
  19. }
  20. free(sts->data);
  21. }
  22. Struct* stsSearch(Structs* sts, const char* name) {
  23. for(int i = 0; i < sts->entries; i++) {
  24. if(strcmp(sts->data[i].name, name) == 0) {
  25. return sts->data + i;
  26. }
  27. }
  28. return NULL;
  29. }
  30. Struct* stsAdd(Structs* sts, const char* name) {
  31. if(sts->entries >= sts->capacity) {
  32. sts->capacity *= 2;
  33. sts->data = realloc(sts->data, sizeof(Struct) * sts->capacity);
  34. }
  35. int index = sts->entries++;
  36. sts->data[index].id = index;
  37. sts->data[index].amount = 0;
  38. sts->data[index].name = name;
  39. sts->data[index].vars = NULL;
  40. return sts->data + index;
  41. }