📄 nxml.h
字号:
//================================
// File:nxml.h
// Modfied by seeker
// 2007-007-01 15:29
//================================#ifndef __N_XML_H__#define __N_XML_H__//#include <curl/curl.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <stdarg.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <errno.h>#ifdef __cplusplusextern "C" {#endif typedef struct nxml_t nxml_t; typedef struct nxml_data_t nxml_data_t; typedef struct nxml_attr_t nxml_attr_t; typedef struct nxml_doctype_t nxml_doctype_t; typedef struct nxml_namespace_t nxml_namespace_t; typedef struct __nxml_doctype_notation_t __nxml_doctype_notation_t; typedef struct __nxml_doctype_element_t __nxml_doctype_element_t; typedef struct __nxml_doctype_element_content_t __nxml_doctype_element_content_t; typedef struct __nxml_doctype_attribute_t __nxml_doctype_attribute_t; typedef struct __nxml_doctype_attribute_attdef_t __nxml_doctype_attribute_attdef_t; typedef struct __nxml_doctype_attribute_attdef_list_t __nxml_doctype_attribute_attdef_list_t; typedef struct __nxml_doctype_entity_t __nxml_doctype_entity_t; typedef struct __nxml_private_t __nxml_private_t; /** This enum describes the error type of libnxml */ typedef enum { NXML_OK = 0, /**< No error */ NXML_ERR_POSIX, /**< For the correct error, use errno */ NXML_ERR_PARSER, /**< Parser error */ NXML_ERR_DATA /**< The parameters are incorrect */ } nxml_error_t; /** This enum describes the type of data element of libnxml */ typedef enum { NXML_TYPE_TEXT, /**< Text element */ NXML_TYPE_COMMENT, /**< Comment element */ NXML_TYPE_ELEMENT, /**< Data element */ NXML_TYPE_PI, /**< PI element */ NXML_TYPE_ELEMENT_CLOSE, /**< Data element - For internal use only */ } nxml_type_t; /** This enum describes the supported XML version */ typedef enum { NXML_VERSION_1_1, /**< XML 1.1 */ NXML_VERSION_1_0 /**< XML 1.0 */ } nxml_version_t; /** This enum describes the CharSet of XML document */ typedef enum { NXML_CHARSET_UTF8, /**< UTF8 chatset detected */ NXML_CHARSET_UTF16LE, /**< UTF 16 Little Endian detected */ NXML_CHARSET_UTF16BE, /**< UTF 16 Big Endian detected */ NXML_CHARSET_UCS4_1234, /**< UCS 4byte order 1234 detected */ NXML_CHARSET_UCS4_4321, /**< UCS 3byte order 4321 detected */ NXML_CHARSET_UCS4_2143, /**< UCS 3byte order 2143 detected */ NXML_CHARSET_UCS4_3412, /**< UCS 3byte order 3412 detected */ NXML_CHARSET_UNKNOWN /**< Unknown format */ } nxml_charset_t;#define NXML_DOCTYPEFLAG_NOFLAG 0x0 /**< No flag */#define NXML_DOCTYPEFLAG_DOWNLOAD 0x1 /**< Download the document */#define NXML_DOCTYPEFLAG_RECURSIVE 0x2 /**< Do it recursive */ /** This enum describes the type of element for the doctype parsing. * It is for internal use only */ typedef enum { NXML_DOCTYPE_ELEMENT_EMPTY, NXML_DOCTYPE_ELEMENT_ANY, NXML_DOCTYPE_ELEMENT_MIXED_OR_CHILDREN, } __nxml_doctype_element_type_t; typedef enum { NXML_DOCTYPE_ELEMENT_CONTENT_ONE = 0, NXML_DOCTYPE_ELEMENT_CONTENT_0_OR_1, NXML_DOCTYPE_ELEMENT_CONTENT_0_OR_MORE, NXML_DOCTYPE_ELEMENT_CONTENT_1_OR_MORE } __nxml_doctype_element_content_type_t; typedef enum { NXML_DOCTYPE_ATTRIBUTE_TYPE_ENUMERATION, NXML_DOCTYPE_ATTRIBUTE_TYPE_NOTATION, NXML_DOCTYPE_ATTRIBUTE_TYPE_CDATA, NXML_DOCTYPE_ATTRIBUTE_TYPE_ID, NXML_DOCTYPE_ATTRIBUTE_TYPE_IDREF, NXML_DOCTYPE_ATTRIBUTE_TYPE_IDREFS, NXML_DOCTYPE_ATTRIBUTE_TYPE_ENTITY, NXML_DOCTYPE_ATTRIBUTE_TYPE_ENTITIES, NXML_DOCTYPE_ATTRIBUTE_TYPE_NMTOKEN, NXML_DOCTYPE_ATTRIBUTE_TYPE_NMTOKENS, } __nxml_doctype_attribute_attdef_type_t; typedef enum { NXML_DOCTYPE_ATTRIBUTE_VALUE_ANY, NXML_DOCTYPE_ATTRIBUTE_VALUE_REQUIRED, NXML_DOCTYPE_ATTRIBUTE_VALUE_IMPLIED, NXML_DOCTYPE_ATTRIBUTE_VALUE_FIXED } __nxml_doctype_attribute_attdef_value_t; // Data struct for any element of XML stream /files struct nxml_data_t { nxml_type_t type; /**< type of this nxml_data_t struct */ char *value; /**< The value of this data struct */ nxml_attr_t *attributes; /**< List of attributes of this struct. This list exists only if type == NXML_TYPE_ELEMENT */ nxml_namespace_t *ns; /**< Pointer to the correct namespace */ nxml_namespace_t *ns_list; /**< The namespaces in this element */ nxml_data_t *children; /**< The children of this data struct */ nxml_data_t *next; /**< The next element */ nxml_data_t *parent; /**< The parent */ nxml_t *doc; /**< The nxml_t */ }; // Data struct for any element of attribute of xml element struct nxml_attr_t { char *name; char *value; nxml_namespace_t *ns; nxml_attr_t *next; }; //** Data struct for doctype elements struct nxml_doctype_t { char *value; /**< The string no parsers */ char *name; /**< The name of current doctype */ nxml_t *doc; /**< The nxml_t */ nxml_doctype_t *next; /** This information will be set only after a nxml_valid_dtd(...) */ char *system_literal; /**< If the DTD has a system_leteral */ char *pubid_literal; /**< If the DTD has a system_leteral */ }; //** Data struct private for doctype parsing of contents of a element struct __nxml_doctype_element_content_t { char *name; int pcdata; int choose; __nxml_doctype_element_content_type_t type; __nxml_doctype_element_content_t *list; __nxml_doctype_element_content_t *next; }; //** Data struct private for doctype parsing of elements struct __nxml_doctype_element_t { char *name; __nxml_doctype_element_content_t *content; __nxml_doctype_element_type_t type; __nxml_doctype_element_t *next; }; //** Data struct for any element of attribute of xml element struct __nxml_doctype_attribute_attdef_list_t { char *value; __nxml_doctype_attribute_attdef_list_t *next; }; //** Data struct private for doctype parsing of attdefs of a attribute struct __nxml_doctype_attribute_attdef_t { char *name; __nxml_doctype_attribute_attdef_type_t type; __nxml_doctype_attribute_attdef_value_t value; char *fixed_value; __nxml_doctype_attribute_attdef_list_t *list; __nxml_doctype_attribute_attdef_t *next; }; //** Data struct private for doctype parsing of attributes struct __nxml_doctype_attribute_t { char *element; __nxml_doctype_attribute_attdef_t *attdef; __nxml_doctype_attribute_t *next; }; //** Data struct private for doctype parsing of entities struct __nxml_doctype_entity_t { int percent; char *name; char *reference; char *system; char *pubid; char *ndata; __nxml_doctype_entity_t *next; }; //** Data struct private for doctype parsing of notations struct __nxml_doctype_notation_t { char *system_literal; char *pubid_literal; __nxml_doctype_notation_t *next; }; //** Data struct for namespace struct nxml_namespace_t { char *prefix; char *ns; nxml_namespace_t *next; }; //** Data struct private for internal use only struct __nxml_private_t { void (*func) (char *, ...); int line; int timeout; __nxml_doctype_element_t *elements; __nxml_doctype_attribute_t *attributes; __nxml_doctype_entity_t *entities; __nxml_doctype_notation_t *notations; }; //** Principal data struct. It contains pointers to any other structures. struct nxml_t { char *file; /**< XML document filename or url */ size_t size; /**< Size of XML document in byte */ nxml_version_t version; /**< XML document version */ int standalone; /**< This document is standalone ? */ char *encoding; /**< Encoding type */ nxml_charset_t charset_detected; /**< charset detected when the a XML document is parsed. The document will be convert to UTF-8 */ nxml_data_t *data; /**< The data of XML document */ nxml_doctype_t *doctype; /**< The doctype of XML document */ __nxml_private_t priv; /**< For internal use only */ }; //***** INIT FUNCTIONS nxml_error_t nxml_new (nxml_t ** nxml); //***** PARSER FUNCTIONS nxml_error_t nxml_parse_file (nxml_t * nxml, char *file); nxml_error_t nxml_parse_buffer (nxml_t * nxml, char *buffer, size_t size); //***** FREE FUNCTIONS nxml_error_t nxml_empty (nxml_t * nxml); nxml_error_t nxml_free (nxml_t * nxml); nxml_error_t nxml_empty_doctype (nxml_doctype_t * doctype); nxml_error_t nxml_free_doctype (nxml_doctype_t *doctype); nxml_error_t nxml_free_data (nxml_data_t *data); nxml_error_t nxml_free_attribute (nxml_attr_t *data); nxml_error_t nxml_free_namespace (nxml_namespace_t *data); //***** EDIT FUNCTIONS nxml_error_t nxml_root_element (nxml_t *nxml, nxml_data_t **element); nxml_error_t nxml_find_element (nxml_t *nxml, nxml_data_t *parent, char *name, nxml_data_t **element); nxml_error_t nxml_find_attribute (nxml_data_t *data, char *name, nxml_attr_t **attribute); nxml_error_t nxml_find_namespace (nxml_data_t *data, char *name, nxml_namespace_t **ns); //***** ERROR FUNCTIONS char * nxml_strerror (nxml_error_t err); nxml_data_t * nxmle_root_element (nxml_t *nxml, nxml_error_t *err); char * nxmle_find_attribute (nxml_data_t *element, char *name, nxml_error_t *err); // define for easy funtion#define nxmle_empty nxml_empty#define nxmle_free nxml_free#define nxmle_free_data nxml_free_data#define nxmle_free_attribute nxml_free_attribute#define nxmle_strerror nxml_strerror //======================================================= // from nxml_internal.h //======================================================= /* Rule [4] */#define __NXML_NAMESTARTCHARS \ ((ch=__NXML_U8()) == ':' \ || (ch >= 'a' && ch <= 'z') \ || ch=='_' \ || (ch >= 'A' && ch <= 'Z') \ || (ch >= 0xc0 && ch <= 0xd6) \ || (ch >= 0xd8 && ch <= 0xf6) \ || (ch >= 0xf8 && ch <= 0x2ff) \ || (ch >= 0x370 && ch <= 0x37d) \ || (ch >= 0x37f && ch <= 0x1fff) \ || (ch >= 0x200c && ch <= 0x200d) \ || (ch >= 0x2070 && ch <= 0x218f) \ || (ch >= 0x2c00 && ch <= 0x2fef) \ || (ch >= 0x3001 && ch <= 0xd7ff) \ || (ch >= 0xf900 && ch <= 0xfdcf) \ || (ch >= 0xfdf0 && ch <= 0xfffd) \ || (ch >= 0x10000 && ch <= 0xeffff)) /* Rule [4a] */#define __NXML_NAMECHARS \ (__NXML_NAMESTARTCHARS \ || ch=='-' \ || ch=='.' \ || (ch >= '0' && ch <= '9') \ || ch==0xb7 \ || (ch >= 0x0300 && ch <= 0x036f) \ || (ch >= 0x203f && ch <= 0x2040))#define __NXML_U8() __nxml_utf8((unsigned char **)buffer, size, &byte) int64_t __nxml_utf8 (unsigned char **buffer, size_t *size, int *byte); int64_t __nxml_int_charset(int i, unsigned char *buffer,char *charset); int __nxml_utf_detection (char *r_buffer,size_t r_size,char **buffer, size_t *size,nxml_charset_t *); int __nxml_escape_spaces(nxml_t * doc,char **buffer,size_t * size); char * __nxml_get_value(nxml_t * doc,char **buffer,size_t * size); char * __nxml_trim (char *tmp); // namespace void __nxml_namespace_parse(nxml_t *nxml); //=======================================================#ifdef __cplusplus}#endif#endif/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -