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:
Olof hagsand 2022-06-01 20:02:27 +02:00
parent 87719c623c
commit 0c79298e76
49 changed files with 421 additions and 503 deletions

View file

@ -206,7 +206,8 @@ main(int argc, char **argv)
xpath = "/";
if (xmldb_get(h, db, NULL, xpath, &xt) < 0)
goto done;
clicon_xml2file(stdout, xt, 0, 0);
if (clixon_xml2file(stdout, xt, 0, 0, fprintf, 0) < 0)
goto done;
fprintf(stdout, "\n");
if (xt){
xml_free(xt);
@ -229,7 +230,8 @@ main(int argc, char **argv)
clicon_err(OE_DB, 0, "xt is NULL");
goto done;
}
clicon_xml2file(stdout, xt, 0, 0);
if (clixon_xml2file(stdout, xt, 0, 0, fprintf, 0) < 0)
goto done;
if (xt){
xml_free(xt);
xt = NULL;

View file

@ -143,10 +143,12 @@ main(int argc,
xml_print(stderr, xerr);
goto done;
}
if (json)
xml2json_cbuf(cb, xt, pretty, 1); /* print json */
else
clicon_xml2cbuf(cb, xt, 0, pretty, -1, 1); /* print xml */
if (json){
if (clixon_json2cbuf(cb, xt, pretty, 1) < 0)
goto done;
}
else if (clixon_xml2cbuf(cb, xt, 0, pretty, -1, 1) < 0)
goto done;
fprintf(stdout, "%s", cbuf_get(cb));
fflush(stdout);
retval = 0;

View file

@ -281,7 +281,7 @@ main(int argc,
for (i = 0; i < xlen; i++){
xc = xvec[i];
fprintf(stdout, "%d: ", i);
clicon_xml2file(stdout, xc, 0, 0);
clixon_xml2file(stdout, xc, 0, 0, fprintf, 0);
fprintf(stdout, "\n");
fflush(stdout);
}

View file

@ -167,7 +167,7 @@ main(int argc,
fprintf(stderr, "No xml\n");
goto done;
}
if (clicon_xml2cbuf(cb, xc, 0, 0, -1, 0) < 0)
if (clixon_xml2cbuf(cb, xc, 0, 0, -1, 0) < 0)
goto done;
if ((msg = clicon_msg_encode(getpid(), "%s", cbuf_get(cb))) < 0)
goto done;

View file

@ -83,7 +83,6 @@ main(int argc,
{
int retval = -1;
cxobj *xt = NULL;
cxobj *xc;
cbuf *cb = cbuf_new();
int c;
int logdst = CLICON_LOG_STDERR;
@ -147,14 +146,14 @@ main(int argc,
xml_print(stderr, xerr);
goto done;
}
xc = NULL;
while ((xc = xml_child_each(xt, xc, -1)) != NULL){
if (text_syntax_output)
xml2txt(xc, fprintf, stdout, 0);
else{
clicon_xml2cbuf(cb, xc, 0, 1, -1); /* print xml */
fprintf(stdout, "%s", cbuf_get(cb));
}
if (text_syntax_output){
if (clixon_txt2file(stdout, xt, 0, fprintf, 1) < 0)
goto done;
}
else{
if (clixon_xml2cbuf(cb, xt, 0, 1, -1, 1) < 0)
goto done;
fprintf(stdout, "%s", cbuf_get(cb));
}
fflush(stdout);
retval = 0;

View file

@ -334,16 +334,15 @@ main(int argc,
/* 4. Output data (xml/json/text) */
if (output){
if (textout){
xc = NULL;
while ((xc = xml_child_each(xt, xc, -1)) != NULL){
if (xml2txt(xc, fprintf, stdout, 0) < 0)
goto done;
}
if (clixon_txt2file(stdout, xt, 0, fprintf, 1) < 0)
goto done;
}
else if (jsonout)
xml2json_cbuf(cb, xt, pretty, 1); /* print json */
else
clicon_xml2cbuf(cb, xt, 0, pretty, -1, 1); /* print xml */
else if (jsonout){
if (clixon_json2cbuf(cb, xt, pretty, 1) < 0)
goto done;
}
else if (clixon_xml2cbuf(cb, xt, 0, pretty, -1, 1) < 0)
goto done;
fprintf(stdout, "%s", cbuf_get(cb));
fflush(stdout);
}

View file

@ -281,10 +281,14 @@ main(int argc, char **argv)
}
if (sort)
xml_sort_recurse(xb);
if (strcmp(xml_name(xb),"top")==0)
clicon_xml2file(stdout, xml_child_i_type(xb, 0, CX_ELMNT), 0, 0);
else
clicon_xml2file(stdout, xb, 0, 0);
if (strcmp(xml_name(xb),"top")==0){
if (clixon_xml2file(stdout, xb, 0, 0, fprintf, 1) < 0)
goto done;
}
else{
if (clixon_xml2file(stdout, xb, 0, 0, fprintf, 0) < 0)
goto done;
}
fprintf(stdout, "\n");
retval = 0;
done:

View file

@ -91,14 +91,16 @@ static int
ctx_print2(cbuf *cb,
xp_ctx *xc)
{
int i;
int retval = -1;
int i;
cprintf(cb, "%s:", (char*)clicon_int2str(ctxmap, xc->xc_type));
switch (xc->xc_type){
case XT_NODESET:
for (i=0; i<xc->xc_size; i++){
cprintf(cb, "%d:", i);
clicon_xml2cbuf(cb, xc->xc_nodeset[i], 0, 0, -1, 0);
if (clixon_xml2cbuf(cb, xc->xc_nodeset[i], 0, 0, -1, 0) < 0)
goto done;
}
break;
case XT_BOOL:
@ -111,7 +113,9 @@ ctx_print2(cbuf *cb,
cprintf(cb, "%s", xc->xc_string);
break;
}
return 0;
retval = 0;
done:
return retval;
}
int