xml.c
资源名称:Client.rar [点击查看]
上传用户:szjkjd
上传日期:2022-06-27
资源大小:8968k
文件大小:11k
源码类别:
浏览器
开发平台:
Visual C++
- #include <stdlib.h>
- #include <stdarg.h>
- #include <memory.h>
- #include <errno.h>
- #include <string.h>
- #include <libxml/xmlsave.h>
- #include "xml.h"
- TXML *xml_create(const char *filename, const char *encoding, const char *root)
- {
- TXML *xml;
- int saveno;
- xmlNodePtr root_node;
- if (!(xml = (TXML *)malloc(sizeof(TXML))))
- {
- return NULL;
- }
- memset(xml, 0, sizeof(TXML));
- xmlKeepBlanksDefault(0);
- if (!(xml->doc = xmlNewDoc(BAD_CAST("1.0"))))
- {
- saveno = errno;
- free(xml);
- errno = saveno;
- return NULL;
- }
- xml->encoding = strdup(encoding);
- xml->filename = filename ? strdup(filename) : NULL;
- root_node = xmlNewNode(NULL, BAD_CAST(root));
- xmlDocSetRootElement(xml->doc, root_node);
- FIFO_INIT(&xml->rubbish);
- return xml;
- }
- TXML *xml_create_file(const char *filename)
- {
- TXML *xml;
- int saveno;
- if (!filename)
- {
- errno = EINVAL;
- return NULL;
- }
- if (!(xml = (TXML *)malloc(sizeof(TXML))))
- {
- return NULL;
- }
- memset(xml, 0, sizeof(TXML));
- xmlKeepBlanksDefault(0);
- if (!(xml->doc = xmlParseFile(filename)))
- {
- saveno = errno;
- free(xml);
- errno = saveno;
- return NULL;
- }
- xml->filename = strdup(filename);
- FIFO_INIT(&xml->rubbish);
- return xml;
- }
- TXML *xml_create_memory(const char *buffer, size_t size)
- {
- TXML *xml;
- int saveno;
- if (!(buffer && size))
- {
- errno = EINVAL;
- return NULL;
- }
- if (!(xml = (TXML *)malloc(sizeof(TXML))))
- {
- return NULL;
- }
- memset(xml, 0, sizeof(TXML));
- xmlKeepBlanksDefault(0);
- if (!(xml->doc = xmlParseMemory(buffer, size)))
- {
- saveno = errno;
- free(xml);
- errno = saveno;
- return NULL;
- }
- FIFO_INIT(&xml->rubbish);
- return xml;
- }
- void xml_cleanup(TXML *xml)
- {
- TXMLStr *res;
- #ifndef WIN32
- while (FIFO_POP(&xml->rubbish, res, node))
- {
- if (res->str)
- {
- xmlFree((xmlChar *)res->str);
- }
- free(res);
- }
- #else
- FIFO_POP(&xml->rubbish, res, node);
- while (res)
- {
- if (res->str)
- {
- xmlFree((xmlChar *)res->str);
- }
- free(res);
- FIFO_POP(&xml->rubbish, res, node);
- }
- #endif
- return;
- }
- void xml_close(TXML *xml)
- {
- if (xml)
- {
- xml_cleanup(xml);
- if (xml->object)
- {
- xmlXPathFreeObject(xml->object);
- }
- if (xml->doc)
- {
- xmlFreeDoc(xml->doc);
- xmlCleanupParser();
- }
- if (xml->encoding)
- {
- free(xml->encoding);
- }
- if (xml->filename)
- {
- free(xml->filename);
- }
- free(xml);
- }
- return;
- }
- int xml_save_to(TXML *xml, const char *filename)
- {
- char *encoding = xml->encoding ? xml->encoding : (char *)xml->doc->encoding;
- return xmlSaveFormatFileEnc(filename, xml->doc, encoding, XML_SAVE_FORMAT) ? -1 : 0;
- }
- int xml_save(TXML *xml)
- {
- char *encoding;
- if (!xml->filename)
- {
- errno = EINVAL;
- return -1;
- }
- encoding = xml->encoding ? xml->encoding : (char *)xml->doc->encoding;
- return xmlSaveFormatFileEnc(xml->filename, xml->doc, encoding, XML_SAVE_FORMAT) ? -1 : 0;
- }
- int xml_save_close(TXML *xml)
- {
- int ret;
- int saveno;
- ret = xml_save(xml);
- saveno = errno;
- xml_close(xml);
- errno = saveno;
- return ret;
- }
- int xml_select_nodes(TXML *xml, const char *xpath)
- {
- xmlXPathContextPtr context;
- if(!xml || !xpath)
- {
- errno = EINVAL;
- return -1;
- }
- if (!(context = xmlXPathNewContext(xml->doc)))
- {
- return -1;
- }
- if (xml->object)
- {
- xmlXPathFreeObject(xml->object);
- }
- if (!(xml->object = xmlXPathEvalExpression(BAD_CAST(xpath), context)))
- {
- goto err_context;
- }
- if (xmlXPathNodeSetIsEmpty(xml->object->nodesetval))
- {
- goto err_object;
- }
- xmlXPathFreeContext(context);
- return xml->object->nodesetval->nodeNr > 0 ? xml->object->nodesetval->nodeNr : 0;
- err_object:
- xmlXPathFreeObject(xml->object);
- xml->object = NULL;
- err_context:
- xmlXPathFreeContext(context);
- return 0;
- }
- char *xml_read_set_prop(TXML *xml, const int n, const char *name)
- {
- TXMLStr *res;
- if (!(xml && xml->object))
- {
- errno = EINVAL;
- return NULL;
- }
- if (xmlXPathNodeSetIsEmpty(xml->object->nodesetval))
- {
- errno = EINVAL;
- return NULL;
- }
- if (xml->object->nodesetval->nodeNr < n)
- {
- errno = EINVAL;
- return NULL;
- }
- if (!xmlHasProp(xml->object->nodesetval->nodeTab[n], BAD_CAST(name)))
- {
- errno = EINVAL;
- return NULL;
- }
- if (!(res = (TXMLStr *)malloc(sizeof(TXMLStr))))
- {
- return NULL;
- }
- res->str = (char *)xmlGetProp(xml->object->nodesetval->nodeTab[n], BAD_CAST(name));
- FIFO_PUSH(&xml->rubbish, res, node);
- return res->str;
- }
- char *xml_read_set_content(TXML *xml, const int n)
- {
- TXMLStr *res;
- if (!(xml && xml->object))
- {
- errno = EINVAL;
- return NULL;
- }
- if (xmlXPathNodeSetIsEmpty(xml->object->nodesetval))
- {
- errno = EINVAL;
- return NULL;
- }
- if (xml->object->nodesetval->nodeNr < n)
- {
- errno = EINVAL;
- return NULL;
- }
- if (!(res = (TXMLStr *)malloc(sizeof(TXMLStr))))
- {
- return NULL;
- }
- res->str = (char *)xmlNodeGetContent(xml->object->nodesetval->nodeTab[n]);
- FIFO_PUSH(&xml->rubbish, res, node);
- return res->str;
- }
- int xml_write_set_prop(TXML *xml, const int n, const char *name, const char *value)
- {
- if (!(xml && xml->object))
- {
- errno = EINVAL;
- return -1;
- }
- if (xmlXPathNodeSetIsEmpty(xml->object->nodesetval))
- {
- errno = EINVAL;
- return -1;
- }
- if (xml->object->nodesetval->nodeNr < n)
- {
- errno = EINVAL;
- return -1;
- }
- if (!xmlHasProp(xml->object->nodesetval->nodeTab[n], BAD_CAST(name)))
- {
- errno = EINVAL;
- return -1;
- }
- xmlSetProp(xml->object->nodesetval->nodeTab[n], BAD_CAST(name), BAD_CAST(value));
- return 0;
- }
- int xml_write_set_content(TXML *xml, const int n, const char *value)
- {
- if (!(xml && xml->object))
- {
- errno = EINVAL;
- return -1;
- }
- if (xmlXPathNodeSetIsEmpty(xml->object->nodesetval))
- {
- errno = EINVAL;
- return -1;
- }
- if (xml->object->nodesetval->nodeNr < n)
- {
- errno = EINVAL;
- return -1;
- }
- xmlNodeSetContent(xml->object->nodesetval->nodeTab[n], BAD_CAST(value));
- return 0;
- }
- char *xml_read_item_prop(TXML *xml, const char *xpath, const char *name)
- {
- return xml_select_nodes(xml, xpath) > 0 ? xml_read_set_prop(xml, 0, name) : NULL;
- }
- char *xml_read_item_content(TXML *xml, const char *xpath)
- {
- return xml_select_nodes(xml, xpath) > 0 ? xml_read_set_content(xml, 0) : NULL;
- }
- int xml_write_item_prop(TXML *xml, const char *xpath, const char *name, const char *value)
- {
- return xml_select_nodes(xml, xpath) > 0 ? xml_write_set_prop(xml, 0, name, value) : -1;
- }
- int xml_write_item_content(TXML *xml, const char *xpath, const char *value)
- {
- return xml_select_nodes(xml, xpath) > 0 ? xml_write_set_content(xml, 0, value) : -1;
- }
- static int xml_add_node(xmlNodePtr (*add)(xmlNodePtr, xmlNodePtr), TXML *xml, const char *xpath,
- const char *node_name, va_list ap)
- {
- xmlNodePtr node;
- char *str = NULL, *name = NULL;
- if (xml_select_nodes(xml, xpath) != 1)
- {
- return -1;
- }
- node = xmlNewNode(NULL, BAD_CAST(node_name));
- while ((str = va_arg(ap, char *)))
- {
- if (!name)
- {
- name = str;
- }
- else
- {
- xmlSetProp(node, BAD_CAST(name), BAD_CAST(str));
- name = NULL;
- }
- }
- if (name)
- {
- xmlNodeSetContent(node, BAD_CAST(name));
- }
- return add(xml->object->nodesetval->nodeTab[0], node) ? 0 : -1;
- }
- int xml_add_child(TXML *xml, const char *xpath, const char *name, ...)
- {
- int ret;
- va_list ap;
- va_start(ap, name);
- ret = xml_add_node(xmlAddChild, xml, xpath, name, ap);
- va_end(ap);
- return ret;
- }
- int xml_add_prev_sibling(TXML *xml, const char *xpath, const char *name, ...)
- {
- int ret;
- va_list ap;
- va_start(ap, name);
- ret = xml_add_node(xmlAddPrevSibling, xml, xpath, name, ap);
- va_end(ap);
- return ret;
- }
- int xml_add_next_sibling(TXML *xml, const char *xpath, const char *name, ...)
- {
- int ret;
- va_list ap;
- va_start(ap, name);
- ret = xml_add_node(xmlAddNextSibling, xml, xpath, name, ap);
- va_end(ap);
- return ret;
- }
- int xml_add_sibling(TXML *xml, const char *xpath, const char *name, ...)
- {
- int ret;
- va_list ap;
- va_start(ap, name);
- ret = xml_add_node(xmlAddSibling, xml, xpath, name, ap);
- va_end(ap);
- return ret;
- }
- int xml_remove(TXML *xml, const char *xpath)
- {
- int i, n;
- xmlNodePtr tempNode, curNode;
- if ((n = xml_select_nodes(xml, xpath)) <= 0)
- {
- return -1;
- }
- for (i = 0; i < n; ++i)
- {
- curNode = xml->object->nodesetval->nodeTab[i];
- tempNode = curNode->next;
- xmlUnlinkNode(curNode);
- xmlFreeNode(curNode);
- curNode = tempNode;
- }
- if (xml->object)
- {
- xmlXPathFreeObject(xml->object);
- xml->object = NULL;
- }
- return 0;
- }
- // vim: ts=8 sw=8 expandtab