FileSystemFilter.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development Team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the ASSIMP team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the ASSIMP Development Team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file FileSystemFilter.h
  34. * Implements a filter system to filter calls to Exists() and Open()
  35. * in order to improve the sucess rate of file opening ...
  36. */
  37. #ifndef AI_FILESYSTEMFILTER_H_INC
  38. #define AI_FILESYSTEMFILTER_H_INC
  39. #include "../include/IOSystem.h"
  40. #include "fast_atof.h"
  41. #include "ParsingUtils.h"
  42. namespace Assimp {
  43. inline bool IsHex(char s) {
  44. return (s>='0' && s<='9') || (s>='a' && s<='f') || (s>='A' && s<='F');
  45. }
  46. // ---------------------------------------------------------------------------
  47. /** File system filter
  48. */
  49. class FileSystemFilter : public IOSystem
  50. {
  51. public:
  52. /** Constructor. */
  53. FileSystemFilter(const std::string& file, IOSystem* old)
  54. : wrapped (old)
  55. , src_file (file)
  56. {
  57. ai_assert(NULL != wrapped);
  58. // Determine base directory
  59. base = src_file;
  60. std::string::size_type ss2;
  61. if (std::string::npos != (ss2 = base.find_last_of("\\/"))) {
  62. base.erase(ss2,base.length()-ss2);
  63. }
  64. else {
  65. base = "";
  66. // return;
  67. }
  68. // make sure the directory is terminated properly
  69. char s;
  70. if (base.length() == 0) {
  71. base = ".";
  72. base += getOsSeparator();
  73. }
  74. else if ((s = *(base.end()-1)) != '\\' && s != '/')
  75. base += getOsSeparator();
  76. DefaultLogger::get()->info("Import root directory is \'" + base + "\'");
  77. }
  78. /** Destructor. */
  79. ~FileSystemFilter()
  80. {
  81. // haha
  82. }
  83. // -------------------------------------------------------------------
  84. /** Tests for the existence of a file at the given path. */
  85. bool Exists( const char* pFile) const
  86. {
  87. std::string tmp = pFile;
  88. // Currently this IOSystem is also used to open THE ONE FILE.
  89. if (tmp != src_file) {
  90. BuildPath(tmp);
  91. Cleanup(tmp);
  92. }
  93. return wrapped->Exists(tmp);
  94. }
  95. // -------------------------------------------------------------------
  96. /** Returns the directory separator. */
  97. char getOsSeparator() const
  98. {
  99. return wrapped->getOsSeparator();
  100. }
  101. // -------------------------------------------------------------------
  102. /** Open a new file with a given path. */
  103. IOStream* Open( const char* pFile, const char* pMode = "rb")
  104. {
  105. ai_assert(pFile);
  106. ai_assert(pMode);
  107. // First try the unchanged path
  108. IOStream* s = wrapped->Open(pFile,pMode);
  109. if (!s) {
  110. std::string tmp = pFile;
  111. // Try to convert between absolute and relative paths
  112. BuildPath(tmp);
  113. s = wrapped->Open(tmp,pMode);
  114. if (!s) {
  115. // Finally, look for typical issues with paths
  116. // and try to correct them. This is our last
  117. // resort.
  118. Cleanup(tmp);
  119. s = wrapped->Open(tmp,pMode);
  120. }
  121. }
  122. return s;
  123. }
  124. // -------------------------------------------------------------------
  125. /** Closes the given file and releases all resources associated with it. */
  126. void Close( IOStream* pFile)
  127. {
  128. return wrapped->Close(pFile);
  129. }
  130. // -------------------------------------------------------------------
  131. /** Compare two paths */
  132. bool ComparePaths (const char* one, const char* second) const
  133. {
  134. return wrapped->ComparePaths (one,second);
  135. }
  136. private:
  137. IOSystem* wrapped;
  138. std::string src_file, base;
  139. // -------------------------------------------------------------------
  140. /** Build a valid path from a given relative or absolute path.
  141. */
  142. void BuildPath (std::string& in) const
  143. {
  144. // if we can already access the file, great.
  145. if (in.length() < 3 || wrapped->Exists(in.c_str())) {
  146. return;
  147. }
  148. // Determine whether this is a relative path (Windows-specific - most assets are packaged on Windows).
  149. if (in[1] != ':') {
  150. // append base path and try
  151. in = base + in;
  152. if (wrapped->Exists(in.c_str())) {
  153. return;
  154. }
  155. }
  156. // hopefully the underyling file system has another few tricks to access this file ...
  157. }
  158. // -------------------------------------------------------------------
  159. /** Cleanup the given path
  160. */
  161. void Cleanup (std::string& in) const
  162. {
  163. char last = 0;
  164. if(in.empty()) {
  165. return;
  166. }
  167. // Remove a very common issue when we're parsing file names: spaces at the
  168. // beginning of the path.
  169. std::string::iterator it = in.begin();
  170. while (IsSpaceOrNewLine( *it ))++it;
  171. if (it != in.begin())
  172. in.erase(in.begin(),it+1);
  173. const char sep = getOsSeparator();
  174. for (it = in.begin(); it != in.end(); ++it) {
  175. // Exclude :// and \\, which remain untouched.
  176. // https://sourceforge.net/tracker/?func=detail&aid=3031725&group_id=226462&atid=1067632
  177. if ( !strncmp(&*it, "://", 3 )) {
  178. it += 3;
  179. continue;
  180. }
  181. if (it == in.begin() && !strncmp(&*it, "\\\\", 2)) {
  182. it += 2;
  183. continue;
  184. }
  185. // Cleanup path delimiters
  186. if (*it == '/' || (*it) == '\\') {
  187. *it = sep;
  188. // And we're removing double delimiters, frequent issue with
  189. // incorrectly composited paths ...
  190. if (last == *it) {
  191. it = in.erase(it);
  192. --it;
  193. }
  194. }
  195. else if (*it == '%' && in.end() - it > 2) {
  196. // Hex sequence in URIs
  197. if( IsHex((&*it)[0]) && IsHex((&*it)[1]) ) {
  198. *it = HexOctetToDecimal(&*it);
  199. it = in.erase(it+1,it+2);
  200. --it;
  201. }
  202. }
  203. last = *it;
  204. }
  205. }
  206. };
  207. } //!ns Assimp
  208. #endif //AI_DEFAULTIOSYSTEM_H_INC