Quirks.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <cstddef>
  2. #include <cstring>
  3. #include <TINYSTL/string.h>
  4. #include "Quirks.h"
  5. #include "Interrupts.h"
  6. using namespace WalrusRPG;
  7. using namespace Nspire;
  8. using tinystl::string;
  9. namespace
  10. {
  11. static char* base_path = nullptr;
  12. }
  13. void Quirks::init(const char *argv_0)
  14. {
  15. Interrupts::init();
  16. // Find last '/' occurence and remove the trailing characters
  17. // so we get rid of the executable name.
  18. int last_slash_occurence = -1;
  19. for (unsigned i = 0; argv_0[i] != '\0'; i++)
  20. {
  21. if (argv_0[i] == '/')
  22. last_slash_occurence = i;
  23. }
  24. if( last_slash_occurence > 0)
  25. {
  26. base_path = new char[last_slash_occurence+2];
  27. strncpy(base_path, argv_0, last_slash_occurence);
  28. base_path[last_slash_occurence] = '/';
  29. base_path[last_slash_occurence+1] = '\0';
  30. }
  31. }
  32. void Quirks::deinit()
  33. {
  34. Interrupts::off();
  35. delete[] base_path;
  36. }
  37. string Quirks::solve_absolute_path(const char *path)
  38. {
  39. string str;
  40. str.append(base_path, path);
  41. return str;
  42. }