[89] | 1 | /*
|
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
---|
| 3 | * contributor license agreements. See the NOTICE file distributed with
|
---|
| 4 | * this work for additional information regarding copyright ownership.
|
---|
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0
|
---|
| 6 | * (the "License"); you may not use this file except in compliance with
|
---|
| 7 | * the License. You may obtain a copy of the License at
|
---|
| 8 | *
|
---|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 10 | *
|
---|
| 11 | * Unless required by applicable law or agreed to in writing, software
|
---|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 14 | * See the License for the specific language governing permissions and
|
---|
| 15 | * limitations under the License.
|
---|
| 16 | */
|
---|
| 17 |
|
---|
| 18 | #ifndef _LOG4CXX_HELPERS_TRANSCODER_H
|
---|
| 19 | #define _LOG4CXX_HELPERS_TRANSCODER_H
|
---|
| 20 |
|
---|
| 21 | #if defined(_MSC_VER)
|
---|
| 22 | #pragma warning ( push )
|
---|
| 23 | #pragma warning ( disable: 4231 4251 4275 4786 )
|
---|
| 24 | #endif
|
---|
| 25 |
|
---|
| 26 | #include <log4cxx/logstring.h>
|
---|
| 27 |
|
---|
| 28 |
|
---|
| 29 | namespace log4cxx {
|
---|
| 30 | namespace helpers {
|
---|
| 31 | class ByteBuffer;
|
---|
| 32 | class Pool;
|
---|
| 33 | /**
|
---|
| 34 | * Simple transcoder for converting between
|
---|
| 35 | * external char and wchar_t strings and
|
---|
| 36 | * internal strings.
|
---|
| 37 | *
|
---|
| 38 | */
|
---|
| 39 | class LOG4CXX_EXPORT Transcoder {
|
---|
| 40 | public:
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 | /**
|
---|
| 44 | * Appends this specified string of UTF-8 characters to LogString.
|
---|
| 45 | */
|
---|
| 46 | static void decodeUTF8(const std::string& src, LogString& dst);
|
---|
| 47 | /**
|
---|
| 48 | * Converts the LogString to a UTF-8 string.
|
---|
| 49 | */
|
---|
| 50 | static void encodeUTF8(const LogString& src, std::string& dst);
|
---|
| 51 | /**
|
---|
| 52 | * Converts the LogString to a UTF-8 string.
|
---|
| 53 | */
|
---|
| 54 | static char* encodeUTF8(const LogString& src, log4cxx::helpers::Pool& p);
|
---|
| 55 | /**
|
---|
| 56 | * Append UCS-4 code point to a byte buffer as UTF-8.
|
---|
| 57 | */
|
---|
| 58 | static void encodeUTF8(unsigned int sv, ByteBuffer& dst);
|
---|
| 59 | /**
|
---|
| 60 | * Append UCS-4 code point to a byte buffer as UTF-16LE.
|
---|
| 61 | */
|
---|
| 62 | static void encodeUTF16LE(unsigned int sv, ByteBuffer& dst);
|
---|
| 63 | /**
|
---|
| 64 | * Append UCS-4 code point to a byte buffer as UTF-16BE.
|
---|
| 65 | */
|
---|
| 66 | static void encodeUTF16BE(unsigned int sv, ByteBuffer& dst);
|
---|
| 67 |
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | * Decodes next character from a UTF-8 string.
|
---|
| 71 | * @param in string from which the character is extracted.
|
---|
| 72 | * @param iter iterator addressing start of character, will be
|
---|
| 73 | * advanced to next character if successful.
|
---|
| 74 | * @return scalar value (UCS-4) or 0xFFFF if invalid sequence.
|
---|
| 75 | */
|
---|
| 76 | static unsigned int decode(const std::string& in,
|
---|
| 77 | std::string::const_iterator& iter);
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | * Appends UCS-4 value to a UTF-8 string.
|
---|
| 81 | * @param ch UCS-4 value.
|
---|
| 82 | * @param dst destination.
|
---|
| 83 | */
|
---|
| 84 | static void encode(unsigned int ch, std::string& dst);
|
---|
| 85 |
|
---|
| 86 | /**
|
---|
| 87 | * Appends string in the current code-page
|
---|
| 88 | * to a LogString.
|
---|
| 89 | */
|
---|
| 90 | static void decode(const std::string& src, LogString& dst);
|
---|
| 91 | /**
|
---|
| 92 | * Appends a LogString to a string in the current
|
---|
| 93 | * code-page. Unrepresentable characters may be
|
---|
| 94 | * replaced with loss characters.
|
---|
| 95 | */
|
---|
| 96 | static void encode(const LogString& src, std::string& dst);
|
---|
| 97 |
|
---|
| 98 | /**
|
---|
| 99 | * Encodes the specified LogString to the current
|
---|
| 100 | * character set.
|
---|
| 101 | * @param src string to encode.
|
---|
| 102 | * @param p pool from which to allocate return value.
|
---|
| 103 | * @return pool allocated string.
|
---|
| 104 | */
|
---|
| 105 | static char* encode(const LogString& src, log4cxx::helpers::Pool& p);
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 |
|
---|
| 109 | #if LOG4CXX_WCHAR_T_API || LOG4CXX_LOGCHAR_IS_WCHAR_T || defined(WIN32) || defined(_WIN32)
|
---|
| 110 | static void decode(const std::wstring& src, LogString& dst);
|
---|
| 111 | static void encode(const LogString& src, std::wstring& dst);
|
---|
| 112 | static wchar_t* wencode(const LogString& src, log4cxx::helpers::Pool& p);
|
---|
| 113 |
|
---|
| 114 | /**
|
---|
| 115 | * Decodes next character from a wstring.
|
---|
| 116 | * @param in string from which the character is extracted.
|
---|
| 117 | * @param iter iterator addressing start of character, will be
|
---|
| 118 | * advanced to next character if successful.
|
---|
| 119 | * @return scalar value (UCS-4) or 0xFFFF if invalid sequence.
|
---|
| 120 | */
|
---|
| 121 | static unsigned int decode(const std::wstring& in,
|
---|
| 122 | std::wstring::const_iterator& iter);
|
---|
| 123 |
|
---|
| 124 | /**
|
---|
| 125 | * Appends UCS-4 value to a UTF-8 string.
|
---|
| 126 | * @param ch UCS-4 value.
|
---|
| 127 | * @param dst destination.
|
---|
| 128 | */
|
---|
| 129 | static void encode(unsigned int ch, std::wstring& dst);
|
---|
| 130 |
|
---|
| 131 | #endif
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 | #if LOG4CXX_UNICHAR_API || LOG4CXX_CFSTRING_API || LOG4CXX_LOGCHAR_IS_UNICHAR
|
---|
| 135 | static void decode(const std::basic_string<UniChar>& src, LogString& dst);
|
---|
| 136 | static void encode(const LogString& src, std::basic_string<UniChar>& dst);
|
---|
| 137 |
|
---|
| 138 | /**
|
---|
| 139 | * Decodes next character from a UniChar string.
|
---|
| 140 | * @param in string from which the character is extracted.
|
---|
| 141 | * @param iter iterator addressing start of character, will be
|
---|
| 142 | * advanced to next character if successful.
|
---|
| 143 | * @return scalar value (UCS-4) or 0xFFFF if invalid sequence.
|
---|
| 144 | */
|
---|
| 145 | static unsigned int decode(const std::basic_string<UniChar>& in,
|
---|
| 146 | std::basic_string<UniChar>::const_iterator& iter);
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
| 149 | * Appends UCS-4 value to a UTF-8 string.
|
---|
| 150 | * @param ch UCS-4 value.
|
---|
| 151 | * @param dst destination.
|
---|
| 152 | */
|
---|
| 153 | static void encode(unsigned int ch, std::basic_string<UniChar>& dst);
|
---|
| 154 |
|
---|
| 155 | #endif
|
---|
| 156 |
|
---|
| 157 | #if LOG4CXX_CFSTRING_API
|
---|
| 158 | static void decode(const CFStringRef& src, LogString& dst);
|
---|
| 159 | static CFStringRef encode(const LogString& src);
|
---|
| 160 | #endif
|
---|
| 161 |
|
---|
| 162 | enum { LOSSCHAR = 0x3F };
|
---|
| 163 |
|
---|
| 164 | /**
|
---|
| 165 | * Returns a logchar value given a character literal in the ASCII charset.
|
---|
| 166 | * Used to implement the LOG4CXX_STR macro for EBCDIC and UNICHAR.
|
---|
| 167 | */
|
---|
| 168 | static logchar decode(char v);
|
---|
| 169 | /**
|
---|
| 170 | * Returns a LogString given a string literal in the ASCII charset.
|
---|
| 171 | * Used to implement the LOG4CXX_STR macro for EBCDIC and UNICHAR.
|
---|
| 172 | */
|
---|
| 173 | static LogString decode(const char* v);
|
---|
| 174 |
|
---|
| 175 | /**
|
---|
| 176 | * Encodes a charset name in the default encoding
|
---|
| 177 | * without using a CharsetEncoder (which could trigger recursion).
|
---|
| 178 | */
|
---|
| 179 | static std::string encodeCharsetName(const LogString& charsetName);
|
---|
| 180 |
|
---|
| 181 | private:
|
---|
| 182 |
|
---|
| 183 | private:
|
---|
| 184 | Transcoder();
|
---|
| 185 | Transcoder(const Transcoder&);
|
---|
| 186 | Transcoder& operator=(const Transcoder&);
|
---|
| 187 | enum { BUFSIZE = 256 };
|
---|
| 188 | static size_t encodeUTF8(unsigned int ch, char* dst);
|
---|
| 189 | static size_t encodeUTF16BE(unsigned int ch, char* dst);
|
---|
| 190 | static size_t encodeUTF16LE(unsigned int ch, char* dst);
|
---|
| 191 |
|
---|
| 192 | };
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | #define LOG4CXX_ENCODE_CHAR(var, src) \
|
---|
| 197 | std::string var; \
|
---|
| 198 | log4cxx::helpers::Transcoder::encode(src, var)
|
---|
| 199 |
|
---|
| 200 | #define LOG4CXX_DECODE_CHAR(var, src) \
|
---|
| 201 | log4cxx::LogString var; \
|
---|
| 202 | log4cxx::helpers::Transcoder::decode(src, var)
|
---|
| 203 |
|
---|
| 204 | #define LOG4CXX_DECODE_CFSTRING(var, src) \
|
---|
| 205 | log4cxx::LogString var; \
|
---|
| 206 | log4cxx::helpers::Transcoder::decode(src, var)
|
---|
| 207 |
|
---|
| 208 | #define LOG4CXX_ENCODE_CFSTRING(var, src) \
|
---|
| 209 | CFStringRef var = log4cxx::helpers::Transcoder::encode(src)
|
---|
| 210 |
|
---|
| 211 |
|
---|
| 212 | #if LOG4CXX_LOGCHAR_IS_WCHAR
|
---|
| 213 |
|
---|
| 214 | #define LOG4CXX_ENCODE_WCHAR(var, src) \
|
---|
| 215 | const std::wstring& var = src
|
---|
| 216 |
|
---|
| 217 | #define LOG4CXX_DECODE_WCHAR(var, src) \
|
---|
| 218 | const log4cxx::LogString& var = src
|
---|
| 219 |
|
---|
| 220 | #else
|
---|
| 221 |
|
---|
| 222 | #define LOG4CXX_ENCODE_WCHAR(var, src) \
|
---|
| 223 | std::wstring var; \
|
---|
| 224 | log4cxx::helpers::Transcoder::encode(src, var)
|
---|
| 225 |
|
---|
| 226 | #define LOG4CXX_DECODE_WCHAR(var, src) \
|
---|
| 227 | log4cxx::LogString var; \
|
---|
| 228 | log4cxx::helpers::Transcoder::decode(src, var)
|
---|
| 229 |
|
---|
| 230 | #endif
|
---|
| 231 |
|
---|
| 232 | #if LOG4CXX_LOGCHAR_IS_UNICHAR
|
---|
| 233 |
|
---|
| 234 | #define LOG4CXX_ENCODE_UNICHAR(var, src) \
|
---|
| 235 | const std::basic_string<UniChar>& var = src
|
---|
| 236 |
|
---|
| 237 | #define LOG4CXX_DECODE_UNICHAR(var, src) \
|
---|
| 238 | const log4cxx::LogString& var = src
|
---|
| 239 |
|
---|
| 240 | #else
|
---|
| 241 |
|
---|
| 242 | #define LOG4CXX_ENCODE_UNICHAR(var, src) \
|
---|
| 243 | std::basic_string<UniChar> var; \
|
---|
| 244 | log4cxx::helpers::Transcoder::encode(src, var)
|
---|
| 245 |
|
---|
| 246 | #define LOG4CXX_DECODE_UNICHAR(var, src) \
|
---|
| 247 | log4cxx::LogString var; \
|
---|
| 248 | log4cxx::helpers::Transcoder::decode(src, var)
|
---|
| 249 |
|
---|
| 250 | #endif
|
---|
| 251 |
|
---|
| 252 | #if defined(_MSC_VER)
|
---|
| 253 | #pragma warning (pop)
|
---|
| 254 | #endif
|
---|
| 255 |
|
---|
| 256 | #endif //_LOG4CXX_HELPERS_TRANSCODER_H
|
---|