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