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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
#include "Alignment/Geners/interface/IOException.hh"
#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include "Alignment/Geners/interface/ClassId.hh"
#include "Alignment/Geners/interface/IOException.hh"
#include "Alignment/Geners/interface/binaryIO.hh"
#define NLOCAL 1024
namespace gs {
void ClassId::setVersion(const unsigned newVersion) {
if (version_ != newVersion) {
version_ = newVersion;
// Need to update the id string
const std::size_t lastOpen = id_.find_last_of('(');
assert(lastOpen != std::string::npos);
std::ostringstream os;
os << id_.substr(0, lastOpen) << '(' << version_ << ')';
if (isPtr_)
os << '*';
id_ = os.str();
}
}
void ClassId::ensureSameId(const ClassId &id) const {
if (name_.empty())
throw gs::IOInvalidArgument("In gs::ClassId::ensureSameId: reference id is not valid");
if (id.name_.empty())
throw gs::IOInvalidArgument("In gs::ClassId::ensureSameId: argument id is not valid");
if (id_ != id.id_) {
std::ostringstream os;
os << "In gs::ClassId::ensureSameId: expected \"" << id_ << "\", got \"" << id.id_ << "\"";
throw gs::IOInvalidArgument(os.str());
}
}
void ClassId::ensureSameName(const ClassId &id) const {
if (name_.empty())
throw gs::IOInvalidArgument("In gs::ClassId::ensureSameName: reference id is not valid");
if (id.name_.empty())
throw gs::IOInvalidArgument("In gs::ClassId::ensureSameName: argument id is not valid");
if (name_ != id.name_) {
std::ostringstream os;
os << "In gs::ClassId::ensureSameName: expected class name \"" << name_ << "\", got \"" << id.name_ << "\"";
throw gs::IOInvalidArgument(os.str());
}
}
void ClassId::ensureSameVersion(const ClassId &id) const {
if (name_.empty())
throw gs::IOInvalidArgument("In gs::ClassId::ensureSameVersion: reference id is not valid");
if (id.name_.empty())
throw gs::IOInvalidArgument("In gs::ClassId::ensureSameVersion: argument id is not valid");
if (version_ != id.version_) {
std::ostringstream os;
os << "In gs::ClassId::ensureSameVersion: expected version " << version_ << " for class " << id.name_ << ", got "
<< id.version_;
throw gs::IOInvalidArgument(os.str());
}
}
void ClassId::ensureVersionInRange(const unsigned vmin, const unsigned vmax) const {
if (name_.empty())
throw gs::IOInvalidArgument("In gs::ClassId::ensureVersionInRange: id is not valid");
if (version_ < vmin || version_ > vmax) {
std::ostringstream os;
os << "In gs::ClassId::ensureVersionInRange: expected version"
<< " number for class " << name_ << " to be in range [" << vmin << ", " << vmax << "], got " << version_;
throw gs::IOInvalidArgument(os.str());
}
}
bool ClassId::validatePrefix(const char *prefix) {
// Prefix can not be an empty string
if (prefix == nullptr)
return false;
const unsigned len = strlen(prefix);
if (len == 0)
return false;
// Characters '(' and ')' are special and can not be used
// as parts of class names unless they enclose a version
// number. Version number is an unsigned integer.
bool inVersion = false;
unsigned vstart = 0;
for (unsigned i = 0; i < len; ++i) {
if (prefix[i] == '(') {
// Can't have stacked parentheses.
// Can't have '(' as the very first character.
if (inVersion || i == 0)
return false;
inVersion = true;
vstart = i + 1;
} else if (prefix[i] == ')') {
// Can't have closing parentheses withoup opening ones
if (!inVersion)
return false;
inVersion = false;
if (vstart >= i)
return false;
char *endptr;
// Compiler can complain about unused result of "strtoul"
strtoul(prefix + vstart, &endptr, 10);
if (endptr != prefix + i)
return false;
}
}
// Can't have missing closing parentheses
if (inVersion)
return false;
return true;
}
void ClassId::initialize(const char *prefix, const unsigned version, const bool isPtr) {
std::ostringstream os;
if (!validatePrefix(prefix)) {
if (prefix)
os << "In gs::ClassId::initialize: bad class name prefix \"" << prefix
<< "\". Check for problematic parentheses.";
else
os << "In gs::ClassId::initialize: NULL class name prefix.";
throw gs::IOInvalidArgument(os.str());
}
os << prefix << '(' << version << ')';
if (isPtr)
os << '*';
id_ = os.str();
version_ = version;
isPtr_ = isPtr;
makeName();
}
// Remove all version numbers from the class id
bool ClassId::makeName() {
char localbuf[NLOCAL];
char *buf = localbuf;
const unsigned idLen = id_.size();
if (idLen + 1U > NLOCAL)
buf = new char[idLen + 1U];
const char *from = id_.data();
bool inVersion = false;
unsigned ito = 0;
for (unsigned ifrom = 0; ifrom < idLen; ++ifrom) {
if (from[ifrom] == '(') {
if (inVersion) {
if (buf != localbuf)
delete[] buf;
return false;
}
inVersion = true;
} else if (from[ifrom] == ')') {
if (!inVersion) {
if (buf != localbuf)
delete[] buf;
return false;
}
inVersion = false;
} else if (!inVersion)
buf[ito++] = from[ifrom];
}
if (inVersion) {
if (buf != localbuf)
delete[] buf;
return false;
}
buf[ito] = '\0';
name_ = buf;
if (buf != localbuf)
delete[] buf;
return true;
}
// Parse the version number and pointer switch
bool ClassId::makeVersion() {
bool correct = false;
const unsigned ns = id_.size();
const char *const buf = id_.data();
const char *sep = buf + (ns - 1U);
if (*sep == '*') {
isPtr_ = true;
--sep;
} else
isPtr_ = false;
if (*sep == ')') {
const char *closingBrace = sep;
for (; sep != buf; --sep)
if (*sep == '(')
break;
if (sep != buf) {
char *endptr;
version_ = strtoul(sep + 1, &endptr, 10);
if (endptr > sep + 1 && endptr == closingBrace)
correct = true;
}
}
return correct;
}
ClassId::ClassId(const std::string &id) : id_(id) {
if (!(!id_.empty() && makeName() && makeVersion())) {
std::ostringstream os;
os << "In gs::ClassId::ClassId(const std::string&): "
<< "invalid input id string \"" << id_ << "\"";
throw gs::IOInvalidArgument(os.str());
}
}
ClassId::ClassId(std::istream &in, int) {
read_pod(in, &id_);
if (in.fail())
throw IOReadFailure(
"In gs::ClassId::ClassId(std::istream&, int): "
"input stream failure");
if (!(!id_.empty() && makeName() && makeVersion())) {
std::ostringstream os;
os << "In gs::ClassId::ClassId(std::istream&, int): "
<< "read invalid id string \"" << id_ << "\"";
throw IOInvalidData(os.str());
}
}
bool ClassId::write(std::ostream &os) const {
write_pod(os, id_);
return !os.fail();
}
ClassId::ClassId() : name_(""), id_("(0)"), version_(0U), isPtr_(false) {}
ClassId ClassId::invalidId() {
ClassId dummy;
return dummy;
}
bool ClassId::isTemplate() const {
const std::size_t leftBrak = id_.find('<');
const std::size_t rightBrak = id_.rfind('>');
return leftBrak != std::string::npos && rightBrak != std::string::npos && leftBrak < rightBrak;
}
void ClassId::templateParameters(std::vector<std::vector<ClassId>> *params) const {
assert(params);
params->clear();
const std::size_t leftBrak = id_.find('<');
const std::size_t rightBrak = id_.rfind('>');
if (leftBrak != std::string::npos && rightBrak != std::string::npos && leftBrak < rightBrak) {
// Count commas and angle brackets
unsigned ncommas = 0;
int nbrackets = 0;
for (std::size_t pos = leftBrak + 1; pos < rightBrak; ++pos) {
const char c = id_[pos];
if (c == '<')
++nbrackets;
else if (c == '>')
--nbrackets;
else if (c == ',' && nbrackets == 0)
++ncommas;
}
// Must be a well-formed name
if (nbrackets) {
std::ostringstream os;
os << "In gs::ClassId::templateParameters: "
<< "unbalanced angle brackets in the "
<< "template id \"" << id_ << "\"";
throw gs::IOInvalidArgument(os.str());
}
// Reserve a proper size vector
params->resize(ncommas + 1);
for (unsigned i = 0; i <= ncommas; ++i)
(*params)[i].reserve(1);
// Cycle over commas again and fill the ids
ncommas = 0;
nbrackets = 0;
std::size_t begin = leftBrak + 1;
for (std::size_t pos = begin; pos < rightBrak; ++pos) {
const char c = id_[pos];
if (c == '<')
++nbrackets;
else if (c == '>')
--nbrackets;
else if (c == ',' && nbrackets == 0) {
while (isspace(id_[begin]))
++begin;
std::size_t end = pos - 1;
while (isspace(id_[end]))
--end;
++end;
(*params)[ncommas].push_back(ClassId(id_.substr(begin, end - begin)));
begin = pos + 1;
++ncommas;
}
}
while (isspace(id_[begin]))
++begin;
std::size_t end = rightBrak - 1;
while (isspace(id_[end]))
--end;
++end;
(*params)[ncommas].push_back(ClassId(id_.substr(begin, end - begin)));
}
}
} // namespace gs
|