FunctionMap.c 997 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "FunctionMap.h"
  4. void fmInit(FunctionMap* fm) {
  5. fm->capacity = 16;
  6. fm->entries = 0;
  7. fm->data = malloc(sizeof(Function) * fm->capacity);
  8. }
  9. void fmDelete(FunctionMap* fm) {
  10. free(fm->data);
  11. }
  12. int fmSearchAddress(FunctionMap* fm, const char* name, int arguments) {
  13. for(int i = 0; i < fm->entries; i++) {
  14. if(fm->data[i].arguments == arguments && strcmp(fm->data[i].name, name) == 0) {
  15. return fm->data[i].address;
  16. }
  17. }
  18. return -1;
  19. }
  20. bool fmAdd(FunctionMap* fm, const char* name, int arguments, int address) {
  21. if(fmSearchAddress(fm, name, arguments) != -1) {
  22. return false;
  23. }
  24. if(fm->entries >= fm->capacity) {
  25. fm->capacity *= 2;
  26. fm->data = realloc(fm->data, sizeof(Function) * fm->capacity);
  27. }
  28. int index = fm->entries++;
  29. fm->data[index].name = name;
  30. fm->data[index].arguments = arguments;
  31. fm->data[index].address = address;
  32. return true;
  33. }