1 /********************************************************************************
2 *                                                                               *
3 *                      U T F - 1 6  T e x t   C o d e c                         *
4 *                                                                               *
5 *********************************************************************************
6 * Copyright (C) 2002,2005 by Lyle Johnson.   All Rights Reserved.               *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or                 *
9 * modify it under the terms of the GNU Lesser General Public                    *
10 * License as published by the Free Software Foundation; either                  *
11 * version 2.1 of the License, or (at your option) any later version.            *
12 *                                                                               *
13 * This library is distributed in the hope that it will be useful,               *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             *
16 * Lesser General Public License for more details.                               *
17 *                                                                               *
18 * You should have received a copy of the GNU Lesser General Public              *
19 * License along with this library; if not, write to the Free Software           *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
21 *********************************************************************************
22 * $Id: FXUTF16Codec.cpp,v 1.5 2005/01/16 16:06:07 fox Exp $                     *
23 ********************************************************************************/
24 #include "xincs.h"
25 #include "fxver.h"
26 #include "fxdefs.h"
27 #include "FXTextCodec.h"
28 #include "FXUTF16Codec.h"
29 
30 
31 /*
32   Notes:
33   - Some code and algorithms lifted from Roman Czyborra's page on
34     Unicode Transformation Formats (http://czyborra.com/utf).
35   - What about UTF-16LE (little-endian) and UTF-16BE (big-endian)?
36 */
37 
38 /*******************************************************************************/
39 
40 namespace FX {
41 
42 
43 // Convert to UTF-16; return the number of bytes written to dest
fromUnicode(FXuchar * & dest,unsigned long m,const FXwchar * & src,unsigned long n)44 unsigned long FXUTF16Codec::fromUnicode(FXuchar*& dest,unsigned long m,const FXwchar*& src,unsigned long n){
45   FXASSERT(src);
46   FXASSERT(dest);
47   FXwchar c;
48   unsigned long i,j;
49   i=0;
50   j=0;
51   while(i<n && j+1<m){
52     c=src[i++];
53     if(c>0xffff){
54       dest[j++]=(FXuchar)(0xd7c0+(c>>10));
55       dest[j++]=(FXuchar)(0xdc00 | c & 0x3ff);
56       }
57     else{
58       dest[j++]=(FXuchar)(c>>8);
59       dest[j++]=(FXuchar)(c&0xff);
60       }
61     }
62   src=&src[i];
63   dest=&dest[j];
64   return j;
65   }
66 
67 
68 // UTF-16 texts should start with U+FEFF (zero width no-break space) as a byte-order mark
69 static const FXwchar BOM=0xfeff;
70 
71 
72 // Insert byte-order mark (BOM) into the stream
insertBOM(FXuchar * & dest,unsigned long m)73 unsigned long FXUTF16Codec::insertBOM(FXuchar*& dest,unsigned long m){
74   if(m>1){
75     *dest++=(FXuchar)(BOM>>8);
76     *dest++=(FXuchar)(BOM&0xff);
77     return 2;
78     }
79   return 0;
80   }
81 
82 
83 // Convert a sequence of bytes from UTF-16; return the number of bytes read from src
toUnicode(FXwchar * & dest,unsigned long m,const FXuchar * & src,unsigned long n)84 unsigned long FXUTF16Codec::toUnicode(FXwchar*& dest,unsigned long m,const FXuchar*& src,unsigned long n){
85   FXASSERT(src);
86   FXASSERT(dest);
87   return 0;
88   }
89 
90 
91 // Return the IANA mime name for this codec
mimeName() const92 const FXchar* FXUTF16Codec::mimeName() const {
93   return "UTF-16";
94   }
95 
96 
97 // Return code for UTF-16
mibEnum() const98 FXint FXUTF16Codec::mibEnum() const {
99   return 1015;
100   }
101 
102 
103 }
104 
105