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: Fixed2CodingSystem.C /main/1 1996/07/29 16:52:03 cde-hp $ */
24 // Copyright (c) 1994 James Clark
25 // See the file COPYING for copying permission.
27 // This uses a big endian byte order irrespective of host byte order.
28 // Nothing special is done with FEFF/FFFE.
34 #include "Fixed2CodingSystem.h"
40 namespace SP_NAMESPACE {
43 class Fixed2Decoder : public Decoder {
46 size_t decode(Char *to, const char *from, size_t fromLen,
48 Boolean convertOffset(unsigned long &offset) const;
51 class Fixed2Encoder : public Encoder {
55 void output(Char *, size_t, streambuf *);
56 void output(const Char *, size_t, streambuf *);
58 void allocBuf(size_t);
63 Decoder *Fixed2CodingSystem::makeDecoder() const
65 return new Fixed2Decoder;
68 Encoder *Fixed2CodingSystem::makeEncoder() const
70 return new Fixed2Encoder;
73 unsigned Fixed2CodingSystem::fixedBytesPerChar() const
78 Fixed2Decoder::Fixed2Decoder()
83 size_t Fixed2Decoder::decode(Char *to, const char *from, size_t fromLen,
87 if (sizeof(Char) == 2 && from == (char *)to) {
88 *rest = from + (fromLen & ~1);
93 *rest = from + fromLen;
94 for (size_t n = fromLen; n > 0; n -= 2) {
95 *to++ = ((unsigned char)from[0] << 8) + (unsigned char)from[1];
101 Boolean Fixed2Decoder::convertOffset(unsigned long &n) const
107 Fixed2Encoder::Fixed2Encoder()
108 : buf_(0), bufSize_(0)
112 Fixed2Encoder::~Fixed2Encoder()
117 void Fixed2Encoder::allocBuf(size_t n)
121 buf_ = new char[bufSize_ = n];
125 // FIXME handle errors from streambuf::sputn
127 void Fixed2Encoder::output(Char *s, size_t n, streambuf *sb)
130 if (sizeof(Char) == 2) {
131 sb->sputn((char *)s, n*2);
135 ASSERT(sizeof(Char) >= 2);
137 for (size_t i = 0; i < n; i++) {
138 *p++ = (s[i] >> 8) & 0xff;
141 sb->sputn((char *)s, n*2);
144 void Fixed2Encoder::output(const Char *s, size_t n, streambuf *sb)
147 if (sizeof(Char) == 2) {
148 sb->sputn((char *)s, n*2);
153 for (size_t i = 0; i < n; i++) {
154 buf_[i*2] = (s[i] >> 8) & 0xff;
155 buf_[i*2 + 1] = s[i] & 0xff;
157 sb->sputn(buf_, n*2);
164 #else /* not SP_MULTI_BYTE */
167 static char non_empty_translation_unit; // sigh
170 #endif /* not SP_MULTI_BYTE */