DefaultIOSystem.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2018, 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/DefaultIOSystem.h>
  37. #include <assimp/DefaultIOStream.h>
  38. #include <assimp/DefaultLogger.hpp>
  39. #include <assimp/ai_assert.h>
  40. #include <stdlib.h>
  41. #ifdef __unix__
  42. #include <sys/param.h>
  43. #include <stdlib.h>
  44. #endif
  45. #ifdef _WIN32
  46. #include <windows.h>
  47. #endif
  48. using namespace Assimp;
  49. // maximum path length
  50. // XXX http://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html
  51. #ifdef PATH_MAX
  52. # define PATHLIMIT PATH_MAX
  53. #else
  54. # define PATHLIMIT 4096
  55. #endif
  56. // ------------------------------------------------------------------------------------------------
  57. // Tests for the existence of a file at the given path.
  58. bool DefaultIOSystem::Exists( const char* pFile) const
  59. {
  60. #ifdef _WIN32
  61. wchar_t fileName16[PATHLIMIT];
  62. bool isUnicode = IsTextUnicode(pFile, static_cast<int>(strlen(pFile)), NULL) != 0;
  63. if (isUnicode) {
  64. MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, pFile, -1, fileName16, PATHLIMIT);
  65. struct __stat64 filestat;
  66. if (0 != _wstat64(fileName16, &filestat)) {
  67. return false;
  68. }
  69. } else {
  70. FILE* file = ::fopen(pFile, "rb");
  71. if (!file)
  72. return false;
  73. ::fclose(file);
  74. }
  75. #else
  76. FILE* file = ::fopen( pFile, "rb");
  77. if( !file)
  78. return false;
  79. ::fclose( file);
  80. #endif
  81. return true;
  82. }
  83. // ------------------------------------------------------------------------------------------------
  84. // Open a new file with a given path.
  85. IOStream* DefaultIOSystem::Open( const char* strFile, const char* strMode)
  86. {
  87. ai_assert(NULL != strFile);
  88. ai_assert(NULL != strMode);
  89. FILE* file;
  90. #ifdef _WIN32
  91. wchar_t fileName16[PATHLIMIT];
  92. bool isUnicode = IsTextUnicode(strFile, static_cast<int>(strlen(strFile)), NULL) != 0;
  93. if (isUnicode) {
  94. MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, strFile, -1, fileName16, PATHLIMIT);
  95. std::string mode8(strMode);
  96. file = ::_wfopen(fileName16, std::wstring(mode8.begin(), mode8.end()).c_str());
  97. } else {
  98. file = ::fopen(strFile, strMode);
  99. }
  100. #else
  101. file = ::fopen(strFile, strMode);
  102. #endif
  103. if (nullptr == file)
  104. return nullptr;
  105. return new DefaultIOStream(file, (std::string) strFile);
  106. }
  107. // ------------------------------------------------------------------------------------------------
  108. // Closes the given file and releases all resources associated with it.
  109. void DefaultIOSystem::Close( IOStream* pFile)
  110. {
  111. delete pFile;
  112. }
  113. // ------------------------------------------------------------------------------------------------
  114. // Returns the operation specific directory separator
  115. char DefaultIOSystem::getOsSeparator() const
  116. {
  117. #ifndef _WIN32
  118. return '/';
  119. #else
  120. return '\\';
  121. #endif
  122. }
  123. // ------------------------------------------------------------------------------------------------
  124. // IOSystem default implementation (ComparePaths isn't a pure virtual function)
  125. bool IOSystem::ComparePaths (const char* one, const char* second) const
  126. {
  127. return !ASSIMP_stricmp(one,second);
  128. }
  129. // ------------------------------------------------------------------------------------------------
  130. // Convert a relative path into an absolute path
  131. inline static void MakeAbsolutePath (const char* in, char* _out)
  132. {
  133. ai_assert(in && _out);
  134. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  135. bool isUnicode = IsTextUnicode(in, static_cast<int>(strlen(in)), NULL) != 0;
  136. if (isUnicode) {
  137. wchar_t out16[PATHLIMIT];
  138. wchar_t in16[PATHLIMIT];
  139. MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, in, -1, out16, PATHLIMIT);
  140. wchar_t* ret = ::_wfullpath(out16, in16, PATHLIMIT);
  141. if (ret) {
  142. WideCharToMultiByte(CP_UTF8, MB_PRECOMPOSED, out16, -1, _out, PATHLIMIT, nullptr, nullptr);
  143. }
  144. if (!ret) {
  145. // preserve the input path, maybe someone else is able to fix
  146. // the path before it is accessed (e.g. our file system filter)
  147. ASSIMP_LOG_WARN_F("Invalid path: ", std::string(in));
  148. strcpy(_out, in);
  149. }
  150. } else {
  151. char* ret = :: _fullpath(_out, in, PATHLIMIT);
  152. if (!ret) {
  153. // preserve the input path, maybe someone else is able to fix
  154. // the path before it is accessed (e.g. our file system filter)
  155. ASSIMP_LOG_WARN_F("Invalid path: ", std::string(in));
  156. strcpy(_out, in);
  157. }
  158. }
  159. #else
  160. // use realpath
  161. char* ret = realpath(in, _out);
  162. if(!ret) {
  163. // preserve the input path, maybe someone else is able to fix
  164. // the path before it is accessed (e.g. our file system filter)
  165. ASSIMP_LOG_WARN_F("Invalid path: ", std::string(in));
  166. strcpy(_out,in);
  167. }
  168. #endif
  169. }
  170. // ------------------------------------------------------------------------------------------------
  171. // DefaultIOSystem's more specialized implementation
  172. bool DefaultIOSystem::ComparePaths (const char* one, const char* second) const
  173. {
  174. // chances are quite good both paths are formatted identically,
  175. // so we can hopefully return here already
  176. if( !ASSIMP_stricmp(one,second) )
  177. return true;
  178. char temp1[PATHLIMIT];
  179. char temp2[PATHLIMIT];
  180. MakeAbsolutePath (one, temp1);
  181. MakeAbsolutePath (second, temp2);
  182. return !ASSIMP_stricmp(temp1,temp2);
  183. }
  184. // ------------------------------------------------------------------------------------------------
  185. std::string DefaultIOSystem::fileName( const std::string &path )
  186. {
  187. std::string ret = path;
  188. std::size_t last = ret.find_last_of("\\/");
  189. if (last != std::string::npos) ret = ret.substr(last + 1);
  190. return ret;
  191. }
  192. // ------------------------------------------------------------------------------------------------
  193. std::string DefaultIOSystem::completeBaseName( const std::string &path )
  194. {
  195. std::string ret = fileName(path);
  196. std::size_t pos = ret.find_last_of('.');
  197. if(pos != ret.npos) ret = ret.substr(0, pos);
  198. return ret;
  199. }
  200. // ------------------------------------------------------------------------------------------------
  201. std::string DefaultIOSystem::absolutePath( const std::string &path )
  202. {
  203. std::string ret = path;
  204. std::size_t last = ret.find_last_of("\\/");
  205. if (last != std::string::npos) ret = ret.substr(0, last);
  206. return ret;
  207. }
  208. // ------------------------------------------------------------------------------------------------
  209. #undef PATHLIMIT