rpc.cmsd: use TIRPC on Linux
[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 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: 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 #if defined(__linux__) || defined(CSRG_BASED) || defined(sun)
37 #include <streambuf>
38 using namespace std;
39 #else
40 class streambuf;
41 #endif
42
43 #ifdef SP_NAMESPACE
44 namespace SP_NAMESPACE {
45 #endif
46
47 class SP_API OutputCharStream {
48 public:
49   enum Newline { newline };
50   typedef void (*Escaper)(OutputCharStream &, Char);
51   OutputCharStream();
52   virtual ~OutputCharStream();
53   OutputCharStream &put(Char);
54   OutputCharStream &write(const Char *, size_t);
55   virtual void flush() = 0;
56   void setEscaper(Escaper);
57
58   OutputCharStream &operator<<(char);
59   OutputCharStream &operator<<(const char *);
60   OutputCharStream &operator<<(const StringC &);
61   OutputCharStream &operator<<(unsigned long);
62   OutputCharStream &operator<<(int);
63   OutputCharStream &operator<<(Newline);
64 private:
65   OutputCharStream(const OutputCharStream &);   // undefined
66   void operator=(const OutputCharStream &);     // undefined
67
68   virtual void flushBuf(Char) = 0;
69   Escaper escaper_;
70 protected:
71   void escape(OutputCharStream &, Char c);
72   Char *ptr_;
73   Char *end_;
74 };
75
76 class SP_API IosOutputCharStream : public OutputCharStream,
77                             private Encoder::Handler {
78 public:
79   IosOutputCharStream();
80   // the streambuf will not be deleted
81   IosOutputCharStream(streambuf *, const OutputCodingSystem *);
82   ~IosOutputCharStream();
83   void open(streambuf *, const OutputCodingSystem *);
84   void flush();
85 private:
86   IosOutputCharStream(const IosOutputCharStream &); // undefined
87   void operator=(const IosOutputCharStream &);      // undefined
88   IosOutputCharStream(streambuf *, Encoder *);
89   void allocBuf(int bytesPerChar);
90   void flushBuf(Char);
91   void handleUnencodable(Char c, streambuf *);
92   Char *buf_;
93   streambuf *byteStream_;
94   Encoder *encoder_;
95   Owner<Encoder> ownedEncoder_;
96 };
97
98 class SP_API StrOutputCharStream : public OutputCharStream {
99 public:
100   StrOutputCharStream();
101   ~StrOutputCharStream();
102   void extractString(StringC &);
103   void flush();
104 private:
105   void flushBuf(Char);
106   void sync(size_t);
107   StrOutputCharStream(const StrOutputCharStream &); // undefined
108   void operator=(const StrOutputCharStream &);      // undefined
109   Char *buf_;
110   size_t bufSize_;
111 };
112
113 class SP_API RecordOutputCharStream : public OutputCharStream {
114 public:
115   RecordOutputCharStream(OutputCharStream *);
116   ~RecordOutputCharStream();
117   void flush();
118 private:
119   RecordOutputCharStream(const RecordOutputCharStream &); // undefined
120   void operator=(const RecordOutputCharStream &);         // undefined
121   void flushBuf(Char);
122   void outputBuf();
123
124   OutputCharStream *os_;
125   enum { bufSize_ = 1024 };
126   Char buf_[bufSize_];
127 };
128
129 inline
130 OutputCharStream &OutputCharStream::put(Char c)
131 {
132   if (ptr_ < end_)
133     *ptr_++ = c;
134   else
135     flushBuf(c);
136   return *this;
137 }
138
139 inline
140 OutputCharStream &OutputCharStream::operator<<(char c)
141 {
142   return put(Char(c));
143 }
144
145 inline
146 OutputCharStream &OutputCharStream::operator<<(Newline)
147 {
148 #ifdef SP_HAVE_SETMODE
149   put(Char('\r'));
150 #endif
151   return put(Char('\n'));
152 }
153
154 inline
155 OutputCharStream &OutputCharStream::operator<<(const StringC &str)
156 {
157   return write(str.data(), str.size());
158 }
159
160 inline
161 void OutputCharStream::setEscaper(Escaper f)
162 {
163   escaper_ = f;
164 }
165
166 inline
167 void OutputCharStream::escape(OutputCharStream &s, Char c)
168 {
169   if (escaper_)
170     (*escaper_)(s, c);
171 }
172
173 #ifdef SP_NAMESPACE
174 }
175 #endif
176
177 #endif /* not OutputCharStream_INCLUDED */