2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: UnicodeCodingSystem.C /main/1 1996/07/29 17:07:33 cde-hp $ */
24 // Copyright (c) 1994 James Clark
25 // See the file COPYING for copying permission.
31 #include "UnicodeCodingSystem.h"
37 #ifdef DECLARE_MEMMOVE
39 void *memmove(void *, const void *, size_t);
42 #if defined(linux) || defined(CSRG_BASED) || defined(sun)
49 namespace SP_NAMESPACE {
52 const unsigned short byteOrderMark = 0xfeff;
53 const unsigned short swappedByteOrderMark = 0xfffe;
55 class UnicodeDecoder : public Decoder {
57 UnicodeDecoder(const InputCodingSystem *sub);
58 size_t decode(Char *to, const char *from, size_t fromLen,
60 Boolean convertOffset(unsigned long &offset) const;
62 PackedBoolean hadFirstChar_;
63 PackedBoolean hadByteOrderMark_;
64 PackedBoolean swapBytes_;
65 Owner<Decoder> subDecoder_;
66 const InputCodingSystem *subCodingSystem_;
69 class UnicodeEncoder : public Encoder {
73 void output(Char *, size_t, streambuf *);
74 void output(const Char *, size_t, streambuf *);
75 void startFile(streambuf *);
77 void allocBuf(size_t);
82 UnicodeCodingSystem::UnicodeCodingSystem(const InputCodingSystem *sub)
87 Decoder *UnicodeCodingSystem::makeDecoder() const
89 return new UnicodeDecoder(sub_);
92 Encoder *UnicodeCodingSystem::makeEncoder() const
94 return new UnicodeEncoder;
97 unsigned UnicodeCodingSystem::fixedBytesPerChar() const
102 UnicodeDecoder::UnicodeDecoder(const InputCodingSystem *subCodingSystem)
103 : Decoder(subCodingSystem ? 1 : 2), subCodingSystem_(subCodingSystem),
104 hadByteOrderMark_(0), hadFirstChar_(0), swapBytes_(0)
109 size_t UnicodeDecoder::decode(Char *to, const char *from, size_t fromLen,
118 return subDecoder_->decode(to, from, fromLen, rest);
119 if (!hadFirstChar_) {
121 minBytesPerChar_ = 2;
127 u.bytes[0] = from[0];
128 u.bytes[1] = from[1];
129 if (u.word == byteOrderMark) {
130 hadByteOrderMark_ = 1;
134 else if (u.word == swappedByteOrderMark) {
135 hadByteOrderMark_ = 1;
140 else if (subCodingSystem_) {
141 subDecoder_ = subCodingSystem_->makeDecoder();
142 minBytesPerChar_ = subDecoder_->minBytesPerChar();
143 return subDecoder_->decode(to, from, fromLen, rest);
147 *rest = from + fromLen;
148 if (sizeof(Char) == 2) {
150 if (from != (char *)to)
151 memmove(to, from, fromLen);
156 for (size_t n = fromLen; n > 0; n -= 2) {
158 u.bytes[1] = *from++;
159 u.bytes[0] = *from++;
164 for (size_t n = fromLen; n > 0; n -= 2) {
166 u.bytes[0] = *from++;
167 u.bytes[1] = *from++;
174 Boolean UnicodeDecoder::convertOffset(unsigned long &n) const
177 return subDecoder_->convertOffset(n);
179 if (hadByteOrderMark_)
184 UnicodeEncoder::UnicodeEncoder()
185 : buf_(0), bufSize_(0)
189 UnicodeEncoder::~UnicodeEncoder()
194 void UnicodeEncoder::allocBuf(size_t n)
198 buf_ = new unsigned short[bufSize_ = n];
202 void UnicodeEncoder::startFile(streambuf *sb)
204 const unsigned short n = byteOrderMark;
205 sb->sputn((char *)&n, 2);
208 // FIXME handle errors from streambuf::sputn
210 void UnicodeEncoder::output(Char *s, size_t n, streambuf *sb)
212 if (sizeof(Char) == 2) {
213 sb->sputn((char *)s, n*2);
216 ASSERT(sizeof(Char) >= 2);
217 unsigned short *p = (unsigned short *)s;
218 for (size_t i = 0; i < n; i++)
219 p[i] = s[i] & 0xffff;
220 sb->sputn((char *)s, n*2);
223 void UnicodeEncoder::output(const Char *s, size_t n, streambuf *sb)
225 if (sizeof(Char) == 2) {
226 sb->sputn((char *)s, n*2);
230 for (size_t i = 0; i < n; i++)
231 buf_[i] = s[i] & 0xffff;
232 sb->sputn((char *)buf_, n*2);
239 #else /* not SP_MULTI_BYTE */
242 static char non_empty_translation_unit; // sigh
245 #endif /* not SP_MULTI_BYTE */