-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKXmlNodeList.cpp
667 lines (563 loc) · 15.4 KB
/
KXmlNodeList.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
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*
*
* Copyright (C) 2014 Jürg Müller, CH-5524
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation version 3 of the License.
*
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/ .
*/
#if defined(__PYTHON__)
#include <Python.h>
#endif
#include <stdio.h>
#include <string.h>
#include "NTypes.h"
#include "KXmlNodeList.h"
#include "NUtils.h"
#include "KStream.h"
#include "KElsterTable.h"
#if defined(__UVR__)
#include "KCanUVR.h"
#endif
void KXmlNodeList::SetNodeValue(const char * Value, int ValueLen)
{
if (mNodeValue)
delete [] mNodeValue;
mNodeValue = NULL;
if (Value && ValueLen > 0)
{
mNodeValue = new char [ValueLen+1];
memcpy(const_cast<char *> (mNodeValue), Value, ValueLen);
const_cast<char *> (mNodeValue)[ValueLen] = 0;
}
}
////////////////////////////////////////////////////////////////////////////////
//
// KXmlNodeList
//
////////////////////////////////////////////////////////////////////////////////
unsigned KXmlNodeList::ResolveEntities(char * str)
{
unsigned len_out = 0;
unsigned len = (unsigned) strlen(str);
for (int i = 0; i < (int) len; i++)
{
char c = str[i];
if (c == '&')
{
if (!strncmp(str + i, "&", 5))
{
c = '&';
i += 4;
} else
if (!strncmp(str + i, "'", 6))
{
c = '\'';
i += 5;
} else
if (!strncmp(str + i, """, 6))
{
c = '"';
i += 5;
}
}
str[len_out++] = c;
}
str[len_out] = 0;
return len_out;
}
void KXmlNodeList::AppendAttr(const char * Name, const char * ValuePos, int ValueLen)
{
KXmlAttrList * Attr = new KXmlAttrList;
if (Name && Name[0])
{
Attr->mName = new char[strlen(Name)+1];
strcpy(const_cast<char *>(Attr->mName), Name);
}
if (ValuePos && ValueLen > 0)
{
char buffer[1024];
if (ValueLen >= 1023)
ValueLen = 1023;
strncpy(buffer, ValuePos, ValueLen);
buffer[ValueLen] = 0;
unsigned len_out = ResolveEntities(buffer);
Attr->mValue = new char[len_out + 1];
strcpy(const_cast<char *>(Attr->mValue), buffer);
}
if (!mAttrList)
mAttrList = Attr;
else {
KXmlAttrList * AttrList = mAttrList;
while (AttrList->mNext)
AttrList = AttrList->mNext;
AttrList->mNext = Attr;
}
}
void KXmlNodeList::AppendAttr(const char * Name, const char * Value)
{
int len = Value ? (int) strlen(Value) : 0;
AppendAttr(Name, len ? Value : "", len);
}
const char * KXmlNodeList::FindAttr(const char * Name) const
{
const KXmlAttrList * AttrList = mAttrList;
while (AttrList)
{
if (AttrList->mName &&
!strcmp(AttrList->mName, Name))
return AttrList->mValue;
AttrList = AttrList->mNext;
}
return NULL;
}
const char * KXmlNodeList::FindNodeAttr(const char * NodeName, const char * Name)
{
const KXmlNode * Node = Find(NodeName);
return Node ? Node->FindAttr(Name) : NULL;
}
KXmlNodeList::KXmlNodeList()
{
mName[0] = 0;
mFirstNode = NULL;
mAttrList = NULL;
mNodeValue = NULL;
}
KXmlNodeList::~KXmlNodeList()
{
Init();
}
void KXmlNodeList::Init()
{
mName[0] = 0;
while (mFirstNode)
{
KXmlNode * Node = mFirstNode->mNext;
delete mFirstNode;
mFirstNode = Node;
}
if (mAttrList)
delete mAttrList;
mAttrList = NULL;
SetNodeValue(NULL, 0);
}
void KXmlNodeList::Append(KXmlNode * Node)
{
Node->mNext = NULL;
if (mFirstNode)
{
KXmlNode * node = mFirstNode;
while (node->mNext)
node = node->mNext;
node->mNext = Node;
} else
mFirstNode = Node;
}
KXmlNode * KXmlNodeList::AppendNode(const char * Name, const char * Text)
{
KXmlNode * res = new KXmlNode;
if (Text)
{
res->mText = new char[strlen(Text)+1];
strcpy((char *) res->mText, Text);
}
int len = Name ? (int) strlen(Name) : 0;
if (len >= (int) High(res->mName))
len = (int) High(res->mName) - 1;
if (len > 0)
strncpy(res->mName, Name, len);
res->mName[len] = 0;
Append(res);
return res;
}
const KXmlAttrList * KXmlNodeList::GetPrevAttr(const KXmlAttrList * Attr) const
{
if (Attr == mAttrList)
return NULL;
const KXmlAttrList * ResAttr = mAttrList;
while (ResAttr && ResAttr->mNext != Attr)
ResAttr = ResAttr->mNext;
return ResAttr;
}
bool KXmlNodeList::GetNextNode(const KXmlNode * & Node) const
{
if (!Node)
Node = mFirstNode;
else
Node = Node->mNext;
return Node != NULL;
}
bool KXmlNodeList::GetPrevNode(const KXmlNode * & Node) const
{
if (Node == mFirstNode)
{
Node = NULL;
return false;
}
const KXmlNode * ResNode = mFirstNode;
while (ResNode && ResNode->mNext != Node)
ResNode = ResNode->mNext;
Node = ResNode;
return Node != NULL;
}
KXmlNode * KXmlNodeList::GetLastNode() const
{
const KXmlNode * ResNode = NULL;
GetPrevNode(ResNode);
return const_cast<KXmlNode *> (ResNode);
}
KXmlNode * KXmlNodeList::FindNext(const char * Name, KXmlNode * NextNode) const
{
if (!Name || !Name[0])
return NULL;
while (NextNode)
{
if (NextNode->mName &&
!strcmp(NextNode->mName, Name))
return NextNode;
KXmlNode * res = NextNode->Find(Name);
if (res)
return res;
NextNode = NextNode->mNext;
}
return NULL;
}
KXmlNode * KXmlNodeList::Find(const char * Name) const
{
return FindNext(Name, mFirstNode);
}
bool KXmlNodeList::channel_check(bool use_mysql, const char * template_, KStream * Stream) const
{
if (!Stream)
return false;
char param_value[256];
bool Ok = true;
int cnt = 1;
const KXmlNode * Node = NULL;
while (GetNextNode(Node))
{
if (strlen(Node->mName) == 0)
{
printf("invalid xml file\n");
return false;
}
if (!strcmp(Node->mName, "channel"))
{
#if !defined(__UVR__)
const char * can_msg = Node->FindAttr("can_msg");
// elster: <can id>.<elster index> (4-stellig hex.)
const char * can_inst = Node->FindAttr("can_inst");
#else
// uvr: <can_id>.<1..10> (hex.)
const char * can_val = Node->FindAttr("can_val");
#endif
const char * uuid = Node->FindAttr("uuid");
const char * query = Node->FindAttr("query");
const char * name = Node->FindAttr("Name");
const char * format = Node->FindAttr("format");
if (!name || strlen(name) == 0)
{
printf("\"Name\" is missing in channel %d\n", cnt);
return false;
}
Stream->SetLength(0);
if (query || !uuid)
{
if (template_)
{
Ok = Stream->InitString(template_);
if (query)
Ok = Stream->Replace("%s", query);
if (!Ok)
{
printf("\"%cs\" needed within \"template\" (%s)!\n", '%', name);
return false;
}
} else
Ok = Stream->InitString(query);
if (Ok &&
strstr((const char *) Stream->GetMemory(), "${value}"))
Ok = Stream->Replace("${value}", "0.0");
if (Ok &&
strstr((const char *) Stream->GetMemory(), "${ts}"))
Ok = Stream->Replace("${ts}", "0000");
if (Ok &&
strstr((const char *) Stream->GetMemory(), "${timestamp}"))
Ok = Stream->Replace("${timestamp}", "0000");
// search value in xml node
long pos = 0;
while (Ok)
{
const char * param = strstr((const char *) Stream->GetMemory() + pos, "${");
if (!param)
{
break;
}
pos = param - (const char *) Stream->GetMemory();
const char * param_end = param ? strstr(param, "}") : NULL;
if (!param_end)
{
Ok = false;
break;
}
long param_len = param_end - param + 1;
if (param_len <= 3 || param_len >= (long) High(param_value))
{
Ok = false;
break;
}
strncpy(param_value, param + 2, param_len-3);
param_value[param_len-3] = 0;
const char * par = Node->FindAttr(param_value);
if (!par)
{
Ok = false;
break;
}
strncpy(param_value, param, param_len);
param_value[param_len] = 0;
Ok = Stream->Replace(param_value, par);
}
if (!Ok)
{
printf("error in query \"%s\" (%s)\n", Stream->GetMemory(), name);
return false;
}
} else
if (use_mysql)
{
printf("query is missing in channel %d (%s)\n", cnt, name);
return false;
} else {
if (!uuid || strlen(uuid) == 0)
{
printf("uuid is missing in channel %d (%s)\n", cnt, name);
return false;
}
}
if (format)
{
ElsterType type = GetElsterType(format);
if (type == et_default)
{
printf("unknown format \"%s\" (%s)\n", format, name);
return false;
}
}
#if defined(__UVR__)
if (!can_val)
{
printf("can_val is missing in channel %d (%s)\n", cnt, name);
return false;
}
unsigned can_id;
int chan;
if (!KCanUVR::GetIdAndChan(can_val, can_id, chan))
{
printf("invalid can_val \"%s\" (%s)\n", can_val, name);
return false;
}
#else
// Elster-Index
if (can_inst)
{
KCanFrame Frame;
if (!Frame.SetElsterFrame(can_inst))
{
printf("invalid can_inst \"%s\" (%s)\n", can_inst, name);
return false;
}
} else
if (!can_msg || !*can_msg)
{
printf("can_inst/can_msg is missing in channel %d (%s)\n", cnt, name);
return false;
} else {
KCanFrame Frame;
if (!Frame.GetDataFromStream(can_msg))
{
printf("invalid can_msg(%s): \"%s\"\n", name, can_msg);
return false;
}
}
#endif
cnt++;
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
//
// KXmlNode
//
////////////////////////////////////////////////////////////////////////////////
KXmlNode::~KXmlNode()
{
if (mText)
delete [] mText;
}
void KXmlNode::BuildStream(KStream & Stream) const
{
Stream.AppendString("<");
Stream.AppendString(mName);
const KXmlAttrList * List = GetAttrList();
while (List)
{
Stream.AppendString(" ");
Stream.AppendString(List->mName);
Stream.AppendString("=\"");
Stream.AppendString(List->mValue);
Stream.AppendString("\"");
List = List->mNext;
}
if (GetFirstNode() || mText)
{
Stream.AppendString(">");
if (mText)
{
Stream.AppendString(mText);
} else
Stream.AppendString("\n");
const KXmlNode * Node = NULL;
while (GetNextNode(Node))
{
Node->BuildStream(Stream);
}
Stream.AppendString("</");
Stream.AppendString(mName);
Stream.AppendString(">\n");
} else
Stream.AppendString(" />\n");
}
void KXmlNode::BuildRootStream(KStream & Stream) const
{
Stream.SetLength(0);
Stream.AppendString("<?xml version=\"1.0\"?>\n");
const KXmlNode * Node = GetFirstNode();
if (Node)
Node->BuildStream(Stream);
}
bool KXmlNode::set_channel_value(KStream * stream, const char * template_, bool trace)
{
if (strcmp(mName, "channel"))
return false;
char param_value[256];
const char * format = FindAttr("format");
const char * query = FindAttr("query");
const char * uuid = FindAttr("uuid");
ElsterType type = GetElsterType(format);
char Value[64];
bool Ok = true;
if (type == et_double_val ||
type == et_triple_val)
{
SetDoubleType(Value, type, double_value);
} else
SetValueType(Value, type, can_value);
stream->SetLength(0);
if (query || !uuid)
{
// Platzhalter ${...} durch den entsprecheden Wert ersetzen
//
// intern verwendete Platzhalter: ${value}, ${ts} und ${timestamp}
//
// alle anderen Platzhalter werden in der Parameterliste zum "channel" gesucht
// z.B. für ${uuid}
//
// <channel uuid="xxx..." ... />
if (template_)
{
Ok = stream->InitString(template_);
if (Ok && query)
Ok = stream->Replace("%s", query);
} else
Ok = stream->InitString(query);
if (Ok &&
strstr((const char *) stream->GetMemory(), "${value}"))
Ok = stream->Replace("${value}", Value);
if (Ok &&
strstr((const char *) stream->GetMemory(), "${ts}"))
{
NUtils::PrintDateTime(time_days, time_ms, param_value);
Ok = stream->Replace("${ts}", param_value);
}
if (Ok &&
strstr((const char *) stream->GetMemory(), "${timestamp}"))
{
NUtils::PrintMs(time_days, time_ms, param_value);
Ok = stream->Replace("${timestamp}", param_value);
}
// search value in xml
long pos = 0;
while (Ok)
{
const char * param = strstr((const char *) stream->GetMemory() + pos, "${");
if (!param)
break;
pos = param - (const char *) stream->GetMemory();
const char * param_end = param ? strstr(param, "}") : NULL;
if (!param_end)
break;
long param_len = param_end - param + 1;
if (param_len <= 3 || param_len >= (long) High(param_value))
break;
strncpy(param_value, param + 2, param_len-3);
param_value[param_len-3] = 0;
const char * par = FindAttr(param_value);
if (!par)
break;
strncpy(param_value, param, param_len);
param_value[param_len] = 0;
Ok = stream->Replace(param_value, par);
}
} else {
Ok = stream->InitString(template_) &&
stream->Replace("%s", uuid) &&
stream->Replace("%s", Value);
}
if (!Ok && trace)
{
const char * name = FindAttr("Name");
printf("\"%s\" has non value\n", name);
}
return Ok;
}
////////////////////////////////////////////////////////////////////////////////
//
// KXmlAttrList
//
////////////////////////////////////////////////////////////////////////////////
KXmlAttrList::~KXmlAttrList()
{
if (mName)
delete [] mName;
if (mValue)
delete [] mValue;
if (mNext)
delete mNext;
}
////////////////////////////////////////////////////////////////////////////////
bool test_template(const char * template_)
{
if (!template_ || strlen(template_) == 0)
{
printf("template is missing\n");
return false;
}
const char * ptr = strstr(template_, "%s");
if (!ptr ||
!strstr(ptr + 2, "%s"))
{
printf("two \"%c\" are needed within \"template\"!\n", '%');
return false;
}
return true;
}