HLSL: Add an Includer to handle #include.
[platform/upstream/glslang.git] / StandAlone / DirStackFileIncluder.h
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
37 #pragma once
38
39 #include <vector>
40 #include <string>
41 #include <fstream>
42 #include <algorithm>
43
44 #include "./../glslang/Public/ShaderLang.h"
45
46 // Default include class for normal include convention of search backward
47 // through the stack of active include paths (for nested includes).
48 // Can be overridden to customize.
49 class DirStackFileIncluder : public glslang::TShader::Includer {
50 public:
51     virtual IncludeResult* includeLocal(const char* headerName,
52                                         const char* includerName,
53                                         size_t inclusionDepth) override
54     {
55         return readLocalPath(headerName, includerName, inclusionDepth);
56     }
57
58     virtual IncludeResult* includeSystem(const char* headerName,
59                                          const char* /*includerName*/,
60                                          size_t /*inclusionDepth*/) override
61     {
62         return readSystemPath(headerName);
63     }
64
65     virtual void releaseInclude(IncludeResult* result) override
66     {
67         if (result != nullptr) {
68             delete [] static_cast<tUserDataElement*>(result->userData);
69             delete result;
70         }
71     }
72
73     virtual ~DirStackFileIncluder() override { }
74
75 protected:
76     typedef char tUserDataElement;
77     std::vector<std::string> directoryStack;
78
79     // Search for a valid "local" path based on combining the stack of include
80     // directories and the nominal name of the header.
81     virtual IncludeResult* readLocalPath(const char* headerName, const char* includerName, int depth)
82     {
83         // Discard popped include directories, and if first level, initialize.
84         directoryStack.resize(depth);
85         if (depth == 1)
86             directoryStack.front() = getDirectory(includerName);
87
88         // find a directory that works, reverse search of include stack
89         for (auto it = directoryStack.rbegin(); it != directoryStack.rend(); ++it) {
90             std::string path = *it + '/' + headerName;
91             std::replace(path.begin(), path.end(), '\\', '/');
92             std::ifstream file(path, std::ios_base::binary | std::ios_base::ate);
93             if (file) {
94                 directoryStack.push_back(getDirectory(path));
95                 return newIncludeResult(path, file, (int)file.tellg());
96             }
97         }
98
99         return nullptr;
100     }
101
102     // Search for a valid <system> path.
103     // Not implemented yet; returning nullptr signals failure to find.
104     virtual IncludeResult* readSystemPath(const char* /*headerName*/) const
105     {
106         return nullptr;
107     }
108
109     // Do actual reading of the file, filling in a new include result.
110     virtual IncludeResult* newIncludeResult(const std::string& path, std::ifstream& file, int length) const
111     {
112         char* content = new tUserDataElement [length];
113         file.seekg(0, file.beg);
114         file.read(content, length);
115         return new IncludeResult(path, content, length, content);
116     }
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 };