Link with C++ linker
[oweals/cde.git] / cde / programs / nsgmls / IdentityCodingSystem.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: IdentityCodingSystem.C /main/1 1996/07/29 16:54:48 cde-hp $ */
24 // Copyright (c) 1994 James Clark
25 // See the file COPYING for copying permission.
26
27 #include "splib.h"
28 #include "IdentityCodingSystem.h"
29 #include <iostream.h>
30 #include <limits.h>
31
32 #ifdef SP_NAMESPACE
33 namespace SP_NAMESPACE {
34 #endif
35
36 class IdentityDecoder : public Decoder {
37 public:
38   size_t decode(Char *to, const char *from, size_t fromLen,
39                 const char **rest);
40   Boolean convertOffset(unsigned long &offset) const;
41 };
42
43 class IdentityEncoder : public Encoder {
44 public:
45   IdentityEncoder();
46   ~IdentityEncoder();
47   void output(Char *, size_t, streambuf *);
48   void output(const Char *, size_t, streambuf *);
49 private:
50   void allocBuf(size_t);
51   char *buf_;
52   size_t bufSize_;
53 };
54
55 IdentityCodingSystem::IdentityCodingSystem()
56 {
57 }
58
59 Decoder *IdentityCodingSystem::makeDecoder() const
60 {
61   return new IdentityDecoder;
62 }
63
64 Encoder *IdentityCodingSystem::makeEncoder() const
65 {
66   return new IdentityEncoder;
67 }
68
69 Boolean IdentityCodingSystem::isIdentity() const
70 {
71   return 1;
72 }
73
74 size_t IdentityDecoder::decode(Char *to, const char *from, size_t fromLen,
75                                const char **rest)
76 {
77   if (sizeof(Char) == sizeof(char) && from == (char *)to) {
78     *rest = from + fromLen;
79     return fromLen;
80   }
81   for (size_t n = fromLen; n > 0; n--)
82     *to++ = (unsigned char)*from++; // zero extend
83   *rest = from;
84   return fromLen;
85 }
86
87 Boolean IdentityDecoder::convertOffset(unsigned long &) const
88 {
89   return true;
90 }
91
92 IdentityEncoder::IdentityEncoder()
93 : buf_(0), bufSize_(0)
94 {
95 }
96
97 IdentityEncoder::~IdentityEncoder()
98 {
99   delete [] buf_;
100 }
101
102 void IdentityEncoder::allocBuf(size_t n)
103 {
104   if (bufSize_ < n) {
105     delete [] buf_;
106     buf_ = new char[bufSize_ = n];
107   }
108 }
109
110 // FIXME handle errors from streambuf::sputn
111
112 void IdentityEncoder::output(Char *s, size_t n, streambuf *sb)
113 {
114   char *p = (char *)s;
115   if (sizeof(Char) != sizeof(char)) {
116     size_t j = 0;
117     for (size_t i = 0; i < n; i++) {
118       if (s[i] > UCHAR_MAX) {
119         sb->sputn(p, j);
120         j = 0;
121         handleUnencodable(s[i], sb);
122       }
123       else
124         p[j++] = char(s[i]);
125     }
126     sb->sputn(p, j);
127   }
128   else
129     sb->sputn(p, n);
130 }
131
132 void IdentityEncoder::output(const Char *s, size_t n, streambuf *sb)
133 {
134   if (sizeof(Char) != sizeof(char)) {
135     allocBuf(n);
136     size_t j = 0;
137     for (size_t i = 0; i < n; i++) {
138       if (s[i] > UCHAR_MAX) {
139         sb->sputn(buf_, j);
140         j = 0;
141         handleUnencodable(s[i], sb);
142       }
143       else
144         buf_[j++] = char(s[i]);
145     }
146     sb->sputn(buf_, j);
147   }
148   else
149     sb->sputn((const char *)s, n);
150 }
151
152 #ifdef SP_NAMESPACE
153 }
154 #endif