Quirks.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <cstddef>
  2. #include <cstring>
  3. #include <memory>
  4. #include <TINYSTL/string.h>
  5. #include "Quirks.h"
  6. #include "Interrupts.h"
  7. #include "platform.h"
  8. using namespace WalrusRPG;
  9. using namespace Nspire;
  10. using tinystl::string;
  11. namespace
  12. {
  13. static char *base_path = nullptr;
  14. }
  15. void Quirks::init(const char *argv_0)
  16. {
  17. Interrupts::init();
  18. // Find last '/' occurence and remove the trailing characters
  19. // so we get rid of the executable name.
  20. int last_slash_occurence = -1;
  21. for (unsigned i = 0; argv_0[i] != '\0'; i++)
  22. {
  23. if (argv_0[i] == '/')
  24. last_slash_occurence = i;
  25. }
  26. if (last_slash_occurence > 0)
  27. {
  28. base_path = new char[last_slash_occurence + 2];
  29. strncpy(base_path, argv_0, last_slash_occurence);
  30. base_path[last_slash_occurence] = '/';
  31. base_path[last_slash_occurence + 1] = '\0';
  32. }
  33. }
  34. void Quirks::deinit()
  35. {
  36. Interrupts::off();
  37. delete[] base_path;
  38. }
  39. std::unique_ptr<char> Quirks::solve_absolute_path(const char *path)
  40. {
  41. const char nspire_suffix[] = ".tns";
  42. std::unique_ptr<char> result(
  43. new char[strlen(base_path) + strlen(path) + strlen(nspire_suffix) + 1]);
  44. strcpy(result.get(), base_path);
  45. strcpy(&result.get()[strlen(base_path)], path);
  46. strcpy(&result.get()[strlen(base_path) + strlen(path)], nspire_suffix);
  47. result.get()[strlen(base_path) + strlen(path) + strlen(nspire_suffix)] = '\0';
  48. return result;
  49. }
  50. bool Quirks::get_key(keycode_t key)
  51. {
  52. return isKeyPressed(key);
  53. }