⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlio.c

📁 xml开源解析代码.版本为libxml2-2.6.29,可支持GB3212.网络消息发送XML时很有用.
💻 C
📖 第 1 页 / 共 5 页
字号:
 * compiled-in I/O.  * * Returns the number of input callback registered or -1 in case of error. */intxmlPopInputCallbacks(void){    if (!xmlInputCallbackInitialized)        return(-1);    if (xmlInputCallbackNr <= 0)        return(-1);            xmlInputCallbackNr--;    xmlInputCallbackTable[xmlInputCallbackNr].matchcallback = NULL;    xmlInputCallbackTable[xmlInputCallbackNr].opencallback = NULL;    xmlInputCallbackTable[xmlInputCallbackNr].readcallback = NULL;    xmlInputCallbackTable[xmlInputCallbackNr].closecallback = NULL;    return(xmlInputCallbackNr);}#ifdef LIBXML_OUTPUT_ENABLED/** * xmlCleanupOutputCallbacks: * * clears the entire output callback table. this includes the * compiled-in I/O callbacks.  */voidxmlCleanupOutputCallbacks(void){    int i;    if (!xmlOutputCallbackInitialized)        return;    for (i = xmlOutputCallbackNr - 1; i >= 0; i--) {        xmlOutputCallbackTable[i].matchcallback = NULL;        xmlOutputCallbackTable[i].opencallback = NULL;        xmlOutputCallbackTable[i].writecallback = NULL;        xmlOutputCallbackTable[i].closecallback = NULL;    }    xmlOutputCallbackNr = 0;    xmlOutputCallbackInitialized = 0;}#endif /* LIBXML_OUTPUT_ENABLED *//************************************************************************ *									* *		Standard I/O for file accesses				* *									* ************************************************************************/#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)/** *  xmlWrapOpenUtf8: * @path:  the path in utf-8 encoding * @mode:  type of access (0 - read, 1 - write) * * function opens the file specified by @path * */static FILE*xmlWrapOpenUtf8(const char *path,int mode){    FILE *fd = NULL;    wchar_t *wPath;    wPath = __xmlIOWin32UTF8ToWChar(path);    if(wPath)    {       fd = _wfopen(wPath, mode ? L"wb" : L"rb");       xmlFree(wPath);    }    /* maybe path in native encoding */    if(fd == NULL)       fd = fopen(path, mode ? "wb" : "rb");    return fd;}/** *  xmlWrapStatUtf8: * @path:  the path in utf-8 encoding * @info:  structure that stores results * * function obtains information about the file or directory * */static intxmlWrapStatUtf8(const char *path,struct stat *info){#ifdef HAVE_STAT    int retval = -1;    wchar_t *wPath;    wPath = __xmlIOWin32UTF8ToWChar(path);    if (wPath)    {       retval = _wstat(wPath,info);       xmlFree(wPath);    }    /* maybe path in native encoding */    if(retval < 0)       retval = stat(path,info);    return retval;#else    return -1;#endif}/** *  xmlWrapOpenNative: * @path:  the path * @mode:  type of access (0 - read, 1 - write) * * function opens the file specified by @path * */static FILE*xmlWrapOpenNative(const char *path,int mode){    return fopen(path,mode ? "wb" : "rb");}/** *  xmlWrapStatNative: * @path:  the path * @info:  structure that stores results * * function obtains information about the file or directory * */static intxmlWrapStatNative(const char *path,struct stat *info){#ifdef HAVE_STAT    return stat(path,info);#else    return -1;#endif}typedef int (* xmlWrapStatFunc) (const char *f, struct stat *s);static xmlWrapStatFunc xmlWrapStat = xmlWrapStatNative;typedef FILE* (* xmlWrapOpenFunc)(const char *f,int mode);static xmlWrapOpenFunc xmlWrapOpen = xmlWrapOpenNative;/** * xmlInitPlatformSpecificIo: * * Initialize platform specific features. */static voidxmlInitPlatformSpecificIo(void){    static int xmlPlatformIoInitialized = 0;    OSVERSIONINFO osvi;    if(xmlPlatformIoInitialized)      return;    osvi.dwOSVersionInfoSize = sizeof(osvi);    if(GetVersionEx(&osvi) && (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)) {      xmlWrapStat = xmlWrapStatUtf8;      xmlWrapOpen = xmlWrapOpenUtf8;    } else {      xmlWrapStat = xmlWrapStatNative;      xmlWrapOpen = xmlWrapOpenNative;    }    xmlPlatformIoInitialized = 1;    return;}#endif/** * xmlCheckFilename: * @path:  the path to check * * function checks to see if @path is a valid source * (file, socket...) for XML. * * if stat is not available on the target machine, * returns 1.  if stat fails, returns 0 (if calling * stat on the filename fails, it can't be right). * if stat succeeds and the file is a directory, * returns 2.  otherwise returns 1. */intxmlCheckFilename (const char *path){#ifdef HAVE_STAT	struct stat stat_buffer;#endif	if (path == NULL)		return(0);#ifdef HAVE_STAT#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)    if (xmlWrapStat(path, &stat_buffer) == -1)        return 0;#else    if (stat(path, &stat_buffer) == -1)        return 0;#endif#ifdef S_ISDIR    if (S_ISDIR(stat_buffer.st_mode))        return 2;#endif#endif /* HAVE_STAT */    return 1;}static intxmlNop(void) {    return(0);}/** * xmlFdRead: * @context:  the I/O context * @buffer:  where to drop data * @len:  number of bytes to read * * Read @len bytes to @buffer from the I/O channel. * * Returns the number of bytes written */static intxmlFdRead (void * context, char * buffer, int len) {    int ret;    ret = read((int) (long) context, &buffer[0], len);    if (ret < 0) xmlIOErr(0, "read()");    return(ret);}#ifdef LIBXML_OUTPUT_ENABLED/** * xmlFdWrite: * @context:  the I/O context * @buffer:  where to get data * @len:  number of bytes to write * * Write @len bytes from @buffer to the I/O channel. * * Returns the number of bytes written */static intxmlFdWrite (void * context, const char * buffer, int len) {    int ret = 0;    if (len > 0) {	ret = write((int) (long) context, &buffer[0], len);	if (ret < 0) xmlIOErr(0, "write()");    }    return(ret);}#endif /* LIBXML_OUTPUT_ENABLED *//** * xmlFdClose: * @context:  the I/O context * * Close an I/O channel * * Returns 0 in case of success and error code otherwise */static intxmlFdClose (void * context) {    int ret;    ret = close((int) (long) context);    if (ret < 0) xmlIOErr(0, "close()");    return(ret);}/** * xmlFileMatch: * @filename:  the URI for matching * * input from FILE * * * Returns 1 if matches, 0 otherwise */intxmlFileMatch (const char *filename ATTRIBUTE_UNUSED) {    return(1);}/** * xmlFileOpen_real: * @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 */static void *xmlFileOpen_real (const char *filename) {    const char *path = NULL;    FILE *fd;    if (filename == NULL)        return(NULL);    if (!strcmp(filename, "-")) {	fd = stdin;	return((void *) fd);    }    if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17)) {#if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)	path = &filename[17];#else	path = &filename[16];#endif    } else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:///", 8)) {#if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)	path = &filename[8];#else	path = &filename[7];#endif    } else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:/", 6)) {        /* lots of generators seems to lazy to read RFC 1738 */#if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)	path = &filename[6];#else	path = &filename[5];#endif    } else 	path = filename;    if (path == NULL)	return(NULL);    if (!xmlCheckFilename(path))        return(NULL);#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)    fd = xmlWrapOpen(path, 0);#else    fd = fopen(path, "r");#endif /* WIN32 */    if (fd == NULL) xmlIOErr(0, path);    return((void *) fd);}/** * xmlFileOpen: * @filename:  the URI for matching * * Wrapper around xmlFileOpen_real that try it with an unescaped * version of @filename, if this fails fallback to @filename * * Returns a handler or NULL in case or failure */void *xmlFileOpen (const char *filename) {    char *unescaped;    void *retval;    retval = xmlFileOpen_real(filename);    if (retval == NULL) {	unescaped = xmlURIUnescapeString(filename, 0, NULL);	if (unescaped != NULL) {	    retval = xmlFileOpen_real(unescaped);	    xmlFree(unescaped);	}    }    return retval;}#ifdef LIBXML_OUTPUT_ENABLED/** * xmlFileOpenW: * @filename:  the URI for matching * * output to from FILE *, * if @filename is "-" then the standard output is used * * Returns an I/O context or NULL in case of error */static void *xmlFileOpenW (const char *filename) {    const char *path = NULL;    FILE *fd;    if (!strcmp(filename, "-")) {	fd = stdout;	return((void *) fd);    }    if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17))#if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)	path = &filename[17];#else	path = &filename[16];#endif    else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:///", 8)) {#if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)	path = &filename[8];#else	path = &filename[7];#endif    } else 	path = filename;    if (path == NULL)	return(NULL);#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)    fd = xmlWrapOpen(path, 1);#else  	   fd = fopen(path, "wb");#endif /* WIN32 */	 if (fd == NULL) xmlIOErr(0, path);    return((void *) fd);}#endif /* LIBXML_OUTPUT_ENABLED *//** * 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 or < 0 in case of failure */intxmlFileRead (void * context, char * buffer, int len) {    int ret;    if ((context == NULL) || (buffer == NULL))         return(-1);    ret = fread(&buffer[0], 1,  len, (FILE *) context);    if (ret < 0) xmlIOErr(0, "fread()");    return(ret);}#ifdef LIBXML_OUTPUT_ENABLED/** * xmlFileWrite: * @context:  the I/O context * @buffer:  where to drop data * @len:  number of bytes to write * * Write @len bytes from @buffer to the I/O channel. * * Returns the number of bytes written */static intxmlFileWrite (void * context, const char * buffer, int len) {    int items;    if ((context == NULL) || (buffer == NULL))         return(-1);    items = fwrite(&buffer[0], len, 1, (FILE *) context);    if ((items == 0) && (ferror((FILE *) context))) {        xmlIOErr(0, "fwrite()");	return(-1);    }    return(items * len);}#endif /* LIBXML_OUTPUT_ENABLED *//** * xmlFileClose: * @context:  the I/O context * * Close an I/O channel * * Returns 0 or -1 in case of error */intxmlFileClose (void * context) {    FILE *fil;    int ret;    if (context == NULL)        return(-1);    fil = (FILE *) context;    if ((fil == stdout) || (fil == stderr)) {        ret = fflush(fil);	if (ret < 0)	    xmlIOErr(0, "fflush()");	return(0);    }    if (fil == stdin)	return(0);    ret = ( fclose((FILE *) context) == EOF ) ? -1 : 0;    if (ret < 0)        xmlIOErr(0, "fclose()");    return(ret);}/** * xmlFileFlush: * @context:  the I/O context * * Flush an I/O channel */static intxmlFileFlush (void * context) {    int ret;    if (context == NULL)        return(-1);    ret = ( fflush((FILE *) context) == EOF ) ? -1 : 0;    if (ret < 0)        xmlIOErr(0, "fflush()");    return(ret);}#ifdef LIBXML_OUTPUT_ENABLED/** * xmlBufferWrite: * @context:  the xmlBuffer * @buffer:  the data to write

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -