X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=tests%2Futils%2Fxml-utils%2Fextract_xml.cpp;h=9bfd50bda83d8e8ed829e2a7277eef2ae419cc28;hb=HEAD;hp=bc5fc3e51990bd5b46cce987e71b686619f9875e;hpb=4b2b86f24a5f734f6a6564b2b1e7ac6f10e32dfa;p=lttng-tools.git diff --git a/tests/utils/xml-utils/extract_xml.cpp b/tests/utils/xml-utils/extract_xml.cpp index bc5fc3e51..280f2ed02 100644 --- a/tests/utils/xml-utils/extract_xml.cpp +++ b/tests/utils/xml-utils/extract_xml.cpp @@ -24,17 +24,21 @@ * node;b; * node;c; */ +#include "common.hpp" + +#include + +#include +#include +#include +#include #include #include #include #include #include -#include -#include -#include -#include -#include +namespace ll = lttng::libxml; #if defined(LIBXML_XPATH_ENABLED) @@ -56,7 +60,7 @@ static int print_xpath_nodes(xmlDocPtr doc, xmlNodeSetPtr nodes, FILE *output) int i; xmlNodePtr cur; - xmlChar *node_child_value_string = NULL; + xmlChar *node_child_value_string = nullptr; LTTNG_ASSERT(output); size = (nodes) ? nodes->nodeNr : 0; @@ -65,8 +69,9 @@ static int print_xpath_nodes(xmlDocPtr doc, xmlNodeSetPtr nodes, FILE *output) LTTNG_ASSERT(nodes->nodeTab[i]); if (nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) { - fprintf(stderr, "ERR:%s\n", - "This executable does not support xml namespacing\n"); + fprintf(stderr, + "ERR:%s\n", + "This executable does not support xml namespacing\n"); ret = -1; goto end; } else if (nodes->nodeTab[i]->type == XML_ELEMENT_NODE) { @@ -74,16 +79,17 @@ static int print_xpath_nodes(xmlDocPtr doc, xmlNodeSetPtr nodes, FILE *output) if (xmlChildElementCount(cur) == 0) { if (xmlNodeIsText(cur->children)) { - node_child_value_string = xmlNodeListGetString(doc, - cur->children, 1); + node_child_value_string = + xmlNodeListGetString(doc, cur->children, 1); if (node_exist) { result = true; } else if (opt_verbose) { - fprintf(output, "%s;%s;\n", cur->name, - node_child_value_string); + fprintf(output, + "%s;%s;\n", + cur->name, + node_child_value_string); } else { - fprintf(output, "%s\n", - node_child_value_string); + fprintf(output, "%s\n", node_child_value_string); } xmlFree(node_child_value_string); } else { @@ -91,8 +97,9 @@ static int print_xpath_nodes(xmlDocPtr doc, xmlNodeSetPtr nodes, FILE *output) if (node_exist) { result = true; } else { - fprintf(stderr, "ERR:%s\n", - "Xpath expression return non-final xml element"); + fprintf(stderr, + "ERR:%s\n", + "Xpath expression return non-final xml element"); ret = -1; goto end; } @@ -102,8 +109,9 @@ static int print_xpath_nodes(xmlDocPtr doc, xmlNodeSetPtr nodes, FILE *output) result = true; } else { /* We don't want to print non-final element */ - fprintf(stderr, "ERR:%s\n", - "Xpath expression return non-final xml element"); + fprintf(stderr, + "ERR:%s\n", + "Xpath expression return non-final xml element"); ret = -1; goto end; } @@ -131,7 +139,7 @@ static int register_lttng_namespace(xmlXPathContextPtr xpathCtx) { int ret; xmlChar *prefix; - xmlChar *ns = NULL; + xmlChar *ns = nullptr; prefix = xmlCharStrdup("lttng"); if (!prefix) { @@ -165,15 +173,22 @@ end: static int extract_xpath(const char *xml_path, const xmlChar *xpath) { int ret; - xmlDocPtr doc = NULL; - xmlXPathContextPtr xpathCtx = NULL; - xmlXPathObjectPtr xpathObj = NULL; + xmlDocPtr doc = nullptr; + xmlXPathContextPtr xpathCtx = nullptr; + xmlXPathObjectPtr xpathObj = nullptr; LTTNG_ASSERT(xml_path); LTTNG_ASSERT(xpath); + ll::parser_ctx_uptr parserCtx{ xmlNewParserCtxt() }; + + if (!parserCtx) { + fprintf(stderr, "ERR: could not allocate an XML parser context\n"); + return -1; + } + /* Parse the xml file */ - doc = xmlParseFile(xml_path); + doc = xmlCtxtReadFile(parserCtx.get(), xml_path, nullptr, XML_PARSE_NOBLANKS); if (!doc) { fprintf(stderr, "ERR parsing: xml file invalid \"%s\"\n", xml_path); return -1; @@ -249,13 +264,12 @@ int main(int argc, char **argv) /* Init libxml */ xmlInitParser(); - xmlKeepBlanksDefault(0); if (access(argv[optind], F_OK)) { fprintf(stderr, "ERR:%s\n", "Xml path not valid"); return -1; } /* Do the main job */ - if (extract_xpath(argv[optind], (xmlChar *)argv[optind+1])) { + if (extract_xpath(argv[optind], (xmlChar *) argv[optind + 1])) { return -1; }