2 #include <libxml/xmlerror.h>
3 #include <libxml/parser.h>
9 exception::exception(const Glib::ustring& message)
14 exception::~exception() noexcept
17 const char* exception::what() const noexcept
19 return message_.c_str();
22 void exception::raise() const
27 exception* exception::clone() const
29 return new exception(*this);
32 Glib::ustring format_xml_error(const _xmlError* error)
35 error = xmlGetLastError();
37 if (!error || error->code == XML_ERR_OK)
38 return ""; // No error
42 if (error->file && *error->file != '\0')
50 str += (str.empty() ? "Line " : ", line ") + Glib::ustring::format(error->line);
52 str += ", column " + Glib::ustring::format(error->int2);
55 const bool two_lines = !str.empty();
75 str += two_lines ? '\n' : ' ';
77 if (error->message && *error->message != '\0')
78 str += error->message;
80 str += "Error code " + Glib::ustring::format(error->code);
82 // If the string does not end with end-of-line, append an end-of-line.
83 if (*str.rbegin() != '\n')
89 Glib::ustring format_xml_parser_error(const _xmlParserCtxt* parser_context)
92 return "Error. xmlpp::format_xml_parser_error() called with parser_context == nullptr\n";
94 const auto error = xmlCtxtGetLastError(const_cast<_xmlParserCtxt*>(parser_context));
97 return ""; // No error
101 if (!parser_context->wellFormed)
102 str += "Document not well-formed.\n";
104 return str + format_xml_error(error);
107 Glib::ustring format_printf_message(const char* fmt, va_list args)
109 // This code was inspired by the example at
110 // http://en.cppreference.com/w/cpp/io/c/vfprintf
112 va_copy(args2, args);
113 // Number of characters (bytes) in the resulting string;
115 const int nchar = std::vsnprintf(nullptr, 0, fmt, args2);
118 return Glib::ustring::format("Error code from std::vsnprintf = ", nchar);
120 std::vector<char> buf(nchar+1);
121 std::vsnprintf(buf.data(), buf.size(), fmt, args);
122 return Glib::ustring(buf.data());