blob: fbabf0a4e0adbd6aefd53366f42b099f98e2e57c [file] [log] [blame]
MST 2002 John Fleckbd3b4fd2002-11-11 03:41:11 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +00002<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>G. Code for Encoding Conversion Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="apf.html" title="F. Code for Retrieving Attribute Value Example"><link rel="next" href="aph.html" title="H. Acknowledgements"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">G. Code for Encoding Conversion Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="apf.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="aph.html">Next</a></td></tr></table><hr></div><div class="appendix" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="convertappendix"></a>G. Code for Encoding Conversion Example</h2></div></div><div></div></div><p>
3 </p><pre class="programlisting">
4#include &lt;string.h&gt;
5#include &lt;libxml/parser.h&gt;
6
7
8unsigned char*
9convert (unsigned char *in, char *encoding)
10{
11 unsigned char *out;
12 int ret,size,out_size,temp;
13 xmlCharEncodingHandlerPtr handler;
14
15 size = (int)strlen(in)+1;
16 out_size = size*2-1;
17 out = malloc((size_t)out_size);
18
19 if (out) {
20 handler = xmlFindCharEncodingHandler(encoding);
21
22 if (!handler) {
23 free(out);
24 out = NULL;
25 }
26 }
27 if (out) {
28 temp=size-1;
29 ret = handler-&gt;input(out, &amp;out_size, in, &amp;temp);
30 if (ret || temp-size+1) {
31 if (ret) {
32 printf(&quot;conversion wasn't successful.\n&quot;);
33 } else {
34 printf(&quot;conversion wasn't successful. converted: %i octets.\n&quot;,temp);
35 }
36 free(out);
37 out = NULL;
38 } else {
39 out = realloc(out,out_size+1);
40 out[out_size]=0; /*null terminating out*/
41
42 }
43 } else {
44 printf(&quot;no mem\n&quot;);
45 }
46 return (out);
47}
48
49
50int
51main(int argc, char **argv) {
52
53 unsigned char *content, *out;
54 xmlDocPtr doc;
55 xmlNodePtr rootnode;
56 char *encoding = &quot;ISO-8859-1&quot;;
57
58
59 if (argc &lt;= 1) {
60 printf(&quot;Usage: %s content\n&quot;, argv[0]);
61 return(0);
62 }
63
64 content = argv[1];
65
66 out = convert(content, encoding);
67
68 doc = xmlNewDoc (&quot;1.0&quot;);
69 rootnode = xmlNewDocNode(doc, NULL, (const xmlChar*)&quot;root&quot;, out);
70 xmlDocSetRootElement(doc, rootnode);
71
72 xmlSaveFormatFileEnc(&quot;-&quot;, doc, encoding, 1);
73 return (1);
74}
75
76</pre><p>
77 </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="apf.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="aph.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">F. Code for Retrieving Attribute Value Example </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> H. Acknowledgements</td></tr></table></div></body></html>