-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathassetPath.cpp
161 lines (141 loc) · 4.68 KB
/
assetPath.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
//
// Copyright 2016 Pixar
//
// Licensed under the terms set forth in the LICENSE.txt file available at
// https://openusd.org/license.
//
#include "pxr/pxr.h"
#include "pxr/usd/sdf/assetPath.h"
#include "pxr/base/tf/diagnostic.h"
#include "pxr/base/tf/registryManager.h"
#include "pxr/base/tf/staticTokens.h"
#include "pxr/base/tf/stringUtils.h"
#include "pxr/base/tf/type.h"
#include "pxr/base/vt/array.h"
#include "pxr/base/vt/value.h"
#include <ostream>
PXR_NAMESPACE_OPEN_SCOPE
// Register this class with the TfType registry
// Array registration included to facilitate Sdf/Types and Sdf/ParserHelpers
TF_REGISTRY_FUNCTION(TfType)
{
TfType::Define<SdfAssetPath>();
TfType::Define<VtArray<SdfAssetPath>>();
}
TF_REGISTRY_FUNCTION(VtValue)
{
VtValue::RegisterSimpleCast<std::string, SdfAssetPath>();
}
static const char Delimiter = '@';
// Read a UTF-8 char starting at 'cp' and return its value as an int. Also
// advance 'cp' to the start of the next UTF-8 character. If 'cp' does not
// point to a valid UTF-8 char, leave 'cp' unmodified and return -1.
static int
_ReadUTF8(unsigned char const *&cp, std::string *errMsg)
{
// Return a byte with the high `n` bits set, rest clear.
auto highBits = [](int n) {
return static_cast<unsigned char>(((1 << n) - 1) << (8 - n));
};
// Return true if `ch` is a continuation byte.
auto isContinuation = [&highBits](unsigned char ch) {
return (ch & highBits(2)) == highBits(1);
};
// Check for single-character code.
if ((*cp & highBits(1)) == 0) {
return *cp++;
}
// Check for 2, 3, or 4-byte code.
for (int i = 2; i <= 4; ++i) {
// This is an N-byte code if the high-order N+1 bits are N 1s
// followed by a single 0.
if ((*cp & highBits(i + 1)) == highBits(i)) {
int ret = *cp & ~highBits(i + 1);
// If that's the case then the following N-1 bytes must be
// "continuation bytes".
for (int j = 1; j != i; ++j) {
if (!isContinuation(cp[j])) {
char const *ordinalWords[] = {
"first", "second", "third", "fourth"
};
*errMsg = TfStringPrintf("%d-byte UTF-8 code point lacks "
"%s continuation byte",
i, ordinalWords[j-1]);
return -1;
}
ret = (ret << 6) | (cp[j] & ~highBits(2));
}
cp += i;
return ret;
}
}
*errMsg = TfStringPrintf("invalid UTF-8 code point byte 0x%hhx", *cp);
return -1;
}
// Check that the given \p path is valid UTF-8 sans C0 & C1 control characters.
// Return true if so. Return false and issue an error indicating the problem if
// not.
static bool
_ValidateAssetPathString(char const *path)
{
// Return true if 'code' is a C0 or C1 control code, false otherwise.
auto isControlCode = [](int code) {
return ((0x0 <= code && code <= 0x1f) ||
code == 0x7f ||
(0x80 <= code && code <= 0x9f));
};
unsigned char const *cp = reinterpret_cast<unsigned char const*>(path);
std::string err;
int utf8Char = _ReadUTF8(cp, &err);
int charNum = 1;
for (; utf8Char > 0; utf8Char = _ReadUTF8(cp, &err)) {
if (isControlCode(utf8Char)) {
TF_CODING_ERROR("Invalid asset path string -- character %d is "
"control character 0x%x",
charNum, utf8Char);
return false;
}
++charNum;
}
if (utf8Char == -1) {
TF_CODING_ERROR("Invalid asset path string -- character %d: %s\n",
charNum, err.c_str());
return false;
}
return true;
}
SdfAssetPath::SdfAssetPath()
{
}
SdfAssetPath::SdfAssetPath(const std::string &path)
: _assetPath(path)
{
if (!_ValidateAssetPathString(path.c_str())) {
*this = SdfAssetPath();
}
}
SdfAssetPath::SdfAssetPath(const std::string &path,
const std::string &resolvedPath)
: _assetPath(path)
, _resolvedPath(resolvedPath)
{
if (!_ValidateAssetPathString(path.c_str()) ||
!_ValidateAssetPathString(resolvedPath.c_str())) {
*this = SdfAssetPath();
}
}
bool
SdfAssetPath::operator<(const SdfAssetPath &rhs) const
{
if (_assetPath < rhs._assetPath)
return true;
if (rhs._assetPath < _assetPath)
return false;
return _resolvedPath < rhs._resolvedPath;
}
std::ostream&
operator<<(std::ostream& out, const SdfAssetPath& ap)
{
return out << Delimiter << ap.GetAssetPath() << Delimiter;
}
PXR_NAMESPACE_CLOSE_SCOPE