Fix dttypes for BSD systems
[oweals/cde.git] / cde / programs / nsgmls / Fixed2CodingSystem.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
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.
26
27 // This uses a big endian byte order irrespective of host byte order.
28 // Nothing special is done with FEFF/FFFE.
29
30 #include "splib.h"
31
32 #ifdef SP_MULTI_BYTE
33
34 #include "Fixed2CodingSystem.h"
35 #include "macros.h"
36
37 #if defined(linux)
38 #include <iostream>
39 #else
40 #include <iostream.h>
41 #endif
42
43 #ifdef SP_NAMESPACE
44 namespace SP_NAMESPACE {
45 #endif
46
47 class Fixed2Decoder : public Decoder {
48 public:
49   Fixed2Decoder();
50   size_t decode(Char *to, const char *from, size_t fromLen,
51                 const char **rest);
52   Boolean convertOffset(unsigned long &offset) const;
53 };
54
55 class Fixed2Encoder : public Encoder {
56 public:
57   Fixed2Encoder();
58   ~Fixed2Encoder();
59   void output(Char *, size_t, streambuf *);
60   void output(const Char *, size_t, streambuf *);
61 private:
62   void allocBuf(size_t);
63   char *buf_;
64   size_t bufSize_;
65 };
66
67 Decoder *Fixed2CodingSystem::makeDecoder() const
68 {
69   return new Fixed2Decoder;
70 }
71
72 Encoder *Fixed2CodingSystem::makeEncoder() const
73 {
74   return new Fixed2Encoder;
75 }
76
77 unsigned Fixed2CodingSystem::fixedBytesPerChar() const
78 {
79   return 2;
80 }
81
82 Fixed2Decoder::Fixed2Decoder()
83 : Decoder(2)
84 {
85 }
86
87 size_t Fixed2Decoder::decode(Char *to, const char *from, size_t fromLen,
88                            const char **rest)
89 {
90 #ifdef BIG_ENDIAN
91   if (sizeof(Char) == 2 && from == (char *)to) {
92     *rest = from + (fromLen & ~1);
93     return fromLen/2;
94   }
95 #endif
96   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];
100     from += 2;
101   }
102   return fromLen/2;
103 }
104
105 Boolean Fixed2Decoder::convertOffset(unsigned long &n) const
106 {
107   n *= 2;
108   return true;
109 }
110
111 Fixed2Encoder::Fixed2Encoder()
112 : buf_(0), bufSize_(0)
113 {
114 }
115
116 Fixed2Encoder::~Fixed2Encoder()
117 {
118   delete [] buf_;
119 }
120
121 void Fixed2Encoder::allocBuf(size_t n)
122 {
123   if (bufSize_ < n) {
124     delete [] buf_;
125     buf_ = new char[bufSize_ = n];
126   }
127 }
128
129 // FIXME handle errors from streambuf::sputn
130
131 void Fixed2Encoder::output(Char *s, size_t n, streambuf *sb)
132 {
133 #ifdef BIG_ENDIAN
134   if (sizeof(Char) == 2) {
135     sb->sputn((char *)s, n*2);
136     return;
137   }
138 #endif
139   ASSERT(sizeof(Char) >= 2);
140   char *p = (char *)s;
141   for (size_t i = 0; i < n; i++) {
142     *p++ = (s[i] >> 8) & 0xff;
143     *p++ = s[i] & 0xff;
144   }
145   sb->sputn((char *)s, n*2);
146 }
147
148 void Fixed2Encoder::output(const Char *s, size_t n, streambuf *sb)
149 {
150 #ifdef BIG_ENDIAN
151   if (sizeof(Char) == 2) {
152     sb->sputn((char *)s, n*2);
153     return;
154   }
155 #endif
156   allocBuf(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;
160   }
161   sb->sputn(buf_, n*2);
162 }
163
164 #ifdef SP_NAMESPACE
165 }
166 #endif
167
168 #else /* not SP_MULTI_BYTE */
169
170 #ifndef __GNUG__
171 static char non_empty_translation_unit; // sigh
172 #endif
173
174 #endif /* not SP_MULTI_BYTE */