DirStackFileIncluder.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "./../glslang/Public/ShaderLang.h"
  42. // Default include class for normal include convention of search backward
  43. // through the stack of active include paths (for nested includes).
  44. // Can be overridden to customize.
  45. class DirStackFileIncluder : public glslang::TShader::Includer {
  46. public:
  47. DirStackFileIncluder() : externalLocalDirectoryCount(0) { }
  48. virtual IncludeResult* includeLocal(const char* headerName,
  49. const char* includerName,
  50. size_t inclusionDepth) override
  51. {
  52. return readLocalPath(headerName, includerName, (int)inclusionDepth);
  53. }
  54. virtual IncludeResult* includeSystem(const char* headerName,
  55. const char* /*includerName*/,
  56. size_t /*inclusionDepth*/) override
  57. {
  58. return readSystemPath(headerName);
  59. }
  60. // Externally set directories. E.g., from a command-line -I<dir>.
  61. // - Most-recently pushed are checked first.
  62. // - All these are checked after the parse-time stack of local directories
  63. // is checked.
  64. // - This only applies to the "local" form of #include.
  65. // - Makes its own copy of the path.
  66. virtual void pushExternalLocalDirectory(const std::string& dir)
  67. {
  68. directoryStack.push_back(dir);
  69. externalLocalDirectoryCount = (int)directoryStack.size();
  70. }
  71. virtual void releaseInclude(IncludeResult* result) override
  72. {
  73. if (result != nullptr) {
  74. delete [] static_cast<tUserDataElement*>(result->userData);
  75. delete result;
  76. }
  77. }
  78. virtual ~DirStackFileIncluder() override { }
  79. protected:
  80. typedef char tUserDataElement;
  81. std::vector<std::string> directoryStack;
  82. int externalLocalDirectoryCount;
  83. // Search for a valid "local" path based on combining the stack of include
  84. // directories and the nominal name of the header.
  85. virtual IncludeResult* readLocalPath(const char* headerName, const char* includerName, int depth)
  86. {
  87. // Discard popped include directories, and
  88. // initialize when at parse-time first level.
  89. directoryStack.resize(depth + externalLocalDirectoryCount);
  90. if (depth == 1)
  91. directoryStack.back() = getDirectory(includerName);
  92. // Find a directory that works, using a reverse search of the include stack.
  93. for (auto it = directoryStack.rbegin(); it != directoryStack.rend(); ++it) {
  94. std::string path = *it + '/' + headerName;
  95. std::replace(path.begin(), path.end(), '\\', '/');
  96. std::ifstream file(path, std::ios_base::binary | std::ios_base::ate);
  97. if (file) {
  98. directoryStack.push_back(getDirectory(path));
  99. return newIncludeResult(path, file, (int)file.tellg());
  100. }
  101. }
  102. return nullptr;
  103. }
  104. // Search for a valid <system> path.
  105. // Not implemented yet; returning nullptr signals failure to find.
  106. virtual IncludeResult* readSystemPath(const char* /*headerName*/) const
  107. {
  108. return nullptr;
  109. }
  110. // Do actual reading of the file, filling in a new include result.
  111. virtual IncludeResult* newIncludeResult(const std::string& path, std::ifstream& file, int length) const
  112. {
  113. char* content = new tUserDataElement [length];
  114. file.seekg(0, file.beg);
  115. file.read(content, length);
  116. return new IncludeResult(path, content, length, content);
  117. }
  118. // If no path markers, return current working directory.
  119. // Otherwise, strip file name and return path leading up to it.
  120. virtual std::string getDirectory(const std::string path) const
  121. {
  122. size_t last = path.find_last_of("/\\");
  123. return last == std::string::npos ? "." : path.substr(0, last);
  124. }
  125. };