📄 streamconverter.cpp
字号:
//
// StreamConverter.cpp
//
// $Id: //poco/Main/Foundation/src/StreamConverter.cpp#5 $
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include "Foundation/StreamConverter.h"
#include "Foundation/TextEncoding.h"
Foundation_BEGIN
StreamConverterBuf::StreamConverterBuf(std::istream& istr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar):
_pIstr(&istr),
_pOstr(0),
_inEncoding(inEncoding),
_outEncoding(outEncoding),
_defaultChar(defaultChar),
_sequenceLength(0),
_pos(0),
_errors(0)
{
}
StreamConverterBuf::StreamConverterBuf(std::ostream& ostr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar):
_pIstr(0),
_pOstr(&ostr),
_inEncoding(inEncoding),
_outEncoding(outEncoding),
_defaultChar(defaultChar),
_sequenceLength(0),
_pos(0),
_errors(0)
{
}
StreamConverterBuf::~StreamConverterBuf()
{
}
int StreamConverterBuf::readFromDevice()
{
poco_assert_dbg (_pIstr);
if (_pos < _sequenceLength) return _buffer[_pos++];
_pos = 0;
_sequenceLength = 0;
int c = _pIstr->get();
if (c == -1) return -1;
poco_assert (c < 256);
int uc;
int n = _inEncoding.characterMap()[c];
if (n == -1)
{
uc = _defaultChar;
++_errors;
}
else if (n >= 0)
uc = n;
else
{
poco_assert_dbg(-n <= sizeof(_buffer));
_buffer[0] = (unsigned char) c;
_pIstr->read((char*) _buffer + 1, -n - 1);
if (_pIstr->gcount() == -n - 1)
{
uc = _inEncoding.convert(_buffer);
if (uc == -1) uc = _defaultChar;
}
else
{
uc = _defaultChar;
++_errors;
}
}
_sequenceLength = _outEncoding.convert(uc, _buffer, sizeof(_buffer));
if (_sequenceLength == 0)
_sequenceLength = _outEncoding.convert(_defaultChar, _buffer, sizeof(_buffer));
if (_sequenceLength == 0)
return -1;
else
return _buffer[_pos++];
}
int StreamConverterBuf::writeToDevice(char c)
{
poco_assert_dbg (_pOstr);
if (_sequenceLength == 0)
{
int n = _inEncoding.characterMap()[(unsigned char) c];
if (n == -1)
{
++_errors;
return -1;
}
_buffer[0] = (unsigned char) c;
_sequenceLength = n < 0 ? -n : 1;
_pos = 1;
}
else _buffer[_pos++] = (unsigned char) c;
if (_pos == _sequenceLength)
{
int uc = _inEncoding.convert(_buffer);
if (uc == -1)
{
++_errors;
return -1;
}
int n = _outEncoding.convert(uc, _buffer, sizeof(_buffer));
if (n == 0) n = _outEncoding.convert(_defaultChar, _buffer, sizeof(_buffer));
poco_assert_dbg (n <= sizeof(_buffer));
_pOstr->write((char*) _buffer, n);
_sequenceLength = 0;
_pos = 0;
}
return c;
}
int StreamConverterBuf::errors() const
{
return _errors;
}
StreamConverterIOS::StreamConverterIOS(std::istream& istr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar):
_buf(istr, inEncoding, outEncoding, defaultChar)
{
init(&_buf);
}
StreamConverterIOS::StreamConverterIOS(std::ostream& ostr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar):
_buf(ostr, inEncoding, outEncoding, defaultChar)
{
init(&_buf);
}
StreamConverterIOS::~StreamConverterIOS()
{
}
StreamConverterBuf* StreamConverterIOS::rdbuf()
{
return &_buf;
}
int StreamConverterIOS::errors() const
{
return _buf.errors();
}
InputStreamConverter::InputStreamConverter(std::istream& istr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar):
StreamConverterIOS(istr, inEncoding, outEncoding, defaultChar),
std::istream(&_buf)
{
}
InputStreamConverter::~InputStreamConverter()
{
}
OutputStreamConverter::OutputStreamConverter(std::ostream& ostr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar):
StreamConverterIOS(ostr, inEncoding, outEncoding, defaultChar),
std::ostream(&_buf)
{
}
OutputStreamConverter::~OutputStreamConverter()
{
}
Foundation_END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -