path_utils.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*************************************************************************/
  2. /* path_utils.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/os/dir_access.h"
  32. #include "core/os/file_access.h"
  33. #include "core/os/os.h"
  34. #include "core/project_settings.h"
  35. #ifdef WINDOWS_ENABLED
  36. #define ENV_PATH_SEP ";"
  37. #else
  38. #define ENV_PATH_SEP ":"
  39. #include <limits.h>
  40. #endif
  41. #include <stdlib.h>
  42. String path_which(const String &p_name) {
  43. #ifdef WINDOWS_ENABLED
  44. Vector<String> exts = OS::get_singleton()->get_environment("PATHEXT").split(ENV_PATH_SEP, false);
  45. #endif
  46. Vector<String> env_path = OS::get_singleton()->get_environment("PATH").split(ENV_PATH_SEP, false);
  47. if (env_path.empty())
  48. return String();
  49. for (int i = 0; i < env_path.size(); i++) {
  50. String p = path_join(env_path[i], p_name);
  51. #ifdef WINDOWS_ENABLED
  52. for (int j = 0; j < exts.size(); j++) {
  53. String p2 = p + exts[j].to_lower(); // lowercase to reduce risk of case mismatch warning
  54. if (FileAccess::exists(p2))
  55. return p2;
  56. }
  57. #else
  58. if (FileAccess::exists(p))
  59. return p;
  60. #endif
  61. }
  62. return String();
  63. }
  64. void fix_path(const String &p_path, String &r_out) {
  65. r_out = p_path.replace("\\", "/");
  66. while (true) { // in case of using 2 or more slash
  67. String compare = r_out.replace("//", "/");
  68. if (r_out == compare)
  69. break;
  70. else
  71. r_out = compare;
  72. }
  73. }
  74. bool rel_path_to_abs(const String &p_existing_path, String &r_abs_path) {
  75. #ifdef WINDOWS_ENABLED
  76. CharType ret[_MAX_PATH];
  77. if (::_wfullpath(ret, p_existing_path.c_str(), _MAX_PATH)) {
  78. String abspath = String(ret).replace("\\", "/");
  79. int pos = abspath.find(":/");
  80. if (pos != -1) {
  81. r_abs_path = abspath.substr(pos - 1, abspath.length());
  82. } else {
  83. r_abs_path = abspath;
  84. }
  85. return true;
  86. }
  87. #else
  88. char *resolved_path = ::realpath(p_existing_path.utf8().get_data(), NULL);
  89. if (resolved_path) {
  90. String retstr;
  91. bool success = !retstr.parse_utf8(resolved_path);
  92. ::free(resolved_path);
  93. if (success) {
  94. r_abs_path = retstr;
  95. return true;
  96. }
  97. }
  98. #endif
  99. return false;
  100. }