Re: [xml] context reuse for push parser

On Tue, Oct 21, 2003 at 10:19:43PM +0100, Graham Bennett wrote:
Hi all,

I added the following function to enable me to reuse the parser context
using the push parser.  It corresponds to xmlCreatePushParserCtxt, but
takes an existing context.  Not sure if it's generally useful but it
seems to work for my code.

  Okay might make a good addition to the new parser interfaces.

static void xmlCtxtInitPushParser(xmlParserCtxtPtr ctxt, const char
*chunk, int size, const char *filename)
  xmlParserInputPtr inputStream;
  xmlParserInputBufferPtr buf;
  xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;

  if ((chunk != NULL) && (size >= 4))
    enc = xmlDetectCharEncoding((const xmlChar *) chunk, size);

  buf = xmlAllocParserInputBuffer(enc);
  if (buf == NULL) return;

  if (ctxt == NULL) {
    xmlErrMemory(NULL, "No parser context\n");

  Hum, this isn't really a memory error. It's an API use error, I would rather
make the function return 0 in case of success and -1 in case of error, and
return -1 in that case (and check on function entry).



  ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * sizeof(xmlChar

  Hum, you're likely to memleak there. ctxt->pushTab is probably not NULL,

  if (ctxt->pushTab == NULL) {
    xmlErrMemory(ctxt, NULL);

  if (filename == NULL) {
    ctxt->directory = NULL;
  } else {
    ctxt->directory = xmlParserGetDirectory(filename);

  inputStream = xmlNewInputStream(ctxt);
  if (inputStream == NULL) {

  if (filename == NULL)
    inputStream->filename = NULL;
    inputStream->filename = (char *)
      xmlCanonicPath((const xmlChar *) filename);
  inputStream->buf = buf;
  inputStream->base = inputStream->buf->buffer->content;
  inputStream->cur = inputStream->buf->buffer->content;
  inputStream->end =

  inputPush(ctxt, inputStream);

  if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
      (ctxt->input->buf != NULL))  {
    int base = ctxt->input->base - ctxt->input->buf->buffer->content;
    int cur = ctxt->input->cur - ctxt->input->base;

    xmlParserInputBufferPush(ctxt->input->buf, size, chunk);

    ctxt->input->base = ctxt->input->buf->buffer->content + base;
    ctxt->input->cur = ctxt->input->base + cur;
    ctxt->input->end =
    xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);

  if (enc != XML_CHAR_ENCODING_NONE) {
    xmlSwitchEncoding(ctxt, enc);

  maybe an extra encoding passed as a const char * is needed. C.f. 
section F.2. of the spec:
in that case the context encoding overrides the one fine in the XML
declaration or autodetected by xmlDetectCharEncoding() which implement
the F.1. detection algorithm.


  it's a good idea, that could be reused within the xmlreader code.


Daniel Veillard      | Red Hat Network
veillard redhat com  | libxml GNOME XML XSLT toolkit | Rpmfind RPM search engine

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]