Quirks.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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(new char[strlen(base_path) + strlen(path) + strlen(nspire_suffix)+1]);
  43. strcpy(result.get(), base_path);
  44. strcpy(&result.get()[strlen(base_path)], path);
  45. strcpy(&result.get()[strlen(base_path)+strlen(path)], nspire_suffix);
  46. result.get()[strlen(base_path) + strlen(path) + strlen(nspire_suffix)] = '\0';
  47. return result;
  48. }
  49. bool Quirks::get_key(keycode_t key)
  50. {
  51. return isKeyPressed(key);
  52. }