Blame doc/examples/io1.c

Packit 423ecb
/**
Packit 423ecb
 * section: InputOutput
Packit 423ecb
 * synopsis: Example of custom Input/Output
Packit 423ecb
 * purpose: Demonstrate the use of xmlRegisterInputCallbacks
Packit 423ecb
 *          to build a custom I/O layer, this is used in an
Packit 423ecb
 *          XInclude method context to show how dynamic document can
Packit 423ecb
 *          be built in a clean way.
Packit 423ecb
 * usage: io1
Packit 423ecb
 * test: io1 > io1.tmp && diff io1.tmp $(srcdir)/io1.res
Packit 423ecb
 * author: Daniel Veillard
Packit 423ecb
 * copy: see Copyright for the status of this software.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
#include <stdio.h>
Packit 423ecb
#include <string.h>
Packit 423ecb
#include <libxml/parser.h>
Packit 423ecb
#include <libxml/tree.h>
Packit 423ecb
#include <libxml/xinclude.h>
Packit 423ecb
#include <libxml/xmlIO.h>
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_XINCLUDE_ENABLED
Packit 423ecb
static const char *result = "<list><people>a</people><people>b</people></list>";
Packit 423ecb
static const char *cur = NULL;
Packit 423ecb
static int rlen;
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * sqlMatch:
Packit 423ecb
 * @URI: an URI to test
Packit 423ecb
 *
Packit 423ecb
 * Check for an sql: query
Packit 423ecb
 *
Packit 423ecb
 * Returns 1 if yes and 0 if another Input module should be used
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
sqlMatch(const char * URI) {
Packit 423ecb
    if ((URI != NULL) && (!strncmp(URI, "sql:", 4)))
Packit 423ecb
        return(1);
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * sqlOpen:
Packit 423ecb
 * @URI: an URI to test
Packit 423ecb
 *
Packit 423ecb
 * Return a pointer to the sql: query handler, in this example simply
Packit 423ecb
 * the current pointer...
Packit 423ecb
 *
Packit 423ecb
 * Returns an Input context or NULL in case or error
Packit 423ecb
 */
Packit 423ecb
static void *
Packit 423ecb
sqlOpen(const char * URI) {
Packit 423ecb
    if ((URI == NULL) || (strncmp(URI, "sql:", 4)))
Packit 423ecb
        return(NULL);
Packit 423ecb
    cur = result;
Packit 423ecb
    rlen = strlen(result);
Packit 423ecb
    return((void *) cur);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * sqlClose:
Packit 423ecb
 * @context: the read context
Packit 423ecb
 *
Packit 423ecb
 * Close the sql: query handler
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 or -1 in case of error
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
sqlClose(void * context) {
Packit 423ecb
    if (context == NULL) return(-1);
Packit 423ecb
    cur = NULL;
Packit 423ecb
    rlen = 0;
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * sqlRead:
Packit 423ecb
 * @context: the read context
Packit 423ecb
 * @buffer: where to store data
Packit 423ecb
 * @len: number of bytes to read
Packit 423ecb
 *
Packit 423ecb
 * Implement an sql: query read.
Packit 423ecb
 *
Packit 423ecb
 * Returns the number of bytes read or -1 in case of error
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
sqlRead(void * context, char * buffer, int len) {
Packit 423ecb
   const char *ptr = (const char *) context;
Packit 423ecb
Packit 423ecb
   if ((context == NULL) || (buffer == NULL) || (len < 0))
Packit 423ecb
       return(-1);
Packit 423ecb
Packit 423ecb
   if (len > rlen) len = rlen;
Packit 423ecb
   memcpy(buffer, ptr, len);
Packit 423ecb
   rlen -= len;
Packit 423ecb
   return(len);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
const char *include = "\n\
Packit 423ecb
<document xmlns:xi=\"http://www.w3.org/2003/XInclude\">\n\
Packit 423ecb
  

List of people:

\n\
Packit 423ecb
  <xi:include href=\"sql:select_name_from_people\"/>\n\
Packit 423ecb
</document>\n";
Packit 423ecb
Packit 423ecb
int main(void) {
Packit 423ecb
    xmlDocPtr doc;
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * this initialize the library and check potential ABI mismatches
Packit 423ecb
     * between the version it was compiled for and the actual shared
Packit 423ecb
     * library used.
Packit 423ecb
     */
Packit 423ecb
    LIBXML_TEST_VERSION
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * register the new I/O handlers
Packit 423ecb
     */
Packit 423ecb
    if (xmlRegisterInputCallbacks(sqlMatch, sqlOpen, sqlRead, sqlClose) < 0) {
Packit 423ecb
        fprintf(stderr, "failed to register SQL handler\n");
Packit 423ecb
	exit(1);
Packit 423ecb
    }
Packit 423ecb
    /*
Packit 423ecb
     * parse include into a document
Packit 423ecb
     */
Packit 423ecb
    doc = xmlReadMemory(include, strlen(include), "include.xml", NULL, 0);
Packit 423ecb
    if (doc == NULL) {
Packit 423ecb
        fprintf(stderr, "failed to parse the including file\n");
Packit 423ecb
	exit(1);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * apply the XInclude process, this should trigger the I/O just
Packit 423ecb
     * registered.
Packit 423ecb
     */
Packit 423ecb
    if (xmlXIncludeProcess(doc) <= 0) {
Packit 423ecb
        fprintf(stderr, "XInclude processing failed\n");
Packit 423ecb
	exit(1);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_OUTPUT_ENABLED
Packit 423ecb
    /*
Packit 423ecb
     * save the output for checking to stdout
Packit 423ecb
     */
Packit 423ecb
    xmlDocDump(stdout, doc);
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * Free the document
Packit 423ecb
     */
Packit 423ecb
    xmlFreeDoc(doc);
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * Cleanup function for the XML library.
Packit 423ecb
     */
Packit 423ecb
    xmlCleanupParser();
Packit 423ecb
    /*
Packit 423ecb
     * this is to debug memory for regression tests
Packit 423ecb
     */
Packit 423ecb
    xmlMemoryDump();
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
#else
Packit 423ecb
int main(void) {
Packit 423ecb
    fprintf(stderr, "XInclude support not compiled in\n");
Packit 423ecb
    exit(1);
Packit 423ecb
}
Packit 423ecb
#endif