DefaultIOSystem.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2024, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Default implementation of IOSystem using the standard C file functions */
  35. #include <assimp/StringComparison.h>
  36. #include <assimp/DefaultIOStream.h>
  37. #include <assimp/DefaultIOSystem.h>
  38. #include <assimp/ai_assert.h>
  39. #include <stdlib.h>
  40. #include <assimp/DefaultLogger.hpp>
  41. #ifdef __unix__
  42. # include <stdlib.h>
  43. # include <sys/param.h>
  44. #endif
  45. #ifdef _WIN32
  46. # include <windows.h>
  47. #endif
  48. using namespace Assimp;
  49. #ifdef _WIN32
  50. const std::wstring wdummy;
  51. static std::wstring Utf8ToWide(const char *in) {
  52. if (nullptr == in) {
  53. return wdummy;
  54. }
  55. int size = MultiByteToWideChar(CP_UTF8, 0, in, -1, nullptr, 0);
  56. // size includes terminating null; std::wstring adds null automatically
  57. std::wstring out(static_cast<size_t>(size) - 1, L'\0');
  58. MultiByteToWideChar(CP_UTF8, 0, in, -1, &out[0], size);
  59. return out;
  60. }
  61. const std::string dummy;
  62. static std::string WideToUtf8(const wchar_t *in) {
  63. if (nullptr == in) {
  64. return dummy;
  65. }
  66. int size = WideCharToMultiByte(CP_UTF8, 0, in, -1, nullptr, 0, nullptr, nullptr);
  67. // size includes terminating null; std::string adds null automatically
  68. std::string out(static_cast<size_t>(size) - 1, '\0');
  69. WideCharToMultiByte(CP_UTF8, 0, in, -1, &out[0], size, nullptr, nullptr);
  70. return out;
  71. }
  72. #endif
  73. // ------------------------------------------------------------------------------------------------
  74. // Tests for the existence of a file at the given path.
  75. bool DefaultIOSystem::Exists(const char *pFile) const {
  76. if (pFile == nullptr) {
  77. return false;
  78. }
  79. #ifdef _WIN32
  80. struct __stat64 filestat;
  81. if (_wstat64(Utf8ToWide(pFile).c_str(), &filestat) != 0) {
  82. return false;
  83. }
  84. #else
  85. struct stat statbuf;
  86. if (stat(pFile, &statbuf) != 0) {
  87. return false;
  88. }
  89. // test for a regular file
  90. if (!S_ISREG(statbuf.st_mode)) {
  91. return false;
  92. }
  93. #endif
  94. return true;
  95. }
  96. // ------------------------------------------------------------------------------------------------
  97. // Open a new file with a given path.
  98. IOStream *DefaultIOSystem::Open(const char *strFile, const char *strMode) {
  99. ai_assert(strFile != nullptr);
  100. ai_assert(strMode != nullptr);
  101. FILE *file;
  102. #ifdef _WIN32
  103. std::wstring name = Utf8ToWide(strFile);
  104. if (name.empty()) {
  105. return nullptr;
  106. }
  107. file = ::_wfopen(name.c_str(), Utf8ToWide(strMode).c_str());
  108. #else
  109. file = ::fopen(strFile, strMode);
  110. #endif
  111. if (!file) {
  112. return nullptr;
  113. }
  114. return new DefaultIOStream(file, strFile);
  115. }
  116. // ------------------------------------------------------------------------------------------------
  117. // Closes the given file and releases all resources associated with it.
  118. void DefaultIOSystem::Close(IOStream *pFile) {
  119. delete pFile;
  120. }
  121. // ------------------------------------------------------------------------------------------------
  122. // Returns the operation specific directory separator
  123. char DefaultIOSystem::getOsSeparator() const {
  124. #ifndef _WIN32
  125. return '/';
  126. #else
  127. return '\\';
  128. #endif
  129. }
  130. // ------------------------------------------------------------------------------------------------
  131. // IOSystem default implementation (ComparePaths isn't a pure virtual function)
  132. bool IOSystem::ComparePaths(const char *one, const char *second) const {
  133. return !ASSIMP_stricmp(one, second);
  134. }
  135. // ------------------------------------------------------------------------------------------------
  136. // Convert a relative path into an absolute path
  137. inline static std::string MakeAbsolutePath(const char *in) {
  138. ai_assert(in);
  139. std::string out;
  140. #ifdef _WIN32
  141. wchar_t *ret = ::_wfullpath(nullptr, Utf8ToWide(in).c_str(), 0);
  142. if (ret) {
  143. out = WideToUtf8(ret);
  144. free(ret);
  145. }
  146. #else
  147. char *ret = realpath(in, nullptr);
  148. if (ret) {
  149. out = ret;
  150. free(ret);
  151. }
  152. #endif
  153. else {
  154. // preserve the input path, maybe someone else is able to fix
  155. // the path before it is accessed (e.g. our file system filter)
  156. ASSIMP_LOG_WARN("Invalid path: ", std::string(in));
  157. out = in;
  158. }
  159. return out;
  160. }
  161. // ------------------------------------------------------------------------------------------------
  162. // DefaultIOSystem's more specialized implementation
  163. bool DefaultIOSystem::ComparePaths(const char *one, const char *second) const {
  164. // chances are quite good both paths are formatted identically,
  165. // so we can hopefully return here already
  166. if (!ASSIMP_stricmp(one, second))
  167. return true;
  168. std::string temp1 = MakeAbsolutePath(one);
  169. std::string temp2 = MakeAbsolutePath(second);
  170. return !ASSIMP_stricmp(temp1, temp2);
  171. }
  172. // ------------------------------------------------------------------------------------------------
  173. std::string DefaultIOSystem::fileName(const std::string &path) {
  174. std::string ret = path;
  175. std::size_t last = ret.find_last_of("\\/");
  176. if (last != std::string::npos) ret = ret.substr(last + 1);
  177. return ret;
  178. }
  179. // ------------------------------------------------------------------------------------------------
  180. std::string DefaultIOSystem::completeBaseName(const std::string &path) {
  181. std::string ret = fileName(path);
  182. std::size_t pos = ret.find_last_of('.');
  183. if (pos != std::string::npos) ret = ret.substr(0, pos);
  184. return ret;
  185. }
  186. // ------------------------------------------------------------------------------------------------
  187. std::string DefaultIOSystem::absolutePath(const std::string &path) {
  188. std::string ret = path;
  189. std::size_t last = ret.find_last_of("\\/");
  190. if (last != std::string::npos) ret = ret.substr(0, last);
  191. return ret;
  192. }
  193. // ------------------------------------------------------------------------------------------------