FileSystemFilter.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, assimp 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 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 success rate of file opening ...
  36. */
  37. #ifndef AI_FILESYSTEMFILTER_H_INC
  38. #define AI_FILESYSTEMFILTER_H_INC
  39. #include "../include/assimp/IOSystem.hpp"
  40. #include "../include/assimp/DefaultLogger.hpp"
  41. #include "fast_atof.h"
  42. #include "ParsingUtils.h"
  43. namespace Assimp {
  44. inline bool IsHex(char s) {
  45. return (s>='0' && s<='9') || (s>='a' && s<='f') || (s>='A' && s<='F');
  46. }
  47. // ---------------------------------------------------------------------------
  48. /** File system filter
  49. */
  50. class FileSystemFilter : public IOSystem
  51. {
  52. public:
  53. /** Constructor. */
  54. FileSystemFilter(const std::string& file, IOSystem* old)
  55. : wrapped (old)
  56. , src_file (file)
  57. , sep(wrapped->getOsSeparator())
  58. {
  59. ai_assert(NULL != wrapped);
  60. // Determine base directory
  61. base = src_file;
  62. std::string::size_type ss2;
  63. if (std::string::npos != (ss2 = base.find_last_of("\\/"))) {
  64. base.erase(ss2,base.length()-ss2);
  65. }
  66. else {
  67. base = "";
  68. // return;
  69. }
  70. // make sure the directory is terminated properly
  71. char s;
  72. if (base.length() == 0) {
  73. base = ".";
  74. base += getOsSeparator();
  75. }
  76. else if ((s = *(base.end()-1)) != '\\' && s != '/') {
  77. base += getOsSeparator();
  78. }
  79. DefaultLogger::get()->info("Import root directory is \'" + base + "\'");
  80. }
  81. /** Destructor. */
  82. ~FileSystemFilter()
  83. {
  84. // haha
  85. }
  86. // -------------------------------------------------------------------
  87. /** Tests for the existence of a file at the given path. */
  88. bool Exists( const char* pFile) const
  89. {
  90. std::string tmp = pFile;
  91. // Currently this IOSystem is also used to open THE ONE FILE.
  92. if (tmp != src_file) {
  93. BuildPath(tmp);
  94. Cleanup(tmp);
  95. }
  96. return wrapped->Exists(tmp);
  97. }
  98. // -------------------------------------------------------------------
  99. /** Returns the directory separator. */
  100. char getOsSeparator() const
  101. {
  102. return sep;
  103. }
  104. // -------------------------------------------------------------------
  105. /** Open a new file with a given path. */
  106. IOStream* Open( const char* pFile, const char* pMode = "rb")
  107. {
  108. ai_assert(pFile);
  109. ai_assert(pMode);
  110. // First try the unchanged path
  111. IOStream* s = wrapped->Open(pFile,pMode);
  112. if (!s) {
  113. std::string tmp = pFile;
  114. // Try to convert between absolute and relative paths
  115. BuildPath(tmp);
  116. s = wrapped->Open(tmp,pMode);
  117. if (!s) {
  118. // Finally, look for typical issues with paths
  119. // and try to correct them. This is our last
  120. // resort.
  121. tmp = pFile;
  122. Cleanup(tmp);
  123. BuildPath(tmp);
  124. s = wrapped->Open(tmp,pMode);
  125. }
  126. }
  127. return s;
  128. }
  129. // -------------------------------------------------------------------
  130. /** Closes the given file and releases all resources associated with it. */
  131. void Close( IOStream* pFile)
  132. {
  133. return wrapped->Close(pFile);
  134. }
  135. // -------------------------------------------------------------------
  136. /** Compare two paths */
  137. bool ComparePaths (const char* one, const char* second) const
  138. {
  139. return wrapped->ComparePaths (one,second);
  140. }
  141. private:
  142. // -------------------------------------------------------------------
  143. /** Build a valid path from a given relative or absolute path.
  144. */
  145. void BuildPath (std::string& in) const
  146. {
  147. // if we can already access the file, great.
  148. if (in.length() < 3 || wrapped->Exists(in)) {
  149. return;
  150. }
  151. // Determine whether this is a relative path (Windows-specific - most assets are packaged on Windows).
  152. if (in[1] != ':') {
  153. // append base path and try
  154. const std::string tmp = base + in;
  155. if (wrapped->Exists(tmp)) {
  156. in = tmp;
  157. return;
  158. }
  159. }
  160. // Chop of the file name and look in the model directory, if
  161. // this fails try all sub paths of the given path, i.e.
  162. // if the given path is foo/bar/something.lwo, try
  163. // <base>/something.lwo
  164. // <base>/bar/something.lwo
  165. // <base>/foo/bar/something.lwo
  166. std::string::size_type pos = in.rfind('/');
  167. if (std::string::npos == pos) {
  168. pos = in.rfind('\\');
  169. }
  170. if (std::string::npos != pos) {
  171. std::string tmp;
  172. std::string::size_type last_dirsep = std::string::npos;
  173. while(true) {
  174. tmp = base;
  175. tmp += sep;
  176. std::string::size_type dirsep = in.rfind('/', last_dirsep);
  177. if (std::string::npos == dirsep) {
  178. dirsep = in.rfind('\\', last_dirsep);
  179. }
  180. if (std::string::npos == dirsep || dirsep == 0) {
  181. // we did try this already.
  182. break;
  183. }
  184. last_dirsep = dirsep-1;
  185. tmp += in.substr(dirsep+1, in.length()-pos);
  186. if (wrapped->Exists(tmp)) {
  187. in = tmp;
  188. return;
  189. }
  190. }
  191. }
  192. // hopefully the underlying file system has another few tricks to access this file ...
  193. }
  194. // -------------------------------------------------------------------
  195. /** Cleanup the given path
  196. */
  197. void Cleanup (std::string& in) const
  198. {
  199. char last = 0;
  200. if(in.empty()) {
  201. return;
  202. }
  203. // Remove a very common issue when we're parsing file names: spaces at the
  204. // beginning of the path.
  205. std::string::iterator it = in.begin();
  206. while (IsSpaceOrNewLine( *it ))++it;
  207. if (it != in.begin()) {
  208. in.erase(in.begin(),it+1);
  209. }
  210. const char sep = getOsSeparator();
  211. for (it = in.begin(); it != in.end(); ++it) {
  212. // Exclude :// and \\, which remain untouched.
  213. // https://sourceforge.net/tracker/?func=detail&aid=3031725&group_id=226462&atid=1067632
  214. if ( !strncmp(&*it, "://", 3 )) {
  215. it += 3;
  216. continue;
  217. }
  218. if (it == in.begin() && !strncmp(&*it, "\\\\", 2)) {
  219. it += 2;
  220. continue;
  221. }
  222. // Cleanup path delimiters
  223. if (*it == '/' || (*it) == '\\') {
  224. *it = sep;
  225. // And we're removing double delimiters, frequent issue with
  226. // incorrectly composited paths ...
  227. if (last == *it) {
  228. it = in.erase(it);
  229. --it;
  230. }
  231. }
  232. else if (*it == '%' && in.end() - it > 2) {
  233. // Hex sequence in URIs
  234. if( IsHex((&*it)[0]) && IsHex((&*it)[1]) ) {
  235. *it = HexOctetToDecimal(&*it);
  236. it = in.erase(it+1,it+2);
  237. --it;
  238. }
  239. }
  240. last = *it;
  241. }
  242. }
  243. private:
  244. IOSystem* wrapped;
  245. std::string src_file, base;
  246. char sep;
  247. };
  248. } //!ns Assimp
  249. #endif //AI_DEFAULTIOSYSTEM_H_INC