xmlwriter.c
来自「xml开源解析代码.版本为libxml2-2.6.29,可支持GB3212.网络」· C语言 代码 · 共 2,416 行 · 第 1/5 页
C
2,416 行
*/intxmlTextWriterWriteString(xmlTextWriterPtr writer, const xmlChar * content){ int count; int sum; xmlLinkPtr lk; xmlTextWriterStackEntry *p; xmlChar *buf; if ((writer == NULL) || (content == NULL)) return -1; sum = 0; buf = (xmlChar *) content; lk = xmlListFront(writer->nodes); if (lk != 0) { p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk); if (p != 0) { switch (p->state) { case XML_TEXTWRITER_NAME: case XML_TEXTWRITER_TEXT:#if 0 buf = NULL; xmlOutputBufferWriteEscape(writer->out, content, NULL);#endif buf = xmlEncodeSpecialChars(NULL, content); break; case XML_TEXTWRITER_ATTRIBUTE: buf = NULL; xmlAttrSerializeTxtContent(writer->out->buffer, writer->doc, NULL, content); break; default: break; } } } if (buf != NULL) { count = xmlTextWriterWriteRaw(writer, buf); if (count < 0) return -1; sum += count; if (buf != content) /* buf was allocated by us, so free it */ xmlFree(buf); } return sum;}/** * xmlOutputBufferWriteBase64: * @out: the xmlOutputBufferPtr * @data: binary data * @len: the number of bytes to encode * * Write base64 encoded data to an xmlOutputBuffer. * Adapted from John Walker's base64.c (http://www.fourmilab.ch/). * * Returns the bytes written (may be 0 because of buffering) or -1 in case of error */static intxmlOutputBufferWriteBase64(xmlOutputBufferPtr out, int len, const unsigned char *data){ static unsigned char dtable[64] = {'A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z', 'a','b','c','d','e','f','g','h','i','j','k','l','m', 'n','o','p','q','r','s','t','u','v','w','x','y','z', '0','1','2','3','4','5','6','7','8','9','+','/'}; int i; int linelen; int count; int sum; if ((out == NULL) || (len < 0) || (data == NULL)) return(-1); linelen = 0; sum = 0; i = 0; while (1) { unsigned char igroup[3]; unsigned char ogroup[4]; int c; int n; igroup[0] = igroup[1] = igroup[2] = 0; for (n = 0; n < 3 && i < len; n++, i++) { c = data[i]; igroup[n] = (unsigned char) c; } if (n > 0) { ogroup[0] = dtable[igroup[0] >> 2]; ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)]; ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)]; ogroup[3] = dtable[igroup[2] & 0x3F]; if (n < 3) { ogroup[3] = '='; if (n < 2) { ogroup[2] = '='; } } if (linelen >= B64LINELEN) { count = xmlOutputBufferWrite(out, 2, B64CRLF); if (count == -1) return -1; sum += count; linelen = 0; } count = xmlOutputBufferWrite(out, 4, (const char *) ogroup); if (count == -1) return -1; sum += count; linelen += 4; } if (i >= len) break; } return sum;}/** * xmlTextWriterWriteBase64: * @writer: the xmlTextWriterPtr * @data: binary data * @start: the position within the data of the first byte to encode * @len: the number of bytes to encode * * Write an base64 encoded xml text. * * Returns the bytes written (may be 0 because of buffering) or -1 in case of error */intxmlTextWriterWriteBase64(xmlTextWriterPtr writer, const char *data, int start, int len){ int count; int sum; xmlLinkPtr lk; xmlTextWriterStackEntry *p; if ((writer == NULL) || (data == NULL) || (start < 0) || (len < 0)) return -1; sum = 0; lk = xmlListFront(writer->nodes); if (lk != 0) { p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk); if (p != 0) { count = xmlTextWriterHandleStateDependencies(writer, p); if (count < 0) return -1; sum += count; } } if (writer->indent) writer->doindent = 0; count = xmlOutputBufferWriteBase64(writer->out, len, (unsigned char *) data + start); if (count < 0) return -1; sum += count; return sum;}/** * xmlOutputBufferWriteBinHex: * @out: the xmlOutputBufferPtr * @data: binary data * @len: the number of bytes to encode * * Write hqx encoded data to an xmlOutputBuffer. * ::todo * * Returns the bytes written (may be 0 because of buffering) * or -1 in case of error */static intxmlOutputBufferWriteBinHex(xmlOutputBufferPtr out, int len, const unsigned char *data){ int count; int sum; static char hex[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; int i; if ((out == NULL) || (data == NULL) || (len < 0)) { return -1; } sum = 0; for (i = 0; i < len; i++) { count = xmlOutputBufferWrite(out, 1, (const char *) &hex[data[i] >> 4]); if (count == -1) return -1; sum += count; count = xmlOutputBufferWrite(out, 1, (const char *) &hex[data[i] & 0xF]); if (count == -1) return -1; sum += count; } return sum;}/** * xmlTextWriterWriteBinHex: * @writer: the xmlTextWriterPtr * @data: binary data * @start: the position within the data of the first byte to encode * @len: the number of bytes to encode * * Write a BinHex encoded xml text. * * Returns the bytes written (may be 0 because of buffering) or -1 in case of error */intxmlTextWriterWriteBinHex(xmlTextWriterPtr writer, const char *data, int start, int len){ int count; int sum; xmlLinkPtr lk; xmlTextWriterStackEntry *p; if ((writer == NULL) || (data == NULL) || (start < 0) || (len < 0)) return -1; sum = 0; lk = xmlListFront(writer->nodes); if (lk != 0) { p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk); if (p != 0) { count = xmlTextWriterHandleStateDependencies(writer, p); if (count < 0) return -1; sum += count; } } if (writer->indent) writer->doindent = 0; count = xmlOutputBufferWriteBinHex(writer->out, len, (unsigned char *) data + start); if (count < 0) return -1; sum += count; return sum;}/** * xmlTextWriterStartAttribute: * @writer: the xmlTextWriterPtr * @name: element name * * Start an xml attribute. * * Returns the bytes written (may be 0 because of buffering) or -1 in case of error */intxmlTextWriterStartAttribute(xmlTextWriterPtr writer, const xmlChar * name){ int count; int sum; xmlLinkPtr lk; xmlTextWriterStackEntry *p; if ((writer == NULL) || (name == NULL) || (*name == '\0')) return -1; sum = 0; lk = xmlListFront(writer->nodes); if (lk == 0) return -1; p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk); if (p == 0) return -1; switch (p->state) { case XML_TEXTWRITER_ATTRIBUTE: count = xmlTextWriterEndAttribute(writer); if (count < 0) return -1; sum += count; /* fallthrough */ case XML_TEXTWRITER_NAME: count = xmlOutputBufferWriteString(writer->out, " "); if (count < 0) return -1; sum += count; count = xmlOutputBufferWriteString(writer->out, (const char *) name); if (count < 0) return -1; sum += count; count = xmlOutputBufferWriteString(writer->out, "="); if (count < 0) return -1; sum += count; count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar); if (count < 0) return -1; sum += count; p->state = XML_TEXTWRITER_ATTRIBUTE; break; default: return -1; } return sum;}/** * xmlTextWriterStartAttributeNS: * @writer: the xmlTextWriterPtr * @prefix: namespace prefix or NULL * @name: element local name * @namespaceURI: namespace URI or NULL * * Start an xml attribute with namespace support. * * Returns the bytes written (may be 0 because of buffering) or -1 in case of error */intxmlTextWriterStartAttributeNS(xmlTextWriterPtr writer, const xmlChar * prefix, const xmlChar * name, const xmlChar * namespaceURI){ int count; int sum; xmlChar *buf; xmlTextWriterNsStackEntry *p; if ((writer == NULL) || (name == NULL) || (*name == '\0')) return -1; /* Handle namespace first in case of error */ if (namespaceURI != 0) { xmlTextWriterNsStackEntry nsentry, *curns; buf = xmlStrdup(BAD_CAST "xmlns"); if (prefix != 0) { buf = xmlStrcat(buf, BAD_CAST ":"); buf = xmlStrcat(buf, prefix); } nsentry.prefix = buf; nsentry.uri = (xmlChar *)namespaceURI; nsentry.elem = xmlListFront(writer->nodes); curns = (xmlTextWriterNsStackEntry *)xmlListSearch(writer->nsstack, (void *)&nsentry); if ((curns != NULL)) { xmlFree(buf); if (xmlStrcmp(curns->uri, namespaceURI) == 0) { /* Namespace already defined on element skip */ buf = NULL; } else { /* Prefix mismatch so error out */ return -1; } } /* Do not add namespace decl to list - it is already there */ if (buf != NULL) { p = (xmlTextWriterNsStackEntry *) xmlMalloc(sizeof(xmlTextWriterNsStackEntry)); if (p == 0) { xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY, "xmlTextWriterStartAttributeNS : out of memory!\n"); return -1; } p->prefix = buf; p->uri = xmlStrdup(namespaceURI); if (p->uri == 0) { xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY, "xmlTextWriterStartAttributeNS : out of memory!\n"); xmlFree(p); return -1; } p->elem = xmlListFront(writer->nodes); xmlListPushFront(writer->nsstack, p); } } buf = NULL; if (prefix != 0) { buf = xmlStrdup(prefix); buf = xmlStrcat(buf, BAD_CAST ":"); } buf = xmlStrcat(buf, name); sum = 0; count = xmlTextWriterStartAttribute(writer, buf); xmlFree(buf); if (count < 0) return -1; sum += count; return sum;}/** * xmlTextWriterEndAttribute: * @writer: the xmlTextWriterPtr * * End the current xml element. * * Returns the bytes written (may be 0 because of buffering) or -1 in case of error */intxmlTextWriterEndAttribute(xmlTextWriterPtr writer){ int count; int sum; xmlLinkPtr lk; xmlTextWriterStackEntry *p; if (writer == NULL) return -1; lk = xmlListFront(writer->nodes); if (lk == 0) { return -1; } p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk); if (p == 0) { return -1; } sum = 0; switch (p->state) { case XML_TEXTWRITER_ATTRIBUTE: p->state = XML_TEXTWRITER_NAME; count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar); if (count < 0) { return -1; } sum += count; break; default: return -1; } return sum;}/** * xmlTextWriterWriteFormatAttribute: * @writer: the xmlTextWriterPtr * @name: attribute name * @format: format string (see printf) * @...: extra parameters for the format
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?