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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
/*
* GraphLCD skin library
*
* function.c - skin functions class
*
* This file is released under the GNU General Public License. Refer
* to the COPYING file distributed with this package.
*
* based on text2skin
*
*/
#include <syslog.h>
#include <string.h>
#include "skin.h"
#include "function.h"
namespace GLCD
{
static const char * Internals[] =
{
"not", "and", "or", "equal", "eq", "gt", "lt", "ge", "le", "ne", "file", "trans",
"add", "sub", "mul", "div",
"FontTotalWidth",
"FontTotalHeight",
"FontTotalAscent",
"FontSpaceBetween",
"FontLineHeight",
"FontTextWidth",
"FontTextHeight",
"ImageWidth",
"ImageHeight",
"QueryFeature",
NULL
};
cSkinFunction::cSkinFunction(cSkinObject *Parent)
: mObject(Parent),
mSkin(Parent->Skin()),
mType(string),
mString(mObject, false),
mNumber(0),
mNumParams(0)
{
}
cSkinFunction::cSkinFunction(const cSkinString & String)
: mObject(String.Object()),
mSkin(String.Skin()),
mType(string),
mString(String),
mNumber(0),
mNumParams(0)
{
}
cSkinFunction::cSkinFunction(const cSkinFunction & Src)
: mObject(Src.mObject),
mSkin(Src.mSkin),
mType(Src.mType),
mString(Src.mString),
mNumber(Src.mNumber),
mNumParams(Src.mNumParams)
{
for (uint32_t i = 0; i < mNumParams; ++i)
mParams[i] = new cSkinFunction(*Src.mParams[i]);
}
cSkinFunction::~cSkinFunction()
{
for (uint32_t i = 0; i < mNumParams; ++i)
delete mParams[i];
}
bool cSkinFunction::Parse(const std::string & Text)
{
const char *text = Text.c_str();
const char *ptr = text, *last = text;
eType type = undefined_function;
int inExpr = 0;
cSkinFunction *expr;
if (*ptr == '\'' || *ptr == '{')
{
// must be string
if (strlen(ptr) < 2
|| (*ptr == '\'' && *(ptr + strlen(ptr) - 1) != '\'')
|| (*ptr == '{' && *(ptr + strlen(ptr) - 1) != '}'))
{
syslog(LOG_ERR, "ERROR: Unmatched string end\n");
return false;
}
std::string temp;
if (*ptr == '\'')
temp.assign(ptr + 1, strlen(ptr) - 2);
else
temp.assign(ptr);
int pos = -1;
while ((pos = temp.find("\\'", pos + 1)) != -1)
temp.replace(pos, 2, "'");
if (!mString.Parse(temp))
return false;
mType = string;
}
if (*ptr == '#')
{
// must be a variable id
if (strlen(ptr) < 2)
{
syslog(LOG_ERR, "ERROR: No variable id given\n");
return false;
}
mVariableId.assign(ptr + 1);
mType = variable;
}
else if ((*ptr >= '0' && *ptr <= '9') || *ptr == '-' || *ptr == '+')
{
// must be number
char *end;
int num = strtol(ptr, &end, 10);
if (end == ptr || *end != '\0')
{
// don't log this because when parsing a string starting with a digit (eg: 0%) this may result in a load of false positives
//syslog(LOG_ERR, "ERROR: Invalid numeric value (%s)\n", Text.c_str());
return false;
}
mNumber = num;
mType = number;
}
else
{
bool inToken = false;
// expression
for (; *ptr; ++ptr)
{
if (*ptr == '{')
inToken = true;
else if (inToken && *ptr == '}')
inToken = false;
if (*ptr == '(')
{
if (inExpr++ == 0)
{
int i;
for (i = 0; Internals[i] != NULL; ++i)
{
if ((size_t)(ptr - last) == strlen(Internals[i])
&& memcmp(last, Internals[i], ptr - last) == 0)
{
type = (eType)(INTERNAL + i);
break;
}
}
if (Internals[i] == NULL)
{
syslog(LOG_ERR, "ERROR: Unknown function %.*s", (int)(ptr - last), last);
return false;
}
last = ptr + 1;
}
}
else if ( ( (!inToken) && (*ptr == ',') ) || *ptr == ')')
{
if (inExpr == 0)
{
syslog(LOG_ERR, "ERROR: Unmatched '%c' in expression (%s)", *ptr, Text.c_str());
return false;
}
if (inExpr == 1)
{
expr = new cSkinFunction(mObject);
if (!expr->Parse(std::string(last, ptr - last)))
{
delete expr;
return false;
}
if (mNumParams == MAXPARAMETERS)
{
syslog(LOG_ERR, "ERROR: Too many parameters to function, maximum is %d",
MAXPARAMETERS);
return false;
}
mType = type;
mParams[mNumParams++] = expr;
last = ptr + 1;
}
if (*ptr == ')')
{
if (inExpr == 1)
{
int params = 0;
switch (mType)
{
case fun_and:
case fun_or:
params = -1;
break;
case fun_equal:
case fun_eq:
case fun_ne:
case fun_gt:
case fun_lt:
case fun_ge:
case fun_le:
params = 2;
break;
case fun_not:
case fun_trans:
case fun_file:
params = 1;
break;
case funAdd:
case funSub:
case funMul:
params = -1;
break;
case funDiv:
params = 2;
break;
case funFontTotalWidth:
case funFontTotalHeight:
case funFontTotalAscent:
case funFontSpaceBetween:
case funFontLineHeight:
params = 1;
break;
case funFontTextWidth:
case funFontTextHeight:
params = 2;
break;
case funImageWidth:
case funImageHeight:
params = 1;
break;
case funQueryFeature:
params = 1;
break;
default:
break;
}
if (params != -1 && mNumParams != (uint32_t) params)
{
syslog(LOG_ERR, "ERROR: Text2Skin: Wrong number of parameters to %s, "
"expecting %d", Internals[mType - INTERNAL], params);
return false;
}
}
--inExpr;
}
}
}
if (inExpr > 0)
{
syslog(LOG_ERR, "ERROR: Expecting ')' in expression");
return false;
}
}
return true;
}
cType cSkinFunction::FunFile(const cType & Param) const
{
cImageCache * cache = mSkin->ImageCache();
GLCD::cImage * image = cache->Get(Param);
return image ? (cType) Param : (cType) false;
}
cType cSkinFunction::FunFont(eType Function, const cType &Param1, const cType &Param2) const
{
cSkinFont * skinFont = mSkin->GetFont(Param1);
if (!skinFont)
return false;
const cFont * font = skinFont->Font();
if (!font)
return false;
switch (Function)
{
case funFontTotalWidth:
return font->TotalWidth();
case funFontTotalHeight:
return font->TotalHeight();
case funFontTotalAscent:
return font->TotalAscent();
case funFontSpaceBetween:
return font->SpaceBetween();
case funFontLineHeight:
return font->LineHeight();
case funFontTextWidth:
return font->Width((const std::string) Param2);
case funFontTextHeight:
return font->Height((const std::string) Param2);
default:
break;
}
return false;
}
cType cSkinFunction::FunImage(eType Function, const cType &Param) const
{
cImageCache * cache = mSkin->ImageCache();
GLCD::cImage * image = cache->Get(Param);
if (!image)
return false;
switch (Function)
{
case funImageWidth:
return (int) image->Width();
case funImageHeight:
return (int) image->Height();
default:
break;
}
return false;
}
cType cSkinFunction::Evaluate(void) const
{
switch (mType)
{
case string:
return mString.Evaluate();
case number:
return mNumber;
case variable:
{
cSkinVariable * variable = mSkin->GetVariable(mVariableId);
if (variable) {
cType rv = variable->Value();
if (rv.IsString()) {
std::string val = rv;
if (val.find("{") != std::string::npos || val.find("#") != std::string::npos) {
cSkinString *result = new cSkinString(mObject, false);
if (result->Parse(val)) {
val = (std::string) result->Evaluate();
rv = cType(val);
}
delete result;
}
}
return rv;
}
return false;
}
case fun_not:
return !mParams[0]->Evaluate();
case fun_and:
for (uint32_t i = 0; i < mNumParams; ++i)
{
if (!mParams[i]->Evaluate())
return false;
}
return true;
case fun_or:
for (uint32_t i = 0; i < mNumParams; ++i)
{
if (mParams[i]->Evaluate())
return true;
}
return false;
case fun_equal:
case fun_eq:
return mParams[0]->Evaluate() == mParams[1]->Evaluate();
case fun_ne:
return mParams[0]->Evaluate() != mParams[1]->Evaluate();
case fun_gt:
return mParams[0]->Evaluate() > mParams[1]->Evaluate();
case fun_lt:
return mParams[0]->Evaluate() < mParams[1]->Evaluate();
case fun_ge:
return mParams[0]->Evaluate() >= mParams[1]->Evaluate();
case fun_le:
return mParams[0]->Evaluate() <= mParams[1]->Evaluate();
case fun_file:
return FunFile(mParams[0]->Evaluate());
case fun_trans:
return mSkin->Config().Translate(mParams[0]->Evaluate());
case funAdd:
{
int result = 0;
for (uint32_t i = 0; i < mNumParams; ++i)
{
result += (int) mParams[i]->Evaluate();
}
return result;
}
case funSub:
if (mNumParams > 0)
{
int result = (int) mParams[0]->Evaluate();
for (uint32_t i = 1; i < mNumParams; ++i)
{
result -= (int) mParams[i]->Evaluate();
}
return result;
}
return 0;
case funMul:
{
int result = 1;
for (uint32_t i = 0; i < mNumParams; ++i)
{
result *= (int) mParams[i]->Evaluate();
}
return result;
}
case funDiv:
return ((int) mParams[0]->Evaluate() / (int) mParams[1]->Evaluate());
case funFontTotalWidth:
case funFontTotalHeight:
case funFontTotalAscent:
case funFontSpaceBetween:
case funFontLineHeight:
return FunFont(mType, mParams[0]->Evaluate(), "");
case funFontTextWidth:
case funFontTextHeight:
return FunFont(mType, mParams[0]->Evaluate(), mParams[1]->Evaluate());
case funImageWidth:
case funImageHeight:
return FunImage(mType, mParams[0]->Evaluate());
case funQueryFeature: {
int value;
if (mSkin->Config().GetDriver()->GetFeature((const std::string)(mParams[0]->Evaluate()), value)) {
return (value) ? true : false;
} else {
return false;
}
}
default:
//Dprintf("unknown function code\n");
syslog(LOG_ERR, "ERROR: Unknown function code called (this shouldn't happen)");
break;
}
return false;
}
} // end of namespace
|