filesystem.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstddef>
  5. #include <iomanip>
  6. #include <sstream>
  7. #include "common/logging/log.h"
  8. #include "common/string_util.h"
  9. #include "core/file_sys/filesystem.h"
  10. #include "core/memory.h"
  11. namespace FileSys {
  12. Path::Path(LowPathType type, u32 size, u32 pointer) : type(type) {
  13. switch (type) {
  14. case Binary: {
  15. binary.resize(size);
  16. Memory::ReadBlock(pointer, binary.data(), binary.size());
  17. break;
  18. }
  19. case Char: {
  20. string.resize(size - 1); // Data is always null-terminated.
  21. Memory::ReadBlock(pointer, &string[0], string.size());
  22. break;
  23. }
  24. case Wchar: {
  25. u16str.resize(size / 2 - 1); // Data is always null-terminated.
  26. Memory::ReadBlock(pointer, &u16str[0], u16str.size() * sizeof(char16_t));
  27. break;
  28. }
  29. default:
  30. break;
  31. }
  32. }
  33. std::string Path::DebugStr() const {
  34. switch (GetType()) {
  35. case Invalid:
  36. default:
  37. return "[Invalid]";
  38. case Empty:
  39. return "[Empty]";
  40. case Binary: {
  41. std::stringstream res;
  42. res << "[Binary: ";
  43. for (unsigned byte : binary)
  44. res << std::hex << std::setw(2) << std::setfill('0') << byte;
  45. res << ']';
  46. return res.str();
  47. }
  48. case Char:
  49. return "[Char: " + AsString() + ']';
  50. case Wchar:
  51. return "[Wchar: " + AsString() + ']';
  52. }
  53. }
  54. std::string Path::AsString() const {
  55. switch (GetType()) {
  56. case Char:
  57. return string;
  58. case Wchar:
  59. return Common::UTF16ToUTF8(u16str);
  60. case Empty:
  61. return {};
  62. case Invalid:
  63. case Binary:
  64. default:
  65. // TODO(yuriks): Add assert
  66. NGLOG_ERROR(Service_FS, "LowPathType cannot be converted to string!");
  67. return {};
  68. }
  69. }
  70. std::u16string Path::AsU16Str() const {
  71. switch (GetType()) {
  72. case Char:
  73. return Common::UTF8ToUTF16(string);
  74. case Wchar:
  75. return u16str;
  76. case Empty:
  77. return {};
  78. case Invalid:
  79. case Binary:
  80. // TODO(yuriks): Add assert
  81. NGLOG_ERROR(Service_FS, "LowPathType cannot be converted to u16string!");
  82. return {};
  83. }
  84. UNREACHABLE();
  85. }
  86. std::vector<u8> Path::AsBinary() const {
  87. switch (GetType()) {
  88. case Binary:
  89. return binary;
  90. case Char:
  91. return std::vector<u8>(string.begin(), string.end());
  92. case Wchar: {
  93. // use two u8 for each character of u16str
  94. std::vector<u8> to_return(u16str.size() * 2);
  95. for (size_t i = 0; i < u16str.size(); ++i) {
  96. u16 tmp_char = u16str.at(i);
  97. to_return[i * 2] = (tmp_char & 0xFF00) >> 8;
  98. to_return[i * 2 + 1] = (tmp_char & 0x00FF);
  99. }
  100. return to_return;
  101. }
  102. case Empty:
  103. return {};
  104. case Invalid:
  105. default:
  106. // TODO(yuriks): Add assert
  107. NGLOG_ERROR(Service_FS, "LowPathType cannot be converted to binary!");
  108. return {};
  109. }
  110. }
  111. } // namespace FileSys