libxml2 2.9.1 clean sources
[tomato.git] / release / src / router / libxml2 / doc / tutorial / includeconvert.c
blob482e56fc021a5d86112067e9270ba21153d7ea84
1 <![CDATA[
2 #include <string.h>
3 #include <libxml/parser.h>
6 unsigned char*
7 convert (unsigned char *in, char *encoding)
9 unsigned char *out;
10 int ret,size,out_size,temp;
11 xmlCharEncodingHandlerPtr handler;
13 size = (int)strlen(in)+1;
14 out_size = size*2-1;
15 out = malloc((size_t)out_size);
17 if (out) {
18 handler = xmlFindCharEncodingHandler(encoding);
20 if (!handler) {
21 free(out);
22 out = NULL;
25 if (out) {
26 temp=size-1;
27 ret = handler->input(out, &out_size, in, &temp);
28 if (ret || temp-size+1) {
29 if (ret) {
30 printf("conversion wasn't successful.\n");
31 } else {
32 printf("conversion wasn't successful. converted: %i octets.\n",temp);
34 free(out);
35 out = NULL;
36 } else {
37 out = realloc(out,out_size+1);
38 out[out_size]=0; /*null terminating out*/
41 } else {
42 printf("no mem\n");
44 return (out);
48 int
49 main(int argc, char **argv) {
51 unsigned char *content, *out;
52 xmlDocPtr doc;
53 xmlNodePtr rootnode;
54 char *encoding = "ISO-8859-1";
57 if (argc <= 1) {
58 printf("Usage: %s content\n", argv[0]);
59 return(0);
62 content = argv[1];
64 out = convert(content, encoding);
66 doc = xmlNewDoc ("1.0");
67 rootnode = xmlNewDocNode(doc, NULL, (const xmlChar*)"root", out);
68 xmlDocSetRootElement(doc, rootnode);
70 xmlSaveFormatFileEnc("-", doc, encoding, 1);
71 return (1);
73 ]]>