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"
31 #if defined(linux) || defined(CSRG_BASED)
39 namespace SP_NAMESPACE {
42 OutputCharStream::OutputCharStream()
43 : ptr_(0), end_(0), escaper_(0)
47 OutputCharStream::~OutputCharStream()
51 OutputCharStream &OutputCharStream::write(const Char *s, size_t n)
54 size_t spare = end_ - ptr_;
56 memcpy(ptr_, s, n*sizeof(Char));
61 memcpy(ptr_, s, spare*sizeof(Char));
72 OutputCharStream &OutputCharStream::operator<<(const char *s)
81 OutputCharStream &OutputCharStream::operator<<(unsigned long n)
83 char buf[sizeof(unsigned long)*3 + 1];
84 sprintf(buf, "%lu", n);
88 OutputCharStream &OutputCharStream::operator<<(int n)
90 char buf[sizeof(int)*3 + 2];
91 sprintf(buf, "%d", n);
96 IosOutputCharStream::IosOutputCharStream()
97 : buf_(0), byteStream_(0)
101 IosOutputCharStream::IosOutputCharStream(streambuf *byteStream,
102 const OutputCodingSystem *codingSystem)
104 byteStream_(byteStream),
105 ownedEncoder_(codingSystem->makeEncoder())
107 encoder_ = ownedEncoder_.pointer();
108 encoder_->setUnencodableHandler(this);
109 allocBuf(codingSystem->fixedBytesPerChar());
110 encoder_->startFile(byteStream_);
113 IosOutputCharStream::IosOutputCharStream(streambuf *byteStream,
116 byteStream_(byteStream),
122 IosOutputCharStream::~IosOutputCharStream()
129 void IosOutputCharStream::open(streambuf *byteStream,
130 const OutputCodingSystem *codingSystem)
134 byteStream_ = byteStream;
135 ownedEncoder_ = codingSystem->makeEncoder();
136 encoder_ = ownedEncoder_.pointer();
137 encoder_->setUnencodableHandler(this);
141 allocBuf(codingSystem->fixedBytesPerChar());
142 encoder_->startFile(byteStream_);
145 void IosOutputCharStream::flush()
148 encoder_->output(buf_, ptr_ - buf_, byteStream_);
151 #if defined(linux) || defined(CSRG_BASED)
152 byteStream_->pubsync();
158 void IosOutputCharStream::flushBuf(Char c)
161 encoder_->output(buf_, ptr_ - buf_, byteStream_);
166 void IosOutputCharStream::allocBuf(int bytesPerChar)
168 const int blockSize = 1024;
169 size_t bufSize = bytesPerChar ? blockSize/bytesPerChar : blockSize;
170 ptr_ = buf_ = new Char[bufSize];
171 end_ = buf_ + bufSize;
174 void IosOutputCharStream::handleUnencodable(Char c, streambuf *)
176 IosOutputCharStream tem(byteStream_, encoder_);
180 StrOutputCharStream::StrOutputCharStream()
181 : buf_(0), bufSize_(0)
186 StrOutputCharStream::~StrOutputCharStream()
191 void StrOutputCharStream::extractString(StringC &str)
193 str.assign(buf_, ptr_ - buf_);
197 void StrOutputCharStream::flushBuf(Char c)
199 size_t used = ptr_ - buf_;
200 size_t oldSize = bufSize_;
201 bufSize_ = oldSize ? 2*oldSize : 10;
203 buf_ = new Char[bufSize_];
205 memcpy(buf_, oldBuf, oldSize * sizeof(Char));
212 void StrOutputCharStream::flush()
216 void StrOutputCharStream::sync(size_t length)
218 ptr_ = buf_ + length;
219 end_ = buf_ + bufSize_;
222 RecordOutputCharStream::RecordOutputCharStream(OutputCharStream *os)
226 end_ = buf_ + bufSize_;
229 RecordOutputCharStream::~RecordOutputCharStream()
235 void RecordOutputCharStream::flush()
241 void RecordOutputCharStream::flushBuf(Char c)
247 void RecordOutputCharStream::outputBuf()
253 case '\r': // translate RE to newline
255 os_->write(start, p - start);
259 case '\n': // ignore RS
261 os_->write(start, p - start);
270 os_->write(start, p - start);
272 end_ = buf_ + bufSize_;