/
nokogiri.c
141 lines (126 loc) · 4.2 KB
/
nokogiri.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <nokogiri.h>
VALUE mNokogiri ;
VALUE mNokogiriXml ;
VALUE mNokogiriHtml ;
VALUE mNokogiriXslt ;
VALUE mNokogiriXmlSax ;
VALUE mNokogiriHtmlSax ;
#ifdef USE_INCLUDED_VASPRINTF
/*
* I srsly hate windows. it doesn't have vasprintf.
* Thank you Geoffroy Couprie for this implementation of vasprintf!
*/
int vasprintf (char **strp, const char *fmt, va_list ap)
{
/* Mingw32/64 have a broken vsnprintf implementation that fails when
* using a zero-byte limit in order to retrieve the required size for malloc.
* So we use a one byte buffer instead.
*/
char tmp[1];
int len = vsnprintf (tmp, 1, fmt, ap) + 1;
char *res = (char *)malloc((unsigned int)len);
if (res == NULL)
return -1;
*strp = res;
return vsnprintf(res, (unsigned int)len, fmt, ap);
}
#endif
void vasprintf_free (void *p)
{
free(p);
}
#ifdef HAVE_RUBY_UTIL_H
#include "ruby/util.h"
#else
#include "util.h"
#endif
void nokogiri_root_node(xmlNodePtr node)
{
xmlDocPtr doc;
nokogiriTuplePtr tuple;
doc = node->doc;
if (doc->type == XML_DOCUMENT_FRAG_NODE) doc = doc->doc;
tuple = (nokogiriTuplePtr)doc->_private;
st_insert(tuple->unlinkedNodes, (st_data_t)node, (st_data_t)node);
}
void nokogiri_root_nsdef(xmlNsPtr ns, xmlDocPtr doc)
{
nokogiriTuplePtr tuple;
if (doc->type == XML_DOCUMENT_FRAG_NODE) doc = doc->doc;
tuple = (nokogiriTuplePtr)doc->_private;
st_insert(tuple->unlinkedNodes, (st_data_t)ns, (st_data_t)ns);
}
void Init_nokogiri()
{
xmlMemSetup(
(xmlFreeFunc)ruby_xfree,
(xmlMallocFunc)ruby_xmalloc,
(xmlReallocFunc)ruby_xrealloc,
ruby_strdup
);
mNokogiri = rb_define_module("Nokogiri");
mNokogiriXml = rb_define_module_under(mNokogiri, "XML");
mNokogiriHtml = rb_define_module_under(mNokogiri, "HTML");
mNokogiriXslt = rb_define_module_under(mNokogiri, "XSLT");
mNokogiriXmlSax = rb_define_module_under(mNokogiriXml, "SAX");
mNokogiriHtmlSax = rb_define_module_under(mNokogiriHtml, "SAX");
rb_const_set( mNokogiri,
rb_intern("LIBXML_VERSION"),
NOKOGIRI_STR_NEW2(LIBXML_DOTTED_VERSION)
);
rb_const_set( mNokogiri,
rb_intern("LIBXML_PARSER_VERSION"),
NOKOGIRI_STR_NEW2(xmlParserVersion)
);
#ifdef NOKOGIRI_USE_PACKAGED_LIBRARIES
rb_const_set(mNokogiri, rb_intern("NOKOGIRI_USE_PACKAGED_LIBRARIES"), Qtrue);
rb_const_set(mNokogiri, rb_intern("NOKOGIRI_LIBXML2_PATH"), NOKOGIRI_STR_NEW2(NOKOGIRI_LIBXML2_PATH));
rb_const_set(mNokogiri, rb_intern("NOKOGIRI_LIBXSLT_PATH"), NOKOGIRI_STR_NEW2(NOKOGIRI_LIBXSLT_PATH));
rb_const_set(mNokogiri, rb_intern("NOKOGIRI_LIBXML2_PATCHES"), rb_str_split(NOKOGIRI_STR_NEW2(NOKOGIRI_LIBXML2_PATCHES), " "));
rb_const_set(mNokogiri, rb_intern("NOKOGIRI_LIBXSLT_PATCHES"), rb_str_split(NOKOGIRI_STR_NEW2(NOKOGIRI_LIBXSLT_PATCHES), " "));
#else
rb_const_set(mNokogiri, rb_intern("NOKOGIRI_USE_PACKAGED_LIBRARIES"), Qfalse);
rb_const_set(mNokogiri, rb_intern("NOKOGIRI_LIBXML2_PATH"), Qnil);
rb_const_set(mNokogiri, rb_intern("NOKOGIRI_LIBXSLT_PATH"), Qnil);
rb_const_set(mNokogiri, rb_intern("NOKOGIRI_LIBXML2_PATCHES"), Qnil);
rb_const_set(mNokogiri, rb_intern("NOKOGIRI_LIBXSLT_PATCHES"), Qnil);
#endif
#ifdef LIBXML_ICONV_ENABLED
rb_const_set(mNokogiri, rb_intern("LIBXML_ICONV_ENABLED"), Qtrue);
#else
rb_const_set(mNokogiri, rb_intern("LIBXML_ICONV_ENABLED"), Qfalse);
#endif
xmlInitParser();
init_xml_document();
init_html_document();
init_xml_node();
init_xml_document_fragment();
init_xml_text();
init_xml_cdata();
init_xml_processing_instruction();
init_xml_attr();
init_xml_entity_reference();
init_xml_comment();
init_xml_node_set();
init_xml_xpath_context();
init_xml_sax_parser_context();
init_xml_sax_parser();
init_xml_sax_push_parser();
init_xml_reader();
init_xml_dtd();
init_xml_element_content();
init_xml_attribute_decl();
init_xml_element_decl();
init_xml_entity_decl();
init_xml_namespace();
init_html_sax_parser_context();
init_html_sax_push_parser();
init_xslt_stylesheet();
init_xml_syntax_error();
init_html_entity_lookup();
init_html_element_description();
init_xml_schema();
init_xml_relax_ng();
init_nokogiri_io();
init_xml_encoding_handler();
}