1 // Copyright (c) 1997 James Clark
2 // See the file COPYING for copying permission.
3 #pragma ident "%Z%%M% %I% %E% SMI"
4
5 #include "splib.h"
6
7 #ifdef SP_MULTI_BYTE
8
9 #include "Big5CodingSystem.h"
10
11 #ifdef SP_NAMESPACE
12 namespace SP_NAMESPACE {
13 #endif
14
15 class Big5Decoder : public Decoder {
16 public:
Big5Decoder()17 Big5Decoder() { }
18 size_t decode(Char *, const char *, size_t, const char **);
19 private:
20 };
21
22 class Big5Encoder : public Encoder {
23 public:
Big5Encoder()24 Big5Encoder() { }
25 void output(const Char *, size_t, OutputByteStream *);
output(Char * tmp_char,size_t tmp_size_t,OutputByteStream * tmp_obs)26 void output(Char *tmp_char, size_t tmp_size_t, OutputByteStream *tmp_obs) {
27 output((const Char *)tmp_char, (size_t) tmp_size_t, (OutputByteStream *)tmp_obs);
28 }
29
30 };
31
makeDecoder() const32 Decoder *Big5CodingSystem::makeDecoder() const
33 {
34 return new Big5Decoder;
35 }
36
makeEncoder() const37 Encoder *Big5CodingSystem::makeEncoder() const
38 {
39 return new Big5Encoder;
40 }
41
decode(Char * to,const char * s,size_t slen,const char ** rest)42 size_t Big5Decoder::decode(Char *to, const char *s,
43 size_t slen, const char **rest)
44 {
45 Char *start = to;
46 const unsigned char *us = (const unsigned char *)s;
47 while (slen > 0) {
48 if (!(*us & 0x80)) {
49 *to++ = *us++;
50 slen--;
51 }
52 else {
53 if (slen < 2)
54 break;
55 slen -= 2;
56 unsigned short n = *us++ << 8;
57 n |= *us++;
58 *to++ = n;
59 }
60 }
61 *rest = (const char *)us;
62 return to - start;
63 }
64
output(const Char * s,size_t n,OutputByteStream * sb)65 void Big5Encoder::output(const Char *s, size_t n, OutputByteStream *sb)
66 {
67 for (; n > 0; s++, n--) {
68 Char c = *s;
69 if (c < 0x80)
70 sb->sputc((unsigned char)c);
71 else if (c & 0x8000) {
72 sb->sputc((unsigned char)(c >> 8));
73 sb->sputc((unsigned char)(c & 0xff));
74 }
75 else
76 handleUnencodable(c, sb);
77 }
78 }
79
80 #ifdef SP_NAMESPACE
81 }
82 #endif
83
84 #else /* not SP_MULTI_BYTE */
85
86 #ifndef __GNUG__
87 static char non_empty_translation_unit; // sigh
88 #endif
89
90 #endif /* not SP_MULTI_BYTE */
91