2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
23 /* $XConsortium: OutputCharStream.C /main/1 1996/07/29 16:59:34 cde-hp $ */
24 // Copyright (c) 1994 James Clark
25 // See the file COPYING for copying permission.
28 #include "OutputCharStream.h"
29 #include "CodingSystem.h"
35 namespace SP_NAMESPACE {
38 OutputCharStream::OutputCharStream()
39 : ptr_(0), end_(0), escaper_(0)
43 OutputCharStream::~OutputCharStream()
47 OutputCharStream &OutputCharStream::write(const Char *s, size_t n)
50 size_t spare = end_ - ptr_;
52 memcpy(ptr_, s, n*sizeof(Char));
57 memcpy(ptr_, s, spare*sizeof(Char));
68 OutputCharStream &OutputCharStream::operator<<(const char *s)
77 OutputCharStream &OutputCharStream::operator<<(unsigned long n)
79 char buf[sizeof(unsigned long)*3 + 1];
80 sprintf(buf, "%lu", n);
84 OutputCharStream &OutputCharStream::operator<<(int n)
86 char buf[sizeof(int)*3 + 2];
87 sprintf(buf, "%d", n);
92 IosOutputCharStream::IosOutputCharStream()
93 : buf_(0), byteStream_(0)
97 IosOutputCharStream::IosOutputCharStream(streambuf *byteStream,
98 const OutputCodingSystem *codingSystem)
100 byteStream_(byteStream),
101 ownedEncoder_(codingSystem->makeEncoder())
103 encoder_ = ownedEncoder_.pointer();
104 encoder_->setUnencodableHandler(this);
105 allocBuf(codingSystem->fixedBytesPerChar());
106 encoder_->startFile(byteStream_);
109 IosOutputCharStream::IosOutputCharStream(streambuf *byteStream,
112 byteStream_(byteStream),
118 IosOutputCharStream::~IosOutputCharStream()
125 void IosOutputCharStream::open(streambuf *byteStream,
126 const OutputCodingSystem *codingSystem)
130 byteStream_ = byteStream;
131 ownedEncoder_ = codingSystem->makeEncoder();
132 encoder_ = ownedEncoder_.pointer();
133 encoder_->setUnencodableHandler(this);
137 allocBuf(codingSystem->fixedBytesPerChar());
138 encoder_->startFile(byteStream_);
141 void IosOutputCharStream::flush()
144 encoder_->output(buf_, ptr_ - buf_, byteStream_);
150 void IosOutputCharStream::flushBuf(Char c)
153 encoder_->output(buf_, ptr_ - buf_, byteStream_);
158 void IosOutputCharStream::allocBuf(int bytesPerChar)
160 const int blockSize = 1024;
161 size_t bufSize = bytesPerChar ? blockSize/bytesPerChar : blockSize;
162 ptr_ = buf_ = new Char[bufSize];
163 end_ = buf_ + bufSize;
166 void IosOutputCharStream::handleUnencodable(Char c, streambuf *)
168 IosOutputCharStream tem(byteStream_, encoder_);
172 StrOutputCharStream::StrOutputCharStream()
173 : buf_(0), bufSize_(0)
178 StrOutputCharStream::~StrOutputCharStream()
183 void StrOutputCharStream::extractString(StringC &str)
185 str.assign(buf_, ptr_ - buf_);
189 void StrOutputCharStream::flushBuf(Char c)
191 size_t used = ptr_ - buf_;
192 size_t oldSize = bufSize_;
193 bufSize_ = oldSize ? 2*oldSize : 10;
195 buf_ = new Char[bufSize_];
197 memcpy(buf_, oldBuf, oldSize * sizeof(Char));
204 void StrOutputCharStream::flush()
208 void StrOutputCharStream::sync(size_t length)
210 ptr_ = buf_ + length;
211 end_ = buf_ + bufSize_;
214 RecordOutputCharStream::RecordOutputCharStream(OutputCharStream *os)
218 end_ = buf_ + bufSize_;
221 RecordOutputCharStream::~RecordOutputCharStream()
227 void RecordOutputCharStream::flush()
233 void RecordOutputCharStream::flushBuf(Char c)
239 void RecordOutputCharStream::outputBuf()
245 case '\r': // translate RE to newline
247 os_->write(start, p - start);
251 case '\n': // ignore RS
253 os_->write(start, p - start);
262 os_->write(start, p - start);
264 end_ = buf_ + bufSize_;