You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4cxx-dev@logging.apache.org by ca...@apache.org on 2007/11/14 05:30:02 UTC

svn commit: r594758 - in /logging/log4cxx/trunk/src/main: cpp/charsetdecoder.cpp cpp/charsetencoder.cpp include/log4cxx/helpers/charsetdecoder.h include/log4cxx/helpers/charsetencoder.h

Author: carnold
Date: Tue Nov 13 20:30:01 2007
New Revision: 594758

URL: http://svn.apache.org/viewvc?rev=594758&view=rev
Log:
LOGCXX-167: Use non-APR encoders/decoders for default for common encodings

Modified:
    logging/log4cxx/trunk/src/main/cpp/charsetdecoder.cpp
    logging/log4cxx/trunk/src/main/cpp/charsetencoder.cpp
    logging/log4cxx/trunk/src/main/include/log4cxx/helpers/charsetdecoder.h
    logging/log4cxx/trunk/src/main/include/log4cxx/helpers/charsetencoder.h

Modified: logging/log4cxx/trunk/src/main/cpp/charsetdecoder.cpp
URL: http://svn.apache.org/viewvc/logging/log4cxx/trunk/src/main/cpp/charsetdecoder.cpp?rev=594758&r1=594757&r2=594758&view=diff
==============================================================================
--- logging/log4cxx/trunk/src/main/cpp/charsetdecoder.cpp (original)
+++ logging/log4cxx/trunk/src/main/cpp/charsetdecoder.cpp Tue Nov 13 20:30:01 2007
@@ -26,6 +26,7 @@
 #include <log4cxx/private/log4cxx_private.h>
 #include <locale.h>
 #include <apr_portable.h>
+#include <log4cxx/helpers/stringhelper.h>
 
 
 using namespace log4cxx;
@@ -361,16 +362,15 @@
         USASCIICharsetDecoder& operator=(const USASCIICharsetDecoder&);
 };
 
-#if APR_HAS_XLATE
           /**
-           *    Charset decoder that uses an embedded APRCharsetDecoder consistent
+           *    Charset decoder that uses an embedded CharsetDecoder consistent
            *     with current locale settings.
            */
