📄 charsetdecoder.cpp
字号:
virtual log4cxx_status_t decode(ByteBuffer& in,
LogString& out) {
if (in.remaining() > 0) {
std::string tmp(in.current(), in.remaining());
std::string::const_iterator iter = tmp.begin();
while(iter != tmp.end()) {
unsigned int sv = Transcoder::decode(tmp, iter);
if (sv == 0xFFFF) {
size_t offset = iter - tmp.begin();
in.position(in.position() + offset);
return APR_BADARG;
} else {
Transcoder::encode(sv, out);
}
}
in.position(in.limit());
}
return APR_SUCCESS;
}
private:
UTF8CharsetDecoder(const UTF8CharsetDecoder&);
UTF8CharsetDecoder& operator=(const UTF8CharsetDecoder&);
};
#endif
/**
* Converts from ISO-8859-1 to LogString.
*
*/
class ISOLatinCharsetDecoder : public CharsetDecoder
{
public:
ISOLatinCharsetDecoder() {
}
virtual ~ISOLatinCharsetDecoder() {
}
private:
virtual log4cxx_status_t decode(ByteBuffer& in,
LogString& out) {
if (in.remaining() > 0) {
const unsigned char* src = (unsigned char*) in.current();
const unsigned char* srcEnd = src + in.remaining();
while(src < srcEnd) {
unsigned int sv = *(src++);
Transcoder::encode(sv, out);
}
in.position(in.limit());
}
return APR_SUCCESS;
}
private:
ISOLatinCharsetDecoder(const ISOLatinCharsetDecoder&);
ISOLatinCharsetDecoder& operator=(const ISOLatinCharsetDecoder&);
};
/**
* Converts from US-ASCII to LogString.
*
*/
class USASCIICharsetDecoder : public CharsetDecoder
{
public:
USASCIICharsetDecoder() {
}
virtual ~USASCIICharsetDecoder() {
}
private:
virtual log4cxx_status_t decode(ByteBuffer& in,
LogString& out) {
log4cxx_status_t stat = APR_SUCCESS;
if (in.remaining() > 0) {
const unsigned char* src = (unsigned char*) in.current();
const unsigned char* srcEnd = src + in.remaining();
while(src < srcEnd) {
unsigned char sv = *src;
if (sv < 0x80) {
src++;
Transcoder::encode(sv, out);
} else {
stat = APR_BADARG;
break;
}
}
in.position(src - (const unsigned char*) in.data());
}
return stat;
}
private:
USASCIICharsetDecoder(const USASCIICharsetDecoder&);
USASCIICharsetDecoder& operator=(const USASCIICharsetDecoder&);
};
/**
* Charset decoder that uses an embedded CharsetDecoder consistent
* with current locale settings.
*/
class LocaleCharsetDecoder : public CharsetDecoder {
public:
LocaleCharsetDecoder() : pool(), mutex(pool), decoder(), encoding() {
}
virtual ~LocaleCharsetDecoder() {
}
virtual log4cxx_status_t decode(ByteBuffer& in,
LogString& out) {
const char* p = in.current();
size_t i = in.position();
#if !LOG4CXX_CHARSET_EBCDIC
for (; i < in.limit() && ((unsigned int) *p) < 0x80; i++, p++) {
out.append(1, *p);
}
in.position(i);
#endif
if (i < in.limit()) {
Pool subpool;
const char* enc = apr_os_locale_encoding(subpool.getAPRPool());
{
synchronized sync(mutex);
if (enc == 0) {
if (decoder == 0) {
encoding = "C";
decoder = new USASCIICharsetDecoder();
}
} else if (encoding != enc) {
encoding = enc;
try {
LogString e;
Transcoder::decode(encoding, e);
decoder = getDecoder(e);
} catch (IllegalArgumentException& ex) {
decoder = new USASCIICharsetDecoder();
}
}
}
return decoder->decode(in, out);
}
return APR_SUCCESS;
}
private:
Pool pool;
Mutex mutex;
CharsetDecoderPtr decoder;
std::string encoding;
};
} // namespace helpers
} //namespace log4cxx
CharsetDecoder::CharsetDecoder() {
}
CharsetDecoder::~CharsetDecoder() {
}
CharsetDecoder* CharsetDecoder::createDefaultDecoder() {
#if LOG4CXX_CHARSET_UTF8
return new UTF8CharsetDecoder();
#elif LOG4CXX_CHARSET_ISO88591 || defined(_WIN32_WCE)
return new ISOLatinCharsetDecoder();
#elif LOG4CXX_CHARSET_USASCII
return new USASCIICharsetDecoder();
#elif LOG4CXX_LOGCHAR_IS_WCHAR && LOG4CXX_HAS_MBSRTOWCS
return new MbstowcsCharsetDecoder();
#else
return new LocaleCharsetDecoder();
#endif
}
CharsetDecoderPtr CharsetDecoder::getDefaultDecoder() {
static CharsetDecoderPtr decoder(createDefaultDecoder());
//
// if invoked after static variable destruction
// (if logging is called in the destructor of a static object)
// then create a new decoder.
//
if (decoder == 0) {
return createDefaultDecoder();
}
return decoder;
}
CharsetDecoderPtr CharsetDecoder::getUTF8Decoder() {
static CharsetDecoderPtr decoder(new UTF8CharsetDecoder());
//
// if invoked after static variable destruction
// (if logging is called in the destructor of a static object)
// then create a new decoder.
//
if (decoder == 0) {
return new UTF8CharsetDecoder();
}
return decoder;
}
CharsetDecoderPtr CharsetDecoder::getISOLatinDecoder() {
return new ISOLatinCharsetDecoder();
}
CharsetDecoderPtr CharsetDecoder::getDecoder(const LogString& charset) {
if (StringHelper::equalsIgnoreCase(charset, LOG4CXX_STR("UTF-8"), LOG4CXX_STR("utf-8")) ||
StringHelper::equalsIgnoreCase(charset, LOG4CXX_STR("UTF8"), LOG4CXX_STR("utf8"))) {
return new UTF8CharsetDecoder();
} else if (StringHelper::equalsIgnoreCase(charset, LOG4CXX_STR("C"), LOG4CXX_STR("c")) ||
charset == LOG4CXX_STR("646") ||
StringHelper::equalsIgnoreCase(charset, LOG4CXX_STR("US-ASCII"), LOG4CXX_STR("us-ascii")) ||
StringHelper::equalsIgnoreCase(charset, LOG4CXX_STR("ISO646-US"), LOG4CXX_STR("iso646-US")) ||
StringHelper::equalsIgnoreCase(charset, LOG4CXX_STR("ANSI_X3.4-1968"), LOG4CXX_STR("ansi_x3.4-1968"))) {
return new USASCIICharsetDecoder();
} else if (StringHelper::equalsIgnoreCase(charset, LOG4CXX_STR("ISO-8859-1"), LOG4CXX_STR("iso-8859-1")) ||
StringHelper::equalsIgnoreCase(charset, LOG4CXX_STR("ISO-LATIN-1"), LOG4CXX_STR("iso-latin-1"))) {
return new ISOLatinCharsetDecoder();
}
#if APR_HAS_XLATE || !defined(_WIN32)
return new APRCharsetDecoder(charset);
#else
throw IllegalArgumentException(charset);
#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -