DefaultIOSystem.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2015, 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 "DefaultIOSystem.h"
  36. #include "DefaultIOStream.h"
  37. #include "StringComparison.h"
  38. #include "../include/assimp/DefaultLogger.hpp"
  39. #include "../include/assimp/ai_assert.h"
  40. #include <stdlib.h>
  41. #ifdef __unix__
  42. #include <sys/param.h>
  43. #include <stdlib.h>
  44. #endif
  45. using namespace Assimp;
  46. // ------------------------------------------------------------------------------------------------
  47. // Constructor.
  48. DefaultIOSystem::DefaultIOSystem()
  49. {
  50. // nothing to do here
  51. }
  52. // ------------------------------------------------------------------------------------------------
  53. // Destructor.
  54. DefaultIOSystem::~DefaultIOSystem()
  55. {
  56. // nothing to do here
  57. }
  58. // ------------------------------------------------------------------------------------------------
  59. // Tests for the existence of a file at the given path.
  60. bool DefaultIOSystem::Exists( const char* pFile) const
  61. {
  62. FILE* file = ::fopen( pFile, "rb");
  63. if( !file)
  64. return false;
  65. ::fclose( file);
  66. return true;
  67. }
  68. // ------------------------------------------------------------------------------------------------
  69. // Open a new file with a given path.
  70. IOStream* DefaultIOSystem::Open( const char* strFile, const char* strMode)
  71. {
  72. ai_assert(NULL != strFile);
  73. ai_assert(NULL != strMode);
  74. FILE* file = ::fopen( strFile, strMode);
  75. if( NULL == file)
  76. return NULL;
  77. return new DefaultIOStream(file, (std::string) strFile);
  78. }
  79. // ------------------------------------------------------------------------------------------------
  80. // Closes the given file and releases all resources associated with it.
  81. void DefaultIOSystem::Close( IOStream* pFile)
  82. {
  83. delete pFile;
  84. }
  85. // ------------------------------------------------------------------------------------------------
  86. // Returns the operation specific directory separator
  87. char DefaultIOSystem::getOsSeparator() const
  88. {
  89. #ifndef _WIN32
  90. return '/';
  91. #else
  92. return '\\';
  93. #endif
  94. }
  95. // ------------------------------------------------------------------------------------------------
  96. // IOSystem default implementation (ComparePaths isn't a pure virtual function)
  97. bool IOSystem::ComparePaths (const char* one, const char* second) const
  98. {
  99. return !ASSIMP_stricmp(one,second);
  100. }
  101. // maximum path length
  102. // XXX http://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html
  103. #ifdef PATH_MAX
  104. # define PATHLIMIT PATH_MAX
  105. #else
  106. # define PATHLIMIT 4096
  107. #endif
  108. // ------------------------------------------------------------------------------------------------
  109. // Convert a relative path into an absolute path
  110. inline void MakeAbsolutePath (const char* in, char* _out)
  111. {
  112. ai_assert(in && _out);
  113. char* ret;
  114. #ifdef _WIN32
  115. ret = ::_fullpath(_out, in,PATHLIMIT);
  116. #else
  117. // use realpath
  118. ret = realpath(in, _out);
  119. #endif
  120. if(!ret) {
  121. // preserve the input path, maybe someone else is able to fix
  122. // the path before it is accessed (e.g. our file system filter)
  123. DefaultLogger::get()->warn("Invalid path: "+std::string(in));
  124. strcpy(_out,in);
  125. }
  126. }
  127. // ------------------------------------------------------------------------------------------------
  128. // DefaultIOSystem's more specialized implementation
  129. bool DefaultIOSystem::ComparePaths (const char* one, const char* second) const
  130. {
  131. // chances are quite good both paths are formatted identically,
  132. // so we can hopefully return here already
  133. if( !ASSIMP_stricmp(one,second) )
  134. return true;
  135. char temp1[PATHLIMIT];
  136. char temp2[PATHLIMIT];
  137. MakeAbsolutePath (one, temp1);
  138. MakeAbsolutePath (second, temp2);
  139. return !ASSIMP_stricmp(temp1,temp2);
  140. }
  141. std::string DefaultIOSystem::fileName(std::string path)
  142. {
  143. std::string ret = path;
  144. std::size_t last = ret.find_last_of("\\/");
  145. if (last != std::string::npos) ret = ret.substr(last + 1);
  146. return ret;
  147. }
  148. std::string DefaultIOSystem::completeBaseName(std::string path)
  149. {
  150. std::string ret = fileName(path);
  151. std::size_t pos = ret.find_last_of('.');
  152. if(pos != ret.npos) ret = ret.substr(0, pos);
  153. return ret;
  154. }
  155. std::string DefaultIOSystem::absolutePath(std::string path)
  156. {
  157. std::string ret = path;
  158. std::size_t last = ret.find_last_of("\\/");
  159. if (last != std::string::npos) ret = ret.substr(0, last);
  160. return ret;
  161. }
  162. #undef PATHLIMIT