• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- mode: C++ -*-
3 //
4 // Copyright (C) 2013-2020 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 
25 using sptr_utils::build_sptr;
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.
36 struct textReaderDeleter
37 {
38   void
operatortextReaderDeleter39   operator()(xmlTextReaderPtr reader)
40   {xmlFreeTextReader(reader);}
41 };
42 
43 /// This functor is used to instantiate a shared_ptr for xmlChar
44 struct charDeleter
45 {
46   void
operatorcharDeleter47   operator()(xmlChar* str)
48   { xmlFree(str); }
49 };
50 
51 reader_sptr new_reader_from_file(const std::string& path);
52 reader_sptr new_reader_from_buffer(const std::string& buffer);
53 reader_sptr new_reader_from_istream(std::istream*);
54 bool xml_char_sptr_to_string(xml_char_sptr, std::string&);
55 
56 int get_xml_node_depth(xmlNodePtr);
57 
58 /// Get the name of the current element node the reader is pointing
59 /// to.  Note that this macro returns an instance of
60 /// shared_ptr<xmlChar> so that the caller doesn't have to worry about
61 /// managing memory itself.  Also note that the reader is a
62 /// shared_ptr<xmlTextReader>
63 #define XML_READER_GET_NODE_NAME(reader) \
64   xml::build_sptr(xmlTextReaderName(reader.get()))
65 
66 /// Get the type of the current node of the shared_ptr<xmlTextReader>
67 /// passed in argument.
68 #define XML_READER_GET_NODE_TYPE(reader) \
69   static_cast<xmlReaderTypes> (xmlTextReaderNodeType(reader.get()))
70 
71 /// Get the value of attribute 'name' on the current node of 'reader'
72 /// which is an instance of shared_ptr<xmlTextReader>.
73 #define XML_READER_GET_ATTRIBUTE(reader, name) \
74   xml::build_sptr(xmlTextReaderGetAttribute(reader.get(), BAD_CAST(name)))
75 
76 /// Get the value of attribute 'name' ont the instance of xmlNodePtr
77 /// denoted by 'node'.
78 #define XML_NODE_GET_ATTRIBUTE(node, name) \
79   xml::build_sptr(xmlGetProp(node, BAD_CAST(name)))
80 
81 #define CHAR_STR(xml_char_str) \
82   reinterpret_cast<char*>(xml_char_str.get())
83 
84 void
85 escape_xml_string(const std::string& str,
86 		  std::string& escaped);
87 
88 std::string
89 escape_xml_string(const std::string& str);
90 
91 void
92 escape_xml_comment(const std::string& str,
93 		   std::string& escaped);
94 
95 std::string
96 escape_xml_comment(const std::string& str);
97 
98 void
99 unescape_xml_string(const std::string& str,
100 		    std::string& escaped);
101 
102 std::string
103 unescape_xml_string(const std::string& str);
104 
105 void
106 unescape_xml_comment(const std::string& str,
107 		     std::string& escaped);
108 
109 std::string
110 unescape_xml_comment(const std::string& str);
111 
112 }//end namespace xml
113 
114 namespace sptr_utils
115 {
116 /// Specialization of sptr_utils::build_sptr for xmlTextReader
117 template<>
118 xml::reader_sptr
119 build_sptr<xmlTextReader>(xmlTextReader *p);
120 
121 /// Specialization of build_str for xmlChar.
122 template<>
123 xml::xml_char_sptr
124 build_sptr<xmlChar>(xmlChar *p);
125 }// end namespace sptr_utils
126 
127 }//end namespace abigail
128 #endif //__ABG_LIBXML_UTILS_H__
129