TinyDomElement* TinyDom::parse(std::istream& istr) { XML_Parser parser = XML_ParserCreate(0); TinyDomParser tdparser; XML_SetUserData(parser, &tdparser); XML_SetCharacterDataHandler(parser, characterDataHandler); XML_SetElementHandler(parser, startElementHandler, endElementHandler); XML_SetCommentHandler(parser, commentHandler); XML_SetProcessingInstructionHandler(parser, processingInstructionHandler); XML_SetXmlDeclHandler(parser, xmlDeclHandler); XML_SetDefaultHandler(parser, defaultHandler); XML_SetDoctypeDeclHandler(parser, startDoctypeDeclHandler, endDoctypeDeclHandler); try { while (1) { void* buf = XML_GetBuffer(parser, 1024); if (!buf) { throw std::runtime_error("out of memory!"); } istr.read((char*)buf, 1024); std::streamsize len = istr.gcount(); if (istr.fail() && !istr.eof()) { throw std::runtime_error("failed IO"); } bool isFinal = (istr.eof() || len < 1024); if (! XML_ParseBuffer(parser, len, isFinal)) { std::ostringstream ostr; ostr << "parse error at line " << XML_GetErrorLineNumber(parser) << ", column " << XML_GetErrorColumnNumber(parser) << ": " << XML_ErrorString(XML_GetErrorCode(parser)); throw std::runtime_error(ostr.str()); } if (isFinal) { break; } } XML_ParserFree(parser); } catch (...) { //std::cerr << "Got exception: " << e.what() << "\n"; if (parser) { XML_ParserFree(parser); } delete tdparser.rootElement; throw; } return tdparser.rootElement; }
static void reportError(XML_Parser parser, const XML_Char *filename) { enum XML_Error code = XML_GetErrorCode(parser); const XML_Char *message = XML_ErrorString(code); if (message) ftprintf(stdout, T("%s:%" XML_FMT_INT_MOD "u:%" XML_FMT_INT_MOD "u: %s\n"), filename, XML_GetErrorLineNumber(parser), XML_GetErrorColumnNumber(parser), message); else ftprintf(stderr, T("%s: (unknown message %d)\n"), filename, code); }
char* xmlnode_file_borked(char *file) { XML_Parser p; char buf[BUFSIZ]; static char err[1024]; int fd, len, done; if(NULL == file) return "no file specified"; fd = open(file,O_RDONLY); if(fd < 0) return "unable to open file"; p = XML_ParserCreate(NULL); while(1) { len = read(fd, buf, BUFSIZ); done = len < BUFSIZ; if(!XML_Parse(p, buf, len, done)) { snprintf(err,1023,"%s at line %d and column %d",XML_ErrorString(XML_GetErrorCode(p)),XML_GetErrorLineNumber(p),XML_GetErrorColumnNumber(p)); XML_ParserFree(p); close(fd); return err; } } }
CP_C_API cp_plugin_info_t * cp_load_plugin_descriptor(cp_context_t *context, const char *path, cp_status_t *error) { char *file = NULL; cp_status_t status = CP_OK; FILE *fh = NULL; XML_Parser parser = NULL; ploader_context_t *plcontext = NULL; cp_plugin_info_t *plugin = NULL; CHECK_NOT_NULL(context); CHECK_NOT_NULL(path); cpi_lock_context(context); cpi_check_invocation(context, CPI_CF_ANY, __func__); do { int path_len; // Construct the file name for the plug-in descriptor path_len = strlen(path); if (path_len == 0) { status = CP_ERR_IO; break; } if (path[path_len - 1] == CP_FNAMESEP_CHAR) { path_len--; } file = malloc((path_len + strlen(CP_PLUGIN_DESCRIPTOR) + 2) * sizeof(char)); if (file == NULL) { status = CP_ERR_RESOURCE; break; } strcpy(file, path); file[path_len] = CP_FNAMESEP_CHAR; strcpy(file + path_len + 1, CP_PLUGIN_DESCRIPTOR); // Open the file if ((fh = fopen(file, "rb")) == NULL) { status = CP_ERR_IO; break; } // Initialize the XML parsing parser = XML_ParserCreate(NULL); if (parser == NULL) { status = CP_ERR_RESOURCE; break; } XML_SetElementHandler(parser, start_element_handler, end_element_handler); // Initialize the parsing context if ((plcontext = malloc(sizeof(ploader_context_t))) == NULL) { status = CP_ERR_RESOURCE; break; } memset(plcontext, 0, sizeof(ploader_context_t)); if ((plcontext->plugin = malloc(sizeof(cp_plugin_info_t))) == NULL) { status = CP_ERR_RESOURCE; break; } plcontext->context = context; plcontext->configuration = NULL; plcontext->value = NULL; plcontext->parser = parser; plcontext->file = file; plcontext->state = PARSER_BEGIN; memset(plcontext->plugin, 0, sizeof(cp_plugin_info_t)); plcontext->plugin->name = NULL; plcontext->plugin->identifier = NULL; plcontext->plugin->version = NULL; plcontext->plugin->provider_name = NULL; plcontext->plugin->abi_bw_compatibility = NULL; plcontext->plugin->api_bw_compatibility = NULL; plcontext->plugin->plugin_path = NULL; plcontext->plugin->req_cpluff_version = NULL; plcontext->plugin->imports = NULL; plcontext->plugin->runtime_lib_name = NULL; plcontext->plugin->runtime_funcs_symbol = NULL; plcontext->plugin->ext_points = NULL; plcontext->plugin->extensions = NULL; plcontext->plugin->url = NULL; plcontext->plugin->resourcetype = NULL; XML_SetUserData(parser, plcontext); // Parse the plug-in descriptor while (1) { int bytes_read; void *xml_buffer; int i; // Get buffer from Expat if ((xml_buffer = XML_GetBuffer(parser, CP_XML_PARSER_BUFFER_SIZE)) == NULL) { status = CP_ERR_RESOURCE; break; } // Read data into buffer bytes_read = fread(xml_buffer, 1, CP_XML_PARSER_BUFFER_SIZE, fh); if (ferror(fh)) { status = CP_ERR_IO; break; } // Parse the data if (!(i = XML_ParseBuffer(parser, bytes_read, bytes_read == 0)) && context != NULL) { cpi_lock_context(context); cpi_errorf(context, N_("XML parsing error in %s, line %d, column %d (%s)."), file, XML_GetErrorLineNumber(parser), XML_GetErrorColumnNumber(parser) + 1, XML_ErrorString(XML_GetErrorCode(parser))); cpi_unlock_context(context); } if (!i || plcontext->state == PARSER_ERROR) { status = CP_ERR_MALFORMED; break; } if (bytes_read == 0) { break; } } if (status == CP_OK) { if (plcontext->state != PARSER_END || plcontext->error_count > 0) { status = CP_ERR_MALFORMED; } if (plcontext->resource_error_count > 0) { status = CP_ERR_RESOURCE; } } if (status != CP_OK) { break; } // Initialize the plug-in path *(file + path_len) = '\0'; plcontext->plugin->plugin_path = file; file = NULL; // Increase plug-in usage count if ((status = cpi_register_info(context, plcontext->plugin, (void (*)(cp_context_t *, void *)) dealloc_plugin_info)) != CP_OK) { break; } } while (0); // Report possible errors if (status != CP_OK) { switch (status) { case CP_ERR_MALFORMED: cpi_errorf(context, N_("Plug-in descriptor in %s is invalid."), path); break; case CP_ERR_IO: cpi_errorf(context, N_("An I/O error occurred while loading a plug-in descriptor from %s."), path); break; case CP_ERR_RESOURCE: cpi_errorf(context, N_("Insufficient system resources to load a plug-in descriptor from %s."), path); break; default: cpi_errorf(context, N_("Failed to load a plug-in descriptor from %s."), path); break; } } cpi_unlock_context(context); // Release persistently allocated data on failure if (status != CP_OK) { if (file != NULL) { free(file); file = NULL; } if (plcontext != NULL && plcontext->plugin != NULL) { cpi_free_plugin(plcontext->plugin); plcontext->plugin = NULL; } } // Otherwise copy the plug-in pointer else { plugin = plcontext->plugin; } // Release data allocated for parsing if (parser != NULL) { XML_ParserFree(parser); } if (fh != NULL) { fclose(fh); } if (plcontext != NULL) { if (plcontext->value != NULL) { free(plcontext->value); } free(plcontext); plcontext = NULL; } // Return error code if (error != NULL) { *error = status; } return plugin; }