-
-
Notifications
You must be signed in to change notification settings - Fork 623
/
Copy pathalgebra.cc
288 lines (266 loc) · 6.66 KB
/
algebra.cc
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
/*
*
* Conky, a system monitor, based on torsmo
*
* Any original torsmo code is licensed under the BSD license
*
* All code written since the fork of torsmo is licensed under the GPL
*
* Please see COPYING for details
*
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
* Copyright (c) 2005-2018 Brenden Matthews, Philip Kovacs, et. al.
* (see AUTHORS)
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "algebra.h"
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <memory>
#include "config.h"
#include "conky.h"
#include "logging.h"
/* find the operand in the given expression
* returns the index of the first op character or -1 on error
*/
int find_match_op(const char *expr) {
unsigned int idx;
for (idx = 0; idx < strlen(expr); idx++) {
switch (expr[idx]) {
case '=':
case '!':
if (expr[idx + 1] != '=') {
return -1;
}
/* fall through */
case '<':
case '>':
return idx;
break;
}
}
return -1;
}
int get_match_type(const char *expr) {
int idx;
const char *str;
if ((idx = find_match_op(expr)) == -1) {
return -1;
}
str = expr + idx;
if (*str == '=' && *(str + 1) == '=') {
return OP_EQ;
}
if (*str == '!' && *(str + 1) == '=') {
return OP_NEQ;
}
if (*str == '>') {
if (*(str + 1) == '=') {
return OP_GEQ;
}
return OP_GT;
}
if (*str == '<') {
if (*(str + 1) == '=') {
return OP_LEQ;
}
return OP_LT;
}
return -1;
}
/* generic compare function
*
* v is actually the difference of the compared values. For strings
* this is equal to the output of str(n)cmp(). Use a macro here, as
* it's type-independent.
*/
#define COMPARE(v, t) \
switch (t) { \
case OP_GT: \
return ((v) > 0); \
case OP_LT: \
return ((v) < 0); \
case OP_EQ: \
return ((v) == 0); \
case OP_GEQ: \
return ((v) >= 0); \
case OP_LEQ: \
return ((v) <= 0); \
case OP_NEQ: \
return ((v) != 0); \
} \
return 0
int lcompare(long a, enum match_type mtype, long b) {
DBGP2("comparing longs '%ld' and '%ld'", a, b);
COMPARE((a - b), mtype);
}
int dcompare(double a, enum match_type mtype, double b) {
DBGP2("comparing doubles '%.lf' and '%.lf'", a, b);
COMPARE((a - b), mtype);
}
int scompare(const char *a, enum match_type mtype, const char *b) {
DBGP2("comparing strings '%s' and '%s'", a, b);
COMPARE(strcmp(a, b), mtype);
}
enum arg_type get_arg_type(const char *arg) {
const char *p, *e;
p = arg;
e = arg + strlen(arg) - 1;
while (p != e && (*e != 0) && *e == ' ') {
e--;
}
while (p != e && *p == ' ') {
p++;
}
if (*p == '"' && *e == '"') {
return ARG_STRING;
}
if (*p == '-') { // allow negative values
p++;
}
while (p <= e) {
if (isdigit((unsigned char)*p) == 0) {
break;
}
p++;
}
if (p == e + 1) {
return ARG_LONG;
}
if (*p == '.') {
p++;
while (p <= e) {
if (isdigit((unsigned char)*p) == 0) {
return ARG_BAD;
}
p++;
}
return ARG_DOUBLE;
}
return ARG_BAD;
}
char *arg_to_string(const char *arg) {
const char *start;
int len;
start = arg;
len = 0;
while ((*start != 0) && *start == ' ') {
start++;
}
if (!(*(start++) == '"')) {
return nullptr;
}
while (start[len] != '"') {
len++;
}
return strndup(start, len);
}
double arg_to_double(const char *arg) {
double d;
if (sscanf(arg, "%lf", &d) != 1) {
NORM_ERR("converting '%s' to double failed", arg);
return 0.0;
}
return d;
}
long arg_to_long(const char *arg) {
long l;
if (sscanf(arg, "%ld", &l) != 1) {
NORM_ERR("converting '%s' to long failed", arg);
return 0;
}
return l;
}
int compare(const char *expr) {
char *expr_dup;
int idx, mtype;
enum arg_type type1, type2;
long lng_a, lng_b;
double dbl_a, dbl_b;
idx = find_match_op(expr);
mtype = get_match_type(expr);
if ((idx == 0) || mtype == -1) {
NORM_ERR("failed to parse compare string '%s'", expr);
return -2;
}
expr_dup = strdup(expr);
expr_dup[idx] = '\0';
if (expr_dup[idx + 1] == '=') {
expr_dup[++idx] = '\0';
}
type1 = get_arg_type(expr_dup);
type2 = get_arg_type(expr_dup + idx + 1);
if (type1 == ARG_BAD || type2 == ARG_BAD) {
NORM_ERR("Bad arguments: '%s' and '%s'", expr_dup, (expr_dup + idx + 1));
free(expr_dup);
return -2;
}
if (type1 == ARG_LONG && type2 == ARG_DOUBLE) {
type1 = ARG_DOUBLE;
}
if (type1 == ARG_DOUBLE && type2 == ARG_LONG) {
type2 = ARG_DOUBLE;
}
if (type1 != type2) {
NORM_ERR("trying to compare args '%s' and '%s' of different type", expr_dup,
(expr_dup + idx + 1));
free(expr_dup);
return -2;
}
switch (type1) {
case ARG_STRING: {
char *a, *b;
a = arg_to_string(expr_dup);
b = arg_to_string(expr_dup + idx + 1);
idx = scompare(a, static_cast<enum match_type>(mtype), b);
free(a);
free(b);
free(expr_dup);
return idx;
}
case ARG_LONG:
lng_a = arg_to_long(expr_dup);
lng_b = arg_to_long(expr_dup + idx + 1);
free(expr_dup);
return lcompare(lng_a, static_cast<enum match_type>(mtype), lng_b);
case ARG_DOUBLE:
dbl_a = arg_to_double(expr_dup);
dbl_b = arg_to_double(expr_dup + idx + 1);
free(expr_dup);
return dcompare(dbl_a, static_cast<enum match_type>(mtype), dbl_b);
case ARG_BAD: /* make_gcc_happy() */;
}
/* not reached */
free(expr_dup);
return -2;
}
int check_if_match(struct text_object *obj) {
std::unique_ptr<char[]> expression(new char[max_user_text.get(*state)]);
int val;
int result = 1;
generate_text_internal(expression.get(), max_user_text.get(*state),
*obj->sub);
DBGP("parsed arg into '%s'", expression.get());
val = compare(expression.get());
if (val == -2) {
NORM_ERR("compare failed for expression '%s'", expression.get());
} else if (val == 0) {
result = 0;
}
return result;
}