forked from niftools/nifskope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nifexpr.cpp
262 lines (239 loc) · 8.7 KB
/
nifexpr.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
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
/***** BEGIN LICENSE BLOCK *****
BSD License
Copyright (c) 2005-2012, NIF File Format Library and Tools
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the NIF File Format Library and Tools project may not be
used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
***** END LICENCE BLOCK *****/
#include "nifexpr.h"
#include "basemodel.h"
static bool matchGroup(const QString & cond, int offset, int& startpos, int& endpos)
{
int scandepth = 0;
startpos = -1;
endpos = -1;
for (int scanpos = offset, len=cond.length(); scanpos != len; ++scanpos) {
QChar c = cond[scanpos];
if (c == '(') {
if (startpos == -1)
startpos = scanpos;
++scandepth;
} else if ( c == ')' ) {
if (--scandepth == 0) {
endpos = scanpos;
return true;
}
}
}
if (startpos != -1 || endpos != -1)
throw "expression syntax error (non-matching brackets?)";
return false;
}
static quint32 version2number( const QString & s )
{
if ( s.isEmpty() ) return 0;
if ( s.contains( "." ) )
{
QStringList l = s.split( "." );
quint32 v = 0;
if ( l.count() > 4 ) {
//Should probaby post a warning here or something. Version # has more than 3 dots in it.
return 0;
} else if ( l.count() == 2 ) {
//This is an old style version number. Take each digit following the first one at a time.
//The first one is the major version
v += l[0].toInt() << (3 * 8);
if ( l[1].size() >= 1 ) {
v += l[1].mid(0, 1).toInt() << (2 * 8);
}
if ( l[1].size() >= 2 ) {
v += l[1].mid(1, 1).toInt() << (1 * 8);
}
if ( l[1].size() >= 3 ) {
v += l[1].mid(2, -1).toInt();
}
return v;
} else {
//This is a new style version number with dots separating the digits
for ( int i = 0; i < 4 && i < l.count(); i++ ) {
v += l[i].toInt( 0, 10 ) << ( (3-i) * 8 );
}
return v;
}
} else {
bool ok;
quint32 i = s.toUInt( &ok );
return ( i == 0xffffffff ? 0 : i );
}
}
Expression::Operator Expression::operatorFromString( const QString& str )
{
if ( str == "!" ) return Expression::e_not;
else if ( str == "!=" ) return Expression::e_not_eq;
else if ( str == "==" ) return Expression::e_eq;
else if ( str == ">=" ) return Expression::e_gte;
else if ( str == "<=" ) return Expression::e_lte;
else if ( str == ">" ) return Expression::e_gt;
else if ( str == "<" ) return Expression::e_lt;
else if ( str == "&" ) return Expression::e_bit_and;
else if ( str == "|" ) return Expression::e_bit_or;
else if ( str == "+" ) return Expression::e_add;
else if ( str == "-" ) return Expression::e_sub;
else if ( str == "&&" ) return Expression::e_bool_and;
else if ( str == "||" ) return Expression::e_bool_or;
return Expression::e_nop;
}
void Expression::partition( const QString & cond, int offset /*= 0*/ )
{
int pos;
if (cond.isEmpty())
{
this->opcode = Expression::e_nop;
return;
}
// Handle unary operators
QRegExp reUnary("^\\s*!(.*)");
pos = reUnary.indexIn(cond, offset, QRegExp::CaretAtOffset);
if (pos != -1) {
Expression e(reUnary.cap(1).trimmed());
this->opcode = Expression::e_not;
this->rhs = QVariant::fromValue( e );
return;
}
// Check for left group
int lstartpos=-1, lendpos=-1, ostartpos=-1, oendpos=-1, rstartpos=-1, rendpos=-1;
//QRegExp tokens("\b(!=|==|>=|<=|>|<|\\&|\+|-|\\&\\&|\\|\\||\(|\)|[a-zA-Z0-9][a-zA-Z0-9_ \\?]*[a-zA-Z0-9_\\?]?)\b");
QRegExp reOps("(!=|==|>=|<=|>|<|\\&|\\||\\+|-|\\&\\&|\\|\\|)");
QRegExp reLParen("^\\s*\\(.*");
pos = reLParen.indexIn(cond,offset, QRegExp::CaretAtOffset);
if (pos != -1) {
matchGroup(cond, pos, lstartpos, lendpos);
pos = reOps.indexIn(cond, lendpos+1, QRegExp::CaretAtOffset);
++lstartpos, --lendpos;
if (pos != -1) {
ostartpos = pos;
oendpos = ostartpos + reOps.cap(0).length();
} else {
partition(cond.mid(lstartpos, lendpos-lstartpos+1));
return;
}
} else {
pos = reOps.indexIn(cond, offset, QRegExp::CaretAtOffset);
if (pos != -1) {
lstartpos = offset;
lendpos = pos - 1;
ostartpos = pos;
oendpos = ostartpos + reOps.cap(0).length();
} else {
static QRegExp reInt("[-+]?[0-9]+");
static QRegExp reUInt("0[xX][0-9]+");
static QRegExp reFloat("^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$");
static QRegExp reVersion("[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+");
// termination
this->lhs.setValue(cond);
if (reUInt.exactMatch(cond)) {
this->lhs.convert(QVariant::UInt);
} else if (reInt.exactMatch(cond)) {
this->lhs.convert(QVariant::Int);
} else if (reVersion.exactMatch(cond)) {
this->lhs.setValue( version2number(cond) );
}
this->opcode = Expression::e_nop;
return;
}
}
rstartpos = oendpos+1;
rendpos = cond.size() - 1;
Expression lhsexp(cond.mid(lstartpos, lendpos-lstartpos+1).trimmed());
Expression rhsexp(cond.mid(rstartpos, rendpos-rstartpos+1).trimmed());
if (lhsexp.opcode == Expression::e_nop) {
this->lhs = lhsexp.lhs;
} else {
this->lhs = QVariant::fromValue( lhsexp );
}
this->opcode = operatorFromString(cond.mid(ostartpos, oendpos-ostartpos));
if (rhsexp.opcode == Expression::e_nop) {
this->rhs = rhsexp.lhs;
} else {
this->rhs = QVariant::fromValue( rhsexp );
}
}
QString Expression::toString() const
{
QString l = lhs.toString();
QString r = rhs.toString();
if (lhs.type() == QVariant::UserType && lhs.canConvert<Expression>())
l = lhs.value<Expression>().toString();
if (rhs.type() == QVariant::UserType && rhs.canConvert<Expression>())
r = rhs.value<Expression>().toString();
switch (opcode)
{
case Expression::e_not:
return QString("!%1").arg(r);
case Expression::e_not_eq:
return QString("(%1 != %2)").arg(l).arg(r);
case Expression::e_eq:
return QString("(%1 == %2)").arg(l).arg(r);
case Expression::e_gte:
return QString("(%1 >= %2)").arg(l).arg(r);
case Expression::e_lte:
return QString("(%1 <= %2)").arg(l).arg(r);
case Expression::e_gt:
return QString("(%1 > %2)").arg(l).arg(r);
case Expression::e_lt:
return QString("(%1 < %2)").arg(l).arg(r);
case Expression::e_bit_and:
return QString("(%1 & %2)").arg(l).arg(r);
case Expression::e_bit_or:
return QString("(%1 | %2)").arg(l).arg(r);
case Expression::e_add:
return QString("(%1 + %2)").arg(l).arg(r);
case Expression::e_sub:
return QString("(%1 - %2)").arg(l).arg(r);
case Expression::e_bool_and:
return QString("(%1 && %2)").arg(l).arg(r);
case Expression::e_bool_or:
return QString("(%1 || %2)").arg(l).arg(r);
case Expression::e_nop:
return QString("%1").arg(l);
}
return QString();
}
void Expression::NormalizeVariants( QVariant &l, QVariant &r ) const
{
if (l.isValid() && r.isValid()) {
if (l.type() != r.type()) {
if ( l.type() == QVariant::String && l.canConvert(r.type()) )
l.convert(r.type());
else if ( r.type() == QVariant::String && r.canConvert(l.type()) )
r.convert(l.type());
else
{
QVariant::Type t = l.type() > r.type() ? l.type() : r.type();
if (r.canConvert(t) && l.canConvert(t)) {
l.convert(t);
r.convert(t);
}
}
}
}
}