Link with C++ linker
[oweals/cde.git] / cde / programs / nsgmls / SJISCodingSystem.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: SJISCodingSystem.C /main/1 1996/07/29 17:03:20 cde-hp $ */
24 // Copyright (c) 1994 James Clark
25 // See the file COPYING for copying permission.
26
27 #include "splib.h"
28
29 #ifdef SP_MULTI_BYTE
30
31 #include "SJISCodingSystem.h"
32 #include <iostream.h>
33
34 #ifdef SP_NAMESPACE
35 namespace SP_NAMESPACE {
36 #endif
37
38 class SJISDecoder : public Decoder {
39 public:
40   SJISDecoder();
41   size_t decode(Char *, const char *, size_t, const char **);
42 private:
43 };
44
45 class SJISEncoder : public Encoder {
46 public:
47   SJISEncoder();
48   void output(const Char *, size_t, streambuf *);
49 };
50
51 Decoder *SJISCodingSystem::makeDecoder() const
52 {
53   return new SJISDecoder;
54 }
55
56 Encoder *SJISCodingSystem::makeEncoder() const
57 {
58   return new SJISEncoder;
59 }
60
61
62 SJISDecoder::SJISDecoder()
63 {
64 }
65
66 size_t SJISDecoder::decode(Char *to, const char *s,
67                            size_t slen, const char **rest)
68 {
69   Char *start = to;
70   while (slen > 0) {
71     unsigned char c = *(unsigned char *)s;
72     if (!(c & 0x80)) {
73       *to++ = c;
74       s++;
75       slen--;
76     }
77     else if (129 <= c && c <= 159) {
78       if (slen < 2)
79         break;
80       s++;
81       slen -= 2;
82       unsigned char c2 = *(unsigned char *)s++;
83       unsigned short n = ((c - 112) << 9) | c2;
84       if (64 <= c2 && c2 <= 127)
85         n -= 31 + (1 << 8);
86       else if (c2 <= 158)
87         n -= 32 + (1 << 8);
88       else if (c2 <= 252)
89         n -= 126;
90       else
91         continue;
92       n |= 0x8080;
93       *to++ = n;
94     }
95     else if (224 <= c && c <= 239) {
96       if (slen < 2)
97         break;
98       s++;
99       slen -= 2;
100       unsigned char c2 = *(unsigned char *)s++;
101       unsigned short n = ((c - 176) << 9) | c2;
102       if (64 <= c2 && c2 <= 127)
103         n -= 31 + (1 << 8);
104       else if (c2 <= 158)
105         n -= 32 + (1 << 8);
106       else if (c2 <= 252)
107         n -= 126;
108       else
109         continue;
110       n |= 0x8080;
111       *to++ = n;
112     }
113     else if (161 <= c && c <= 223) {
114       slen--;
115       s++;
116       *to++ = c;
117     }
118     else {
119       // 128, 160, 240-255
120       slen--;
121       s++;
122     }
123   }
124   *rest = s;
125   return to - start;
126 }
127
128 SJISEncoder::SJISEncoder()
129 {
130 }
131
132 // FIXME handle errors from streambuf::sputc
133
134 void SJISEncoder::output(const Char *s, size_t n, streambuf *sb)
135 {
136   for (; n > 0; s++, n--) {
137     Char c = *s;
138     unsigned short mask = (unsigned short)(c & 0x8080);
139     if (mask == 0)
140       sb->sputc(char(c & 0xff));
141     else if (mask == 0x8080) {
142       unsigned char c1 = (c >> 8) & 0x7f;
143       unsigned char c2 = c & 0x7f;
144       char out1;
145       if (c1 < 33)
146         out1 = 0;
147       else if (c1 < 95)
148         out1 = ((c1 + 1) >> 1) + 112;
149       else if (c1 < 127)
150         out1 = ((c1 + 1) >> 1) + 176;
151       else
152         out1 = 0;
153       if (out1) {
154         char out2;
155         if (c1 & 1) {
156           if (c2 < 33)
157             out2 = 0;
158           else if (c2 <= 95)
159             out2 = c2 + 31;
160           else if (c2 <= 126)
161             out2 = c2 + 32;
162           else
163             out2 = 0;
164         }
165         else {
166           if (33 <= c2 && c2 <= 126)
167             out2 = c2 + 126;
168           else
169             out2 = 0;
170         }
171         if (out2) {
172           sb->sputc(out1);
173           sb->sputc(out2);
174         }
175         else
176           handleUnencodable(c, sb);
177       }
178       else
179         handleUnencodable(c, sb);
180     }
181     else if (mask == 0x0080) {
182       if (161 <= c && c <= 223)
183         sb->sputc(char(c & 0xff));
184       else
185         handleUnencodable(c, sb);
186     }
187     else
188       handleUnencodable(c, sb);
189   }
190 }
191
192 #ifdef SP_NAMESPACE
193 }
194 #endif
195
196 #else /* not SP_MULTI_BYTE */
197
198 #ifndef __GNUG__
199 static char non_empty_translation_unit; // sigh
200 #endif
201
202 #endif /* not SP_MULTI_BYTE */