MST 2002 John Fleck | bd3b4fd | 2002-11-11 03:41:11 +0000 | [diff] [blame] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame^] | 2 | <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 <string.h> |
| 5 | #include <libxml/parser.h> |
| 6 | |
| 7 | |
| 8 | unsigned char* |
| 9 | convert (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->input(out, &out_size, in, &temp); |
| 30 | if (ret || temp-size+1) { |
| 31 | if (ret) { |
| 32 | printf("conversion wasn't successful.\n"); |
| 33 | } else { |
| 34 | printf("conversion wasn't successful. converted: %i octets.\n",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("no mem\n"); |
| 45 | } |
| 46 | return (out); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | int |
| 51 | main(int argc, char **argv) { |
| 52 | |
| 53 | unsigned char *content, *out; |
| 54 | xmlDocPtr doc; |
| 55 | xmlNodePtr rootnode; |
| 56 | char *encoding = "ISO-8859-1"; |
| 57 | |
| 58 | |
| 59 | if (argc <= 1) { |
| 60 | printf("Usage: %s content\n", argv[0]); |
| 61 | return(0); |
| 62 | } |
| 63 | |
| 64 | content = argv[1]; |
| 65 | |
| 66 | out = convert(content, encoding); |
| 67 | |
| 68 | doc = xmlNewDoc ("1.0"); |
| 69 | rootnode = xmlNewDocNode(doc, NULL, (const xmlChar*)"root", out); |
| 70 | xmlDocSetRootElement(doc, rootnode); |
| 71 | |
| 72 | xmlSaveFormatFileEnc("-", 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> |