structtable.c 943 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "structtable.h"
  2. #include <string.h>
  3. #include <stdio.h>
  4. #define STRUCT_TABLE_MAX 100
  5. struct struct_entry structtable[STRUCT_TABLE_MAX];
  6. int structtable_lastentry = -1;
  7. int struct_insert(struct struct_entry *e) {
  8. int index = struct_lookup(e->name);
  9. if (index >= 0) {
  10. return index;
  11. }
  12. if (structtable_lastentry +1 >= STRUCT_TABLE_MAX) {
  13. return -1;
  14. }
  15. structtable_lastentry++;
  16. memcpy(&structtable[structtable_lastentry], e, sizeof(struct struct_entry));
  17. return structtable_lastentry;
  18. }
  19. int struct_lookup(const char *name) {
  20. for (int i = 0; i <= structtable_lastentry; i++) {
  21. if (strcmp(structtable[i].name, name) == 0) {
  22. return i;
  23. }
  24. }
  25. return -1;
  26. }
  27. struct struct_entry *struct_entryat(int index) {
  28. if (index >= 0 && index <= structtable_lastentry) {
  29. return structtable +index;
  30. }
  31. }
  32. void struct_print() {
  33. for (int i = 0; i <= structtable_lastentry; i++) {
  34. printf("struct: %s\n", structtable[i].name);
  35. }
  36. }