| Line | |
|---|
| 1 |
|
|---|
| 2 |
#include "noit_xml.h" |
|---|
| 3 |
|
|---|
| 4 |
struct noit_xml_buffer_ptr { |
|---|
| 5 |
char *buff; |
|---|
| 6 |
int raw_len; |
|---|
| 7 |
int len; |
|---|
| 8 |
int allocd; |
|---|
| 9 |
}; |
|---|
| 10 |
static int |
|---|
| 11 |
noit_xml_save_writer(void *vstr, const char *buffer, int len) { |
|---|
| 12 |
struct noit_xml_buffer_ptr *clv = vstr; |
|---|
| 13 |
if(!clv->buff) { |
|---|
| 14 |
clv->allocd = 8192; |
|---|
| 15 |
clv->buff = malloc(clv->allocd); |
|---|
| 16 |
} |
|---|
| 17 |
while(len + clv->len > clv->allocd) { |
|---|
| 18 |
char *newbuff; |
|---|
| 19 |
int newsize = clv->allocd; |
|---|
| 20 |
newsize <<= 1; |
|---|
| 21 |
newbuff = realloc(clv->buff, newsize); |
|---|
| 22 |
if(!newbuff) { |
|---|
| 23 |
return -1; |
|---|
| 24 |
} |
|---|
| 25 |
clv->allocd = newsize; |
|---|
| 26 |
clv->buff = newbuff; |
|---|
| 27 |
} |
|---|
| 28 |
memcpy(clv->buff + clv->len, buffer, len); |
|---|
| 29 |
clv->len += len; |
|---|
| 30 |
return len; |
|---|
| 31 |
} |
|---|
| 32 |
static int |
|---|
| 33 |
noit_xml_save_closer(void *vstr) { |
|---|
| 34 |
struct noit_xml_buffer_ptr *clv = vstr; |
|---|
| 35 |
if(clv->buff == NULL) return 0; |
|---|
| 36 |
clv->buff[clv->len] = '\0'; |
|---|
| 37 |
return 0; |
|---|
| 38 |
} |
|---|
| 39 |
|
|---|
| 40 |
char * |
|---|
| 41 |
noit_xmlSaveToBuffer(xmlDocPtr doc) { |
|---|
| 42 |
char *outbuff; |
|---|
| 43 |
xmlOutputBufferPtr out; |
|---|
| 44 |
xmlCharEncodingHandlerPtr enc; |
|---|
| 45 |
struct noit_xml_buffer_ptr *buf; |
|---|
| 46 |
|
|---|
| 47 |
buf = calloc(1, sizeof(*buf)); |
|---|
| 48 |
enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8); |
|---|
| 49 |
out = xmlOutputBufferCreateIO(noit_xml_save_writer, |
|---|
| 50 |
noit_xml_save_closer, |
|---|
| 51 |
buf, enc); |
|---|
| 52 |
xmlSaveFormatFileTo(out, doc, "utf8", 1); |
|---|
| 53 |
outbuff = buf->buff; |
|---|
| 54 |
free(buf); |
|---|
| 55 |
return outbuff; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|