Link with C++ linker
[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 #include <iostream.h>
38
39 #ifdef SP_NAMESPACE
40 namespace SP_NAMESPACE {
41 #endif
42
43 class Fixed2Decoder : public Decoder {
44 public:
45   Fixed2Decoder();
46   size_t decode(Char *to, const char *from, size_t fromLen,
47                 const char **rest);
48   Boolean convertOffset(unsigned long &offset) const;
49 };
50
51 class Fixed2Encoder : public Encoder {
52 public:
53   Fixed2Encoder();
54   ~Fixed2Encoder();
55   void output(Char *, size_t, streambuf *);
56   void output(const Char *, size_t, streambuf *);
57 private:
58   void allocBuf(size_t);
59   char *buf_;
60   size_t bufSize_;
61 };
62
63 Decoder *Fixed2CodingSystem::makeDecoder() const
64 {
65   return new Fixed2Decoder;
66 }
67
68 Encoder *Fixed2CodingSystem::makeEncoder() const
69 {
70   return new Fixed2Encoder;
71 }
72
73 unsigned Fixed2CodingSystem::fixedBytesPerChar() const
74 {
75   return 2;
76 }
77
78 Fixed2Decoder::Fixed2Decoder()
79 : Decoder(2)
80 {
81 }
82
83 size_t Fixed2Decoder::decode(Char *to, const char *from, size_t fromLen,
84                            const char **rest)
85 {
86 #ifdef BIG_ENDIAN
87   if (sizeof(Char) == 2 && from == (char *)to) {
88     *rest = from + (fromLen & ~1);
89     return fromLen/2;
90   }
91 #endif
92   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];
96     from += 2;
97   }
98   return fromLen/2;
99 }
100
101 Boolean Fixed2Decoder::convertOffset(unsigned long &n) const
102 {
103   n *= 2;
104   return true;
105 }
106
107 Fixed2Encoder::Fixed2Encoder()
108 : buf_(0), bufSize_(0)
109 {
110 }
111
112 Fixed2Encoder::~Fixed2Encoder()
113 {
114   delete [] buf_;
115 }
116
117 void Fixed2Encoder::allocBuf(size_t n)
118 {
119   if (bufSize_ < n) {
120     delete [] buf_;
121     buf_ = new char[bufSize_ = n];
122   }
123 }
124
125 // FIXME handle errors from streambuf::sputn
126
127 void Fixed2Encoder::output(Char *s, size_t n, streambuf *sb)
128 {
129 #ifdef BIG_ENDIAN
130   if (sizeof(Char) == 2) {
131     sb->sputn((char *)s, n*2);
132     return;
133   }
134 #endif
135   ASSERT(sizeof(Char) >= 2);
136   char *p = (char *)s;
137   for (size_t i = 0; i < n; i++) {
138     *p++ = (s[i] >> 8) & 0xff;
139     *p++ = s[i] & 0xff;
140   }
141   sb->sputn((char *)s, n*2);
142 }
143
144 void Fixed2Encoder::output(const Char *s, size_t n, streambuf *sb)
145 {
146 #ifdef BIG_ENDIAN
147   if (sizeof(Char) == 2) {
148     sb->sputn((char *)s, n*2);
149     return;
150   }
151 #endif
152   allocBuf(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;
156   }
157   sb->sputn(buf_, n*2);
158 }
159
160 #ifdef SP_NAMESPACE
161 }
162 #endif
163
164 #else /* not SP_MULTI_BYTE */
165
166 #ifndef __GNUG__
167 static char non_empty_translation_unit; // sigh
168 #endif
169
170 #endif /* not SP_MULTI_BYTE */