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
|
/*
* GraphLCD skin library
*
* parser.c - xml parsing
*
* This file is released under the GNU General Public License. Refer
* to the COPYING file distributed with this package.
*
* based on text2skin
*
*/
#include <stdio.h>
#include <syslog.h>
#include <vector>
#include <string>
#include <clocale>
#include "parser.h"
#include "xml.h"
#include "skin.h"
namespace GLCD
{
#define TAG_ERR_REMAIN(_context) do { \
errorDetail = "Unexpected tag "+name+" within "+ _context; \
syslog(LOG_ERR, "ERROR: graphlcd/skin: %s", errorDetail.c_str()); \
return false; \
} while (0)
#define TAG_ERR_CHILD(_context) do { \
errorDetail = "No child tag "+name+" expected within "+ _context; \
syslog(LOG_ERR, "ERROR: graphlcd/skin: %s", errorDetail.c_str()); \
return false; \
} while (0)
#define TAG_ERR_END(_context) do { \
errorDetail = "Unexpected closing tag for "+name+" within "+ _context; \
syslog(LOG_ERR, "ERROR: graphlcd/skin: %s", errorDetail.c_str()); \
return false; \
} while (0)
#define ATTRIB_OPT_STRING(_attr,_target) \
if (attrs.find(_attr) != attrs.end()) { \
_target = attrs[_attr]; \
}
#define ATTRIB_MAN_STRING(_attr,_target) \
ATTRIB_OPT_STRING(_attr,_target) \
else { \
errorDetail = "Mandatory attribute "+ (std::string)_attr +" missing in tag "+ name; \
syslog(LOG_ERR, "ERROR: graphlcd/skin: %s", errorDetail.c_str()); \
return false; \
}
#define ATTRIB_OPT_NUMBER(_attr,_target) \
if (attrs.find(_attr) != attrs.end()) { \
char *_e; const char *_t = attrs[_attr].c_str(); \
long _l = strtol(_t, &_e, 10); \
if (_e ==_t || *_e != '\0') { \
errorDetail = "Invalid numeric value \""+ (std::string)_t +"\" in attribute "+ (std::string)_attr; \
syslog(LOG_ERR, "ERROR: graphlcd/skin: %s", errorDetail.c_str()); \
return false; \
} else \
_target = _l; \
}
#define ATTRIB_MAN_NUMBER(_attr,_target) \
ATTRIB_OPT_NUMBER(_attr,_target) \
else { \
errorDetail = "Mandatory attribute "+ (std::string)_attr +" missing in tag "+name; \
syslog(LOG_ERR, "ERROR: graphlcd/skin: %s", errorDetail.c_str()); \
return false; \
}
#define ATTRIB_OPT_BOOL(_attr,_target) \
if (attrs.find(_attr) != attrs.end()) { \
if (attrs[_attr] == "yes") \
_target = true; \
else if (attrs[_attr] == "no") \
_target = false; \
else { \
errorDetail = "Invalid boolean value \""+ attrs[_attr] +"\" in attribute "+ _attr; \
syslog(LOG_ERR, "ERROR: graphlcd/skin: %s", errorDetail.c_str()); \
return false; \
} \
}
#define ATTRIB_MAN_BOOL(_attr,_target) \
ATTRIB_OPT_BOOL(_attr,_target) \
else { \
errorDetail = "Mandatory attribute "+ (std::string)_attr +" missing in tag "+name; \
syslog(LOG_ERR, "ERROR: graphlcd/skin: %s", errorDetail.c_str()); \
return false; \
}
#define ATTRIB_OPT_FUNC(_attr,_func) \
if (attrs.find(_attr) != attrs.end()) { \
if (!_func(attrs[_attr])) { \
errorDetail = "Unexpected value \""+ attrs[_attr] +"\" for attribute "+ (std::string)_attr; \
syslog(LOG_ERR, "ERROR: graphlcd/skin: %s", errorDetail.c_str()); \
return false; \
} \
}
#define ATTRIB_MAN_FUNC(_attr,_func) \
ATTRIB_OPT_FUNC(_attr,_func) \
else { \
errorDetail = "Mandatory attribute "+ (std::string)_attr +" missing in tag "+name; \
syslog(LOG_ERR, "ERROR: graphlcd/skin: %s", errorDetail.c_str()); \
return false; \
}
#define ATTRIB_OPT_FUNC_PARAM(_attr,_func,_param) \
if (attrs.find(_attr) != attrs.end()) { \
if (!_func(attrs[_attr],_param)) { \
errorDetail = "Unexpected value "+ attrs[_attr] +" for attribute "+ (std::string)_attr; \
syslog(LOG_ERR, "ERROR: graphlcd/skin: %s", errorDetail.c_str()); \
return false; \
} \
}
#define ATTRIB_MAN_FUNC_PARAM(_attr,_func,_param) \
ATTRIB_OPT_FUNC_PARAM(_attr,_func,_param) \
else { \
errorDetail = "Mandatory attribute "+ (std::string)_attr +" missing in tag "+name; \
syslog(LOG_ERR, "ERROR: graphlcd/skin: %s", errorDetail.c_str()); \
return false; \
}
static std::vector<std::string> context;
static cSkin * skin = NULL;
static cSkinFont * font = NULL;
static cSkinVariable * variable = NULL;
static cSkinVariable * variable_default = NULL;
static cSkinDisplay * display = NULL;
static std::vector <cSkinObject *> parents;
static cSkinObject * object = NULL;
static uint32_t oindex = 0;
static std::string errorDetail = "";
static std::string condblock_cond = "";
static bool CheckSkinVersion(const std::string & version) {
float currv;
char* ecptr = NULL;
const char* verscstr = version.c_str();
// only accept floating point numbers with '.' as separator, no ','
char* curr_locale = setlocale(LC_NUMERIC, "C");
currv = strtof(verscstr, &ecptr);
setlocale(LC_NUMERIC, curr_locale);
if ( (*ecptr != '\0') || (ecptr == NULL) /*|| (ecptr != verscstr)*/ ||
((int)(GLCDSKIN_SKIN_VERSION * 100.0) < (int)(currv * 100.0))
)
{
return false;
}
return true;
}
bool StartElem(const std::string & name, std::map<std::string,std::string> & attrs)
{
//printf("start element: %s\n", name.c_str());
// if (context.size() > 0) fprintf(stderr, "context: %s\n", context[context.size() - 1].c_str());
if (context.size() == 0)
{
if (name == "skin")
{
ATTRIB_MAN_STRING("version", skin->version);
ATTRIB_MAN_STRING("name", skin->title);
ATTRIB_OPT_FUNC("enable", skin->ParseEnable);
if (! CheckSkinVersion(skin->version) ) {
errorDetail = "skin version '"+ skin->version +"' not supported.";
syslog(LOG_ERR, "ERROR: graphlcd/skin: %s", errorDetail.c_str());
return false;
}
}
else
TAG_ERR_REMAIN("document");
}
else if (name == "condblock")
{
int i = context.size() - 1;
while (i >= 0) {
if (context[i] == "condblock") {
errorDetail = "'condblock' must not be nested in another 'condblock'.";
syslog(LOG_ERR, "ERROR: graphlcd/skin: %s", errorDetail.c_str());
return false;
}
i--;
}
ATTRIB_MAN_STRING("condition", condblock_cond);
}
else if (name == "variable")
{
variable = new cSkinVariable(skin);
ATTRIB_MAN_STRING("id", variable->mId);
ATTRIB_MAN_FUNC("value", variable->ParseValue);
if (context[context.size() - 1] == "condblock") {
if (attrs.find("condition") != attrs.end()) {
errorDetail = "variable \""+variable->mId+"\" must not contain a condition when context = 'condblock'.";
syslog(LOG_ERR, "ERROR: graphlcd/skin: %s", errorDetail.c_str());
return false;
} else {
if (! variable->ParseCondition(condblock_cond)) {
errorDetail = "Unexpected value \""+ attrs["condition"] +"\" for attribute "+ (std::string)"condition";
syslog(LOG_ERR, "ERROR: graphlcd/skin: %s", errorDetail.c_str());
return false;
}
}
} else {
ATTRIB_OPT_FUNC("condition", variable->ParseCondition);
}
// if a 'default' value is set, create a second variable w/o condition: will be used if condition is not true
// as variables all have global scope (no matter where defined) and will always be sought from the start of the array,
// the default variable will be inserted _after_ the variable containing the condition.
if (attrs.find("default") != attrs.end()) {
variable_default = new cSkinVariable(skin);
ATTRIB_MAN_STRING("id", variable_default->mId);
ATTRIB_MAN_FUNC("default", variable_default->ParseValue);
}
}
else if (context[context.size() - 1] == "skin")
{
if (name == "font")
{
font = new cSkinFont(skin);
ATTRIB_MAN_STRING("id", font->mId);
ATTRIB_MAN_FUNC("url", font->ParseUrl);
ATTRIB_OPT_FUNC("condition", font->ParseCondition);
}
else if (name == "display")
{
display = new cSkinDisplay(skin);
ATTRIB_MAN_STRING("id", display->mId);
}
else
TAG_ERR_REMAIN("skin");
}
else if (context[context.size() - 1] == "display"
|| context[context.size() - 1] == "list"
|| context[context.size() - 1] == "block")
{
if (object != NULL)
{
parents.push_back(object);
object = NULL;
}
object = new cSkinObject(display);
/* default settings */
object->ParseColor("transparent", object->mBackgroundColor);
if (object->ParseType(name))
{
ATTRIB_OPT_FUNC_PARAM("x", object->ParseIntParam, object->mPos1.x);
ATTRIB_OPT_FUNC_PARAM("y", object->ParseIntParam, object->mPos1.y);
ATTRIB_OPT_FUNC_PARAM("x1", object->ParseIntParam, object->mPos1.x);
ATTRIB_OPT_FUNC_PARAM("y1", object->ParseIntParam, object->mPos1.y);
ATTRIB_OPT_FUNC_PARAM("x2", object->ParseIntParam, object->mPos2.x);
ATTRIB_OPT_FUNC_PARAM("y2", object->ParseIntParam, object->mPos2.y);
ATTRIB_OPT_FUNC("width", object->ParseWidth);
ATTRIB_OPT_FUNC("height", object->ParseHeight);
ATTRIB_OPT_FUNC("condition", object->ParseCondition);
ATTRIB_OPT_STRING("action", object->mAction);
if (name == "image")
{
//ATTRIB_OPT_FUNC_PARAM("x", object->ParseIntParam, object->mPos2.x);
//ATTRIB_OPT_FUNC_PARAM("y", object->ParseIntParam, object->mPos2.y);
ATTRIB_OPT_FUNC_PARAM("color", object->ParseColor, object->mColor);
ATTRIB_OPT_FUNC_PARAM("bgcolor", object->ParseColor, object->mBackgroundColor);
ATTRIB_MAN_FUNC("path", object->mPath.Parse);
ATTRIB_OPT_FUNC("loop", object->ParseScrollLoopMode);
ATTRIB_OPT_FUNC_PARAM("opacity", object->ParseIntParam, object->mOpacity);
}
else if (name == "text"
|| name == "scrolltext")
{
ATTRIB_OPT_FUNC_PARAM("color", object->ParseColor, object->mColor);
ATTRIB_OPT_FUNC_PARAM("bgcolor", object->ParseColor, object->mBackgroundColor);
ATTRIB_OPT_FUNC("align", object->ParseAlignment);
ATTRIB_OPT_FUNC("valign", object->ParseVerticalAlignment);
ATTRIB_MAN_FUNC("font", object->ParseFontFace);
ATTRIB_OPT_BOOL("multiline", object->mMultiline);
ATTRIB_OPT_FUNC("scrollmode", object->ParseScrollLoopMode);
ATTRIB_OPT_FUNC("scrollspeed", object->ParseScrollSpeed);
ATTRIB_OPT_FUNC("scrolltime", object->ParseScrollTime);
ATTRIB_OPT_STRING("alttext", object->mAltText);
ATTRIB_OPT_FUNC("altcondition", object->ParseAltCondition);
ATTRIB_OPT_FUNC_PARAM("effectcolor", object->ParseColor, object->mEffectColor);
ATTRIB_OPT_FUNC("effect", object->ParseEffect);
ATTRIB_OPT_NUMBER("radius", object->mRadius);
}
else if (name == "button")
{
ATTRIB_OPT_FUNC_PARAM("labelcolor", object->ParseColor, object->mColor);
ATTRIB_OPT_FUNC_PARAM("color", object->ParseColor, object->mBackgroundColor);
ATTRIB_MAN_FUNC("font", object->ParseFontFace);
ATTRIB_OPT_NUMBER("radius", object->mRadius);
}
else if (name == "pixel")
{
ATTRIB_OPT_FUNC_PARAM("color", object->ParseColor, object->mColor);
}
else if (name == "line")
{
ATTRIB_OPT_FUNC_PARAM("color", object->ParseColor, object->mColor);
}
else if (name == "rectangle")
{
ATTRIB_OPT_FUNC_PARAM("color", object->ParseColor, object->mColor);
ATTRIB_OPT_BOOL("filled", object->mFilled);
ATTRIB_OPT_NUMBER("radius", object->mRadius);
}
else if (name == "ellipse" || name == "slope")
{
ATTRIB_OPT_FUNC_PARAM("color", object->ParseColor, object->mColor);
ATTRIB_OPT_BOOL("filled", object->mFilled);
ATTRIB_OPT_NUMBER("arc", object->mArc);
}
else if (name == "progress"
|| name == "scrollbar")
{
ATTRIB_OPT_FUNC_PARAM("color", object->ParseColor, object->mColor);
ATTRIB_OPT_NUMBER("direction", object->mDirection);
ATTRIB_OPT_FUNC("current", object->mCurrent.Parse);
ATTRIB_OPT_FUNC("total", object->mTotal.Parse);
}
#if 0
else if (name == "item") {
ATTRIB_MAN_NUMBER("height", object->mPos2.y);
--object->mPos2.y;
}
#endif
// range checks
if (object->mOpacity < 0)
object->mOpacity = 0;
else if (object->mOpacity > 255)
object->mOpacity = 255;
}
else
TAG_ERR_REMAIN(context[context.size() - 1].c_str());
}
else
TAG_ERR_CHILD(context[context.size() - 1].c_str());
context.push_back(name);
return true;
}
bool CharData(const std::string & text)
{
if (text.length() == 0)
return true;
if (context.size() == 0)
return true;
//printf("char data : %s\n", text.c_str());
//printf("context: %s\n", context[context.size() - 1].c_str());
if (context[context.size() - 1] == "text"
|| context[context.size() - 1] == "scrolltext"
|| context[context.size() - 1] == "button")
{
if (!object->mText.Parse(text))
return false;
}
else
syslog(LOG_ERR, "ERROR: Bad character data");
return true;
}
bool EndElem(const std::string & name)
{
//Dprintf("end element: %s\n", name.c_str());
if (context[context.size() - 1] == name)
{
if (name == "font")
{
skin->fonts.push_back(font);
font = NULL;
}
else if (name == "variable")
{
skin->mVariables.push_back(variable);
//fprintf(stderr, " variable '%s', value: %s\n", variable->mId.c_str(), ((std::string)variable->Value()).c_str());
variable = NULL;
if (variable_default != NULL) {
skin->mVariables.push_back(variable_default);
//fprintf(stderr, " variable default '%s', value: %s\n", variable_default->mId.c_str(), ((std::string)variable_default->Value()).c_str());
variable_default = NULL;
}
}
else if (name == "display")
{
//display->mNumMarquees = mindex;
skin->displays.push_back(display);
display = NULL;
oindex = 0;
}
else if (object != NULL || parents.size() > 0)
{
if (object == NULL)
{
//printf("rotating parent to object\n");
object = parents[parents.size() - 1];
parents.pop_back();
}
#if 0
if (object->mCondition == NULL)
{
switch (object->mType)
{
case cSkinObject::text:
case cSkinObject::scrolltext:
object->mCondition = new cxFunction(object->mText);
break;
default:
break;
}
}
object->mIndex = oindex++;
#endif
if (parents.size() > 0)
{
//printf("pushing to parent\n");
cSkinObject *parent = parents[parents.size() - 1];
if (parent->mObjects == NULL)
parent->mObjects = new cSkinObjects();
parent->mObjects->push_back(object);
}
else
display->mObjects.push_back(object);
object = NULL;
}
context.pop_back();
}
else
TAG_ERR_END(context[context.size() - 1].c_str());
return true;
}
cSkin * XmlParse(cSkinConfig & Config, const std::string & Name, const std::string & fileName, std::string & errorString)
{
skin = new cSkin(Config, Name);
context.clear();
cXML xml(fileName, skin->Config().CharSet());
xml.SetNodeStartCB(StartElem);
xml.SetNodeEndCB(EndElem);
xml.SetCDataCB(CharData);
if (xml.Parse() != 0)
{
char buff[8];
snprintf(buff, 7, "%d", xml.LineNr());
syslog(LOG_ERR, "ERROR: graphlcd/skin: Parse error in %s, line %d", fileName.c_str(), xml.LineNr());
// shorter version outgoing errorString (eg. displaying errorString on the display)
errorString = "Parse error in skin "+Name+", line "+buff;
if (errorDetail != "")
errorString += ":\n"+errorDetail;
delete skin;
skin = NULL;
delete display;
display = NULL;
delete object;
object = NULL;
return NULL;
}
cSkin * result = skin;
skin = NULL;
errorString = "";
return result;
}
} // end of namespace
|