Skip to content

Commit 72016cb

Browse files
author
David Sommerseth
committed
Added 'emptyValue' attribute in the Map tag
This attribute defines a default value when XML source data is empty
1 parent fff6c8a commit 72016cb

2 files changed

Lines changed: 34 additions & 16 deletions

File tree

src/xmlpythonizer.c

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ void ptzmap_Free_func(ptzMAP *ptr)
108108
ptr->list_index = NULL;
109109
}
110110

111+
if( ptr->emptyValue != NULL ) {
112+
free(ptr->emptyValue);
113+
ptr->emptyValue = NULL;
114+
}
115+
111116
free(ptr->key);
112117
ptr->key = NULL;
113118

@@ -271,14 +276,14 @@ ptzMAP *_do_dmimap_parsing(xmlNode *node) {
271276
retmap = ptzmap_Add(retmap, rootpath, type_key, key, type_value, NULL,
272277
_do_dmimap_parsing(ptr_n->children->next));
273278
} else {
274-
char *emptyIsNone = NULL;
279+
char *tmpstr = NULL;
275280

276281
// Append the value as a normal value when the
277282
// value type is not a Python Dict
278283
retmap = ptzmap_Add(retmap, rootpath, type_key, key, type_value, value, NULL);
279284

280285
// Set emptyIsNone flag
281-
if( (emptyIsNone = dmixml_GetAttrValue(ptr_n, "emptyIsNone")) != NULL ) {
286+
if( (tmpstr = dmixml_GetAttrValue(ptr_n, "emptyIsNone")) != NULL ) {
282287
switch( retmap->type_value ) {
283288
case ptzSTR:
284289
case ptzINT:
@@ -288,12 +293,15 @@ ptzMAP *_do_dmimap_parsing(xmlNode *node) {
288293
case ptzLIST_INT:
289294
case ptzLIST_FLOAT:
290295
case ptzLIST_BOOL:
291-
retmap->emptyIsNone = (emptyIsNone[0] == '1' ? 1 : 0);
296+
retmap->emptyIsNone = (tmpstr[0] == '1' ? 1 : 0);
292297
break;
293298
default:
294299
break;
295300
}
296301
}
302+
if( (tmpstr = dmixml_GetAttrValue(ptr_n, "emptyValue")) != NULL ) {
303+
retmap->emptyValue = strdup(tmpstr);
304+
}
297305
}
298306

299307
if( (retmap != NULL) && (listidx != NULL) && (fixedsize > 0) ) {
@@ -356,15 +364,16 @@ ptzMAP *dmiMAP_ParseMappingXML(xmlDoc *xmlmap, const char *mapname) {
356364
//
357365
// Parser routines for converting XML data into Python structures
358366
//
359-
inline PyObject *StringToPyObj(ptzMAP *val_m, const char *str) {
367+
inline PyObject *StringToPyObj(ptzMAP *val_m, const char *instr) {
360368
PyObject *value;
369+
const char *workstr = NULL;
361370

362-
if( str == NULL ) {
371+
if( instr == NULL ) {
363372
return Py_None;
364373
}
365374

366-
if( val_m->emptyIsNone == 1) {
367-
char *cp = strdup(str);
375+
if( (val_m->emptyIsNone == 1) || (val_m->emptyValue != NULL) ) {
376+
char *cp = strdup(instr);
368377
char *cp_p = NULL;
369378
assert( cp != NULL );
370379

@@ -380,36 +389,45 @@ inline PyObject *StringToPyObj(ptzMAP *val_m, const char *str) {
380389
// there is no data here
381390
if( cp_p <= cp ) {
382391
free(cp);
383-
return Py_None;
392+
if( val_m->emptyIsNone == 1 ) {
393+
return Py_None;
394+
}
395+
if( val_m->emptyValue != NULL ) {
396+
workstr = (const char *)val_m->emptyValue;
397+
}
398+
} else {
399+
free(cp);
384400
}
385-
free(cp);
386401
}
387402

403+
if( workstr == NULL ) {
404+
workstr = instr;
405+
}
388406

389407

390408
switch( val_m->type_value ) {
391409
case ptzINT:
392410
case ptzLIST_INT:
393-
value = PyInt_FromLong(atoi(str));
411+
value = PyInt_FromLong(atoi(workstr));
394412
break;
395413

396414
case ptzFLOAT:
397415
case ptzLIST_FLOAT:
398-
value = PyFloat_FromDouble(atof(str));
416+
value = PyFloat_FromDouble(atof(workstr));
399417
break;
400418

401419
case ptzBOOL:
402420
case ptzLIST_BOOL:
403-
value = PyBool_FromLong((atoi(str) == 1 ? 1:0));
421+
value = PyBool_FromLong((atoi(workstr) == 1 ? 1:0));
404422
break;
405423

406424
case ptzSTR:
407425
case ptzLIST_STR:
408-
value = PyString_FromString(str);
426+
value = PyString_FromString(workstr);
409427
break;
410428

411429
default:
412-
fprintf(stderr, "Invalid type '%i' for value '%s'\n", val_m->type_value, str);
430+
fprintf(stderr, "Invalid type '%i' for value '%s'\n", val_m->type_value, instr);
413431
value = Py_None;
414432
}
415433
return value;

src/xmlpythonizer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ typedef struct ptzMAP_s {
4141
// the rest of types, an XPath to XML data
4242
int fixed_list_size; // Only to be used on lists
4343
char *list_index ; // Only to be used on fixed lists
44-
int emptyIsNone; // Only for ptzINT/ptzFLOAT values
45-
// - if set to 1, empty input strings sets the result to Py_None
44+
int emptyIsNone; // If set to 1, empty input (right trimmed) strings sets the result to Py_None
45+
char *emptyValue; // If set, this value will be used when input is empty
4646
struct ptzMAP_s *child; // Only used for type_value == pyDICT
4747
struct ptzMAP_s *next; // Pointer chain
4848

0 commit comments

Comments
 (0)