FileSystemFilter.h 11 KB

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