Quirks.cpp 1.2 KB

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