69 typedef typename traits_type::int_type
int_type;
72 typedef typename traits_type::pos_type
pos_type;
75 typedef typename traits_type::off_type
off_type;
80 typedef typename CodecType::result CodecResult;
81 typedef MBState state_type;
146 _readState = state_type();
147 _writeState = state_type();
158 void attach(std::basic_ios<extern_type>& target)
186 void reset(std::basic_ios<extern_type>& target)
201 _readState = state_type();
202 _writeState = state_type();
209 std::streamsize
import(std::streamsize size = 0)
213 this->setg(_ibuf, _ibuf, _ibuf);
216 std::streamsize n = 0;
217 std::basic_streambuf<extern_type>* rdbuf = _target ? _target->rdbuf()
221 if(size == 0 && rdbuf)
222 size = rdbuf->in_avail();
225 const std::streamsize ebufavail = _ebufmax - _iebufsize;
226 size = ebufavail < size ? ebufavail : size;
228 if(size > 0 && rdbuf)
230 n = rdbuf->sgetn( _iebuf + _iebufsize, size );
231 _iebufsize +=
static_cast<int>(n);
239 if( this->gptr() - this->eback() > _pbmax)
241 std::streamsize movelen = this->egptr() - this->gptr() + _pbmax;
242 std::char_traits<char_type>::move( _ibuf,
243 this->gptr() - _pbmax,
244 static_cast<std::size_t
>(movelen));
245 this->setg(_ibuf, _ibuf + _pbmax, _ibuf + movelen);
248 typename CodecType::result r = decode();
252 if( rdbuf == 0 && _iebufsize == 0 && r == CodecType::partial)
262 std::streamsize
import(
const extern_type* buf, std::streamsize size)
266 this->setg(_ibuf, _ibuf, _ibuf);
269 const std::streamsize bufavail = _ebufmax - _iebufsize;
270 size = bufavail < size ? bufavail : size;
273 std::char_traits<extern_type>::copy( _iebuf + _iebufsize, buf,
static_cast<std::size_t
>(size) );
274 _iebufsize +=
static_cast<int>(size);
277 if( this->gptr() - this->eback() > _pbmax)
279 std::streamsize movelen = this->egptr() - this->gptr() + _pbmax;
280 std::char_traits<char_type>::move( _ibuf,
281 this->gptr() - _pbmax,
282 static_cast<std::size_t
>(movelen));
283 this->setg(_ibuf, _ibuf + _pbmax, _ibuf + movelen);
299 while( this->pptr() > this->pbase() )
301 if( this->overflow( traits_type::eof() ) == traits_type::eof() )
306 if( _target && _target->rdbuf() && _codec )
308 typename CodecType::result res = CodecType::error;
313 res = _codec->
unshift(_writeState, _oebuf, _oebuf + _ebufmax, next);
314 if(res == CodecType::noconv)
317 if(res == CodecType::error)
320 std::streamsize n =
static_cast<std::streamsize
>(next - _oebuf);
321 if( n != _target->rdbuf()->sputn(_oebuf, n) )
324 while(res == CodecType::partial);
329 _writeState = state_type();
335 virtual std::streamsize showmanyc()
345 return _target && _target->rdbuf() ? 0 : -1;
351 if( ! _target || ! _target->rdbuf() )
352 return traits_type::eof();
356 this->setp( _obuf, _obuf + _obufmax );
359 while( this->pptr() > this->pbase() )
368 typename CodecType::result res = CodecType::noconv;
370 res = _codec->out(_writeState, fromBegin, fromEnd, fromNext, toBegin, toEnd, toNext);
372 if(res == CodecType::noconv)
374 std::size_t fromSize = fromEnd - fromBegin;
375 std::size_t toSize = toEnd - toBegin;
376 std::size_t size = toSize < fromSize ? toSize : fromSize;
378 this->copyChars( toBegin, fromBegin, size );
384 _oebufsize +=
static_cast<int>(toNext - toBegin);
385 std::size_t leftover = fromEnd - fromNext;
386 if(leftover && fromNext > fromBegin)
388 std::char_traits<char_type>::move(_obuf, fromNext, leftover);
391 this->setp( _obuf, _obuf + _obufmax );
392 this->pbump(
static_cast<int>(leftover) );
394 if(res == CodecType::error)
397 if(res == CodecType::partial && _oebufsize == 0)
400 _oebufsize -=
static_cast<int>(_target->rdbuf()->sputn(_oebuf, _oebufsize));
402 return traits_type::eof();
405 if( ! traits_type::eq_int_type(ch, traits_type::eof()) )
407 *( this->pptr() ) = traits_type::to_char_type(ch);
411 return traits_type::not_eof(ch);
417 if( this->gptr() < this->egptr() )
418 return traits_type::to_int_type( *this->gptr() );
422 return this->gptr() < this->egptr() ? traits_type::to_int_type( *this->gptr() )
437 typename CodecType::result r = CodecType::noconv;
439 r = _codec->in(_readState, fromBegin, fromEnd, fromNext, toBegin, toEnd, toNext);
441 if(r == CodecType::noconv)
444 int n =_iebufsize > _ibufmax ? _ibufmax : _iebufsize ;
445 this->copyChars(toBegin, fromBegin, n);
450 std::streamsize consumed = fromNext - fromBegin;
453 _iebufsize -=
static_cast<int>(consumed);
454 std::char_traits<extern_type>::move( _iebuf, _iebuf + consumed, _iebufsize);
457 std::streamsize generated = toNext - toBegin;
460 this->setg(this->eback(),
462 this->egptr() + generated );
465 if(r == CodecType::error)
472 template <
typename T>
473 void copyChars(T* s1,
const T* s2, std::size_t n)
475 std::char_traits<T>::copy(s1, s2, n);
479 template <
typename A,
typename B>
480 void copyChars(A* s1,
const B* s2, std::size_t n)
491 template <
typename A>
492 void copyChars(A* s1,
const Char* s2, std::size_t n)
496 *s1 =
static_cast<A
>(s2->value());
503 static const int _pbmax = 4;
505 static const int _ebufmax = 256;
511 static const int _ibufmax = 256;
513 static const int _obufmax = 256;
516 state_type _readState;
517 state_type _writeState;
519 std::size_t _codecRefs;
520 std::basic_ios<extern_type>* _target;
traits_type::off_type off_type
Stream offset type.
Definition TextBuffer.h:75
BasicTextBuffer(std::basic_ios< extern_type > &target, CodecType *codec=0)
Construct with external device and codec.
Definition TextBuffer.h:92
~BasicTextBuffer()
Destructor.
Definition TextBuffer.h:124
void setCodec(CodecType *codec)
Sets the text codec.
Definition TextBuffer.h:144
CharT intern_type
Internal character type.
Definition TextBuffer.h:60
traits_type::pos_type pos_type
Stream position type.
Definition TextBuffer.h:72
TextCodec< char_type, extern_type > CodecType
Codec type.
Definition TextBuffer.h:78
traits_type::int_type int_type
Integer type.
Definition TextBuffer.h:69
void attach(std::basic_ios< extern_type > &target)
Attach to external target.
Definition TextBuffer.h:158
std::char_traits< CharT > traits_type
Internal character traits.
Definition TextBuffer.h:66
CodecType * codec()
Definition TextBuffer.h:136
void detach()
Detach from external target.
Definition TextBuffer.h:165
CharT char_type
Internal character type.
Definition TextBuffer.h:63
void reset()
Resets the buffer and target.
Definition TextBuffer.h:175
BasicTextBuffer(CodecType *codec=0)
Construct with codec.
Definition TextBuffer.h:111
ByteT extern_type
External character type.
Definition TextBuffer.h:57
void reset(std::basic_ios< extern_type > &target)
Resets the buffer and target.
Definition TextBuffer.h:186
Converts character sequences with different encodings.
Converts between character encodings.
Definition Api-TextCodec.h:44
result unshift(MBState &state, ByteT *to, ByteT *to_end, ByteT *&to_next) const
Unshifts a character sequence.