forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.cpp
More file actions
122 lines (98 loc) · 2.88 KB
/
Util.cpp
File metadata and controls
122 lines (98 loc) · 2.88 KB
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
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#include <MaterialXCore/Util.h>
#include <MaterialXCore/Element.h>
namespace MaterialX
{
const string EMPTY_STRING;
namespace {
const string LIBRARY_VERSION_STRING = std::to_string(MATERIALX_MAJOR_VERSION) + "." +
std::to_string(MATERIALX_MINOR_VERSION) + "." +
std::to_string(MATERIALX_BUILD_VERSION);
const std::tuple<int, int, int> LIBRARY_VERSION_TUPLE(MATERIALX_MAJOR_VERSION,
MATERIALX_MINOR_VERSION,
MATERIALX_BUILD_VERSION);
bool invalidNameChar(char c)
{
return !isalnum(c) && c != '_' && c != ':';
}
} // anonymous namespace
//
// Utility methods
//
string getVersionString()
{
return LIBRARY_VERSION_STRING;
}
std::tuple<int, int, int> getVersionIntegers()
{
return LIBRARY_VERSION_TUPLE;
}
string createValidName(string name, char replaceChar)
{
std::replace_if(name.begin(), name.end(), invalidNameChar, replaceChar);
return name;
}
bool isValidName(const string& name)
{
auto it = std::find_if(name.begin(), name.end(), invalidNameChar);
return it == name.end();
}
string incrementName(const string& name)
{
size_t split = name.length();
while (split > 0)
{
if (!isdigit(name[split - 1]))
break;
split--;
}
if (split < name.length())
{
string prefix = name.substr(0, split);
string suffix = name.substr(split, name.length());
return prefix + std::to_string(std::stoi(suffix) + 1);
}
return name + "2";
}
StringVec splitString(const string& str, const string& sep)
{
StringVec split;
string::size_type lastPos = str.find_first_not_of(sep, 0);
string::size_type pos = str.find_first_of(sep, lastPos);
while (pos != string::npos || lastPos != string::npos)
{
split.push_back(str.substr(lastPos, pos - lastPos));
lastPos = str.find_first_not_of(sep, pos);
pos = str.find_first_of(sep, lastPos);
}
return split;
}
string replaceSubstrings(string str, const StringMap& stringMap)
{
for (auto& pair : stringMap)
{
if (pair.first.empty())
continue;
size_t pos = 0;
while ((pos = str.find(pair.first, pos)) != string::npos)
{
str.replace(pos, pair.first.length(), pair.second);
pos += pair.second.length();
}
}
return str;
}
string prettyPrint(ConstElementPtr elem)
{
string text;
for (TreeIterator it = elem->traverseTree().begin(); it != TreeIterator::end(); ++it)
{
string indent(it.getElementDepth() * 2, ' ');
text += indent + it.getElement()->asString() + "\n";
}
return text;
}
} // namespace MaterialX