3065 some functions in the tcp module can be static
[unleashed.git] / usr / src / cmd / man / src / util / nsgmls.src / lib / OutputCharStream.cxx
blobdc47c112d591f075bb5eb721f837797996301ab5
1 // Copyright (c) 1994 James Clark
2 // See the file COPYING for copying permission.
3 #pragma ident "%Z%%M% %I% %E% SMI"
5 #include "splib.h"
6 #include "OutputCharStream.h"
7 #include "CodingSystem.h"
8 #include "macros.h"
9 #include <stdio.h>
11 #ifdef SP_NAMESPACE
12 namespace SP_NAMESPACE {
13 #endif
15 OutputCharStream::OutputCharStream()
16 : ptr_(0), end_(0)
20 OutputCharStream::~OutputCharStream()
24 void OutputCharStream::setEscaper(Escaper)
28 OutputCharStream &OutputCharStream::write(const Char *s, size_t n)
30 for (;;) {
31 size_t spare = end_ - ptr_;
32 if (n <= spare) {
33 memcpy(ptr_, s, n*sizeof(Char));
34 ptr_ += n;
35 break;
37 if (spare > 0) {
38 memcpy(ptr_, s, spare*sizeof(Char));
39 ptr_ += spare;
40 s += spare;
41 n -= spare;
43 n--;
44 flushBuf(*s++);
46 return *this;
49 OutputCharStream &OutputCharStream::operator<<(const char *s)
51 while (*s)
52 put(*s++);
53 return *this;
56 // FIXME Avoid stdio
58 OutputCharStream &OutputCharStream::operator<<(unsigned long n)
60 char buf[sizeof(unsigned long)*3 + 1];
61 sprintf(buf, "%lu", n);
62 return *this << buf;
65 OutputCharStream &OutputCharStream::operator<<(int n)
67 char buf[sizeof(int)*3 + 2];
68 sprintf(buf, "%d", n);
69 return *this << buf;
72 EncodeOutputCharStream::EncodeOutputCharStream()
73 : buf_(0), byteStream_(0), escaper_(0)
77 EncodeOutputCharStream::EncodeOutputCharStream(OutputByteStream *byteStream,
78 const OutputCodingSystem *codingSystem)
79 : buf_(0),
80 byteStream_(byteStream),
81 escaper_(0),
82 ownedEncoder_(codingSystem->makeEncoder())
84 encoder_ = ownedEncoder_.pointer();
85 encoder_->setUnencodableHandler(this);
86 allocBuf(codingSystem->fixedBytesPerChar());
87 encoder_->startFile(byteStream_);
90 EncodeOutputCharStream::EncodeOutputCharStream(OutputByteStream *byteStream,
91 Encoder *encoder)
92 : buf_(0),
93 byteStream_(byteStream),
94 escaper_(0),
95 encoder_(encoder)
97 allocBuf(0);
100 EncodeOutputCharStream::~EncodeOutputCharStream()
102 if (byteStream_)
103 flush();
104 delete [] buf_;
107 void EncodeOutputCharStream::open(OutputByteStream *byteStream,
108 const OutputCodingSystem *codingSystem)
110 if (byteStream_)
111 flush();
112 byteStream_ = byteStream;
113 ownedEncoder_ = codingSystem->makeEncoder();
114 encoder_ = ownedEncoder_.pointer();
115 encoder_->setUnencodableHandler(this);
116 delete [] buf_;
117 buf_ = 0;
118 ptr_ = end_ = buf_;
119 allocBuf(codingSystem->fixedBytesPerChar());
120 encoder_->startFile(byteStream_);
123 void EncodeOutputCharStream::flush()
125 if (ptr_ > buf_) {
126 encoder_->output(buf_, ptr_ - buf_, byteStream_);
127 ptr_ = buf_;
129 byteStream_->flush();
132 void EncodeOutputCharStream::flushBuf(Char c)
134 ASSERT(buf_ != 0);
135 encoder_->output(buf_, ptr_ - buf_, byteStream_);
136 ptr_ = buf_;
137 *ptr_++ = c;
140 void EncodeOutputCharStream::allocBuf(int bytesPerChar)
142 const int blockSize = 1024;
143 size_t bufSize = bytesPerChar ? blockSize/bytesPerChar : blockSize;
144 ptr_ = buf_ = new Char[bufSize];
145 end_ = buf_ + bufSize;
148 void EncodeOutputCharStream::setEscaper(Escaper f)
150 escaper_ = f;
153 void EncodeOutputCharStream::handleUnencodable(Char c, OutputByteStream *)
155 EncodeOutputCharStream tem(byteStream_, encoder_);
156 if (escaper_)
157 (*escaper_)(tem, c);
160 StrOutputCharStream::StrOutputCharStream()
161 : buf_(0), bufSize_(0)
163 sync(0);
166 StrOutputCharStream::~StrOutputCharStream()
168 delete [] buf_;
171 void StrOutputCharStream::extractString(StringC &str)
173 str.assign(buf_, ptr_ - buf_);
174 sync(0);
177 void StrOutputCharStream::flushBuf(Char c)
179 size_t used = ptr_ - buf_;
180 size_t oldSize = bufSize_;
181 bufSize_ = oldSize ? 2*oldSize : 10;
182 Char *oldBuf = buf_;
183 buf_ = new Char[bufSize_];
184 if (oldSize) {
185 memcpy(buf_, oldBuf, oldSize * sizeof(Char));
186 delete [] oldBuf;
188 sync(used);
189 *ptr_++ = c;
192 void StrOutputCharStream::flush()
196 void StrOutputCharStream::sync(size_t length)
198 ptr_ = buf_ + length;
199 end_ = buf_ + bufSize_;
202 RecordOutputCharStream::RecordOutputCharStream(OutputCharStream *os)
203 : os_(os)
205 ptr_ = buf_;
206 end_ = buf_ + bufSize_;
209 RecordOutputCharStream::~RecordOutputCharStream()
211 outputBuf();
212 delete os_;
215 void RecordOutputCharStream::setEscaper(Escaper f)
217 os_->setEscaper(f);
220 void RecordOutputCharStream::flush()
222 outputBuf();
223 os_->flush();
226 void RecordOutputCharStream::flushBuf(Char c)
228 outputBuf();
229 *ptr_++ = c;
232 void RecordOutputCharStream::outputBuf()
234 Char *start = buf_;
235 Char *p = start;
236 while (p < ptr_) {
237 switch (*p) {
238 case '\r': // translate RE to newline
239 if (start < p)
240 os_->write(start, p - start);
241 start = ++p;
242 *os_ << newline;
243 break;
244 case '\n': // ignore RS
245 if (start < p)
246 os_->write(start, p - start);
247 start = ++p;
248 break;
249 default:
250 ++p;
251 break;
254 if (start < p)
255 os_->write(start, p - start);
256 ptr_ = buf_;
257 end_ = buf_ + bufSize_;
260 #ifdef SP_NAMESPACE
262 #endif