Quirks.cpp 1.6 KB

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