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