# SPDX-License-Identifier: GPL-2.0-only
+EXTRA_DIST = common.hpp
+
noinst_PROGRAMS = validate_xml extract_xml pretty_xml
validate_xml_SOURCES = validate_xml.cpp
validate_xml_CPPFLAGS = $(libxml2_CFLAGS) $(AM_CPPFLAGS)
--- /dev/null
+/*
+ * Copyright (C) 2024 EfficiOS Inc.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#ifndef TESTS_UTILS_XML_UTILS_COMMON_HPP
+#define TESTS_UTILS_XML_UTILS_COMMON_HPP
+
+#include "common/make-unique-wrapper.hpp"
+
+#include <libxml/parser.h>
+#include <memory>
+
+using xml_parser_ctx_uptr = std::unique_ptr<
+ xmlParserCtxt,
+ lttng::memory::create_deleter_class<xmlParserCtxt, xmlFreeParserCtxt>::deleter>;
+
+#endif /* TESTS_UTILS_XML_UTILS_COMMON_HPP */
* node;b;
* node;c;
*/
+#include "common.hpp"
+
#include <common/defaults.hpp>
#include <libxml/parser.h>
LTTNG_ASSERT(xml_path);
LTTNG_ASSERT(xpath);
+ xml_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;
/* Init libxml */
xmlInitParser();
- xmlKeepBlanksDefault(0);
if (access(argv[optind], F_OK)) {
fprintf(stderr, "ERR:%s\n", "Xml path not valid");
return -1;
* This allows a more human friendly format for xml testing when problems occur.
*/
+#include "common.hpp"
+
#include <libxml/parser.h>
+#include <unistd.h>
-int main(void)
+int main()
{
xmlDocPtr doc = NULL;
/* Init libxml. */
xmlInitParser();
- xmlKeepBlanksDefault(0);
- /* Parse the XML document from stdin. */
- doc = xmlParseFile("-");
- if (!doc) {
- fprintf(stderr, "ERR parsing: xml input invalid");
- return -1;
- }
+ {
+ xml_parser_ctx_uptr parserCtx{ xmlNewParserCtxt() };
+
+ /* Parse the XML document from stdin. */
+ doc = xmlCtxtReadFd(
+ parserCtx.get(), STDIN_FILENO, nullptr, nullptr, XML_PARSE_NOBLANKS);
+ if (!doc) {
+ fprintf(stderr, "ERR parsing: xml input invalid");
+ return -1;
+ }
- xmlDocFormatDump(stdout, doc, 1);
+ xmlDocFormatDump(stdout, doc, 1);
+
+ xmlFreeDoc(doc);
+ }
- xmlFreeDoc(doc);
/* Shutdown libxml. */
xmlCleanupParser();