diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2016-02-12 20:33:43 +0300 |
---|---|---|
committer | Sergey Sharybin <sergey.vfx@gmail.com> | 2016-03-25 15:55:42 +0300 |
commit | 700722f68633d270584af5463a804742537e30ed (patch) | |
tree | 5b80a5a51dd278e649647f8d0a64c07d570e8eaa /intern/cycles/util | |
parent | 0e47e0cc9e9b19a30717042d97cb3b8fb50132ff (diff) |
Cycles: Cleanup, indent nested preprocessor directives
Quite straightforward, main trick is happening in path_source_replace_includes().
Reviewers: brecht, dingto, lukasstockner97, juicyfruit
Differential Revision: https://developer.blender.org/D1794
Diffstat (limited to 'intern/cycles/util')
-rw-r--r-- | intern/cycles/util/util_path.cpp | 55 |
1 files changed, 35 insertions, 20 deletions
diff --git a/intern/cycles/util/util_path.cpp b/intern/cycles/util/util_path.cpp index 468d5c20ee1..196e2c49dcb 100644 --- a/intern/cycles/util/util_path.cpp +++ b/intern/cycles/util/util_path.cpp @@ -720,31 +720,46 @@ bool path_remove(const string& path) return remove(path.c_str()) == 0; } -string path_source_replace_includes(const string& source_, const string& path) +string path_source_replace_includes(const string& source, const string& path) { - /* our own little c preprocessor that replaces #includes with the file + /* Our own little c preprocessor that replaces #includes with the file * contents, to work around issue of opencl drivers not supporting - * include paths with spaces in them */ - string source = source_; - const string include = "#include \""; - size_t n, pos = 0; - - while((n = source.find(include, pos)) != string::npos) { - size_t n_start = n + include.size(); - size_t n_end = source.find("\"", n_start); - string filename = source.substr(n_start, n_end - n_start); - - string text, filepath = path_join(path, filename); - - if(path_read_text(filepath, text)) { - text = path_source_replace_includes(text, path_dirname(filepath)); - source.replace(n, n_end + 1 - n, "\n" + text + "\n"); + * include paths with spaces in them. + */ + + string result = ""; + vector<string> lines; + string_split(lines, source, "\n"); + + for(size_t i = 0; i < lines.size(); ++i) { + string line = lines[i]; + if(line[0] == '#') { + string token = string_strip(line.substr(1, line.size() - 1)); + if(string_startswith(token, "include")) { + token = string_strip(token.substr(7, token.size() - 7)); + if(token[0] == '"') { + size_t n_start = 1; + size_t n_end = token.find("\"", n_start); + string filename = token.substr(n_start, n_end - n_start); + string text, filepath = path_join(path, filename); + if(path_read_text(filepath, text)) { + /* Replace include directories with both current path + * and path extracted from the include file. + * Not totally robust, but works fine for Cycles kernel + * and avoids having list of include directories.x + */ + text = path_source_replace_includes( + text, path_dirname(filepath)); + text = path_source_replace_includes(text, path); + line = token.replace(0, n_end + 1, "\n" + text + "\n"); + } + } + } } - else - pos = n_end; + result += line + "\n"; } - return source; + return result; } FILE *path_fopen(const string& path, const string& mode) |