Linux has the same value for ENOTSUP as another var, therefore protect one
[oweals/cde.git] / cde / programs / nsgmls / OutputCharStream.h
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: OutputCharStream.h /main/1 1996/07/29 16:59:39 cde-hp $ */
24 // Copyright (c) 1994 James Clark
25 // See the file COPYING for copying permission.
26
27 #ifndef OutputCharStream_INCLUDED
28 #define OutputCharStream_INCLUDED 1
29
30 #include "types.h"
31 #include <stddef.h>
32 #include "StringC.h"
33 #include "Owner.h"
34 #include "CodingSystem.h"
35
36 class streambuf;
37
38 #ifdef SP_NAMESPACE
39 namespace SP_NAMESPACE {
40 #endif
41
42 class SP_API OutputCharStream {
43 public:
44   enum Newline { newline };
45   typedef void (*Escaper)(OutputCharStream &, Char);
46   OutputCharStream();
47   virtual ~OutputCharStream();
48   OutputCharStream &put(Char);
49   OutputCharStream &write(const Char *, size_t);
50   virtual void flush() = 0;
51   void setEscaper(Escaper);
52
53   OutputCharStream &operator<<(char);
54   OutputCharStream &operator<<(const char *);
55   OutputCharStream &operator<<(const StringC &);
56   OutputCharStream &operator<<(unsigned long);
57   OutputCharStream &operator<<(int);
58   OutputCharStream &operator<<(Newline);
59 private:
60   OutputCharStream(const OutputCharStream &);   // undefined
61   void operator=(const OutputCharStream &);     // undefined
62
63   virtual void flushBuf(Char) = 0;
64   Escaper escaper_;
65 protected:
66   void escape(OutputCharStream &, Char c);
67   Char *ptr_;
68   Char *end_;
69 };
70
71 class SP_API IosOutputCharStream : public OutputCharStream,
72                             private Encoder::Handler {
73 public:
74   IosOutputCharStream();
75   // the streambuf will not be deleted
76   IosOutputCharStream(streambuf *, const OutputCodingSystem *);
77   ~IosOutputCharStream();
78   void open(streambuf *, const OutputCodingSystem *);
79   void flush();
80 private:
81   IosOutputCharStream(const IosOutputCharStream &); // undefined
82   void operator=(const IosOutputCharStream &);      // undefined
83   IosOutputCharStream(streambuf *, Encoder *);
84   void allocBuf(int bytesPerChar);
85   void flushBuf(Char);
86   void handleUnencodable(Char c, streambuf *);
87   Char *buf_;
88   streambuf *byteStream_;
89   Encoder *encoder_;
90   Owner<Encoder> ownedEncoder_;
91 };
92
93 class SP_API StrOutputCharStream : public OutputCharStream {
94 public:
95   StrOutputCharStream();
96   ~StrOutputCharStream();
97   void extractString(StringC &);
98   void flush();
99 private:
100   void flushBuf(Char);
101   void sync(size_t);
102   StrOutputCharStream(const StrOutputCharStream &); // undefined
103   void operator=(const StrOutputCharStream &);      // undefined
104   Char *buf_;
105   size_t bufSize_;
106 };
107
108 class SP_API RecordOutputCharStream : public OutputCharStream {
109 public:
110   RecordOutputCharStream(OutputCharStream *);
111   ~RecordOutputCharStream();
112   void flush();
113 private:
114   RecordOutputCharStream(const RecordOutputCharStream &); // undefined
115   void operator=(const RecordOutputCharStream &);         // undefined
116   void flushBuf(Char);
117   void outputBuf();
118
119   OutputCharStream *os_;
120   enum { bufSize_ = 1024 };
121   Char buf_[bufSize_];
122 };
123
124 inline
125 OutputCharStream &OutputCharStream::put(Char c)
126 {
127   if (ptr_ < end_)
128     *ptr_++ = c;
129   else
130     flushBuf(c);
131   return *this;
132 }
133
134 inline
135 OutputCharStream &OutputCharStream::operator<<(char c)
136 {
137   return put(Char(c));
138 }
139
140 inline
141 OutputCharStream &OutputCharStream::operator<<(Newline)
142 {
143 #ifdef SP_HAVE_SETMODE
144   put(Char('\r'));
145 #endif
146   return put(Char('\n'));
147 }
148
149 inline
150 OutputCharStream &OutputCharStream::operator<<(const StringC &str)
151 {
152   return write(str.data(), str.size());
153 }
154
155 inline
156 void OutputCharStream::setEscaper(Escaper f)
157 {
158   escaper_ = f;
159 }
160
161 inline
162 void OutputCharStream::escape(OutputCharStream &s, Char c)
163 {
164   if (escaper_)
165     (*escaper_)(s, c);
166 }
167
168 #ifdef SP_NAMESPACE
169 }
170 #endif
171
172 #endif /* not OutputCharStream_INCLUDED */