You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

299 lines
7.7 KiB

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