| 1 |
#include <nokogiri.h> |
| 2 |
|
| 3 |
VALUE mNokogiri ; |
| 4 |
VALUE mNokogiriGumbo ; |
| 5 |
VALUE mNokogiriHtml4 ; |
| 6 |
VALUE mNokogiriHtml4Sax ; |
| 7 |
VALUE mNokogiriHtml5 ; |
| 8 |
VALUE mNokogiriXml ; |
| 9 |
VALUE mNokogiriXmlSax ; |
| 10 |
VALUE mNokogiriXmlXpath ; |
| 11 |
VALUE mNokogiriXslt ; |
| 12 |
|
| 13 |
VALUE cNokogiriSyntaxError; |
| 14 |
VALUE cNokogiriXmlCharacterData; |
| 15 |
VALUE cNokogiriXmlElement; |
| 16 |
VALUE cNokogiriXmlXpathSyntaxError; |
| 17 |
|
| 18 |
void noko_init_xml_attr(void); |
| 19 |
void noko_init_xml_attribute_decl(void); |
| 20 |
void noko_init_xml_cdata(void); |
| 21 |
void noko_init_xml_comment(void); |
| 22 |
void noko_init_xml_document(void); |
| 23 |
void noko_init_xml_document_fragment(void); |
| 24 |
void noko_init_xml_dtd(void); |
| 25 |
void noko_init_xml_element_content(void); |
| 26 |
void noko_init_xml_element_decl(void); |
| 27 |
void noko_init_xml_encoding_handler(void); |
| 28 |
void noko_init_xml_entity_decl(void); |
| 29 |
void noko_init_xml_entity_reference(void); |
| 30 |
void noko_init_xml_namespace(void); |
| 31 |
void noko_init_xml_node(void); |
| 32 |
void noko_init_xml_node_set(void); |
| 33 |
void noko_init_xml_processing_instruction(void); |
| 34 |
void noko_init_xml_reader(void); |
| 35 |
void noko_init_xml_relax_ng(void); |
| 36 |
void noko_init_xml_sax_parser(void); |
| 37 |
void noko_init_xml_sax_parser_context(void); |
| 38 |
void noko_init_xml_sax_push_parser(void); |
| 39 |
void noko_init_xml_schema(void); |
| 40 |
void noko_init_xml_syntax_error(void); |
| 41 |
void noko_init_xml_text(void); |
| 42 |
void noko_init_xml_xpath_context(void); |
| 43 |
void noko_init_xslt_stylesheet(void); |
| 44 |
void noko_init_html_document(void); |
| 45 |
void noko_init_html_element_description(void); |
| 46 |
void noko_init_html_entity_lookup(void); |
| 47 |
void noko_init_html_sax_parser_context(void); |
| 48 |
void noko_init_html_sax_push_parser(void); |
| 49 |
void noko_init_html4_sax_parser(void); |
| 50 |
void noko_init_gumbo(void); |
| 51 |
void noko_init_test_global_handlers(void); |
| 52 |
|
| 53 |
static ID id_read, id_write, id_external_encoding; |
| 54 |
|
| 55 |
|
| 56 |
static VALUE |
| 57 |
noko_io_read_check(VALUE val) |
| 58 |
{ |
| 59 |
VALUE *args = (VALUE *)val; |
| 60 |
return rb_funcall(args[0], id_read, 1, args[1]); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
static VALUE |
| 65 |
noko_io_read_failed(VALUE arg, VALUE exc) |
| 66 |
{ |
| 67 |
return Qundef; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
int |
| 72 |
noko_io_read(void *io, char *c_buffer, int c_buffer_len) |
| 73 |
{ |
| 74 |
VALUE rb_io = (VALUE)io; |
| 75 |
VALUE rb_read_string, rb_args[2]; |
| 76 |
size_t n_bytes_read, safe_len; |
| 77 |
|
| 78 |
rb_args[0] = rb_io; |
| 79 |
rb_args[1] = INT2NUM(c_buffer_len); |
| 80 |
|
| 81 |
rb_read_string = rb_rescue(noko_io_read_check, (VALUE)rb_args, noko_io_read_failed, 0); |
| 82 |
|
| 83 |
if (NIL_P(rb_read_string)) { return 0; } |
| 84 |
if (rb_read_string == Qundef) { return -1; } |
| 85 |
if (TYPE(rb_read_string) != T_STRING) { return -1; } |
| 86 |
|
| 87 |
n_bytes_read = (size_t)RSTRING_LEN(rb_read_string); |
| 88 |
safe_len = (n_bytes_read > (size_t)c_buffer_len) ? (size_t)c_buffer_len : n_bytes_read; |
| 89 |
memcpy(c_buffer, StringValuePtr(rb_read_string), safe_len); |
| 90 |
|
| 91 |
return (int)safe_len; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
static VALUE |
| 96 |
noko_io_write_check(VALUE rb_args) |
| 97 |
{ |
| 98 |
VALUE rb_io = ((VALUE *)rb_args)[0]; |
| 99 |
VALUE rb_output = ((VALUE *)rb_args)[1]; |
| 100 |
return rb_funcall(rb_io, id_write, 1, rb_output); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
static VALUE |
| 105 |
noko_io_write_failed(VALUE arg, VALUE exc) |
| 106 |
{ |
| 107 |
return Qundef; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
int |
| 112 |
noko_io_write(void *io, char *c_buffer, int c_buffer_len) |
| 113 |
{ |
| 114 |
VALUE rb_args[2], rb_n_bytes_written; |
| 115 |
VALUE rb_io = (VALUE)io; |
| 116 |
VALUE rb_enc = Qnil; |
| 117 |
rb_encoding *io_encoding; |
| 118 |
|
| 119 |
if (rb_respond_to(rb_io, id_external_encoding)) { |
| 120 |
rb_enc = rb_funcall(rb_io, id_external_encoding, 0); |
| 121 |
} |
| 122 |
io_encoding = RB_NIL_P(rb_enc) ? rb_ascii8bit_encoding() : rb_to_encoding(rb_enc); |
| 123 |
|
| 124 |
rb_args[0] = rb_io; |
| 125 |
rb_args[1] = rb_enc_str_new(c_buffer, (long)c_buffer_len, io_encoding); |
| 126 |
|
| 127 |
rb_n_bytes_written = rb_rescue(noko_io_write_check, (VALUE)rb_args, noko_io_write_failed, 0); |
| 128 |
if (rb_n_bytes_written == Qundef) { return -1; } |
| 129 |
|
| 130 |
return NUM2INT(rb_n_bytes_written); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
int |
| 135 |
noko_io_close(void *io) |
| 136 |
{ |
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
#if defined(_WIN32) && !defined(NOKOGIRI_PACKAGED_LIBRARIES) |
| 142 |
# define NOKOGIRI_WINDOWS_DLLS 1 |
| 143 |
#else |
| 144 |
# define NOKOGIRI_WINDOWS_DLLS 0 |
| 145 |
#endif |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
static void |
| 160 |
set_libxml_memory_management(void) |
| 161 |
{ |
| 162 |
const char *nlmm = getenv("NOKOGIRI_LIBXML_MEMORY_MANAGEMENT"); |
| 163 |
if (nlmm) { |
| 164 |
if (strcmp(nlmm, "default") == 0) { |
| 165 |
goto libxml_uses_default_memory_management; |
| 166 |
} else if (strcmp(nlmm, "ruby") == 0) { |
| 167 |
goto libxml_uses_ruby_memory_management; |
| 168 |
} |
| 169 |
} |
| 170 |
if (NOKOGIRI_WINDOWS_DLLS) { |
| 171 |
libxml_uses_default_memory_management: |
| 172 |
rb_const_set(mNokogiri, rb_intern("LIBXML_MEMORY_MANAGEMENT"), NOKOGIRI_STR_NEW2("default")); |
| 173 |
return; |
| 174 |
} else { |
| 175 |
libxml_uses_ruby_memory_management: |
| 176 |
rb_const_set(mNokogiri, rb_intern("LIBXML_MEMORY_MANAGEMENT"), NOKOGIRI_STR_NEW2("ruby")); |
| 177 |
xmlMemSetup((xmlFreeFunc)ruby_xfree, (xmlMallocFunc)ruby_xmalloc, (xmlReallocFunc)ruby_xrealloc, ruby_strdup); |
| 178 |
return; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
void |
| 184 |
Init_nokogiri(void) |
| 185 |
{ |
| 186 |
mNokogiri = rb_define_module("Nokogiri"); |
| 187 |
mNokogiriGumbo = rb_define_module_under(mNokogiri, "Gumbo"); |
| 188 |
mNokogiriHtml4 = rb_define_module_under(mNokogiri, "HTML4"); |
| 189 |
mNokogiriHtml4Sax = rb_define_module_under(mNokogiriHtml4, "SAX"); |
| 190 |
mNokogiriHtml5 = rb_define_module_under(mNokogiri, "HTML5"); |
| 191 |
mNokogiriXml = rb_define_module_under(mNokogiri, "XML"); |
| 192 |
mNokogiriXmlSax = rb_define_module_under(mNokogiriXml, "SAX"); |
| 193 |
mNokogiriXmlXpath = rb_define_module_under(mNokogiriXml, "XPath"); |
| 194 |
mNokogiriXslt = rb_define_module_under(mNokogiri, "XSLT"); |
| 195 |
|
| 196 |
set_libxml_memory_management(); |
| 197 |
xmlInitParser(); |
| 198 |
exsltRegisterAll(); |
| 199 |
|
| 200 |
rb_const_set(mNokogiri, rb_intern("LIBXML_COMPILED_VERSION"), NOKOGIRI_STR_NEW2(LIBXML_DOTTED_VERSION)); |
| 201 |
rb_const_set(mNokogiri, rb_intern("LIBXML_LOADED_VERSION"), NOKOGIRI_STR_NEW2(xmlParserVersion)); |
| 202 |
|
| 203 |
rb_const_set(mNokogiri, rb_intern("LIBXSLT_COMPILED_VERSION"), NOKOGIRI_STR_NEW2(LIBXSLT_DOTTED_VERSION)); |
| 204 |
rb_const_set(mNokogiri, rb_intern("LIBXSLT_LOADED_VERSION"), NOKOGIRI_STR_NEW2(xsltEngineVersion)); |
| 205 |
|
| 206 |
rb_const_set(mNokogiri, rb_intern("LIBXML_ZLIB_ENABLED"), |
| 207 |
xmlHasFeature(XML_WITH_ZLIB) == 1 ? Qtrue : Qfalse); |
| 208 |
|
| 209 |
#ifdef NOKOGIRI_PACKAGED_LIBRARIES |
| 210 |
rb_const_set(mNokogiri, rb_intern("PACKAGED_LIBRARIES"), Qtrue); |
| 211 |
# ifdef NOKOGIRI_PRECOMPILED_LIBRARIES |
| 212 |
rb_const_set(mNokogiri, rb_intern("PRECOMPILED_LIBRARIES"), Qtrue); |
| 213 |
# else |
| 214 |
rb_const_set(mNokogiri, rb_intern("PRECOMPILED_LIBRARIES"), Qfalse); |
| 215 |
# endif |
| 216 |
rb_const_set(mNokogiri, rb_intern("LIBXML2_PATCHES"), rb_str_split(NOKOGIRI_STR_NEW2(NOKOGIRI_LIBXML2_PATCHES), " ")); |
| 217 |
rb_const_set(mNokogiri, rb_intern("LIBXSLT_PATCHES"), rb_str_split(NOKOGIRI_STR_NEW2(NOKOGIRI_LIBXSLT_PATCHES), " ")); |
| 218 |
#else |
| 219 |
rb_const_set(mNokogiri, rb_intern("PACKAGED_LIBRARIES"), Qfalse); |
| 220 |
rb_const_set(mNokogiri, rb_intern("PRECOMPILED_LIBRARIES"), Qfalse); |
| 221 |
rb_const_set(mNokogiri, rb_intern("LIBXML2_PATCHES"), Qnil); |
| 222 |
rb_const_set(mNokogiri, rb_intern("LIBXSLT_PATCHES"), Qnil); |
| 223 |
#endif |
| 224 |
|
| 225 |
#ifdef LIBXML_ICONV_ENABLED |
| 226 |
rb_const_set(mNokogiri, rb_intern("LIBXML_ICONV_ENABLED"), Qtrue); |
| 227 |
#else |
| 228 |
rb_const_set(mNokogiri, rb_intern("LIBXML_ICONV_ENABLED"), Qfalse); |
| 229 |
#endif |
| 230 |
|
| 231 |
#ifdef NOKOGIRI_OTHER_LIBRARY_VERSIONS |
| 232 |
rb_const_set(mNokogiri, rb_intern("OTHER_LIBRARY_VERSIONS"), NOKOGIRI_STR_NEW2(NOKOGIRI_OTHER_LIBRARY_VERSIONS)); |
| 233 |
#endif |
| 234 |
|
| 235 |
if (xsltExtModuleFunctionLookup((const xmlChar *)"date-time", EXSLT_DATE_NAMESPACE)) { |
| 236 |
rb_const_set(mNokogiri, rb_intern("LIBXSLT_DATETIME_ENABLED"), Qtrue); |
| 237 |
} else { |
| 238 |
rb_const_set(mNokogiri, rb_intern("LIBXSLT_DATETIME_ENABLED"), Qfalse); |
| 239 |
} |
| 240 |
|
| 241 |
cNokogiriSyntaxError = rb_define_class_under(mNokogiri, "SyntaxError", rb_eStandardError); |
| 242 |
noko_init_xml_syntax_error(); |
| 243 |
assert(cNokogiriXmlSyntaxError); |
| 244 |
cNokogiriXmlXpathSyntaxError = rb_define_class_under(mNokogiriXmlXpath, "SyntaxError", cNokogiriXmlSyntaxError); |
| 245 |
|
| 246 |
noko_init_xml_element_content(); |
| 247 |
noko_init_xml_encoding_handler(); |
| 248 |
noko_init_xml_namespace(); |
| 249 |
noko_init_xml_node_set(); |
| 250 |
noko_init_xml_reader(); |
| 251 |
|
| 252 |
noko_init_xml_sax_parser(); |
| 253 |
noko_init_html4_sax_parser(); |
| 254 |
|
| 255 |
noko_init_xml_xpath_context(); |
| 256 |
noko_init_xslt_stylesheet(); |
| 257 |
noko_init_html_element_description(); |
| 258 |
noko_init_html_entity_lookup(); |
| 259 |
|
| 260 |
noko_init_xml_schema(); |
| 261 |
noko_init_xml_relax_ng(); |
| 262 |
|
| 263 |
noko_init_xml_sax_parser_context(); |
| 264 |
noko_init_html_sax_parser_context(); |
| 265 |
|
| 266 |
noko_init_xml_sax_push_parser(); |
| 267 |
noko_init_html_sax_push_parser(); |
| 268 |
|
| 269 |
noko_init_xml_node(); |
| 270 |
noko_init_xml_attr(); |
| 271 |
noko_init_xml_attribute_decl(); |
| 272 |
noko_init_xml_dtd(); |
| 273 |
noko_init_xml_element_decl(); |
| 274 |
noko_init_xml_entity_decl(); |
| 275 |
noko_init_xml_entity_reference(); |
| 276 |
noko_init_xml_processing_instruction(); |
| 277 |
assert(cNokogiriXmlNode); |
| 278 |
cNokogiriXmlElement = rb_define_class_under(mNokogiriXml, "Element", cNokogiriXmlNode); |
| 279 |
cNokogiriXmlCharacterData = rb_define_class_under(mNokogiriXml, "CharacterData", cNokogiriXmlNode); |
| 280 |
noko_init_xml_comment(); |
| 281 |
noko_init_xml_text(); |
| 282 |
noko_init_xml_cdata(); |
| 283 |
|
| 284 |
noko_init_xml_document_fragment(); |
| 285 |
noko_init_xml_document(); |
| 286 |
noko_init_html_document(); |
| 287 |
noko_init_gumbo(); |
| 288 |
|
| 289 |
noko_init_test_global_handlers(); |
| 290 |
|
| 291 |
id_read = rb_intern("read"); |
| 292 |
id_write = rb_intern("write"); |
| 293 |
id_external_encoding = rb_intern("external_encoding"); |
| 294 |
} |