UFO: Alien Invasion
Toggle main menu visibility
Loading...
Searching...
No Matches
binaryexpressionparser.cpp
Go to the documentation of this file.
1
4
5
/*
6
Copyright (C) 2002-2025 UFO: Alien Invasion.
7
8
This program is free software; you can redistribute it and/or
9
modify it under the terms of the GNU General Public License
10
as published by the Free Software Foundation; either version 2
11
of the License, or (at your option) any later version.
12
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
17
See the GNU General Public License for more details.
18
19
You should have received a copy of the GNU General Public License
20
along with this program; if not, write to the Free Software
21
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23
*/
24
25
#include "
binaryexpressionparser.h
"
26
#include "
common.h
"
27
28
typedef
enum
29
{
30
BEPERR_NONE
,
BEPERR_BRACE
,
BEPERR_NOEND
,
BEPERR_NOTFOUND
31
}
binaryExpressionParserError_t
;
32
36
class
BinaryExpressionParser
{
37
private
:
38
binaryExpressionParserError_t
binaryExpressionParserError
;
39
BEPEvaluteCallback_t
varFunc
;
40
char
varName
[
MAX_VAR
];
41
bool
result
;
42
const
void
*
userdata
;
43
44
inline
void
SkipWhiteSpaces
(
const
char
** s)
const
45
{
46
while
(**s ==
' '
|| **s ==
'\t'
)
47
(*s)++;
48
}
49
53
inline
void
NextChar
(
const
char
** s)
const
54
{
55
(*s)++;
56
/* skip white-spaces too */
57
SkipWhiteSpaces
(s);
58
}
59
60
const
char
*
GetSwitchName
(
const
char
** s)
61
{
62
int
pos = 0;
63
64
/* skip non printable chars and special chars that are used to define the binary expression */
65
while
(**s >
' '
&& **s !=
'^'
&& **s !=
'|'
&& **s !=
'&'
&& **s !=
'!'
&& **s !=
'('
&& **s !=
')'
) {
66
varName
[pos++] = **s;
67
(*s)++;
68
}
69
varName
[pos] =
'\0'
;
70
71
return
varName
;
72
}
73
79
bool
CheckOR
(
const
char
** s)
80
{
81
bool
result
=
false
;
82
enum
{
83
BEP_NONE, BEP_OR, BEP_EXCLUSIVE_OR
84
};
85
int
goOn = BEP_NONE;
86
87
SkipWhiteSpaces
(s);
88
do
{
89
if
(goOn == BEP_EXCLUSIVE_OR)
90
result
^=
CheckAND
(s);
91
else
92
result
|=
CheckAND
(s);
93
94
if
(**s ==
'|'
) {
95
goOn = BEP_OR;
96
NextChar
(s);
97
}
else
if
(**s ==
'^'
) {
98
goOn = BEP_EXCLUSIVE_OR;
99
NextChar
(s);
100
}
else
{
101
goOn = BEP_NONE;
102
}
103
}
while
(goOn != BEP_NONE && !
binaryExpressionParserError
);
104
105
return
result
;
106
}
107
108
bool
CheckAND
(
const
char
** s)
109
{
110
bool
result
=
true
;
111
bool
negate =
false
;
112
bool
goOn =
false
;
113
114
do
{
115
/* parse all negate chars and swap the flag accordingly */
116
while
(**s ==
'!'
) {
117
negate ^=
true
;
118
NextChar
(s);
119
}
120
/* handling braces */
121
if
(**s ==
'('
) {
122
NextChar
(s);
123
/* and the result of the inner clause by calling the entry
124
* point again (and apply the negate flag) */
125
result
&=
CheckOR
(s) ^ negate;
126
if
(**s !=
')'
)
127
binaryExpressionParserError
=
BEPERR_BRACE
;
128
NextChar
(s);
129
}
else
{
130
/* get the variable state by calling the evaluate callback */
131
const
int
value =
varFunc
(
GetSwitchName
(s),
userdata
);
132
if
(value == -1)
133
binaryExpressionParserError
=
BEPERR_NOTFOUND
;
134
else
135
result
&= value ^ negate;
136
SkipWhiteSpaces
(s);
137
}
138
139
/* check whether there is another and clause */
140
if
(**s ==
'&'
) {
141
goOn =
true
;
142
NextChar
(s);
143
}
else
{
144
goOn =
false
;
145
}
146
negate =
false
;
147
}
while
(goOn && !
binaryExpressionParserError
);
148
149
return
result
;
150
}
151
152
public
:
153
BinaryExpressionParser
(
const
char
* expr,
BEPEvaluteCallback_t
varFuncParam,
const
void
* userdataPtr) :
154
binaryExpressionParserError
(
BEPERR_NONE
),
varFunc
(varFuncParam),
userdata
(userdataPtr)
155
{
156
varName
[0] = 0;
157
const
char
* str = expr;
158
result
=
CheckOR
(&str);
159
/* check for no end error */
160
if
(
Q_strvalid
(str) && !
binaryExpressionParserError
)
161
binaryExpressionParserError
=
BEPERR_NOEND
;
162
}
163
164
inline
bool
getResult
()
const
165
{
166
return
result
;
167
}
168
169
inline
binaryExpressionParserError_t
getError
()
const
170
{
171
return
binaryExpressionParserError
;
172
}
173
};
174
175
bool
BEP_Evaluate
(
const
char
* expr,
BEPEvaluteCallback_t
varFuncParam,
const
void
* userdata)
176
{
177
if
(!
Q_strvalid
(expr))
178
return
true
;
179
180
BinaryExpressionParser
bep(expr, varFuncParam, userdata);
181
const
bool
result = bep.
getResult
();
182
const
binaryExpressionParserError_t
error = bep.
getError
();
183
184
switch
(error) {
185
case
BEPERR_NONE
:
186
/* do nothing */
187
return
result;
188
case
BEPERR_BRACE
:
189
Com_Printf
(
"')' expected in binary expression (%s).\n"
, expr);
190
return
true
;
191
case
BEPERR_NOEND
:
192
Com_Printf
(
"Unexpected end of condition in binary expression (%s).\n"
, expr);
193
return
result;
194
case
BEPERR_NOTFOUND
:
195
Com_Printf
(
"Variable not found in binary expression (%s).\n"
, expr);
196
return
false
;
197
}
198
Com_Error
(
ERR_FATAL
,
"Unknown CheckBEP error in binary expression (%s)"
, expr);
199
}
binaryExpressionParserError_t
binaryExpressionParserError_t
Definition
binaryexpressionparser.cpp:29
BEPERR_NOEND
@ BEPERR_NOEND
Definition
binaryexpressionparser.cpp:30
BEPERR_NONE
@ BEPERR_NONE
Definition
binaryexpressionparser.cpp:30
BEPERR_BRACE
@ BEPERR_BRACE
Definition
binaryexpressionparser.cpp:30
BEPERR_NOTFOUND
@ BEPERR_NOTFOUND
Definition
binaryexpressionparser.cpp:30
BEP_Evaluate
bool BEP_Evaluate(const char *expr, BEPEvaluteCallback_t varFuncParam, const void *userdata)
Definition
binaryexpressionparser.cpp:175
binaryexpressionparser.h
BEPEvaluteCallback_t
int(* BEPEvaluteCallback_t)(const char *var, const void *userdata)
Definition
binaryexpressionparser.h:32
BinaryExpressionParser
Evaluates stuff like this expression.
Definition
binaryexpressionparser.cpp:36
BinaryExpressionParser::varName
char varName[MAX_VAR]
Definition
binaryexpressionparser.cpp:40
BinaryExpressionParser::GetSwitchName
const char * GetSwitchName(const char **s)
Definition
binaryexpressionparser.cpp:60
BinaryExpressionParser::NextChar
void NextChar(const char **s) const
Advance to the next char that is no whitespace.
Definition
binaryexpressionparser.cpp:53
BinaryExpressionParser::varFunc
BEPEvaluteCallback_t varFunc
Definition
binaryexpressionparser.cpp:39
BinaryExpressionParser::getResult
bool getResult() const
Definition
binaryexpressionparser.cpp:164
BinaryExpressionParser::getError
binaryExpressionParserError_t getError() const
Definition
binaryexpressionparser.cpp:169
BinaryExpressionParser::userdata
const void * userdata
Definition
binaryexpressionparser.cpp:42
BinaryExpressionParser::CheckAND
bool CheckAND(const char **s)
Definition
binaryexpressionparser.cpp:108
BinaryExpressionParser::binaryExpressionParserError
binaryExpressionParserError_t binaryExpressionParserError
Definition
binaryexpressionparser.cpp:38
BinaryExpressionParser::result
bool result
Definition
binaryexpressionparser.cpp:41
BinaryExpressionParser::BinaryExpressionParser
BinaryExpressionParser(const char *expr, BEPEvaluteCallback_t varFuncParam, const void *userdataPtr)
Definition
binaryexpressionparser.cpp:153
BinaryExpressionParser::CheckOR
bool CheckOR(const char **s)
Evaluates or and xor in the given string. This is the entry point, it delegates to the and checks tha...
Definition
binaryexpressionparser.cpp:79
BinaryExpressionParser::SkipWhiteSpaces
void SkipWhiteSpaces(const char **s) const
Definition
binaryexpressionparser.cpp:44
Com_Error
void Com_Error(int code, const char *fmt,...)
Definition
common.cpp:459
Com_Printf
void Com_Printf(const char *const fmt,...)
Definition
common.cpp:428
common.h
definitions common between client and server, but not game lib
ERR_FATAL
#define ERR_FATAL
Definition
common.h:210
Q_strvalid
#define Q_strvalid(string)
Definition
shared.h:141
MAX_VAR
#define MAX_VAR
Definition
shared.h:36
src
common
binaryexpressionparser.cpp
Generated on __DATE__ __TIME__ for UFO: Alien Invasion by
1.17.0