Debian Codegraph @

src/c_helpers.rs

raw symbols SCIP
1 #![allow(non_camel_case_types)]
2 #![allow(non_snake_case)]
3
4 use crate::bindings::*;
5 use libc::{c_char, c_int, size_t};
6 use std::os::raw::c_void;
7 use std::ptr;
8 use std::slice;
9 // error handling functions
10 // pub fn xmlSetGenericErrorFunc(ctx: *mut c_void, handler: *mut c_void);
11 // pub fn xmlThrDefSetGenericErrorFunc(ctx: *mut c_void, handler: *mut c_void);
12
13 // Taken from Nokogiri (https://github.com/sparklemotion/nokogiri/blob/24bb843327306d2d71e4b2dc337c1e327cbf4516/ext/nokogiri/xml_document.c#L64)
14 pub fn xmlNodeRecursivelyRemoveNs(node: xmlNodePtr) {
15 unsafe {
16 let mut property: xmlAttrPtr;
17
18 xmlSetNs(node, ptr::null_mut());
19 let mut child: xmlNodePtr = (*node).children;
20 while !child.is_null() {
21 xmlNodeRecursivelyRemoveNs(child);
22 child = (*child).next;
23 }
24
25 if (((*node).type_ == xmlElementType_XML_ELEMENT_NODE)
26 || ((*node).type_ == xmlElementType_XML_XINCLUDE_START)
27 || ((*node).type_ == xmlElementType_XML_XINCLUDE_END))
28 && !(*node).nsDef.is_null()
29 {
30 xmlFreeNsList((*node).nsDef);
31 (*node).nsDef = ptr::null_mut();
32 }
33
34 if (*node).type_ == xmlElementType_XML_ELEMENT_NODE && !(*node).properties.is_null() {
35 property = (*node).properties;
36 while !property.is_null() {
37 if !(*property).ns.is_null() {
38 (*property).ns = ptr::null_mut();
39 }
40 property = (*property).next;
41 }
42 }
43 }
44 }
45 pub fn xmlGetDoc(cur: xmlNodePtr) -> xmlDocPtr {
46 unsafe { (*cur).doc }
47 }
48 pub fn xmlNextNsSibling(ns: xmlNsPtr) -> xmlNsPtr {
49 unsafe { (*ns).next }
50 }
51 pub fn xmlNsPrefix(ns: xmlNsPtr) -> *const c_char {
52 unsafe { (*ns).prefix as *const c_char }
53 }
54 pub fn xmlNsHref(ns: xmlNsPtr) -> *const c_char {
55 unsafe { (*ns).href as *const c_char }
56 }
57 pub fn xmlNodeNsDeclarations(cur: xmlNodePtr) -> xmlNsPtr {
58 unsafe { (*cur).nsDef }
59 }
60 pub fn xmlNodeNs(cur: xmlNodePtr) -> xmlNsPtr {
61 unsafe { (*cur).ns }
62 }
63
64 pub fn xmlNextPropertySibling(attr: xmlAttrPtr) -> xmlAttrPtr {
65 unsafe { (*attr).next }
66 }
67 pub fn xmlAttrName(attr: xmlAttrPtr) -> *const c_char {
68 unsafe { (*attr).name as *const c_char }
69 }
70 pub fn xmlAttrNs(attr: xmlAttrPtr) -> xmlNsPtr {
71 unsafe { (*attr).ns }
72 }
73 pub fn xmlGetFirstProperty(node: xmlNodePtr) -> xmlAttrPtr {
74 unsafe { (*node).properties }
75 }
76 pub fn xmlGetNodeType(cur: xmlNodePtr) -> xmlElementType {
77 unsafe { (*cur).type_ }
78 }
79
80 pub fn xmlGetParent(cur: xmlNodePtr) -> xmlNodePtr {
81 unsafe { (*cur).parent }
82 }
83 pub fn xmlGetFirstChild(cur: xmlNodePtr) -> xmlNodePtr {
84 unsafe { (*cur).children }
85 }
86 pub fn xmlPrevSibling(cur: xmlNodePtr) -> xmlNodePtr {
87 unsafe { (*cur).prev }
88 }
89
90 // helper for tree
91 pub fn xmlNextSibling(cur: xmlNodePtr) -> xmlNodePtr {
92 unsafe { (*cur).next }
93 }
94
95 pub fn xmlNodeGetName(cur: xmlNodePtr) -> *const c_char {
96 unsafe { (*cur).name as *const c_char }
97 }
98
99 // dummy function: no debug output at all
100 #[cfg(libxml_older_than_2_12)]
101 unsafe extern "C" fn _ignoreInvalidTagsErrorFunc(_user_data: *mut c_void, error: xmlErrorPtr) {
102 unsafe {
103 if !error.is_null() && (*error).code as xmlParserErrors == xmlParserErrors_XML_HTML_UNKNOWN_TAG {
104 // do not record invalid, in fact (out of despair) claim we ARE well-formed, when a tag is invalid.
105 HACKY_WELL_FORMED = true;
106 }
107 }
108 }
109 #[cfg(not(libxml_older_than_2_12))]
110 unsafe extern "C" fn _ignoreInvalidTagsErrorFunc(_user_data: *mut c_void, error: *const xmlError) {
111 unsafe {
112 if !error.is_null() && (*error).code as xmlParserErrors == xmlParserErrors_XML_HTML_UNKNOWN_TAG {
113 // do not record invalid, in fact (out of despair) claim we ARE well-formed, when a tag is invalid.
114 HACKY_WELL_FORMED = true;
115 }
116 }
117 }
118
119 pub fn setWellFormednessHandler(ctxt: *mut xmlParserCtxt) {
120 unsafe {
121 HACKY_WELL_FORMED = false;
122 xmlSetStructuredErrorFunc(ctxt as *mut c_void, Some(_ignoreInvalidTagsErrorFunc));
123 }
124 }
125 // helper for parser
126 pub fn htmlWellFormed(ctxt: *mut xmlParserCtxt) -> bool {
127 unsafe { (!ctxt.is_null() && (*ctxt).wellFormed > 0) || HACKY_WELL_FORMED }
128 }
129
130 // helper for xpath
131 pub fn xmlXPathObjectNumberOfNodes(val: xmlXPathObjectPtr) -> c_int {
132 unsafe {
133 if val.is_null() {
134 -1
135 } else if (*val).nodesetval.is_null() {
136 -2
137 } else {
138 (*(*val).nodesetval).nodeNr
139 }
140 }
141 }
142
143 pub fn xmlXPathObjectGetNodes(val: xmlXPathObjectPtr, size: size_t) -> Vec<xmlNodePtr> {
144 unsafe { slice::from_raw_parts((*(*val).nodesetval).nodeTab, size).to_vec() }
145 }
146
147 #[cfg(any(target_family = "unix", target_os = "macos", all(target_family="windows", target_env="gnu")))]
148 pub fn bindgenFree(val: *mut c_void) {
149 unsafe {
150 if let Some(xml_free_fn) = xmlFree {
151 xml_free_fn(val);
152 } else {
153 libc::free(val);
154 }
155 }
156 }
157 #[cfg(all(target_family="windows", target_env="msvc"))]
158 pub fn bindgenFree(val: *mut c_void) {
159 unsafe { libc::free(val as *mut c_void); }
160 }