[xml] context reuse for push parser



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.

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");
    xmlFreeParserInputBuffer(buf);
    return;
  }

  xmlCtxtReset(ctxt);

  ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * sizeof(xmlChar
*));
  if (ctxt->pushTab == NULL) {
    xmlErrMemory(ctxt, NULL);
    xmlFreeParserInputBuffer(buf);
    return;
  }

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

  inputStream = xmlNewInputStream(ctxt);
  if (inputStream == NULL) {
    xmlFreeParserInputBuffer(buf);
    return;
  }

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

  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 =
      &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
#ifdef DEBUG_PUSH
    xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
#endif
  }

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

cheers,

Graham.

--
Graham Bennett



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