libabigail
abg-libxml-utils.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- mode: C++ -*-
3 //
4 // Copyright (C) 2013-2025 Red Hat, Inc.
5 
6 /// @file
7 
8 #ifndef __ABG_LIBXML_UTILS_H__
9 #define __ABG_LIBXML_UTILS_H__
10 
11 #include <libxml/xmlreader.h>
12 
13 #include <istream>
14 #include <memory>
15 
16 #include "abg-sptr-utils.h"
17 
18 namespace abigail
19 {
20 
21 /// Internal namespace for xml manipulation utilities.
22 namespace xml
23 {
24 
26 using std::shared_ptr;
27 
28 /// A convenience typedef for a shared pointer of xmlTextReader.
29 typedef shared_ptr<xmlTextReader> reader_sptr;
30 
31 /// A convenience typedef for a shared pointer of xmlChar.
32 typedef shared_ptr<xmlChar> xml_char_sptr;
33 
34 /// This functor is used to instantiate a shared_ptr for the
35 /// xmlTextReader.
37 {
38  void
39  operator()(xmlTextReaderPtr reader)
40  {xmlFreeTextReader(reader);}
41 };
42 
43 /// This functor is used to instantiate a shared_ptr for xmlChar
45 {
46  void
47  operator()(xmlChar* str)
48  { xmlFree(str); }
49 };
50 
51 void initialize();
52 reader_sptr new_reader_from_file(const std::string& path);
53 reader_sptr new_reader_from_buffer(const std::string& buffer);
54 reader_sptr new_reader_from_istream(std::istream*);
55 bool xml_char_sptr_to_string(xml_char_sptr&, std::string&);
56 
57 int get_xml_node_depth(xmlNodePtr);
58 
59 /// Get the name of the current element node the reader is pointing
60 /// to. Note that this macro returns an instance of
61 /// shared_ptr<xmlChar> so that the caller doesn't have to worry about
62 /// managing memory itself. Also note that the reader is a
63 /// shared_ptr<xmlTextReader>
64 #define XML_READER_GET_NODE_NAME(reader) \
65  xml::build_sptr(xmlTextReaderName(reader.get()))
66 
67 /// Get the type of the current node of the shared_ptr<xmlTextReader>
68 /// passed in argument.
69 #define XML_READER_GET_NODE_TYPE(reader) \
70  static_cast<xmlReaderTypes> (xmlTextReaderNodeType(reader.get()))
71 
72 /// Get the value of attribute 'name' on the current node of 'reader'
73 /// which is an instance of shared_ptr<xmlTextReader>.
74 #define XML_READER_GET_ATTRIBUTE(reader, name) \
75  xml::build_sptr(xmlTextReaderGetAttribute(reader.get(), BAD_CAST(name)))
76 
77 /// Get the value of attribute 'name' ont the instance of xmlNodePtr
78 /// denoted by 'node'.
79 #define XML_NODE_GET_ATTRIBUTE(node, name) \
80  xml::build_sptr(xmlGetProp(node, BAD_CAST(name)))
81 
82 #define CHAR_STR(xml_char_str) \
83  reinterpret_cast<char*>(xml_char_str.get())
84 
85 void
86 escape_xml_string(const std::string& str,
87  std::string& escaped);
88 
89 std::string
90 escape_xml_string(const std::string& str);
91 
92 void
93 escape_xml_comment(const std::string& str,
94  std::string& escaped);
95 
96 std::string
97 escape_xml_comment(const std::string& str);
98 
99 void
100 unescape_xml_string(const std::string& str,
101  std::string& escaped);
102 
103 std::string
104 unescape_xml_string(const std::string& str);
105 
106 void
107 unescape_xml_comment(const std::string& str,
108  std::string& escaped);
109 
110 std::string
111 unescape_xml_comment(const std::string& str);
112 
113 }//end namespace xml
114 
115 namespace sptr_utils
116 {
117 /// Specialization of sptr_utils::build_sptr for xmlTextReader
118 template<>
120 build_sptr<xmlTextReader>(xmlTextReader *p);
121 
122 /// Specialization of build_str for xmlChar.
123 template<>
125 build_sptr<xmlChar>(xmlChar *p);
126 }// end namespace sptr_utils
127 
128 }//end namespace abigail
129 #endif //__ABG_LIBXML_UTILS_H__
shared_ptr< T > build_sptr(T *p)
This is to be specialized for the diverse C types that needs wrapping in shared_ptr.
reader_sptr new_reader_from_istream(std::istream *in)
Instanciate an xmlTextReader that parses a content coming from an input stream.
void unescape_xml_comment(const std::string &str, std::string &escaped)
Read a string, detect the '#&45;' entity and un-escape it into the '-' character. ...
shared_ptr< xmlChar > xml_char_sptr
A convenience typedef for a shared pointer of xmlChar.
Utilities to ease the wrapping of C types into std::shared_ptr.
bool xml_char_sptr_to_string(xml_char_sptr &ssptr, std::string &s)
Convert a shared pointer to xmlChar into an std::string.
reader_sptr new_reader_from_buffer(const std::string &buffer)
Instanciate an xmlTextReader that parses the content of an in-memory buffer, wrap it into a smart poi...
shared_ptr< xmlTextReader > build_sptr< xmlTextReader >(::xmlTextReader *p)
Build and return a shared_ptr for a pointer to xmlTextReader.
shared_ptr< xmlChar > build_sptr< xmlChar >(xmlChar *p)
Build and return a shared_ptr for a pointer to xmlChar.
shared_ptr< xmlTextReader > reader_sptr
A convenience typedef for a shared pointer of xmlTextReader.
This functor is used to instantiate a shared_ptr for the xmlTextReader.
Toplevel namespace for libabigail.
void escape_xml_comment(const std::string &str, std::string &escaped)
Escape the '-' character, to avoid having a '–' in a comment.
void unescape_xml_string(const std::string &str, std::string &escaped)
Read a string, detect the 5 predefined XML entities it may contain and un-escape them, by writting their corresponding characters back in. The pre-defined entities are:
void initialize()
The initialization function of libxml2 abstraction layer. This function must be called prior to using...
void escape_xml_string(const std::string &str, std::string &escaped)
Escape the 5 characters representing the predefined XML entities.
int get_xml_node_depth(xmlNodePtr n)
Return the depth of an xml element node.
reader_sptr new_reader_from_file(const std::string &path)
Instantiate an xmlTextReader that parses the content of an on-disk file, wrap it into a smart pointer...
This functor is used to instantiate a shared_ptr for xmlChar.