path_utils.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*************************************************************************/
  2. /* path_utils.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "path_utils.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/os/dir_access.h"
  33. #include "core/os/file_access.h"
  34. #include "core/os/os.h"
  35. #ifdef WINDOWS_ENABLED
  36. #include <windows.h>
  37. #define ENV_PATH_SEP ";"
  38. #else
  39. #include <limits.h>
  40. #include <unistd.h>
  41. #define ENV_PATH_SEP ":"
  42. #endif
  43. #include <stdlib.h>
  44. namespace path {
  45. String cwd() {
  46. #ifdef WINDOWS_ENABLED
  47. const DWORD expected_size = ::GetCurrentDirectoryW(0, nullptr);
  48. Char16String buffer;
  49. buffer.resize((int)expected_size);
  50. if (::GetCurrentDirectoryW(expected_size, (wchar_t *)buffer.ptrw()) == 0)
  51. return ".";
  52. String result;
  53. if (result.parse_utf16(buffer.ptr())) {
  54. return ".";
  55. }
  56. return result.simplify_path();
  57. #else
  58. char buffer[PATH_MAX];
  59. if (::getcwd(buffer, sizeof(buffer)) == nullptr) {
  60. return ".";
  61. }
  62. String result;
  63. if (result.parse_utf8(buffer)) {
  64. return ".";
  65. }
  66. return result.simplify_path();
  67. #endif
  68. }
  69. String abspath(const String &p_path) {
  70. if (p_path.is_absolute_path()) {
  71. return p_path.simplify_path();
  72. } else {
  73. return path::join(path::cwd(), p_path).simplify_path();
  74. }
  75. }
  76. String realpath(const String &p_path) {
  77. #ifdef WINDOWS_ENABLED
  78. // Open file without read/write access
  79. HANDLE hFile = ::CreateFileW((LPCWSTR)(p_path.utf16().get_data()), 0,
  80. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  81. nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
  82. if (hFile == INVALID_HANDLE_VALUE)
  83. return p_path;
  84. const DWORD expected_size = ::GetFinalPathNameByHandleW(hFile, nullptr, 0, FILE_NAME_NORMALIZED);
  85. if (expected_size == 0) {
  86. ::CloseHandle(hFile);
  87. return p_path;
  88. }
  89. Char16String buffer;
  90. buffer.resize((int)expected_size);
  91. ::GetFinalPathNameByHandleW(hFile, (wchar_t *)buffer.ptrw(), expected_size, FILE_NAME_NORMALIZED);
  92. ::CloseHandle(hFile);
  93. String result;
  94. if (result.parse_utf16(buffer.ptr())) {
  95. return p_path;
  96. }
  97. return result.simplify_path();
  98. #elif UNIX_ENABLED
  99. char *resolved_path = ::realpath(p_path.utf8().get_data(), nullptr);
  100. if (!resolved_path) {
  101. return p_path;
  102. }
  103. String result;
  104. bool parse_ok = result.parse_utf8(resolved_path);
  105. ::free(resolved_path);
  106. if (parse_ok) {
  107. return p_path;
  108. }
  109. return result.simplify_path();
  110. #endif
  111. }
  112. String join(const String &p_a, const String &p_b) {
  113. if (p_a.is_empty()) {
  114. return p_b;
  115. }
  116. const char32_t a_last = p_a[p_a.length() - 1];
  117. if ((a_last == '/' || a_last == '\\') ||
  118. (p_b.size() > 0 && (p_b[0] == '/' || p_b[0] == '\\'))) {
  119. return p_a + p_b;
  120. }
  121. return p_a + "/" + p_b;
  122. }
  123. String join(const String &p_a, const String &p_b, const String &p_c) {
  124. return path::join(path::join(p_a, p_b), p_c);
  125. }
  126. String join(const String &p_a, const String &p_b, const String &p_c, const String &p_d) {
  127. return path::join(path::join(path::join(p_a, p_b), p_c), p_d);
  128. }
  129. String relative_to_impl(const String &p_path, const String &p_relative_to) {
  130. // This function assumes arguments are normalized and absolute paths
  131. if (p_path.begins_with(p_relative_to)) {
  132. return p_path.substr(p_relative_to.length() + 1);
  133. } else {
  134. String base_dir = p_relative_to.get_base_dir();
  135. if (base_dir.length() <= 2 && (base_dir.is_empty() || base_dir.ends_with(":"))) {
  136. return p_path;
  137. }
  138. return String("..").plus_file(relative_to_impl(p_path, base_dir));
  139. }
  140. }
  141. #ifdef WINDOWS_ENABLED
  142. String get_drive_letter(const String &p_norm_path) {
  143. int idx = p_norm_path.find(":/");
  144. if (idx != -1 && idx < p_norm_path.find("/"))
  145. return p_norm_path.substr(0, idx + 1);
  146. return String();
  147. }
  148. #endif
  149. String relative_to(const String &p_path, const String &p_relative_to) {
  150. String relative_to_abs_norm = abspath(p_relative_to);
  151. String path_abs_norm = abspath(p_path);
  152. #ifdef WINDOWS_ENABLED
  153. if (get_drive_letter(relative_to_abs_norm) != get_drive_letter(path_abs_norm)) {
  154. return path_abs_norm;
  155. }
  156. #endif
  157. return relative_to_impl(path_abs_norm, relative_to_abs_norm);
  158. }
  159. } // namespace path