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 librararies 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);
45 namespace SP_NAMESPACE {
48 const unsigned short byteOrderMark = 0xfeff;
49 const unsigned short swappedByteOrderMark = 0xfffe;
51 class UnicodeDecoder : public Decoder {
53 UnicodeDecoder(const InputCodingSystem *sub);
54 size_t decode(Char *to, const char *from, size_t fromLen,
56 Boolean convertOffset(unsigned long &offset) const;
58 PackedBoolean hadFirstChar_;
59 PackedBoolean hadByteOrderMark_;
60 PackedBoolean swapBytes_;
61 Owner<Decoder> subDecoder_;
62 const InputCodingSystem *subCodingSystem_;
65 class UnicodeEncoder : public Encoder {
69 void output(Char *, size_t, streambuf *);
70 void output(const Char *, size_t, streambuf *);
71 void startFile(streambuf *);
73 void allocBuf(size_t);
78 UnicodeCodingSystem::UnicodeCodingSystem(const InputCodingSystem *sub)
83 Decoder *UnicodeCodingSystem::makeDecoder() const
85 return new UnicodeDecoder(sub_);
88 Encoder *UnicodeCodingSystem::makeEncoder() const
90 return new UnicodeEncoder;
93 unsigned UnicodeCodingSystem::fixedBytesPerChar() const
98 UnicodeDecoder::UnicodeDecoder(const InputCodingSystem *subCodingSystem)
99 : Decoder(subCodingSystem ? 1 : 2), subCodingSystem_(subCodingSystem),
100 hadByteOrderMark_(0), hadFirstChar_(0), swapBytes_(0)
105 size_t UnicodeDecoder::decode(Char *to, const char *from, size_t fromLen,
114 return subDecoder_->decode(to, from, fromLen, rest);
115 if (!hadFirstChar_) {
117 minBytesPerChar_ = 2;
123 u.bytes[0] = from[0];
124 u.bytes[1] = from[1];
125 if (u.word == byteOrderMark) {
126 hadByteOrderMark_ = 1;
130 else if (u.word == swappedByteOrderMark) {
131 hadByteOrderMark_ = 1;
136 else if (subCodingSystem_) {
137 subDecoder_ = subCodingSystem_->makeDecoder();
138 minBytesPerChar_ = subDecoder_->minBytesPerChar();
139 return subDecoder_->decode(to, from, fromLen, rest);
143 *rest = from + fromLen;
144 if (sizeof(Char) == 2) {
146 if (from != (char *)to)
147 memmove(to, from, fromLen);
152 for (size_t n = fromLen; n > 0; n -= 2) {
154 u.bytes[1] = *from++;
155 u.bytes[0] = *from++;
160 for (size_t n = fromLen; n > 0; n -= 2) {
162 u.bytes[0] = *from++;
163 u.bytes[1] = *from++;
170 Boolean UnicodeDecoder::convertOffset(unsigned long &n) const
173 return subDecoder_->convertOffset(n);
175 if (hadByteOrderMark_)
180 UnicodeEncoder::UnicodeEncoder()
181 : buf_(0), bufSize_(0)
185 UnicodeEncoder::~UnicodeEncoder()
190 void UnicodeEncoder::allocBuf(size_t n)
194 buf_ = new unsigned short[bufSize_ = n];
198 void UnicodeEncoder::startFile(streambuf *sb)
200 const unsigned short n = byteOrderMark;
201 sb->sputn((char *)&n, 2);
204 // FIXME handle errors from streambuf::sputn
206 void UnicodeEncoder::output(Char *s, size_t n, streambuf *sb)
208 if (sizeof(Char) == 2) {
209 sb->sputn((char *)s, n*2);
212 ASSERT(sizeof(Char) >= 2);
213 unsigned short *p = (unsigned short *)s;
214 for (size_t i = 0; i < n; i++)
215 p[i] = s[i] & 0xffff;
216 sb->sputn((char *)s, n*2);
219 void UnicodeEncoder::output(const Char *s, size_t n, streambuf *sb)
221 if (sizeof(Char) == 2) {
222 sb->sputn((char *)s, n*2);
226 for (size_t i = 0; i < n; i++)
227 buf_[i] = s[i] & 0xffff;
228 sb->sputn((char *)buf_, n*2);
235 #else /* not SP_MULTI_BYTE */
238 static char non_empty_translation_unit; // sigh
241 #endif /* not SP_MULTI_BYTE */