<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>utf16utf8.cpp Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body> <!-- Generated by Doxygen 1.2.15 --> <center> <a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="namespaces.html">Namespace List</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="classes.html">Alphabetical List</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Compound Members</a> </center> <hr><h1>utf16utf8.cpp</h1><div class="fragment"><pre>00001 <font class="comment">/******************************************************************************</font> 00002 <font class="comment"> *</font> 00003 <font class="comment"> * UTF16UTF8 - SWFilter decendant to convert UTF-16 to UTF-8</font> 00004 <font class="comment"> *</font> 00005 <font class="comment"> */</font> 00006 00007 <font class="preprocessor">#include <stdlib.h></font> 00008 <font class="preprocessor">#include <stdio.h></font> 00009 00010 <font class="preprocessor">#include <utf16utf8.h></font> 00011 00012 UTF16UTF8::UTF16UTF8() { 00013 } 00014 00015 00016 <font class="keywordtype">char</font> UTF16UTF8::ProcessText(<font class="keywordtype">char</font> *text, <font class="keywordtype">int</font> maxlen, <font class="keyword">const</font> <a class="code" href="class_s_w_key.html">SWKey</a> *key, <font class="keyword">const</font> <a class="code" href="class_s_w_module.html">SWModule</a> *module) 00017 { 00018 <font class="keywordtype">unsigned</font> <font class="keywordtype">short</font> *from; 00019 <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *to; 00020 00021 <font class="keywordtype">int</font> len; 00022 <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> uchar; 00023 <font class="keywordtype">unsigned</font> <font class="keywordtype">short</font> schar; 00024 00025 len = 0; 00026 from = (<font class="keywordtype">unsigned</font> <font class="keywordtype">short</font>*) text; 00027 <font class="keywordflow">while</font> (*from) { 00028 len += 2; 00029 from++; 00030 } 00031 00032 <font class="comment">// shift string to right of buffer</font> 00033 <font class="keywordflow">if</font> (len < maxlen) { 00034 memmove(&text[maxlen - len], text, len); 00035 from = (<font class="keywordtype">unsigned</font> <font class="keywordtype">short</font>*)&text[maxlen - len]; 00036 } 00037 <font class="keywordflow">else</font> 00038 from = (<font class="keywordtype">unsigned</font> <font class="keywordtype">short</font>*)text; 00039 00040 00041 <font class="comment">// -------------------------------</font> 00042 00043 <font class="keywordflow">for</font> (to = (<font class="keywordtype">unsigned</font> <font class="keywordtype">char</font>*)text; *from; from++) { 00044 uchar = 0; 00045 00046 <font class="keywordflow">if</font> (*from < 0xD800 || *from > 0xDFFF) { 00047 uchar = *from; 00048 } 00049 <font class="keywordflow">else</font> <font class="keywordflow">if</font> (*from >= 0xD800 && *from <= 0xDBFF) { 00050 uchar = *from; 00051 schar = *(from+1); 00052 <font class="keywordflow">if</font> (uchar < 0xDC00 || uchar > 0xDFFF) { 00053 <font class="comment">//error, do nothing</font> 00054 <font class="keywordflow">continue</font>; 00055 } 00056 uchar &= 0x03ff; 00057 schar &= 0x03ff; 00058 uchar <<= 10; 00059 uchar |= schar; 00060 uchar += 0x10000; 00061 from++; 00062 } 00063 <font class="keywordflow">else</font> { 00064 <font class="comment">//error, do nothing</font> 00065 <font class="keywordflow">continue</font>; 00066 } 00067 00068 <font class="keywordflow">if</font> (uchar < 0x80) { 00069 *to++ = uchar; 00070 } 00071 <font class="keywordflow">else</font> <font class="keywordflow">if</font> (uchar < 0x800) { 00072 *to++ = 0xc0 | (uchar >> 6); 00073 *to++ = 0x80 | (uchar & 0x3f); 00074 } 00075 <font class="keywordflow">else</font> <font class="keywordflow">if</font> (uchar < 0x10000) { 00076 *to++ = 0xe0 | (uchar >> 12); 00077 *to++ = 0x80 | (uchar >> 6) & 0x3f; 00078 *to++ = 0x80 | uchar & 0x3f; 00079 } 00080 <font class="keywordflow">else</font> <font class="keywordflow">if</font> (uchar < 0x200000) { 00081 *to++ = 0xF0 | (uchar >> 18); 00082 *to++ = 0x80 | (uchar >> 12) & 0x3F; 00083 *to++ = 0x80 | (uchar >> 6) & 0x3F; 00084 *to++ = 0x80 | uchar & 0x3F; 00085 } 00086 } 00087 *to++ = 0; 00088 *to = 0; 00089 00090 <font class="keywordflow">return</font> 0; 00091 } 00092 00093 00094 00095 </pre></div><hr><address align="right"><small>Generated on Thu Jun 20 22:13:01 2002 for The Sword Project by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border=0 width=110 height=53></a>1.2.15 </small></address> </body> </html>