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: 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"
37 #if defined(__linux__) || defined(CSRG_BASED) || defined(sun)
44 namespace SP_NAMESPACE {
47 class Fixed2Decoder : public Decoder {
50 size_t decode(Char *to, const char *from, size_t fromLen,
52 Boolean convertOffset(unsigned long &offset) const;
55 class Fixed2Encoder : public Encoder {
59 void output(Char *, size_t, streambuf *);
60 void output(const Char *, size_t, streambuf *);
62 void allocBuf(size_t);
67 Decoder *Fixed2CodingSystem::makeDecoder() const
69 return new Fixed2Decoder;
72 Encoder *Fixed2CodingSystem::makeEncoder() const
74 return new Fixed2Encoder;
77 unsigned Fixed2CodingSystem::fixedBytesPerChar() const
82 Fixed2Decoder::Fixed2Decoder()
87 size_t Fixed2Decoder::decode(Char *to, const char *from, size_t fromLen,
91 if (sizeof(Char) == 2 && from == (char *)to) {
92 *rest = from + (fromLen & ~1);
97 *rest = from + fromLen;
98 for (size_t n = fromLen; n > 0; n -= 2) {
99 *to++ = ((unsigned char)from[0] << 8) + (unsigned char)from[1];
105 Boolean Fixed2Decoder::convertOffset(unsigned long &n) const
111 Fixed2Encoder::Fixed2Encoder()
112 : buf_(0), bufSize_(0)
116 Fixed2Encoder::~Fixed2Encoder()
121 void Fixed2Encoder::allocBuf(size_t n)
125 buf_ = new char[bufSize_ = n];
129 // FIXME handle errors from streambuf::sputn
131 void Fixed2Encoder::output(Char *s, size_t n, streambuf *sb)
134 if (sizeof(Char) == 2) {
135 sb->sputn((char *)s, n*2);
139 ASSERT(sizeof(Char) >= 2);
141 for (size_t i = 0; i < n; i++) {
142 *p++ = (s[i] >> 8) & 0xff;
145 sb->sputn((char *)s, n*2);
148 void Fixed2Encoder::output(const Char *s, size_t n, streambuf *sb)
151 if (sizeof(Char) == 2) {
152 sb->sputn((char *)s, n*2);
157 for (size_t i = 0; i < n; i++) {
158 buf_[i*2] = (s[i] >> 8) & 0xff;
159 buf_[i*2 + 1] = s[i] & 0xff;
161 sb->sputn(buf_, n*2);
168 #else /* not SP_MULTI_BYTE */
171 static char non_empty_translation_unit; // sigh
174 #endif /* not SP_MULTI_BYTE */