-          class APRLocaleCharsetDecoder : public CharsetDecoder {
+          class LocaleCharsetDecoder : public CharsetDecoder {
           public:
-               APRLocaleCharsetDecoder() : pool(), mutex(pool), decoder(), encoding() {
+               LocaleCharsetDecoder() : pool(), mutex(pool), decoder(), encoding() {
                }
-               virtual ~APRLocaleCharsetDecoder() {
+               virtual ~LocaleCharsetDecoder() {
                }
                virtual log4cxx_status_t decode(ByteBuffer& in,
                   LogString& out) {
@@ -385,12 +385,15 @@
                            const char* enc = apr_os_locale_encoding((apr_pool_t*) subpool.getAPRPool());
                            {
                                 synchronized sync(mutex);
-                                if (encoding != enc) {
+                                if (enc == 0 && decoder == 0) {
+                                    encoding = "C";
+                                    decoder = new USASCIICharsetDecoder();
+                                } else if (encoding != enc) {
                                     encoding = enc;
                                     try {
-                                        decoder = new APRCharsetDecoder(enc);
-                                    } catch(IllegalArgumentException ex) {
-                                        decoder = new USASCIICharsetDecoder();
+                                       decoder = getDecoder(encoding);
+                                    } catch (IllegalArgumentException& ex) {
+                                       decoder = new USASCIICharsetDecoder();
                                     }
                                 }
                             }
@@ -409,7 +412,7 @@
                       out.append(1, *p);
                   }                  
 #endif                               
-                  in.position(in.limit());   
+                  in.position(in.limit()); 
                   return APR_SUCCESS;  
                }
           private:
@@ -418,8 +421,6 @@
                CharsetDecoderPtr decoder;
                std::string encoding;
           };
-#endif
-
 
 #if LOG4CXX_LOGCHAR_IS_UTF8 && LOG4CXX_HAS_WCHAR_T && (defined(_WIN32) || defined(__STDC_ISO_10646__))
           /**
@@ -504,10 +505,8 @@
      return new USASCIICharsetDecoder();
 #elif LOG4CXX_LOGCHAR_IS_WCHAR
     return new MbstowcsCharsetDecoder();
-#elif APR_HAS_XLATE
-    return new APRLocaleCharsetDecoder();
 #else
-#error No default charset decoder available
+    return new LocaleCharsetDecoder();
 #endif
 }
 
@@ -541,6 +540,30 @@
     return new ISOLatinCharsetDecoder();
 }
 
+
+CharsetDecoderPtr CharsetDecoder::getDecoder(const std::string& charset) {
+    if (StringHelper::equalsIgnoreCase(charset, "UTF-8", "utf-8") ||
+        StringHelper::equalsIgnoreCase(charset, "UTF8", "utf8")) {
+        return new UTF8CharsetDecoder();
+    } else if (StringHelper::equalsIgnoreCase(charset, "C", "c") ||
+        charset == "646" ||
+        StringHelper::equalsIgnoreCase(charset, "US-ASCII", "us-ascii") ||
+        StringHelper::equalsIgnoreCase(charset, "ISO646-US", "iso646-US") ||
+        StringHelper::equalsIgnoreCase(charset, "ANSI_X3.4-1968", "ansi_x3.4-1968")) {
+        return new USASCIICharsetDecoder();
+    } else if (StringHelper::equalsIgnoreCase(charset, "ISO-8859-1", "iso-8859-1") ||
+        StringHelper::equalsIgnoreCase(charset, "ISO-LATIN-1", "iso-latin-1")) {
+        return new ISOLatinCharsetDecoder();
+    }
+#if APR_HAS_XLATE || !defined(_WIN32)
+    return new APRCharsetDecoder(charset.c_str());
+#else    
+    throw IllegalArgumentException(charset);
+#endif
+}
+
+
+
 #if LOG4CXX_HAS_WCHAR_T
 CharsetDecoder* CharsetDecoder::createWideDecoder() {
 #if LOG4CXX_LOGCHAR_IS_WCHAR
@@ -565,5 +588,16 @@
   }
   return decoder;
 }
+
+CharsetDecoderPtr CharsetDecoder::getDecoder(const std::wstring& charset) {
+   std::string cs(charset.size(), ' ');
+   for(std::wstring::size_type i = 0;
+      i < charset.length();
+     i++) {
+      cs[i] = (char) charset[i];
+   }
+   return getDecoder(cs);
+}
+
 #endif
 

Modified: logging/log4cxx/trunk/src/main/cpp/charsetencoder.cpp
URL: http://svn.apache.org/viewvc/logging/log4cxx/trunk/src/main/cpp/charsetencoder.cpp?rev=594758&r1=594757&r2=594758&view=diff
==============================================================================
--- logging/log4cxx/trunk/src/main/cpp/charsetencoder.cpp (original)
+++ logging/log4cxx/trunk/src/main/cpp/charsetencoder.cpp Tue Nov 13 20:30:01 2007
@@ -463,16 +463,15 @@
           };
 #endif
 
-#if APR_HAS_XLATE
           /**
-           *    Charset encoder that uses an embedded APRCharsetEncoder consistent
+           *    Charset encoder that uses an embedded CharsetEncoder consistent
            *     with current locale settings.
            */
-          class APRLocaleCharsetEncoder : public CharsetEncoder {
+          class LocaleCharsetEncoder : public CharsetEncoder {
           public:
-               APRLocaleCharsetEncoder() : pool(), mutex(pool), encoder(), encoding() {
+               LocaleCharsetEncoder() : pool(), mutex(pool), encoder(), encoding() {
                }
-               virtual ~APRLocaleCharsetEncoder() {
+               virtual ~LocaleCharsetEncoder() {
                }
               virtual log4cxx_status_t encode(const LogString& in,
                     LogString::const_iterator& iter,
@@ -497,7 +496,7 @@
                                 if (encoding != enc) {
                                     encoding = enc;
                                     try {
-                                        encoder = new APRCharsetEncoder(enc);
+                                        encoder = CharsetEncoder::getEncoder(encoding);
                                     } catch(IllegalArgumentException ex) {
                                         encoder = new USASCIICharsetEncoder();
                                     }
@@ -522,7 +521,6 @@
                CharsetEncoderPtr encoder;
                std::string encoding;
           };
-#endif
 
 
         } // namespace helpers
@@ -559,10 +557,8 @@
    return new USASCIICharsetEncoder();
 #elif LOG4CXX_LOGCHAR_IS_WCHAR
   return new WcstombsCharsetEncoder();
-#elif APR_HAS_XLATE
-  return new APRLocaleCharsetEncoder();
 #else
-#error No default encoder available
+  return new LocaleCharsetEncoder();
 #endif
 }
 
@@ -608,25 +604,27 @@
 
 
 CharsetEncoderPtr CharsetEncoder::getEncoder(const std::string& charset) {
-    if (StringHelper::equalsIgnoreCase(charset, "US-ASCII", "us-ascii") ||
+    if (StringHelper::equalsIgnoreCase(charset, "UTF-8", "utf-8")) {
+        return new UTF8CharsetEncoder();
+    } else if (StringHelper::equalsIgnoreCase(charset, "C", "c") ||
+        charset == "646" ||
+        StringHelper::equalsIgnoreCase(charset, "US-ASCII", "us-ascii") ||
         StringHelper::equalsIgnoreCase(charset, "ISO646-US", "iso646-US") ||
         StringHelper::equalsIgnoreCase(charset, "ANSI_X3.4-1968", "ansi_x3.4-1968")) {
         return new USASCIICharsetEncoder();
     } else if (StringHelper::equalsIgnoreCase(charset, "ISO-8859-1", "iso-8859-1") ||
         StringHelper::equalsIgnoreCase(charset, "ISO-LATIN-1", "iso-latin-1")) {
         return new ISOLatinCharsetEncoder();
-    } else if (StringHelper::equalsIgnoreCase(charset, "UTF-8", "utf-8")) {
-        return new UTF8CharsetEncoder();
     } else if (StringHelper::equalsIgnoreCase(charset, "UTF-16BE", "utf-16be")
         || StringHelper::equalsIgnoreCase(charset, "UTF-16", "utf-16")) {
         return new UTF16BECharsetEncoder();
     } else if (StringHelper::equalsIgnoreCase(charset, "UTF-16LE", "utf-16le")) {
         return new UTF16LECharsetEncoder();
     }
-#if defined(_WIN32)
-    throw IllegalArgumentException(charset);
-#else
+#if APR_HAS_XLATE || !defined(_WIN32)
     return new APRCharsetEncoder(charset.c_str());
+#else    
+    throw IllegalArgumentException(charset);
 #endif
 }
 

Modified: logging/log4cxx/trunk/src/main/include/log4cxx/helpers/charsetdecoder.h
URL: http://svn.apache.org/viewvc/logging/log4cxx/trunk/src/main/include/log4cxx/helpers/charsetdecoder.h?rev=594758&r1=594757&r2=594758&view=diff
==============================================================================
--- logging/log4cxx/trunk/src/main/include/log4cxx/helpers/charsetdecoder.h (original)
+++ logging/log4cxx/trunk/src/main/include/log4cxx/helpers/charsetdecoder.h Tue Nov 13 20:30:01 2007
@@ -54,12 +54,30 @@
                *   Get decoder for default charset.
                */
                   static CharsetDecoderPtr getDefaultDecoder();
+              /**
+               *  Get decoder for specified character set.
+               *  @param charset, the following values should be recognized:
+               *     "US-ASCII", "ISO-8859-1", "UTF-8",
+               *     "UTF-16BE", "UTF-16LE".
+               *  @return decoder
+               *  @throws IllegalArgumentException if charset is not recognized.
+               */
+              static CharsetDecoderPtr getDecoder(const std::string& charset);
 
 #if LOG4CXX_HAS_WCHAR_T
               /**
                *   Get decoder for a byte array containing wchar_t values.
                    */
                   static CharsetDecoderPtr getWideDecoder();
+              /**
+               *  Get decoder for specified character set.
+               *  @param charset, the following values should be recognized:
+               *     "US-ASCII", "ISO-8859-1", "UTF-8",
+               *     "UTF-16BE", "UTF-16LE".
+               *  @return decoder
+               *  @throws IllegalArgumentException if charset is not recognized.
+               */
+              static CharsetDecoderPtr getDecoder(const std::wstring& charset);
 #endif
               /**
                *   Get decoder for UTF-8.
@@ -69,6 +87,8 @@
                *   Get decoder for ISO-8859-1.
                */
                   static CharsetDecoderPtr getISOLatinDecoder();
+                  
+                  
 
               /**
                *  Decodes as many bytes as possible from the given

Modified: logging/log4cxx/trunk/src/main/include/log4cxx/helpers/charsetencoder.h
URL: http://svn.apache.org/viewvc/logging/log4cxx/trunk/src/main/include/log4cxx/helpers/charsetencoder.h?rev=594758&r1=594757&r2=594758&view=diff
==============================================================================
--- logging/log4cxx/trunk/src/main/include/log4cxx/helpers/charsetencoder.h (original)
+++ logging/log4cxx/trunk/src/main/include/log4cxx/helpers/charsetencoder.h Tue Nov 13 20:30:01 2007
@@ -63,7 +63,8 @@
                *  @param charset, the following values should be recognized:
                *     "US-ASCII", "ISO-8859-1", "UTF-8",
                *     "UTF-16BE", "UTF-16LE".
-               *  @return encoder, may be null if charset was not recognized.
+               *  @return encoder.
+               *  @throws IllegalArgumentException if encoding is not recognized.
                */
                   static CharsetEncoderPtr getEncoder(const std::string& charset);
 
@@ -78,7 +79,8 @@
                *  @param charset, the following values should be recognized:
                *     "US-ASCII", "ISO-8859-1", "UTF-8",
                *     "UTF-16BE", "UTF-16LE".
-               *  @return encoder, may be null if charset was not recognized.
+               *  @return encoder.
+               *  @throws IllegalArgumentException if encoding is not recognized.
                */
               static CharsetEncoderPtr getEncoder(const std::wstring& charset);
 #endif