forked from autodesk-forks/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.cpp
204 lines (188 loc) · 6.76 KB
/
Util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#include <MaterialXFormat/Util.h>
#include <fstream>
#include <iostream>
#include <sstream>
namespace MaterialX
{
string readFile(const FilePath& filePath)
{
std::ifstream file(filePath.asString(), std::ios::in);
if (file)
{
std::stringstream stream;
stream << file.rdbuf();
file.close();
if (stream)
{
return stream.str();
}
}
return EMPTY_STRING;
}
void getSubdirectories(const FilePathVec& rootDirectories, const FileSearchPath& searchPath, FilePathVec& subDirectories)
{
for (const FilePath& root : rootDirectories)
{
FilePath rootPath = searchPath.find(root);
if (rootPath.exists())
{
FilePathVec childDirectories = rootPath.getSubDirectories();
subDirectories.insert(std::end(subDirectories), std::begin(childDirectories), std::end(childDirectories));
}
}
}
void loadDocuments(const FilePath& rootPath, const FileSearchPath& searchPath, const StringSet& skipFiles,
const StringSet& includeFiles, vector<DocumentPtr>& documents, StringVec& documentsPaths,
const XmlReadOptions* readOptions, StringVec* errors)
{
for (const FilePath& dir : rootPath.getSubDirectories())
{
for (const FilePath& file : dir.getFilesInDirectory(MTLX_EXTENSION))
{
if (!skipFiles.count(file) &&
(includeFiles.empty() || includeFiles.count(file)))
{
DocumentPtr doc = createDocument();
const FilePath filePath = dir / file;
try
{
FileSearchPath readSearchPath(searchPath);
readSearchPath.append(dir);
readFromXmlFile(doc, filePath, readSearchPath, readOptions);
documents.push_back(doc);
documentsPaths.push_back(filePath.asString());
}
catch (Exception& e)
{
if (errors)
{
errors->push_back("Failed to load: " + filePath.asString() + ". Error: " + e.what());
}
}
}
}
}
}
void loadLibrary(const FilePath& file, DocumentPtr doc, const FileSearchPath& searchPath, const XmlReadOptions* readOptions)
{
DocumentPtr libDoc = createDocument();
readFromXmlFile(libDoc, file, searchPath, readOptions);
doc->importLibrary(libDoc);
}
StringSet loadLibraries(const FilePathVec& libraryFolders,
const FileSearchPath& searchPath,
DocumentPtr doc,
const StringSet& excludeFiles,
const XmlReadOptions* readOptions)
{
// Append environment path to the specified search path.
FileSearchPath librarySearchPath = searchPath;
librarySearchPath.append(getEnvironmentPath());
StringSet loadedLibraries;
if (libraryFolders.empty())
{
// No libraries specified so scan in all search paths
for (const FilePath& libraryPath : librarySearchPath)
{
for (const FilePath& path : libraryPath.getSubDirectories())
{
for (const FilePath& filename : path.getFilesInDirectory(MTLX_EXTENSION))
{
if (!excludeFiles.count(filename))
{
const FilePath& file = path / filename;
if (loadedLibraries.count(file) == 0)
{
loadLibrary(file, doc, searchPath, readOptions);
loadedLibraries.insert(file.asString());
}
}
}
}
}
}
else
{
// Look for specific library folders in the search paths
for (const FilePath& libraryName : libraryFolders)
{
FilePath libraryPath = librarySearchPath.find(libraryName);
for (const FilePath& path : libraryPath.getSubDirectories())
{
for (const FilePath& filename : path.getFilesInDirectory(MTLX_EXTENSION))
{
if (!excludeFiles.count(filename))
{
const FilePath& file = path / filename;
if (loadedLibraries.count(file) == 0)
{
loadLibrary(file, doc, searchPath, readOptions);
loadedLibraries.insert(file.asString());
}
}
}
}
}
}
return loadedLibraries;
}
void flattenFilenames(DocumentPtr doc, const FileSearchPath& searchPath, StringResolverPtr customResolver)
{
for (ElementPtr elem : doc->traverseTree())
{
ValueElementPtr valueElem = elem->asA<ValueElement>();
if (!valueElem || valueElem->getType() != FILENAME_TYPE_STRING)
{
continue;
}
FilePath unresolvedValue(valueElem->getValueString());
if (unresolvedValue.isEmpty())
{
continue;
}
StringResolverPtr elementResolver = elem->createStringResolver();
// If the path is already absolute then don't allow an additional prefix
// as this would make the path invalid.
if (unresolvedValue.isAbsolute())
{
elementResolver->setFilePrefix(EMPTY_STRING);
}
string resolvedString = valueElem->getResolvedValueString(elementResolver);
// Convert relative to absolute pathing if the file is not already found
if (!searchPath.isEmpty())
{
FilePath resolvedValue(resolvedString);
if (!resolvedValue.isAbsolute())
{
for (size_t i = 0; i < searchPath.size(); i++)
{
FilePath testPath = searchPath[i] / resolvedValue;
if (testPath.exists())
{
resolvedString = testPath.asString();
break;
}
}
}
}
// Apply any custom filename resolver
if (customResolver && customResolver->isResolvedType(FILENAME_TYPE_STRING))
{
resolvedString = customResolver->resolve(resolvedString, FILENAME_TYPE_STRING);
}
valueElem->setValueString(resolvedString);
}
// Remove any file prefix attributes
for (ElementPtr elem : doc->traverseTree())
{
if (elem->hasFilePrefix())
{
elem->removeAttribute(Element::FILE_PREFIX_ATTRIBUTE);
}
}
}
} // namespace MaterialX