1 // Copyright (c) 1994 James Clark
2 // See the file COPYING for copying permission.
3 #pragma ident "%Z%%M% %I% %E% SMI"
4
5 #include "splib.h"
6 #include "IdentityCodingSystem.h"
7 #include <limits.h>
8
9 #ifdef SP_NAMESPACE
10 namespace SP_NAMESPACE {
11 #endif
12
13 class IdentityDecoder : public Decoder {
14 public:
15 size_t decode(Char *to, const char *from, size_t fromLen,
16 const char **rest);
17 Boolean convertOffset(unsigned long &offset) const;
18 };
19
20 class IdentityEncoder : public RecoveringEncoder {
21 public:
22 IdentityEncoder();
23 void output(const Char *, size_t, OutputByteStream *);
output(Char * tmp_char,size_t tmp_size_t,OutputByteStream * tmp_obs)24 void output(Char *tmp_char, size_t tmp_size_t, OutputByteStream *tmp_obs) {
25 output((const Char *)tmp_char, (size_t) tmp_size_t, (OutputByteStream *)tmp_obs);
26 }
27 };
28
IdentityCodingSystem()29 IdentityCodingSystem::IdentityCodingSystem()
30 {
31 }
32
makeDecoder() const33 Decoder *IdentityCodingSystem::makeDecoder() const
34 {
35 return new IdentityDecoder;
36 }
37
makeEncoder() const38 Encoder *IdentityCodingSystem::makeEncoder() const
39 {
40 return new IdentityEncoder;
41 }
42
isIdentity() const43 Boolean IdentityCodingSystem::isIdentity() const
44 {
45 return 1;
46 }
47
decode(Char * to,const char * from,size_t fromLen,const char ** rest)48 size_t IdentityDecoder::decode(Char *to, const char *from, size_t fromLen,
49 const char **rest)
50 {
51 if (sizeof(Char) == sizeof(char) && from == (char *)to) {
52 *rest = from + fromLen;
53 return fromLen;
54 }
55 for (size_t n = fromLen; n > 0; n--)
56 *to++ = (unsigned char)*from++; // zero extend
57 *rest = from;
58 return fromLen;
59 }
60
convertOffset(unsigned long &) const61 Boolean IdentityDecoder::convertOffset(unsigned long &) const
62 {
63 return true;
64 }
65
IdentityEncoder()66 IdentityEncoder::IdentityEncoder()
67 {
68 }
69
output(const Char * s,size_t n,OutputByteStream * sb)70 void IdentityEncoder::output(const Char *s, size_t n, OutputByteStream *sb)
71 {
72 if (sizeof(Char) != sizeof(char)) {
73 for (size_t i = 0; i < n; i++) {
74 Char c = s[i];
75 if (c > UCHAR_MAX)
76 handleUnencodable(c, sb);
77 else
78 sb->sputc((unsigned char)c);
79 }
80 }
81 else
82 sb->sputn((const char *)s, n);
83 }
84
85 #ifdef SP_NAMESPACE
86 }
87 #endif
88