use cbuf for xml value code

This commit is contained in:
Olof hagsand 2019-11-18 21:16:12 +01:00
parent f2cad01fc6
commit 9575d10887
5 changed files with 39 additions and 58 deletions

View file

@ -126,9 +126,7 @@ struct xml{
int x_childvec_len;/* Number of children */
int x_childvec_max;/* Length of allocated vector */
enum cxobj_type x_type; /* type of node: element, attribute, body */
char *x_value; /* attribute and body nodes have values */
uint32_t x_value_len; /* Length of x_value string (excluding null) */
uint32_t x_value_max; /* allocated size for x_value */
cbuf *x_value_cb; /* attribute and body nodes have values */
int x_flags; /* Flags according to XML_FLAG_* */
yang_stmt *x_spec; /* Pointer to specification, eg yang, by
reference, dont free */
@ -624,43 +622,7 @@ xml_flag_reset(cxobj *xn,
char*
xml_value(cxobj *xn)
{
return xn->x_value;
}
/*! xml value string reallocator
*/
static int
xml_value_realloc(cxobj *xn,
int len0,
char *val)
{
int retval = -1;
int len;
int diff;
if (val==NULL)
goto ok;
len = len0 + strlen(val);
diff = xn->x_value_max - (len + 1);
if (diff <= 0){
while (diff <= 0){
if (xn->x_value_max == 0)
xn->x_value_max = XML_VALUE_MAX_DEFAULT;
else
xn->x_value_max *= 2;
diff = xn->x_value_max - (len + 1);
}
if ((xn->x_value = realloc(xn->x_value, xn->x_value_max)) == NULL){
clicon_err(OE_XML, errno, "realloc");
goto done;
}
}
strncpy(xn->x_value + len0, val, len-len0+1);
xn->x_value_len = len;
ok:
retval = 0;
done:
return retval;
return xn->x_value_cb?cbuf_get(xn->x_value_cb):NULL;
}
/*! Set value of xml node, value is copied
@ -675,12 +637,15 @@ xml_value_set(cxobj *xn,
{
int retval = -1;
if (xn->x_value && strlen(xn->x_value)){
xn->x_value[0] = '\0';
xn->x_value_len = 0;
if (xn->x_value_cb == NULL){
if ((xn->x_value_cb = cbuf_new()) == NULL){
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
}
if (xml_value_realloc(xn, 0, val) < 0)
goto done;
else
cbuf_reset(xn->x_value_cb);
cprintf(xn->x_value_cb, "%s", val);
retval = 0;
done:
return retval;
@ -692,13 +657,25 @@ xml_value_set(cxobj *xn,
* @retval NULL on error with clicon-err set, or if value is set to NULL
* @retval new value
*/
char *
int
xml_value_append(cxobj *xn,
char *val)
{
if (xml_value_realloc(xn, xn->x_value_len, val) < 0)
return NULL;
return xn->x_value;
int retval = -1;
if (xn->x_value_cb == NULL){
if ((xn->x_value_cb = cbuf_new()) == NULL){
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
}
if (cprintf(xn->x_value_cb, "%s", val) < 0){
clicon_err(OE_XML, errno, "cprintf");
goto done;
}
retval = 0;
done:
return retval;
}
/*! Get type of xnode
@ -1623,8 +1600,8 @@ xml_free(cxobj *x)
if (x->x_name)
free(x->x_name);
if (x->x_value)
free(x->x_value);
if (x->x_value_cb)
cbuf_free(x->x_value_cb);
if (x->x_prefix)
free(x->x_prefix);
for (i=0; i<x->x_childvec_len; i++){
@ -2648,8 +2625,10 @@ clicon_log_xml(int level,
int retval = -1;
/* Print xml as cbuf */
if ((cb = cbuf_new()) == NULL)
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (clicon_xml2cbuf(cb, x, 0, 0, -1) < 0)
goto done;