FileSystemFilter.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. // ---------------------------------------------------------------------------
  44. /** File system filter
  45. */
  46. class FileSystemFilter : public IOSystem
  47. {
  48. public:
  49. /** Constructor. */
  50. FileSystemFilter(const std::string& file, IOSystem* old)
  51. : wrapped (old)
  52. , src_file (file)
  53. {
  54. ai_assert(NULL != wrapped);
  55. // Determine base directory
  56. base = src_file;
  57. std::string::size_type ss2;
  58. if (std::string::npos != (ss2 = base.find_last_of("\\/"))) {
  59. base.erase(ss2,base.length()-ss2);
  60. }
  61. else {
  62. base = "";
  63. // return;
  64. }
  65. // make sure the directory is terminated properly
  66. char s;
  67. if (base.length() == 0) {
  68. base = ".";
  69. base += getOsSeparator();
  70. }
  71. else if ((s = *(base.end()-1)) != '\\' && s != '/')
  72. base += getOsSeparator();
  73. DefaultLogger::get()->info("Import root directory is \'" + base + "\'");
  74. }
  75. /** Destructor. */
  76. ~FileSystemFilter()
  77. {
  78. // haha
  79. }
  80. // -------------------------------------------------------------------
  81. /** Tests for the existence of a file at the given path. */
  82. bool Exists( const char* pFile) const
  83. {
  84. std::string tmp = pFile;
  85. // Currently this IOSystem is also used to open THE ONE FILE.
  86. if (tmp != src_file) {
  87. BuildPath(tmp);
  88. Cleanup(tmp);
  89. }
  90. return wrapped->Exists(tmp);
  91. }
  92. // -------------------------------------------------------------------
  93. /** Returns the directory separator. */
  94. char getOsSeparator() const
  95. {
  96. return wrapped->getOsSeparator();
  97. }
  98. // -------------------------------------------------------------------
  99. /** Open a new file with a given path. */
  100. IOStream* Open( const char* pFile, const char* pMode = "rb")
  101. {
  102. std::string tmp = pFile;
  103. // Currently this IOSystem is also used to open THE ONE FILE.
  104. if (tmp != src_file) {
  105. BuildPath(tmp);
  106. Cleanup(tmp);
  107. }
  108. return wrapped->Open(tmp,pMode);
  109. }
  110. // -------------------------------------------------------------------
  111. /** Closes the given file and releases all resources associated with it. */
  112. void Close( IOStream* pFile)
  113. {
  114. return wrapped->Close(pFile);
  115. }
  116. // -------------------------------------------------------------------
  117. /** Compare two paths */
  118. bool ComparePaths (const char* one, const char* second) const
  119. {
  120. return wrapped->ComparePaths (one,second);
  121. }
  122. private:
  123. IOSystem* wrapped;
  124. std::string src_file, base;
  125. // -------------------------------------------------------------------
  126. /** Build a valid path from a given relative or absolute path.
  127. */
  128. void BuildPath (std::string& in) const
  129. {
  130. // if we can already access the file, great.
  131. if (in.length() < 3 || wrapped->Exists(in.c_str())) {
  132. return;
  133. }
  134. // Determine whether this is a relative path.
  135. if (in[1] != ':') {
  136. // append base path and try
  137. in = base + in;
  138. if (wrapped->Exists(in.c_str())) {
  139. return;
  140. }
  141. }
  142. // hopefully the underyling file system has another few tricks to access this file ...
  143. }
  144. // -------------------------------------------------------------------
  145. /** Cleanup the given path
  146. */
  147. void Cleanup (std::string& in) const
  148. {
  149. char last = 0;
  150. // Remove a very common issue when we're parsing file names: spaces at the
  151. // beginning of the path.
  152. std::string::iterator it = in.begin();
  153. while (IsSpaceOrNewLine( *it ))++it;
  154. if (it != in.begin())
  155. in.erase(in.begin(),it+1);
  156. const char sep = getOsSeparator();
  157. for (it = in.begin(); it != in.end(); ++it) {
  158. // Both Windows and Linux accept both separators, but to avoid conflicts
  159. // or mixed seperators in a single path we're cleaning up.
  160. if (*it == '/' || (*it) == '\\') {
  161. *it = sep;
  162. // And we're removing double delimiters, frequent issue with
  163. // incorrectly composited paths ...
  164. if (last == *it) {
  165. it = in.erase(it);
  166. --it;
  167. }
  168. }
  169. else if (*it == '%' && in.end() - it > 2) {
  170. // Hex sequence, common _artifact_ in URIs
  171. uint32_t tmp;
  172. if( 0xffffffff != (tmp = HexOctetToDecimal(&*it))) {
  173. *it = (char)tmp;
  174. it = in.erase(it+1,it+2);
  175. --it;
  176. }
  177. }
  178. last = *it;
  179. }
  180. }
  181. };
  182. } //!ns Assimp
  183. #endif //AI_DEFAULTIOSYSTEM_H_INC