DefaultIOSystem.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2020, 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. static std::wstring Utf8ToWide(const char *in) {
  51. int size = MultiByteToWideChar(CP_UTF8, 0, in, -1, nullptr, 0);
  52. // size includes terminating null; std::wstring adds null automatically
  53. std::wstring out(static_cast<size_t>(size) - 1, L'\0');
  54. MultiByteToWideChar(CP_UTF8, 0, in, -1, &out[0], size);
  55. return out;
  56. }
  57. static std::string WideToUtf8(const wchar_t *in) {
  58. int size = WideCharToMultiByte(CP_UTF8, 0, in, -1, nullptr, 0, nullptr, nullptr);
  59. // size includes terminating null; std::string adds null automatically
  60. std::string out(static_cast<size_t>(size) - 1, '\0');
  61. WideCharToMultiByte(CP_UTF8, 0, in, -1, &out[0], size, nullptr, nullptr);
  62. return out;
  63. }
  64. #endif
  65. // ------------------------------------------------------------------------------------------------
  66. // Tests for the existence of a file at the given path.
  67. bool DefaultIOSystem::Exists(const char *pFile) const {
  68. #ifdef _WIN32
  69. struct __stat64 filestat;
  70. if (_wstat64(Utf8ToWide(pFile).c_str(), &filestat) != 0) {
  71. return false;
  72. }
  73. #else
  74. FILE *file = ::fopen(pFile, "rb");
  75. if (!file) {
  76. return false;
  77. }
  78. ::fclose(file);
  79. #endif
  80. return true;
  81. }
  82. // ------------------------------------------------------------------------------------------------
  83. // Open a new file with a given path.
  84. IOStream *DefaultIOSystem::Open(const char *strFile, const char *strMode) {
  85. ai_assert(strFile != nullptr);
  86. ai_assert(strMode != nullptr);
  87. FILE *file;
  88. #ifdef _WIN32
  89. file = ::_wfopen(Utf8ToWide(strFile).c_str(), Utf8ToWide(strMode).c_str());
  90. #else
  91. file = ::fopen(strFile, strMode);
  92. #endif
  93. if (!file) {
  94. return nullptr;
  95. }
  96. return new DefaultIOStream(file, strFile);
  97. }
  98. // ------------------------------------------------------------------------------------------------
  99. // Closes the given file and releases all resources associated with it.
  100. void DefaultIOSystem::Close(IOStream *pFile) {
  101. delete pFile;
  102. }
  103. // ------------------------------------------------------------------------------------------------
  104. // Returns the operation specific directory separator
  105. char DefaultIOSystem::getOsSeparator() const {
  106. #ifndef _WIN32
  107. return '/';
  108. #else
  109. return '\\';
  110. #endif
  111. }
  112. // ------------------------------------------------------------------------------------------------
  113. // IOSystem default implementation (ComparePaths isn't a pure virtual function)
  114. bool IOSystem::ComparePaths(const char *one, const char *second) const {
  115. return !ASSIMP_stricmp(one, second);
  116. }
  117. // ------------------------------------------------------------------------------------------------
  118. // Convert a relative path into an absolute path
  119. inline static std::string MakeAbsolutePath(const char *in) {
  120. ai_assert(in);
  121. std::string out;
  122. #ifdef _WIN32
  123. wchar_t *ret = ::_wfullpath(nullptr, Utf8ToWide(in).c_str(), 0);
  124. if (ret) {
  125. out = WideToUtf8(ret);
  126. free(ret);
  127. }
  128. #else
  129. char *ret = realpath(in, nullptr);
  130. if (ret) {
  131. out = ret;
  132. free(ret);
  133. }
  134. #endif
  135. if (!ret) {
  136. // preserve the input path, maybe someone else is able to fix
  137. // the path before it is accessed (e.g. our file system filter)
  138. ASSIMP_LOG_WARN_F("Invalid path: ", std::string(in));
  139. out = in;
  140. }
  141. return out;
  142. }
  143. // ------------------------------------------------------------------------------------------------
  144. // DefaultIOSystem's more specialized implementation
  145. bool DefaultIOSystem::ComparePaths(const char *one, const char *second) const {
  146. // chances are quite good both paths are formatted identically,
  147. // so we can hopefully return here already
  148. if (!ASSIMP_stricmp(one, second))
  149. return true;
  150. std::string temp1 = MakeAbsolutePath(one);
  151. std::string temp2 = MakeAbsolutePath(second);
  152. return !ASSIMP_stricmp(temp1, temp2);
  153. }
  154. // ------------------------------------------------------------------------------------------------
  155. std::string DefaultIOSystem::fileName(const std::string &path) {
  156. std::string ret = path;
  157. std::size_t last = ret.find_last_of("\\/");
  158. if (last != std::string::npos) ret = ret.substr(last + 1);
  159. return ret;
  160. }
  161. // ------------------------------------------------------------------------------------------------
  162. std::string DefaultIOSystem::completeBaseName(const std::string &path) {
  163. std::string ret = fileName(path);
  164. std::size_t pos = ret.find_last_of('.');
  165. if (pos != std::string::npos) ret = ret.substr(0, pos);
  166. return ret;
  167. }
  168. // ------------------------------------------------------------------------------------------------
  169. std::string DefaultIOSystem::absolutePath(const std::string &path) {
  170. std::string ret = path;
  171. std::size_t last = ret.find_last_of("\\/");
  172. if (last != std::string::npos) ret = ret.substr(0, last);
  173. return ret;
  174. }
  175. // ------------------------------------------------------------------------------------------------