Changed C-API for xml translation/print the internal cxobj tree data structure to other formats
New API is as follows: * `clixon_xml2file()` - Print internal tree as XML to file * `clixon_xml2cbuf()` - Print internal tree as XML to buffer * `clixon_json2file()` - Print internal tree as JSON to file * `clixon_json2cbuf()` - Print internal tree as JSON to buffer * `clixon_cli2file()` - Print internal tree as CLI format to file * `clixon_txt2file()` - Print internal tree as text format to file
This commit is contained in:
parent
87719c623c
commit
0c79298e76
49 changed files with 421 additions and 503 deletions
|
|
@ -745,7 +745,8 @@ xmldb_get_nocache(clicon_handle h,
|
|||
clicon_log(LOG_NOTICE, "%s: sort verify failed #2", __FUNCTION__);
|
||||
#endif
|
||||
if (clicon_debug_get()>1)
|
||||
clicon_xml2file(stderr, xt, 0, 1);
|
||||
if (clixon_xml2file(stderr, xt, 0, 1, fprintf, 0) < 0)
|
||||
goto done;
|
||||
*xtop = xt;
|
||||
xt = NULL;
|
||||
retval = 1;
|
||||
|
|
@ -916,7 +917,8 @@ xmldb_get_cache(clicon_handle h,
|
|||
* If cache was empty, also update to datastore cache
|
||||
*/
|
||||
if (clicon_debug_get()>1)
|
||||
clicon_xml2file(stderr, x1t, 0, 1);
|
||||
if (clixon_xml2file(stderr, x1t, 0, 1, fprintf, 0) < 0)
|
||||
goto done;
|
||||
*xtop = x1t;
|
||||
retval = 1;
|
||||
done:
|
||||
|
|
@ -1016,8 +1018,9 @@ xmldb_get_zerocopy(clicon_handle h,
|
|||
if (disable_nacm_on_empty(x0t, yspec) < 0)
|
||||
goto done;
|
||||
}
|
||||
if (clicon_debug_get()>1)
|
||||
clicon_xml2file(stderr, x0t, 0, 1);
|
||||
if (clicon_debug_get() > 1)
|
||||
if (clixon_xml2file(stderr, x0t, 0, 1, fprintf, 0) < 0)
|
||||
goto done;
|
||||
*xtop = x0t;
|
||||
retval = 1;
|
||||
done:
|
||||
|
|
|
|||
|
|
@ -1206,10 +1206,10 @@ xmldb_put(clicon_handle h,
|
|||
}
|
||||
pretty = clicon_option_bool(h, "CLICON_XMLDB_PRETTY");
|
||||
if (strcmp(format,"json")==0){
|
||||
if (xml2json_file(f, x0, pretty, fprintf, 0) < 0)
|
||||
if (clixon_json2file(f, x0, pretty, fprintf, 0) < 0)
|
||||
goto done;
|
||||
}
|
||||
else if (clicon_xml2file(f, x0, 0, pretty) < 0)
|
||||
else if (clixon_xml2file(f, x0, 0, pretty, fprintf, 0) < 0)
|
||||
goto done;
|
||||
/* Remove modules state after writing to file
|
||||
*/
|
||||
|
|
@ -1264,10 +1264,10 @@ xmldb_dump(clicon_handle h,
|
|||
}
|
||||
pretty = clicon_option_bool(h, "CLICON_XMLDB_PRETTY");
|
||||
if (strcmp(format,"json")==0){
|
||||
if (xml2json_file(f, xt, pretty, fprintf, 0) < 0)
|
||||
if (clixon_json2file(f, xt, pretty, fprintf, 0) < 0)
|
||||
goto done;
|
||||
}
|
||||
else if (clicon_xml2file(f, xt, 0, pretty) < 0)
|
||||
else if (clixon_xml2file(f, xt, 0, pretty, fprintf, 0) < 0)
|
||||
goto done;
|
||||
retval = 0;
|
||||
done:
|
||||
|
|
|
|||
|
|
@ -1046,14 +1046,7 @@ xml2json1_cbuf(cbuf *cb,
|
|||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
*
|
||||
* @code
|
||||
* cbuf *cb;
|
||||
* cb = cbuf_new();
|
||||
* if (xml2json_cbuf(cb, xn, 0, 0) < 0)
|
||||
* goto err;
|
||||
* cbuf_free(cb);
|
||||
* @endcode
|
||||
* @see clicon_xml2cbuf XML corresponding function
|
||||
* @see clixon_xml2cbuf XML corresponding function
|
||||
* @see xml2json_cbuf_vec Top symbol is list
|
||||
*/
|
||||
static int
|
||||
|
|
@ -1110,13 +1103,19 @@ xml2json_cbuf1(cbuf *cb,
|
|||
* @param[in] skiptop 0: Include top object 1: Skip top-object, only children,
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
* @code
|
||||
* cbuf *cb = cbuf_new();
|
||||
* if (xml2json_cbuf(cb, xn, 0, 0, 0) < 0)
|
||||
* goto err;
|
||||
* cbuf_free(cb);
|
||||
* @endcode
|
||||
* @see xml2json_cbuf where the top level object is included
|
||||
*/
|
||||
int
|
||||
xml2json_cbuf(cbuf *cb,
|
||||
cxobj *xt,
|
||||
int pretty,
|
||||
int skiptop)
|
||||
clixon_json2cbuf(cbuf *cb,
|
||||
cxobj *xt,
|
||||
int pretty,
|
||||
int skiptop)
|
||||
{
|
||||
int retval = -1;
|
||||
cxobj *xc;
|
||||
|
|
@ -1126,7 +1125,6 @@ xml2json_cbuf(cbuf *cb,
|
|||
while ((xc = xml_child_each(xt, xc, CX_ELMNT)) != NULL)
|
||||
if (xml2json_cbuf1(cb, xc, pretty) < 0)
|
||||
goto done;
|
||||
|
||||
}
|
||||
else {
|
||||
if (xml2json_cbuf1(cb, xt, pretty) < 0)
|
||||
|
|
@ -1148,7 +1146,7 @@ xml2json_cbuf(cbuf *cb,
|
|||
* @retval -1 Error
|
||||
* @note This only works if the vector is uniform, ie same object name.
|
||||
* Example: <b/><c/> --> <a><b/><c/></a> --> {"b" : null,"c" : null}
|
||||
* @see xml2json_cbuf
|
||||
* @see clixon_json2cbuf
|
||||
*/
|
||||
int
|
||||
xml2json_cbuf_vec(cbuf *cb,
|
||||
|
|
@ -1204,8 +1202,9 @@ xml2json_cbuf_vec(cbuf *cb,
|
|||
|
||||
/*! Translate from xml tree to JSON and print to file using a callback
|
||||
* @param[in] f File to print to
|
||||
* @param[in] x XML tree to translate from
|
||||
* @param[in] xn XML tree to translate from
|
||||
* @param[in] pretty Set if output is pretty-printed
|
||||
* @param[in] fn File print function (if NULL, use fprintf)
|
||||
* @param[in] skiptop 0: Include top object 1: Skip top-object, only children,
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
|
|
@ -1213,25 +1212,27 @@ xml2json_cbuf_vec(cbuf *cb,
|
|||
* @note yang is necessary to translate to one-member lists,
|
||||
* eg if a is a yang LIST <a>0</a> -> {"a":["0"]} and not {"a":"0"}
|
||||
* @code
|
||||
* if (xml2json_file(stderr, xn, 0, fprintf, 0) < 0)
|
||||
* if (clixon_json2file(stderr, xn, 0, fprintf, 0) < 0)
|
||||
* goto err;
|
||||
* @endcode
|
||||
*/
|
||||
int
|
||||
xml2json_file(FILE *f,
|
||||
cxobj *x,
|
||||
int pretty,
|
||||
clicon_output_cb *fn,
|
||||
int skiptop)
|
||||
clixon_json2file(FILE *f,
|
||||
cxobj *xn,
|
||||
int pretty,
|
||||
clicon_output_cb *fn,
|
||||
int skiptop)
|
||||
{
|
||||
int retval = 1;
|
||||
cbuf *cb = NULL;
|
||||
|
||||
if (fn == NULL)
|
||||
fn = fprintf;
|
||||
if ((cb = cbuf_new()) ==NULL){
|
||||
clicon_err(OE_XML, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
if (xml2json_cbuf(cb, x, pretty, skiptop) < 0)
|
||||
if (clixon_json2cbuf(cb, xn, pretty, skiptop) < 0)
|
||||
goto done;
|
||||
(*fn)(f, "%s", cbuf_get(cb));
|
||||
retval = 0;
|
||||
|
|
@ -1250,7 +1251,7 @@ int
|
|||
json_print(FILE *f,
|
||||
cxobj *x)
|
||||
{
|
||||
return xml2json_file(f, x, 1, fprintf, 0);
|
||||
return clixon_json2file(f, x, 1, fprintf, 0);
|
||||
}
|
||||
|
||||
/*! Translate a vector of xml objects to JSON File.
|
||||
|
|
|
|||
|
|
@ -186,7 +186,7 @@ netconf_invalid_value(cbuf *cb,
|
|||
|
||||
if (netconf_invalid_value_xml(&xret, type, message) < 0)
|
||||
goto done;
|
||||
if (clicon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
goto done;
|
||||
retval = 0;
|
||||
done:
|
||||
|
|
@ -309,7 +309,7 @@ netconf_missing_attribute(cbuf *cb,
|
|||
|
||||
if (netconf_missing_attribute_xml(&xret, type, attr, message) < 0)
|
||||
goto done;
|
||||
if (clicon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
goto done;
|
||||
retval = 0;
|
||||
done:
|
||||
|
|
@ -338,7 +338,7 @@ netconf_bad_attribute(cbuf *cb,
|
|||
|
||||
if (netconf_bad_attribute_xml(&xret, type, info, message) < 0)
|
||||
goto done;
|
||||
if (clicon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
goto done;
|
||||
retval = 0;
|
||||
done:
|
||||
|
|
@ -521,7 +521,7 @@ netconf_missing_element(cbuf *cb,
|
|||
if (netconf_common_xml(&xret, type, "missing-element",
|
||||
"bad-element", element, message) < 0)
|
||||
goto done;
|
||||
if (clicon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
goto done;
|
||||
retval = 0;
|
||||
done:
|
||||
|
|
@ -567,7 +567,7 @@ netconf_bad_element(cbuf *cb,
|
|||
if (netconf_common_xml(&xret, type, "bad-element",
|
||||
"bad-element",element, message) < 0)
|
||||
goto done;
|
||||
if (clicon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
goto done;
|
||||
retval = 0;
|
||||
done:
|
||||
|
|
@ -605,7 +605,7 @@ netconf_unknown_element(cbuf *cb,
|
|||
if (netconf_common_xml(&xret, type, "unknown-element",
|
||||
"bad-element", element, message) < 0)
|
||||
goto done;
|
||||
if (clicon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
goto done;
|
||||
retval = 0;
|
||||
done:
|
||||
|
|
@ -652,7 +652,7 @@ netconf_unknown_namespace(cbuf *cb,
|
|||
if (netconf_common_xml(&xret, type, "unknown-namespace",
|
||||
"bad-namespace", ns, message) < 0)
|
||||
goto done;
|
||||
if (clicon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
goto done;
|
||||
retval = 0;
|
||||
done:
|
||||
|
|
@ -690,7 +690,7 @@ netconf_access_denied(cbuf *cb,
|
|||
|
||||
if (netconf_access_denied_xml(&xret, type, message) < 0)
|
||||
goto done;
|
||||
if (clicon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
goto done;
|
||||
retval = 0;
|
||||
done:
|
||||
|
|
@ -936,7 +936,7 @@ netconf_data_missing(cbuf *cb,
|
|||
|
||||
if (netconf_data_missing_xml(&xret, missing_choice, message) < 0)
|
||||
goto done;
|
||||
if (clicon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
goto done;
|
||||
retval = 0;
|
||||
done:
|
||||
|
|
@ -1085,7 +1085,7 @@ netconf_operation_not_supported(cbuf *cb,
|
|||
|
||||
if (netconf_operation_not_supported_xml(&xret, type, message) < 0)
|
||||
goto done;
|
||||
if (clicon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
goto done;
|
||||
retval = 0;
|
||||
done:
|
||||
|
|
@ -1113,7 +1113,7 @@ netconf_operation_failed(cbuf *cb,
|
|||
|
||||
if (netconf_operation_failed_xml(&xret, type, message) < 0)
|
||||
goto done;
|
||||
if (clicon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
goto done;
|
||||
retval = 0;
|
||||
done:
|
||||
|
|
@ -1201,7 +1201,7 @@ netconf_malformed_message(cbuf *cb,
|
|||
|
||||
if (netconf_malformed_message_xml(&xret, message) < 0)
|
||||
goto done;
|
||||
if (clicon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
if (clixon_xml2cbuf(cb, xret, 0, 0, -1, 0) < 0)
|
||||
goto done;
|
||||
retval = 0;
|
||||
done:
|
||||
|
|
@ -1606,13 +1606,15 @@ netconf_err2cb(cxobj *xerr,
|
|||
cprintf(cberr, "%s ", xml_body(x));
|
||||
if ((x=xpath_first(xerr, NULL, "//error-info")) != NULL &&
|
||||
xml_child_nr(x) > 0){
|
||||
clicon_xml2cbuf(cberr, xml_child_i(x, 0), 0, 0, -1, 0);
|
||||
if (clixon_xml2cbuf(cberr, xml_child_i(x, 0), 0, 0, -1, 0) < 0)
|
||||
goto done;
|
||||
}
|
||||
if ((x=xpath_first(xerr, NULL, "//error-app-tag"))!=NULL)
|
||||
cprintf(cberr, ": %s ", xml_body(x));
|
||||
if ((x=xpath_first(xerr, NULL, "//error-path"))!=NULL)
|
||||
cprintf(cberr, ": %s ", xml_body(x));
|
||||
retval = 0;
|
||||
done:
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
|
@ -1996,7 +1998,8 @@ netconf_output(int s,
|
|||
if (clicon_debug_get() > 1){ /* XXX: below only works to stderr, clicon_debug may log to syslog */
|
||||
cxobj *xt = NULL;
|
||||
if (clixon_xml_parse_string(buf, YB_NONE, NULL, &xt, NULL) == 0){
|
||||
clicon_xml2file(stderr, xml_child_i(xt, 0), 0, 0);
|
||||
if (clixon_xml2file(stderr, xml_child_i(xt, 0), 0, 0, fprintf, 0) < 0)
|
||||
goto done;
|
||||
fprintf(stderr, "\n");
|
||||
xml_free(xt);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -763,7 +763,7 @@ send_msg_notify_xml(clicon_handle h,
|
|||
clicon_err(OE_PLUGIN, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
if (clicon_xml2cbuf(cb, xev, 0, 0, -1, 0) < 0)
|
||||
if (clixon_xml2cbuf(cb, xev, 0, 0, -1, 0) < 0)
|
||||
goto done;
|
||||
if (send_msg_notify(s, cbuf_get(cb)) < 0)
|
||||
goto done;
|
||||
|
|
|
|||
|
|
@ -420,7 +420,7 @@ clicon_rpc_netconf_xml(clicon_handle h,
|
|||
goto done;
|
||||
}
|
||||
rpcname = xml_name(xname); /* Store rpc name and use in yang binding after reply */
|
||||
if (clicon_xml2cbuf(cb, xml, 0, 0, -1, 0) < 0)
|
||||
if (clixon_xml2cbuf(cb, xml, 0, 0, -1, 0) < 0)
|
||||
goto done;
|
||||
if (clicon_rpc_netconf(h, cbuf_get(cb), xret, sp) < 0)
|
||||
goto done;
|
||||
|
|
|
|||
|
|
@ -990,7 +990,7 @@ stream_publish_cb(clicon_handle h,
|
|||
clicon_err(OE_XML, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
if (clicon_xml2cbuf(d, event, 0, 0, -1, 0) < 0)
|
||||
if (clixon_xml2cbuf(d, event, 0, 0, -1, 0) < 0)
|
||||
goto done;
|
||||
if (url_post(cbuf_get(u), /* url+stream */
|
||||
cbuf_get(d), /* postfields */
|
||||
|
|
|
|||
|
|
@ -237,20 +237,41 @@ xml2txt1(cxobj *xn,
|
|||
}
|
||||
|
||||
/*! Translate XML to a "pseudo-code" textual format using a callback
|
||||
* @param[in] xn XML object to print
|
||||
* @param[in] fn Callback to make print function
|
||||
* @param[in] f File to print to
|
||||
* @param[in] level Print 4 spaces per level in front of each line
|
||||
*
|
||||
* @param[in] f File to print to
|
||||
* @param[in] xn XML object to print
|
||||
* @param[in] level Print 4 spaces per level in front of each line
|
||||
* @param[in] fn File print function (if NULL, use fprintf)
|
||||
* @param[in] skiptop 0: Include top object 1: Skip top-object, only children,
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
*/
|
||||
int
|
||||
xml2txt(cxobj *xn,
|
||||
clicon_output_cb *fn,
|
||||
FILE *f,
|
||||
int level)
|
||||
clixon_txt2file(FILE *f,
|
||||
cxobj *xn,
|
||||
int level,
|
||||
clicon_output_cb *fn,
|
||||
int skiptop)
|
||||
{
|
||||
int leaflist = 0;
|
||||
int retval = 1;
|
||||
cxobj *xc;
|
||||
int leaflist = 0;
|
||||
|
||||
return xml2txt1(xn, fn, f, level, &leaflist);
|
||||
if (fn == NULL)
|
||||
fn = fprintf;
|
||||
if (skiptop){
|
||||
xc = NULL;
|
||||
while ((xc = xml_child_each(xn, xc, CX_ELMNT)) != NULL)
|
||||
if (xml2txt1(xc, fn, f, level, &leaflist) < 0)
|
||||
goto done;
|
||||
}
|
||||
else {
|
||||
if (xml2txt1(xn, fn, f, level, &leaflist) < 0)
|
||||
goto done;
|
||||
}
|
||||
retval = 0;
|
||||
done:
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*! Parse a string containing text syntax and return an XML tree
|
||||
|
|
|
|||
|
|
@ -1680,7 +1680,7 @@ rpc_reply_check(clicon_handle h,
|
|||
if (ret == 0){
|
||||
clicon_debug(1, "%s failure when validating:%s", __FUNCTION__, cbuf_get(cbret));
|
||||
cbuf_reset(cbret);
|
||||
if (clicon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
|
||||
if (clixon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
|
||||
goto done;
|
||||
goto fail;
|
||||
}
|
||||
|
|
@ -1689,7 +1689,7 @@ rpc_reply_check(clicon_handle h,
|
|||
if (ret == 0){
|
||||
clicon_debug(1, "%s failure when validating:%s", __FUNCTION__, cbuf_get(cbret));
|
||||
cbuf_reset(cbret);
|
||||
if (clicon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
|
||||
if (clixon_xml2cbuf(cbret, xret, 0, 0, -1, 0) < 0)
|
||||
goto done;
|
||||
goto fail;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2353,9 +2353,8 @@ clicon_log_xml(int level,
|
|||
clicon_err(OE_XML, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
if (clicon_xml2cbuf(cb, x, 0, 0, -1, 0) < 0)
|
||||
if (clixon_xml2cbuf(cb, x, 0, 0, -1, 0) < 0)
|
||||
goto done;
|
||||
|
||||
/* first round: compute length of debug message */
|
||||
va_start(args, format);
|
||||
len = vsnprintf(NULL, 0, format, args);
|
||||
|
|
|
|||
|
|
@ -94,8 +94,8 @@
|
|||
* @param[in] level how many spaces to insert before each line
|
||||
* @param[in] prettyprint insert \n and spaces tomake the xml more readable.
|
||||
* @param[in] fn Callback to make print function
|
||||
* @see clicon_xml2cbuf
|
||||
* One can use clicon_xml2cbuf to get common code, but using fprintf is
|
||||
* @see clixon_xml2cbuf
|
||||
* One can use clixon_xml2cbuf to get common code, but using fprintf is
|
||||
* much faster than using cbuf and then printing that,...
|
||||
*/
|
||||
int
|
||||
|
|
@ -113,9 +113,14 @@ xml2file_recurse(FILE *f,
|
|||
int haselement;
|
||||
char *val;
|
||||
char *encstr = NULL; /* xml encoded string */
|
||||
int exist = 0;
|
||||
|
||||
if (x == NULL)
|
||||
goto ok;
|
||||
if (yang_extension_value(xml_spec(x), "hide-show", CLIXON_AUTOCLI_NS, &exist, NULL) < 0)
|
||||
goto done;
|
||||
if (exist)
|
||||
goto ok;
|
||||
name = xml_name(x);
|
||||
namespace = xml_prefix(x);
|
||||
switch(xml_type(x)){
|
||||
|
|
@ -195,48 +200,53 @@ xml2file_recurse(FILE *f,
|
|||
|
||||
/*! Print an XML tree structure to an output stream and encode chars "<>&"
|
||||
*
|
||||
* @param[in] f UNIX output stream
|
||||
* @param[in] xn clicon xml tree
|
||||
* @param[in] level how many spaces to insert before each line
|
||||
* @param[in] prettyprint insert \n and spaces tomake the xml more readable.
|
||||
* @see clicon_xml2cbuf print to a cbuf string
|
||||
* @see clicon_xml2cbuf_cb print using a callback
|
||||
* @param[in] f Output file
|
||||
* @param[in] xn XML tree
|
||||
* @param[in] level How many spaces to insert before each line
|
||||
* @param[in] pretty Insert \n and spaces tomake the xml more readable.
|
||||
* @param[in] fn File print function (if NULL, use fprintf)
|
||||
* @param[in] skiptop 0: Include top object 1: Skip top-object, only children,
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
* @see clixon_xml2cbuf print to a cbuf string
|
||||
*/
|
||||
int
|
||||
clicon_xml2file(FILE *f,
|
||||
cxobj *x,
|
||||
int level,
|
||||
int prettyprint)
|
||||
clixon_xml2file(FILE *f,
|
||||
cxobj *xn,
|
||||
int level,
|
||||
int pretty,
|
||||
clicon_output_cb *fn,
|
||||
int skiptop)
|
||||
{
|
||||
return xml2file_recurse(f, x, level, prettyprint, fprintf);
|
||||
}
|
||||
int retval = 1;
|
||||
cxobj *xc;
|
||||
|
||||
if (fn == NULL)
|
||||
fn = fprintf;
|
||||
if (skiptop){
|
||||
xc = NULL;
|
||||
while ((xc = xml_child_each(xn, xc, CX_ELMNT)) != NULL)
|
||||
if (xml2file_recurse(f, xc, level, pretty, fn) < 0)
|
||||
goto done;
|
||||
}
|
||||
else {
|
||||
if (xml2file_recurse(f, xn, level, pretty, fn) < 0)
|
||||
goto done;
|
||||
}
|
||||
retval = 0;
|
||||
done:
|
||||
return retval;
|
||||
|
||||
/*! Print an XML tree structure to an output stream and encode chars "<>&"
|
||||
*
|
||||
* @param[in] f UNIX output stream
|
||||
* @param[in] xn clicon xml tree
|
||||
* @param[in] level how many spaces to insert before each line
|
||||
* @param[in] prettyprint insert \n and spaces tomake the xml more readable.
|
||||
* @see clicon_xml2cbuf
|
||||
*/
|
||||
int
|
||||
clicon_xml2file_cb(FILE *f,
|
||||
cxobj *x,
|
||||
int level,
|
||||
int prettyprint,
|
||||
clicon_output_cb *fn)
|
||||
{
|
||||
return xml2file_recurse(f, x, level, prettyprint, fn);
|
||||
}
|
||||
|
||||
/*! Print an XML tree structure to an output stream
|
||||
*
|
||||
* Uses clicon_xml2file internally
|
||||
* Uses clixon_xml2file internally
|
||||
*
|
||||
* @param[in] f UNIX output stream
|
||||
* @param[in] xn clicon xml tree
|
||||
* @see clicon_xml2cbuf
|
||||
* @see clicon_xml2cbuf_cb print using a callback
|
||||
* @see clixon_xml2cbuf
|
||||
* @see clixon_xml2cbuf_cb print using a callback
|
||||
*/
|
||||
int
|
||||
xml_print(FILE *f,
|
||||
|
|
@ -245,6 +255,8 @@ xml_print(FILE *f,
|
|||
return xml2file_recurse(f, x, 0, 1, fprintf);
|
||||
}
|
||||
|
||||
/*! Dump cxobj structure with pointers and flags for debugging, internal function
|
||||
*/
|
||||
static int
|
||||
xml_dump1(FILE *f,
|
||||
cxobj *x,
|
||||
|
|
@ -297,7 +309,7 @@ xml_dump(FILE *f,
|
|||
* @param[in] depth Limit levels of child resources: -1 is all, 0 is none, 1 is node itself
|
||||
*/
|
||||
static int
|
||||
clicon_xml2cbuf1(cbuf *cb,
|
||||
clixon_xml2cbuf1(cbuf *cb,
|
||||
cxobj *x,
|
||||
int level,
|
||||
int prettyprint,
|
||||
|
|
@ -347,7 +359,7 @@ clicon_xml2cbuf1(cbuf *cb,
|
|||
while ((xc = xml_child_each(x, xc, -1)) != NULL)
|
||||
switch (xml_type(xc)){
|
||||
case CX_ATTR:
|
||||
if (clicon_xml2cbuf1(cb, xc, level+1, prettyprint, -1) < 0)
|
||||
if (clixon_xml2cbuf1(cb, xc, level+1, prettyprint, -1) < 0)
|
||||
goto done;
|
||||
break;
|
||||
case CX_BODY:
|
||||
|
|
@ -369,7 +381,7 @@ clicon_xml2cbuf1(cbuf *cb,
|
|||
xc = NULL;
|
||||
while ((xc = xml_child_each(x, xc, -1)) != NULL)
|
||||
if (xml_type(xc) != CX_ATTR)
|
||||
if (clicon_xml2cbuf1(cb, xc, level+1, prettyprint, depth-1) < 0)
|
||||
if (clixon_xml2cbuf1(cb, xc, level+1, prettyprint, depth-1) < 0)
|
||||
goto done;
|
||||
if (prettyprint && hasbody == 0)
|
||||
cprintf(cb, "%*s", level*XML_INDENT, "");
|
||||
|
|
@ -396,27 +408,26 @@ clicon_xml2cbuf1(cbuf *cb,
|
|||
/*! Print an XML tree structure to a cligen buffer and encode chars "<>&"
|
||||
*
|
||||
* @param[in,out] cb Cligen buffer to write to
|
||||
* @param[in] xt Top-level xml object
|
||||
* @param[in] xn Top-level xml object
|
||||
* @param[in] level Indentation level for prettyprint
|
||||
* @param[in] prettyprint insert \n and spaces tomake the xml more readable.
|
||||
* @param[in] depth Limit levels of child resources: -1: all, 0: none, 1: node itself
|
||||
* @param[in] skiptop 0: Include top object 1: Skip top-object, only children,
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
*
|
||||
* Depth is used in NACM
|
||||
* @code
|
||||
* cbuf *cb;
|
||||
* cb = cbuf_new();
|
||||
* if (clicon_xml2cbuf(cb, xn, 0, 1, -1, 0) < 0)
|
||||
* goto err;
|
||||
* fprintf(stderr, "%s", cbuf_get(cb));
|
||||
* cbuf_free(cb);
|
||||
* cbuf *cb = cbuf_new();
|
||||
* if (clixon_xml2cbuf(cb, xn, 0, 1, -1, 0) < 0)
|
||||
* goto err;
|
||||
* fprintf(stderr, "%s", cbuf_get(cb));
|
||||
* cbuf_free(cb);
|
||||
* @endcode
|
||||
* @see clicon_xml2file
|
||||
* @see clixon_xml2file
|
||||
*/
|
||||
int
|
||||
clicon_xml2cbuf(cbuf *cb,
|
||||
cxobj *xt,
|
||||
clixon_xml2cbuf(cbuf *cb,
|
||||
cxobj *xn,
|
||||
int level,
|
||||
int pretty,
|
||||
int32_t depth,
|
||||
|
|
@ -427,12 +438,12 @@ clicon_xml2cbuf(cbuf *cb,
|
|||
|
||||
if (skiptop){
|
||||
xc = NULL;
|
||||
while ((xc = xml_child_each(xt, xc, CX_ELMNT)) != NULL)
|
||||
if (clicon_xml2cbuf1(cb, xc, level, pretty, depth) < 0)
|
||||
while ((xc = xml_child_each(xn, xc, CX_ELMNT)) != NULL)
|
||||
if (clixon_xml2cbuf1(cb, xc, level, pretty, depth) < 0)
|
||||
goto done;
|
||||
}
|
||||
else {
|
||||
if (clicon_xml2cbuf1(cb, xt, level, pretty, depth) < 0)
|
||||
if (clixon_xml2cbuf1(cb, xn, level, pretty, depth) < 0)
|
||||
goto done;
|
||||
}
|
||||
retval = 0;
|
||||
|
|
@ -440,30 +451,6 @@ clicon_xml2cbuf(cbuf *cb,
|
|||
return retval;
|
||||
}
|
||||
|
||||
/*! Return an xml tree as a pretty-printed malloced string.
|
||||
* @param[in] x XML tree
|
||||
* @retval str Malloced pretty-printed string (should be free:d after use)
|
||||
* @retval NULL Error
|
||||
*/
|
||||
char *
|
||||
clicon_xml2str(cxobj *x)
|
||||
{
|
||||
cbuf *cb;
|
||||
char *str;
|
||||
|
||||
if ((cb = cbuf_new()) == NULL){
|
||||
clicon_err(OE_XML, errno, "cbuf_new");
|
||||
return NULL;
|
||||
}
|
||||
if (clicon_xml2cbuf(cb, x, 0, 1, -1, 0) < 0)
|
||||
return NULL;
|
||||
if ((str = strdup(cbuf_get(cb))) == NULL){
|
||||
clicon_err(OE_XML, errno, "strdup");
|
||||
return NULL;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
/*! Print actual xml tree datastructures (not xml), mainly for debugging
|
||||
* @param[in,out] cb Cligen buffer to write to
|
||||
* @param[in] xn Clicon xml tree
|
||||
|
|
|
|||
|
|
@ -368,6 +368,7 @@ clixon_xvec_rm_pos(clixon_xvec *xv,
|
|||
* @param[in] f UNIX output stream
|
||||
* @param[in] xv XML tree vector
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
*/
|
||||
int
|
||||
clixon_xvec_print(FILE *f,
|
||||
|
|
@ -376,7 +377,8 @@ clixon_xvec_print(FILE *f,
|
|||
int i;
|
||||
|
||||
for (i=0; i<xv->xv_len; i++)
|
||||
clicon_xml2file(f, xv->xv_vec[i], 0, 1);
|
||||
if (clixon_xml2file(f, xv->xv_vec[i], 0, 1, fprintf, 0) < 0)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue