Re: [xslt] XsltSaveResultFileToMemoryBuffer() ?



Daniel Veillard wrote:

>   But it should not be hard to do a routine, actually if you
> submit a patch for this I will integrate it (there is just a bit
> of control needed to check that your output buffer won't be outgrown)
> If your memory area is not constrined just use xmlAllocOutputBuffer ()


Ok, with the code from Tom Moog, the attached works well for me

  - Heiko





/* 
 * $Id:
 */

/*
 * Some extra routines that should go into libxml/libxslt
 */

#ifndef _XML_EXTRAS_H_
#define _XML_EXTRAS_H_

#include <libxml/xpath.h>
#include <libxml/xmlerror.h>

#include <libxslt/xsltInternals.h>

/* xsltutils.h */
int xsltSaveResultToBuffer(char **target,size_t size,
				xmlDocPtr result,
				xsltStylesheetPtr style); 

#endif



/* 
 * $Id:
 */

/*
 * Some extra routines that should go into libxml/libxslt
 */


#include <libxml/xpath.h>
#include <libxml/xmlerror.h>
#include <string.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/xsltutils.h>

#include "extras.h"

/**
 * xsltSaveResultToBuffer:
 * @target: a pointer to a char buffer
 * @size: the size of the provided buffer
 * @result: the result xmlDocPtr
 * @style: the stylesheet
 *
 * Save the xmlDocument @result obtained by applying the @style
 * stylesheet into the buffer provided by @target. If size @size
 * of the buffer is zero, the buffer will be created dynamically.
 *
 * Returns the number of bytes written or -1 in case of failure.
 */
 
int	xsltSaveResultToBuffer(char **target,size_t size,
				xmlDocPtr result,
				xsltStylesheetPtr style){

	xmlOutputBufferPtr outputBuffer;
	long rsize;
	size_t count,nBytes;
	char *resultString;

	if ((style==NULL) || (result==NULL))
		return(-1);
	if (result->children == NULL)
		return(0);

	outputBuffer = xmlAllocOutputBuffer(NULL);
	if (outputBuffer == NULL) {
	    xsltGenericError(xsltGenericErrorContext, 
		"xsltSaveResultToBuffer: xmlAllocOutputBuffer failed\n");
            return(-1);
	}

	count = xsltSaveResultTo(outputBuffer, result, style);
	if (count < 0) {
		xsltGenericError(xsltGenericErrorContext,
			"xmlSaveResultTo failed\n");
		return(-1);
	}
	//  Add null byte to end.
	nBytes = xmlOutputBufferWrite(outputBuffer, 1, "");
	if (nBytes < 0) {
		xsltGenericError(xsltGenericErrorContext,
			"xmlOutputBufferWrite failed\n");
		return (-1);
    	}
	resultString = (char *) outputBuffer->buffer->content;

	if (size==0) {	/* we provide the buffer */
		*target=resultString;
		return(strlen(resultString));
	} else {	/* Buffer is provided */
		if (size<rsize)
			rsize=size;
		memcpy(*target,resultString,rsize-1);
		*((*target)+rsize)='\0';

		return(rsize);
	}
}




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