DefaultIOSystem.cpp 5.4 KB

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