NCBI C++ ToolKit
deref_impl.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /*
2  * Redistribution and use in source and binary forms, with or without
3  * modification, are permitted provided that the following conditions
4  * are met:
5  *
6  * 1. Redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer.
8  * 2. Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in
10  * the documentation and/or other materials provided with the
11  * distribution.
12  * 3. Neither the name of the Author nor the names of its contributors
13  * may be used to endorse or promote products derived from this software
14  * without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
19  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
20  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * $Id: deref_impl.cpp 92635 2021-02-02 14:31:55Z ivanov $
32  */
33 
34 /** @file
35  * This file contains definitions required for iterators dereferencing
36  * support.
37 **/
38 
39 
41 #include "deref_impl.hpp"
42 
43 // libxml2 includes
44 #include <libxml/tree.h>
45 
46 #include <common/ncbi_sanitizers.h>
47 
48 namespace xml
49 {
50  namespace impl
51  {
52  void cleanup_node(xmlNodePtr xmlnode)
53  {
54  // XMLWrapp attaches private data to nodes only while libxml2 calls
55  // this handler for documents and dtds as well
56  if (xmlnode->type == XML_DOCUMENT_NODE ||
57  xmlnode->type == XML_DTD_NODE)
58  return;
59 
60  if (xmlnode->_private == NULL)
61  return;
62 
63  node_private_data * node_data = static_cast<node_private_data *>
64  (xmlnode->_private);
65 
66  // Clean the phantom attributes
67  {{
68  phantom_attr * current = node_data->phantom_attrs_;
70  while (current != NULL)
71  {
72  next = current->next;
73  delete current;
74  current = next;
75  }
76  }}
77 
78  // Clean the attributes instances
79  {{
80  attr_instance * current = node_data->attr_instances_;
82  while (current != NULL)
83  {
84  next = current->next;
85  delete current;
86  current = next;
87  }
88  }}
89 
90  delete node_data;
91  xmlnode->_private = NULL;
92  }
93 
94  // The argument is actually of type xmlNodePtr
96  {
97  if (xmlnode == NULL)
98  throw xml::exception("Dereferencing non-initialized iterator");
99 
100  xmlNodePtr raw_node = static_cast<xmlNodePtr>(xmlnode);
101 
102  NCBI_LSAN_DISABLE_GUARD; // for 'data'
103 
104  if (raw_node->_private == NULL) {
106 
107  data->phantom_attrs_ = NULL;
108  data->attr_instances_ = NULL;
109  data->node_instance_.set_node_data(raw_node);
110  raw_node->_private = data;
111  } else {
112  node_private_data * data = static_cast<node_private_data*>
113  (raw_node->_private);
114  data->node_instance_.set_node_data(raw_node);
115  }
116  return static_cast<node_private_data*>(raw_node->_private);
117  }
118 
119  void * get_ptr_to_attr_instance(void * att)
120  {
121  attributes::attr * att_ptr = static_cast<attributes::attr *>(att);
123  att_ptr->get_node());
124  attr_instance * current = node_data->attr_instances_;
125 
126  while (current != NULL)
127  {
128  if (*att_ptr == current->attr_)
129  return & current->attr_;
130  current = current->next;
131  }
132 
133  // Not found, so insert a new node to the head
134  attr_instance * new_attr_instance = new attr_instance(*att_ptr);
135 
136  new_attr_instance->next = node_data->attr_instances_;
137  node_data->attr_instances_ = new_attr_instance;
138  return & new_attr_instance->attr_;
139  }
140 
141  void invalidate_default_attr_iterators(xmlNodePtr xmlnode)
142  {
143  if (xmlnode == NULL)
144  return;
145  if (xmlnode->_private == NULL)
146  return;
147 
148  node_private_data * data = static_cast<node_private_data*>
149  (xmlnode->_private);
150  // Invalidate default attribute pointers
151  phantom_attr * current = data->phantom_attrs_;
152  while (current != NULL)
153  {
154  current->def_prop_ = NULL;
155  current = current->next;
156  }
157 
158  xmlnode = xmlnode->children;
159  while (xmlnode)
160  {
162  xmlnode = xmlnode->next;
163  }
164  }
165 
166  } // namespace impl
167 } // namespace xml
168 
The xml::attributes::attr class is used to hold information about one attribute.
Definition: attributes.hpp:151
void * get_node(void) const
Definition: ait_impl.cpp:320
This exception class is thrown by xmlwrapp for all runtime XML-related errors along with the xml::par...
Definition: exception.hpp:64
This file contains declarations required for iterators dereferencing support.
static DLIST_TYPE *DLIST_NAME() next(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
Definition: dlist.tmpl.h:56
char data[12]
Definition: iconv.c:80
#define NULL
Definition: ncbistd.hpp:225
This file contains the definition of the xml::exception class.
void * get_ptr_to_attr_instance(void *)
Definition: deref_impl.cpp:119
void cleanup_node(xmlNodePtr xmlnode)
Definition: deref_impl.cpp:52
void invalidate_default_attr_iterators(xmlNodePtr xmlnode)
Definition: deref_impl.cpp:141
node_private_data * attach_node_private_data(void *)
Definition: deref_impl.cpp:95
XML library namespace.
Definition: attributes.hpp:57
Common macro to detect used sanitizers and suppress memory leaks if run under LeakSanitizer.
#define NCBI_LSAN_DISABLE_GUARD
struct attr_instance * next
Definition: deref_impl.hpp:78
xml::attributes::attr attr_
Definition: deref_impl.hpp:77
struct attr_instance * attr_instances_
Definition: deref_impl.hpp:89
struct phantom_attr * phantom_attrs_
Definition: deref_impl.hpp:88
xmlAttributePtr def_prop_
Definition: deref_impl.hpp:65
struct phantom_attr * next
Definition: deref_impl.hpp:67
Modified on Mon Jun 17 05:09:29 2024 by modify_doxy.py rev. 669887