-
Notifications
You must be signed in to change notification settings - Fork 31
/
httpxml.c
90 lines (79 loc) · 2 KB
/
httpxml.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <stdarg.h>
#include "httpxml.h"
#ifdef WIN32
#define snprintf _snprintf
#endif
int mwWriteXmlHeader(char** pbuf, int* pbufsize, int ver, char* charset, char* xsl)
{
int len2 = 0;
int len1 = snprintf(*pbuf, *pbufsize, "<?xml version=\"%d.%d\" encoding=\"%s\"?>\n",
ver / 10, ver % 10, charset ? charset : "utf-8");
*pbuf += len1;
*pbufsize -= len1;
if (xsl){
len2 = snprintf(*pbuf, *pbufsize, "<?xml-stylesheet type=\"text/xsl\" href=\"%s\"?>\n", xsl);
*pbuf += len2;
*pbufsize -= len2;
}
return len1 + len2;
}
void mwWriteXmlString(char** pbuf, int* pbufsize, int indent, char* str)
{
int i;
int len;
int bufsize = *pbufsize;
if (bufsize < indent * 2) return;
for (i = 0; i < indent; i++) {
*((*pbuf) ++) = ' ';
*((*pbuf) ++) = ' ';
}
bufsize -= indent * 2;
len = snprintf(*pbuf, bufsize, "%s%s", str, indent ? "\n" : "");
*pbuf += len;
bufsize -= len;
*pbufsize = bufsize;
}
int mwWriteXmlLine(char** pbuf, int* pbufsize, HTTP_XML_NODE *node, char *attr, const char *fmt, ...)
{
int bufsize = *pbufsize;
int len;
int i;
va_list args;
if (bufsize < node->indent * 2) return -1;
for (i = 0; i < node->indent; i++) {
*((*pbuf)++) = ' ';
*((*pbuf)++) = ' ';
}
bufsize -= node->indent * 2;
len = snprintf(*pbuf, bufsize, "<%s", node->name);
*pbuf += len;
bufsize -= len;
if (attr) {
len = snprintf(*pbuf, bufsize, " %s", attr);
*pbuf += len;
bufsize -= len;
}
*((*pbuf)++) = '>';
bufsize--;
if (node->flags & XN_CDATA) {
len = snprintf(*pbuf, bufsize, "%s", "<![CDATA[");
*pbuf += len;
bufsize -= len;
}
va_start(args, fmt);
len = vsnprintf(*pbuf, bufsize, fmt, args);
va_end(args);
*pbuf += len;
bufsize -= len;
if (node->flags & XN_CDATA) {
len = snprintf(*pbuf, bufsize, "%s", "]]>");
*pbuf += len;
bufsize -= len;
}
len = snprintf(*pbuf, bufsize, "</%s>\n", node->name);
*pbuf += len;
bufsize -= len;
*pbufsize = bufsize;
return 0;
}