DirStackFileIncluder.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
  3. // Copyright (C) 2017 Google, Inc.
  4. //
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions
  9. // are met:
  10. //
  11. // Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. //
  14. // Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following
  16. // disclaimer in the documentation and/or other materials provided
  17. // with the distribution.
  18. //
  19. // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
  20. // contributors may be used to endorse or promote products derived
  21. // from this software without specific prior written permission.
  22. //
  23. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. // POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. #pragma once
  37. #include <vector>
  38. #include <string>
  39. #include <fstream>
  40. #include <algorithm>
  41. #include <set>
  42. #include "./../glslang/Public/ShaderLang.h"
  43. // Default include class for normal include convention of search backward
  44. // through the stack of active include paths (for nested includes).
  45. // Can be overridden to customize.
  46. class DirStackFileIncluder : public glslang::TShader::Includer {
  47. public:
  48. DirStackFileIncluder() : externalLocalDirectoryCount(0) { }
  49. virtual IncludeResult* includeLocal(const char* headerName,
  50. const char* includerName,
  51. size_t inclusionDepth) override
  52. {
  53. return readLocalPath(headerName, includerName, (int)inclusionDepth);
  54. }
  55. virtual IncludeResult* includeSystem(const char* headerName,
  56. const char* /*includerName*/,
  57. size_t /*inclusionDepth*/) override
  58. {
  59. return readSystemPath(headerName);
  60. }
  61. // Externally set directories. E.g., from a command-line -I<dir>.
  62. // - Most-recently pushed are checked first.
  63. // - All these are checked after the parse-time stack of local directories
  64. // is checked.
  65. // - This only applies to the "local" form of #include.
  66. // - Makes its own copy of the path.
  67. virtual void pushExternalLocalDirectory(const std::string& dir)
  68. {
  69. directoryStack.push_back(dir);
  70. externalLocalDirectoryCount = (int)directoryStack.size();
  71. }
  72. virtual void releaseInclude(IncludeResult* result) override
  73. {
  74. if (result != nullptr) {
  75. delete [] static_cast<tUserDataElement*>(result->userData);
  76. delete result;
  77. }
  78. }
  79. virtual std::set<std::string> getIncludedFiles()
  80. {
  81. return includedFiles;
  82. }
  83. virtual ~DirStackFileIncluder() override { }
  84. protected:
  85. typedef char tUserDataElement;
  86. std::vector<std::string> directoryStack;
  87. int externalLocalDirectoryCount;
  88. std::set<std::string> includedFiles;
  89. // Search for a valid "local" path based on combining the stack of include
  90. // directories and the nominal name of the header.
  91. virtual IncludeResult* readLocalPath(const char* headerName, const char* includerName, int depth)
  92. {
  93. // Discard popped include directories, and
  94. // initialize when at parse-time first level.
  95. directoryStack.resize(depth + externalLocalDirectoryCount);
  96. if (depth == 1)
  97. directoryStack.back() = getDirectory(includerName);
  98. // Find a directory that works, using a reverse search of the include stack.
  99. for (auto it = directoryStack.rbegin(); it != directoryStack.rend(); ++it) {
  100. std::string path = *it + '/' + headerName;
  101. std::replace(path.begin(), path.end(), '\\', '/');
  102. std::ifstream file(path, std::ios_base::binary | std::ios_base::ate);
  103. if (file) {
  104. directoryStack.push_back(getDirectory(path));
  105. includedFiles.insert(path);
  106. return newIncludeResult(path, file, (int)file.tellg());
  107. }
  108. }
  109. return nullptr;
  110. }
  111. // Search for a valid <system> path.
  112. // Not implemented yet; returning nullptr signals failure to find.
  113. virtual IncludeResult* readSystemPath(const char* /*headerName*/) const
  114. {
  115. return nullptr;
  116. }
  117. // Do actual reading of the file, filling in a new include result.
  118. virtual IncludeResult* newIncludeResult(const std::string& path, std::ifstream& file, int length) const
  119. {
  120. char* content = new tUserDataElement [length];
  121. file.seekg(0, file.beg);
  122. file.read(content, length);
  123. return new IncludeResult(path, content, length, content);
  124. }
  125. // If no path markers, return current working directory.
  126. // Otherwise, strip file name and return path leading up to it.
  127. virtual std::string getDirectory(const std::string path) const
  128. {
  129. size_t last = path.find_last_of("/\\");
  130. return last == std::string::npos ? "." : path.substr(0, last);
  131. }
  132. };