📄 xmlio.c
字号:
/* * xmlIO.c : implementation of the I/O interfaces used by the parser * * See Copyright for the status of this software. * * Daniel.Veillard@w3.org */#include "global.h"#ifdef WIN32#include "win32config.h"#else#include "config.h"#endif#include <stdio.h>#include <string.h>#ifdef HAVE_SYS_TYPES_H#include <sys/types.h>#endif#ifdef HAVE_SYS_STAT_H#include <sys/stat.h>#endif#ifdef HAVE_FCNTL_H#include <fcntl.h>#endif#ifdef HAVE_UNISTD_H#include <unistd.h>#endif#ifdef HAVE_STDLIB_H#include <stdlib.h>#endif#ifdef HAVE_ZLIB_H#include <zlib.h>#endif#include <libxml/xmlmemory.h>#include <libxml/parser.h>#include <libxml/parserInternals.h>#include <libxml/xmlIO.h>#include <libxml/nanohttp.h>#include <libxml/nanoftp.h>/* #define DEBUG_INPUT *//* #define VERBOSE_FAILURE *//* #define DEBUG_EXTERNAL_ENTITIES */#ifdef DEBUG_INPUT#define MINLEN 40#else#define MINLEN 4000#endif/* * Input I/O callback sets */typedef struct _xmlInputCallback { xmlInputMatchCallback matchcallback; xmlInputOpenCallback opencallback; xmlInputReadCallback readcallback; xmlInputCloseCallback closecallback;} xmlInputCallback;#define MAX_INPUT_CALLBACK 15xmlInputCallback xmlInputCallbackTable[MAX_INPUT_CALLBACK];int xmlInputCallbackNr = 0;int xmlInputCallbackInitialized = 0;/************************************************************************ * * * Standard I/O for file accesses * * * ************************************************************************//** * xmlFdMatch: * @filename: the URI for matching * * input from file descriptor * * Returns 1 if matches, 0 otherwise */intxmlFdMatch (const char *filename) { return(1);}/** * xmlFdOpen: * @filename: the URI for matching * * input from file descriptor, supports compressed input * if @filename is " " then the standard input is used * * Returns an I/O context or NULL in case of error */void *xmlFdOpen (const char *filename) { const char *path = NULL; int fd; if (!strcmp(filename, "-")) { fd = 0; return((void *) fd); } if (!strncmp(filename, "file://localhost", 16)) path = &filename[16]; else if (!strncmp(filename, "file:///", 8)) path = &filename[8]; else if (filename[0] == '/') path = filename; if (path == NULL) return(NULL);#ifdef WIN32 fd = _open (filename, O_RDONLY | _O_BINARY);#else fd = open (filename, O_RDONLY);#endif return((void *) fd);}/** * xmlFdRead: * @context: the I/O context * @buffer: where to drop data * @len: number of bytes to write * * Read @len bytes to @buffer from the I/O channel. * * Returns the number of bytes written */intxmlFdRead (void * context, char * buffer, int len) { return(read((int) context, &buffer[0], len));}/** * xmlFdClose: * @context: the I/O context * * Close an I/O channel */voidxmlFdClose (void * context) { close((int) context);}/** * xmlFileMatch: * @filename: the URI for matching * * input from FILE * * * Returns 1 if matches, 0 otherwise */intxmlFileMatch (const char *filename) { return(1);}/** * xmlFileOpen: * @filename: the URI for matching * * input from FILE *, supports compressed input * if @filename is " " then the standard input is used * * Returns an I/O context or NULL in case of error */void *xmlFileOpen (const char *filename) { const char *path = NULL; FILE *fd; if (!strcmp(filename, "-")) { fd = stdin; return((void *) fd); } if (!strncmp(filename, "file://localhost", 16)) path = &filename[16]; else if (!strncmp(filename, "file:///", 8)) path = &filename[8]; else path = filename; if (path == NULL) return(NULL);#ifdef WIN32 fd = fopen(path, "rb");#else fd = fopen(path, "r");#endif /* WIN32 */ return((void *) fd);}/** * xmlFileRead: * @context: the I/O context * @buffer: where to drop data * @len: number of bytes to write * * Read @len bytes to @buffer from the I/O channel. * * Returns the number of bytes written */intxmlFileRead (void * context, char * buffer, int len) { return(fread(&buffer[0], 1, len, (FILE *) context));}/** * xmlFileClose: * @context: the I/O context * * Close an I/O channel */voidxmlFileClose (void * context) { fclose((FILE *) context);}#ifdef HAVE_ZLIB_H/************************************************************************ * * * I/O for compressed file accesses * * * ************************************************************************//** * xmlGzfileMatch: * @filename: the URI for matching * * input from compressed file test * * Returns 1 if matches, 0 otherwise */intxmlGzfileMatch (const char *filename) { return(1);}/** * xmlGzfileOpen: * @filename: the URI for matching * * input from compressed file open * if @filename is " " then the standard input is used * * Returns an I/O context or NULL in case of error */void *xmlGzfileOpen (const char *filename) { const char *path = NULL; gzFile fd; if (!strcmp(filename, "-")) { fd = gzdopen (fileno(stdin), "r"); return((void *) fd); } if (!strncmp(filename, "file://localhost", 16)) path = &filename[16]; else if (!strncmp(filename, "file:///", 8)) path = &filename[8]; else path = filename; fd = gzopen(filename, "r"); return((void *) fd);}/** * xmlGzfileRead: * @context: the I/O context * @buffer: where to drop data * @len: number of bytes to write * * Read @len bytes to @buffer from the compressed I/O channel. * * Returns the number of bytes written */intxmlGzfileRead (void * context, char * buffer, int len) { return(gzread((gzFile) context, &buffer[0], len));}/** * xmlGzfileClose: * @context: the I/O context * * Close a compressed I/O channel */voidxmlGzfileClose (void * context) { gzclose((gzFile) context);}#endif /* HAVE_ZLIB_H */#ifdef LIBXML_HTTP_ENABLED/************************************************************************ * * * I/O for HTTP file accesses * * * ************************************************************************//** * xmlIOHTTPMatch: * @filename: the URI for matching * * check if the URI matches an HTTP one * * Returns 1 if matches, 0 otherwise */intxmlIOHTTPMatch (const char *filename) { if (!strncmp(filename, "http://", 7)) return(1); return(0);}/** * xmlIOHTTPOpen: * @filename: the URI for matching * * open an HTTP I/O channel * * Returns an I/O context or NULL in case of error */void *xmlIOHTTPOpen (const char *filename) { return(xmlNanoHTTPOpen(filename, NULL));}/** * xmlIOHTTPRead: * @context: the I/O context * @buffer: where to drop data * @len: number of bytes to write * * Read @len bytes to @buffer from the I/O channel. * * Returns the number of bytes written */int xmlIOHTTPRead(void * context, char * buffer, int len) { return(xmlNanoHTTPRead(context, &buffer[0], len));}/** * xmlIOHTTPClose: * @context: the I/O context * * Close an HTTP I/O channel */voidxmlIOHTTPClose (void * context) { xmlNanoHTTPClose(context);}#endif /* LIBXML_HTTP_ENABLED */#ifdef LIBXML_FTP_ENABLED/************************************************************************ * * * I/O for FTP file accesses * * * ************************************************************************//** * xmlIOFTPMatch: * @filename: the URI for matching * * check if the URI matches an FTP one * * Returns 1 if matches, 0 otherwise */intxmlIOFTPMatch (const char *filename) { if (!strncmp(filename, "ftp://", 6)) return(1); return(0);}/** * xmlIOFTPOpen: * @filename: the URI for matching * * open an FTP I/O channel * * Returns an I/O context or NULL in case of error */void *xmlIOFTPOpen (const char *filename) { return(xmlNanoFTPOpen(filename));}/** * xmlIOFTPRead: * @context: the I/O context * @buffer: where to drop data * @len: number of bytes to write * * Read @len bytes to @buffer from the I/O channel. * * Returns the number of bytes written */int xmlIOFTPRead(void * context, char * buffer, int len) { return(xmlNanoFTPRead(context, &buffer[0], len));}/** * xmlIOFTPClose: * @context: the I/O context * * Close an FTP I/O channel */voidxmlIOFTPClose (void * context) { xmlNanoFTPClose(context);}#endif /* LIBXML_FTP_ENABLED *//** * xmlRegisterInputCallbacks: * @match: the xmlInputMatchCallback * @open: the xmlInputOpenCallback * @read: the xmlInputReadCallback * @close: the xmlInputCloseCallback * * Register a new set of I/O callback for handling parser input. * * Returns the registered handler number or -1 in case of error */intxmlRegisterInputCallbacks(xmlInputMatchCallback match, xmlInputOpenCallback open, xmlInputReadCallback read, xmlInputCloseCallback close) { if (xmlInputCallbackNr >= MAX_INPUT_CALLBACK) { return(-1); } xmlInputCallbackTable[xmlInputCallbackNr].matchcallback = match; xmlInputCallbackTable[xmlInputCallbackNr].opencallback = open; xmlInputCallbackTable[xmlInputCallbackNr].readcallback = read; xmlInputCallbackTable[xmlInputCallbackNr].closecallback = close; return(xmlInputCallbackNr++);}/** * xmlRegisterDefaultInputCallbacks: * * Registers the default compiled-in I/O handlers. */voidxmlRegisterDefaultInputCallbacks(void) { xmlRegisterInputCallbacks(xmlFileMatch, xmlFileOpen, xmlFileRead, xmlFileClose);#ifdef HAVE_ZLIB_H xmlRegisterInputCallbacks(xmlGzfileMatch, xmlGzfileOpen, xmlGzfileRead, xmlGzfileClose);#endif /* HAVE_ZLIB_H */#ifdef LIBXML_HTTP_ENABLED xmlRegisterInputCallbacks(xmlIOHTTPMatch, xmlIOHTTPOpen, xmlIOHTTPRead, xmlIOHTTPClose);#endif /* LIBXML_HTTP_ENABLED */#ifdef LIBXML_FTP_ENABLED xmlRegisterInputCallbacks(xmlIOFTPMatch, xmlIOFTPOpen, xmlIOFTPRead, xmlIOFTPClose);#endif /* LIBXML_FTP_ENABLED */}/** * xmlAllocParserInputBuffer: * @enc: the charset encoding if known * * Create a buffered parser input for progressive parsing * * Returns the new parser input or NULL */xmlParserInputBufferPtrxmlAllocParserInputBuffer(xmlCharEncoding enc) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -