Author: carnold Date: Mon Mar 13 17:51:43 2006 New Revision: 385709 URL: http://svn.apache.org/viewcvs?rev=385709&view=rev Log: Bug LOGCXX-125: force ISO-8859-1 enc, common filespec tweaking
Added: logging/log4cxx/trunk/include/log4cxx/helpers/bytearrayinputstream.h (with props) logging/log4cxx/trunk/src/bytearrayinputstream.cpp (with props) Modified: logging/log4cxx/trunk/include/log4cxx/file.h logging/log4cxx/trunk/include/log4cxx/helpers/bytebuffer.h logging/log4cxx/trunk/include/log4cxx/helpers/charsetdecoder.h logging/log4cxx/trunk/include/log4cxx/helpers/fileinputstream.h logging/log4cxx/trunk/include/log4cxx/helpers/inputstream.h logging/log4cxx/trunk/src/Makefile.am logging/log4cxx/trunk/src/bytebuffer.cpp logging/log4cxx/trunk/src/charsetdecoder.cpp logging/log4cxx/trunk/src/file.cpp logging/log4cxx/trunk/src/fileinputstream.cpp logging/log4cxx/trunk/src/inputstreamreader.cpp logging/log4cxx/trunk/src/logger.cpp logging/log4cxx/trunk/src/properties.cpp logging/log4cxx/trunk/tests/src/filetestcase.cpp Modified: logging/log4cxx/trunk/include/log4cxx/file.h URL: http://svn.apache.org/viewcvs/logging/log4cxx/trunk/include/log4cxx/file.h?rev=385709&r1=385708&r2=385709&view=diff ============================================================================== --- logging/log4cxx/trunk/include/log4cxx/file.h (original) +++ logging/log4cxx/trunk/include/log4cxx/file.h Mon Mar 13 17:51:43 2006 @@ -57,9 +57,6 @@ return osName; } - // @deprecated Use FileOutputStream instead - log4cxx_status_t write(const LogString& src, log4cxx::helpers::Pool& p) const; - log4cxx_status_t open(apr_file_t** file, int flags, int perm, log4cxx::helpers::Pool& p) const; Added: logging/log4cxx/trunk/include/log4cxx/helpers/bytearrayinputstream.h URL: http://svn.apache.org/viewcvs/logging/log4cxx/trunk/include/log4cxx/helpers/bytearrayinputstream.h?rev=385709&view=auto ============================================================================== --- logging/log4cxx/trunk/include/log4cxx/helpers/bytearrayinputstream.h (added) +++ logging/log4cxx/trunk/include/log4cxx/helpers/bytearrayinputstream.h Mon Mar 13 17:51:43 2006 @@ -0,0 +1,84 @@ +/* + * Copyright 2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _LOG4CXX_HELPERS_BYTEARRAYINPUTSTREAM_H +#define _LOG4CXX_HELPERS_BYTEARRAYNPUTSTREAM_H + +#include <vector> +#include <log4cxx/helpers/inputstream.h> + + +namespace log4cxx +{ + + namespace helpers { + + /** + * InputStream implemented on top of a byte array. + * @since 0.9.8 + */ + class LOG4CXX_EXPORT ByteArrayInputStream : public InputStream + { + private: + typedef std::vector<unsigned char> ByteArray; + ByteArray buf; + ByteArray::size_type pos; + + public: + DECLARE_ABSTRACT_LOG4CXX_OBJECT(ByteArrayInputStream) + BEGIN_LOG4CXX_CAST_MAP() + LOG4CXX_CAST_ENTRY(ByteArrayInputStream) + LOG4CXX_CAST_ENTRY_CHAIN(InputStream) + END_LOG4CXX_CAST_MAP() + + /** + * Creates a ByteArrayInputStream. + * + * @param bytes array of bytes to copy into stream. + */ + ByteArrayInputStream(const std::vector<unsigned char>& bytes); + + virtual ~ByteArrayInputStream(); + + /** + * Closes this file input stream and releases any system + * resources associated with the stream. + */ + virtual void close(); + + /** + * Reads a sequence of bytes into the given buffer. + * + * @param dst The buffer into which bytes are to be transferred. + * @return the total number of bytes read into the buffer, or -1 if there + * is no more data because the end of the stream has been reached. + */ + virtual int read(ByteBuffer& buf); + + private: + + ByteArrayInputStream(const ByteArrayInputStream&); + + ByteArrayInputStream& operator=(const ByteArrayInputStream&); + + }; + + typedef helpers::ObjectPtrT<ByteArrayInputStream> ByteArrayInputStreamPtr; + } // namespace helpers + +} //namespace log4cxx + +#endif //_LOG4CXX_HELPERS_FILEINPUTSTREAM_H Propchange: logging/log4cxx/trunk/include/log4cxx/helpers/bytearrayinputstream.h ------------------------------------------------------------------------------ svn:executable = * Modified: logging/log4cxx/trunk/include/log4cxx/helpers/bytebuffer.h URL: http://svn.apache.org/viewcvs/logging/log4cxx/trunk/include/log4cxx/helpers/bytebuffer.h?rev=385709&r1=385708&r2=385709&view=diff ============================================================================== --- logging/log4cxx/trunk/include/log4cxx/helpers/bytebuffer.h (original) +++ logging/log4cxx/trunk/include/log4cxx/helpers/bytebuffer.h Mon Mar 13 17:51:43 2006 @@ -48,6 +48,7 @@ inline char* current() { return base + pos; } inline const char* current() const { return base + pos; } inline size_t limit() const { return lim; } + void limit(size_t newLimit); inline size_t position() const { return pos; } inline size_t remaining() const { return lim - pos; } void position(size_t newPosition); Modified: logging/log4cxx/trunk/include/log4cxx/helpers/charsetdecoder.h URL: http://svn.apache.org/viewcvs/logging/log4cxx/trunk/include/log4cxx/helpers/charsetdecoder.h?rev=385709&r1=385708&r2=385709&view=diff ============================================================================== --- logging/log4cxx/trunk/include/log4cxx/helpers/charsetdecoder.h (original) +++ logging/log4cxx/trunk/include/log4cxx/helpers/charsetdecoder.h Mon Mar 13 17:51:43 2006 @@ -64,6 +64,10 @@ * Get decoder for UTF-8. */ static CharsetDecoderPtr getUTF8Decoder(); + /** + * Get decoder for ISO-8859-1. + */ + static CharsetDecoderPtr getISOLatinDecoder(); /** * Decodes as many bytes as possible from the given Modified: logging/log4cxx/trunk/include/log4cxx/helpers/fileinputstream.h URL: http://svn.apache.org/viewcvs/logging/log4cxx/trunk/include/log4cxx/helpers/fileinputstream.h?rev=385709&r1=385708&r2=385709&view=diff ============================================================================== --- logging/log4cxx/trunk/include/log4cxx/helpers/fileinputstream.h (original) +++ logging/log4cxx/trunk/include/log4cxx/helpers/fileinputstream.h Mon Mar 13 17:51:43 2006 @@ -35,7 +35,7 @@ { private: Pool pool; - void* fileptr; + apr_file_t* fileptr; public: DECLARE_ABSTRACT_LOG4CXX_OBJECT(FileInputStream) @@ -69,16 +69,13 @@ virtual void close(); /** - * Reads up to len bytes of data from this input stream - * into an array of chars. + * Reads a sequence of bytes into the given buffer. * - * @param b The buffer into which the data is read. - * @param off The start offset of the data. - * @param len The maximum number of bytes to read. + * @param dst The buffer into which bytes are to be transferred. * @return the total number of bytes read into the buffer, or -1 if there - * is no more data because the end of the file has been reached. + * is no more data because the end of the stream has been reached. */ - virtual int read(char* buf, int off, int len) const; + virtual int read(ByteBuffer& buf); private: Modified: logging/log4cxx/trunk/include/log4cxx/helpers/inputstream.h URL: http://svn.apache.org/viewcvs/logging/log4cxx/trunk/include/log4cxx/helpers/inputstream.h?rev=385709&r1=385708&r2=385709&view=diff ============================================================================== --- logging/log4cxx/trunk/include/log4cxx/helpers/inputstream.h (original) +++ logging/log4cxx/trunk/include/log4cxx/helpers/inputstream.h Mon Mar 13 17:51:43 2006 @@ -44,16 +44,13 @@ public: /** - * Reads up to len bytes of data from this input stream - * into an array of chars. + * Reads a sequence of bytes into the given buffer. * - * @param b The buffer into which the data is read. - * @param off The start offset of the data. - * @param len The maximum number of bytes to read. + * @param dst The buffer into which bytes are to be transferred. * @return the total number of bytes read into the buffer, or -1 if there * is no more data because the end of the stream has been reached. */ - virtual int read(char* buf, int off, int len) const = 0; + virtual int read(ByteBuffer& dst) = 0; /** * Closes this input stream and releases any system Modified: logging/log4cxx/trunk/src/Makefile.am URL: http://svn.apache.org/viewcvs/logging/log4cxx/trunk/src/Makefile.am?rev=385709&r1=385708&r2=385709&view=diff ============================================================================== --- logging/log4cxx/trunk/src/Makefile.am (original) +++ logging/log4cxx/trunk/src/Makefile.am Mon Mar 13 17:51:43 2006 @@ -11,6 +11,7 @@ asyncappender.cpp \ basicconfigurator.cpp \ bufferedwriter.cpp \ + bytearrayinputstream.cpp \ bytebuffer.cpp \ cacheddateformat.cpp \ charsetdecoder.cpp \ Added: logging/log4cxx/trunk/src/bytearrayinputstream.cpp URL: http://svn.apache.org/viewcvs/logging/log4cxx/trunk/src/bytearrayinputstream.cpp?rev=385709&view=auto ============================================================================== --- logging/log4cxx/trunk/src/bytearrayinputstream.cpp (added) +++ logging/log4cxx/trunk/src/bytearrayinputstream.cpp Mon Mar 13 17:51:43 2006 @@ -0,0 +1,55 @@ +/* + * Copyright 2003,2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <log4cxx/helpers/bytearrayinputstream.h> +#include <log4cxx/helpers/exception.h> +#include <log4cxx/helpers/bytebuffer.h> +#include <apr_file_io.h> +#include <log4cxx/helpers/transcoder.h> +#include <log4cxx/helpers/aprinitializer.h> +#include <algorithm> + +using namespace log4cxx; +using namespace log4cxx::helpers; +using namespace std; + +IMPLEMENT_LOG4CXX_OBJECT(ByteArrayInputStream) + +ByteArrayInputStream::ByteArrayInputStream(const std::vector<unsigned char>& bytes) : + buf(bytes), pos(0) { +} + + + +ByteArrayInputStream::~ByteArrayInputStream() { +} + + +void ByteArrayInputStream::close() { +} + + +int ByteArrayInputStream::read(ByteBuffer& dst) { + if (pos >= buf.size()) { + return -1; + } else { + size_t bytesCopied = min(dst.remaining(), buf.size() - pos); + memcpy(dst.current(), &buf[pos], bytesCopied); + pos += bytesCopied; + dst.position(dst.position() + bytesCopied); + return bytesCopied; + } +} Propchange: logging/log4cxx/trunk/src/bytearrayinputstream.cpp ------------------------------------------------------------------------------ svn:executable = * Modified: logging/log4cxx/trunk/src/bytebuffer.cpp URL: http://svn.apache.org/viewcvs/logging/log4cxx/trunk/src/bytebuffer.cpp?rev=385709&r1=385708&r2=385709&view=diff ============================================================================== --- logging/log4cxx/trunk/src/bytebuffer.cpp (original) +++ logging/log4cxx/trunk/src/bytebuffer.cpp Mon Mar 13 17:51:43 2006 @@ -47,6 +47,14 @@ } } +void ByteBuffer::limit(size_t newLimit) { + if (newLimit < 0 || newLimit > cap) { + throw IllegalArgumentException("newLimit"); + } + lim = newLimit; +} + + bool ByteBuffer::put(char byte) { if (pos < lim) { base[pos++] = byte; Modified: logging/log4cxx/trunk/src/charsetdecoder.cpp URL: http://svn.apache.org/viewcvs/logging/log4cxx/trunk/src/charsetdecoder.cpp?rev=385709&r1=385708&r2=385709&view=diff ============================================================================== --- logging/log4cxx/trunk/src/charsetdecoder.cpp (original) +++ logging/log4cxx/trunk/src/charsetdecoder.cpp Mon Mar 13 17:51:43 2006 @@ -315,7 +315,7 @@ /** -* Converts from ISO-8859-1 to LogString. +* Converts from US-ASCII to LogString. * */ class USASCIICharsetDecoder : public CharsetDecoder @@ -455,6 +455,10 @@ return new UTF8CharsetDecoder(); } return decoder; +} + +CharsetDecoderPtr CharsetDecoder::getISOLatinDecoder() { + return new ISOLatinCharsetDecoder(); } #if LOG4CXX_HAS_WCHAR_T Modified: logging/log4cxx/trunk/src/file.cpp URL: http://svn.apache.org/viewcvs/logging/log4cxx/trunk/src/file.cpp?rev=385709&r1=385708&r2=385709&view=diff ============================================================================== --- logging/log4cxx/trunk/src/file.cpp (original) +++ logging/log4cxx/trunk/src/file.cpp Mon Mar 13 17:51:43 2006 @@ -129,27 +129,6 @@ } -// -// Current implementation is limited to MBCS files -// -// -log4cxx_status_t File::write(const LogString& src, Pool& p) const { - LogString output; - apr_file_t* f = NULL; - apr_status_t rv = open(&f, - APR_WRITE | APR_TRUNCATE | APR_CREATE, APR_OS_DEFAULT, p); - if (rv == APR_SUCCESS) { - std::string encoded; - Transcoder::encode(src, encoded); - size_t len = encoded.length(); - rv = apr_file_write(f, encoded.data(), &len); - apr_status_t close = apr_file_close(f); - assert(close == APR_SUCCESS); - } - return rv; -} - - std::vector<LogString> File::list(Pool& p) const { return std::vector<LogString>(); // Modified: logging/log4cxx/trunk/src/fileinputstream.cpp URL: http://svn.apache.org/viewcvs/logging/log4cxx/trunk/src/fileinputstream.cpp?rev=385709&r1=385708&r2=385709&view=diff ============================================================================== --- logging/log4cxx/trunk/src/fileinputstream.cpp (original) +++ logging/log4cxx/trunk/src/fileinputstream.cpp Mon Mar 13 17:51:43 2006 @@ -29,9 +29,7 @@ FileInputStream::FileInputStream(const LogString& filename) { apr_fileperms_t perm = APR_OS_DEFAULT; apr_int32_t flags = APR_READ; - LOG4CXX_ENCODE_CHAR(fn, filename); - apr_status_t stat = apr_file_open((apr_file_t**) &fileptr, - fn.c_str(), flags, perm, (apr_pool_t*) pool.getAPRPool()); + apr_status_t stat = File(filename).open(&fileptr, flags, perm, pool); if (stat != APR_SUCCESS) { throw IOException(stat); } @@ -41,9 +39,7 @@ FileInputStream::FileInputStream(const File& aFile) { apr_fileperms_t perm = APR_OS_DEFAULT; apr_int32_t flags = APR_READ; - LOG4CXX_ENCODE_CHAR(fn, aFile.getName()); - apr_status_t stat = apr_file_open((apr_file_t**) &fileptr, - fn.c_str(), flags, perm, (apr_pool_t*) pool.getAPRPool()); + apr_status_t stat = aFile.open(&fileptr, flags, perm, pool); if (stat != APR_SUCCESS) { throw IOException(stat); } @@ -52,13 +48,13 @@ FileInputStream::~FileInputStream() { if (fileptr != NULL && !APRInitializer::isDestructed) { - apr_file_close((apr_file_t*) fileptr); + apr_file_close(fileptr); } } void FileInputStream::close() { - apr_status_t stat = apr_file_close((apr_file_t*) fileptr); + apr_status_t stat = apr_file_close(fileptr); if (stat == APR_SUCCESS) { fileptr = NULL; } else { @@ -67,15 +63,16 @@ } -int FileInputStream::read(char* buf, int off, int len) const { - apr_size_t bytesRead = len; - apr_status_t stat = apr_file_read((apr_file_t*) fileptr, buf + off, &bytesRead); +int FileInputStream::read(ByteBuffer& buf) { + apr_size_t bytesRead = buf.remaining(); + apr_status_t stat = apr_file_read(fileptr, buf.current(), &bytesRead); if (APR_STATUS_IS_EOF(stat)) { bytesRead = -1; } else { if (stat != APR_SUCCESS) { throw IOException(stat); } + buf.position(buf.position() + bytesRead); } return bytesRead; Modified: logging/log4cxx/trunk/src/inputstreamreader.cpp URL: http://svn.apache.org/viewcvs/logging/log4cxx/trunk/src/inputstreamreader.cpp?rev=385709&r1=385708&r2=385709&view=diff ============================================================================== --- logging/log4cxx/trunk/src/inputstreamreader.cpp (original) +++ logging/log4cxx/trunk/src/inputstreamreader.cpp Mon Mar 13 17:51:43 2006 @@ -50,40 +50,23 @@ LogString InputStreamReader::read(Pool& p) { const size_t BUFSIZE = 4096; - char* buf = p.palloc(BUFSIZE); - char* contents = buf; - int contentLength = 0; - int bytesRead = 0; + ByteBuffer buf(p.palloc(BUFSIZE), BUFSIZE); + LogString output; // read whole file - do { - bytesRead = in->read(buf, 0, BUFSIZE); - if (bytesRead > 0) { - contentLength += bytesRead; - } - if (bytesRead < BUFSIZE) { - bytesRead = -1; - } - - if (bytesRead != -1) { - // - // file was larger than the buffer - // realloc a bigger buffer - char* newContents = p.palloc(contentLength + BUFSIZE); - buf = newContents + contentLength; - memcpy(newContents, contents, contentLength); - // - // we would free contents here if you did that sort of thing - // - contents = newContents; - } - } while(bytesRead != -1); + while(in->read(buf) >= 0) { + buf.flip(); + log4cxx_status_t stat = dec->decode(buf, output); + if (stat != 0) { + throw IOException(stat); + } + if (buf.remaining() > 0) { + memmove(buf.data(), buf.current(), buf.remaining()); + buf.limit(buf.remaining()); + } else { + buf.clear(); + } + } - // - // finished file - // transcode and exit - LogString output; - ByteBuffer input(contents, contentLength); - dec->decode(input, output); return output; } Modified: logging/log4cxx/trunk/src/logger.cpp URL: http://svn.apache.org/viewcvs/logging/log4cxx/trunk/src/logger.cpp?rev=385709&r1=385708&r2=385709&view=diff ============================================================================== --- logging/log4cxx/trunk/src/logger.cpp (original) +++ logging/log4cxx/trunk/src/logger.cpp Mon Mar 13 17:51:43 2006 @@ -373,8 +373,7 @@ void Logger::l7dlog(const LevelPtr& level, const std::string& key, const LocationInfo& location) { - LogString lkey; - Transcoder::decode(key.c_str(), strlen(key.c_str()), lkey); + LOG4CXX_DECODE_CHAR(lkey, key); std::vector<LogString> values(0); l7dlog(level, lkey, location, values); @@ -382,10 +381,8 @@ void Logger::l7dlog(const LevelPtr& level, const std::string& key, const LocationInfo& location, const std::string& val1) { - LogString lval1; - Transcoder::decode(val1.c_str(), strlen(val1.c_str()), lval1); - LogString lkey; - Transcoder::decode(key.c_str(), strlen(key.c_str()), lkey); + LOG4CXX_DECODE_CHAR(lkey, key); + LOG4CXX_DECODE_CHAR(lval1, val1); std::vector<LogString> values(1); values[0] = lval1; @@ -395,12 +392,9 @@ void Logger::l7dlog(const LevelPtr& level, const std::string& key, const LocationInfo& location, const std::string& val1, const std::string& val2) { - LogString lval1; - LogString lval2; - Transcoder::decode(val1.c_str(), strlen(val1.c_str()), lval1); - Transcoder::decode(val2.c_str(), strlen(val2.c_str()), lval2); - LogString lkey; - Transcoder::decode(key.c_str(), strlen(key.c_str()), lkey); + LOG4CXX_DECODE_CHAR(lkey, key); + LOG4CXX_DECODE_CHAR(lval1, val1); + LOG4CXX_DECODE_CHAR(lval2, val2); std::vector<LogString> values(2); values[0] = lval1; @@ -411,14 +405,10 @@ void Logger::l7dlog(const LevelPtr& level, const std::string& key, const LocationInfo& location, const std::string& val1, const std::string& val2, const std::string& val3) { - LogString lval1; - LogString lval2; - LogString lval3; - Transcoder::decode(val1.c_str(), strlen(val1.c_str()), lval1); - Transcoder::decode(val2.c_str(), strlen(val2.c_str()), lval2); - Transcoder::decode(val3.c_str(), strlen(val3.c_str()), lval3); - LogString lkey; - Transcoder::decode(key.c_str(), strlen(key.c_str()), lkey); + LOG4CXX_DECODE_CHAR(lkey, key); + LOG4CXX_DECODE_CHAR(lval1, val1); + LOG4CXX_DECODE_CHAR(lval2, val2); + LOG4CXX_DECODE_CHAR(lval3, val3); std::vector<LogString> values(3); values[0] = lval1; Modified: logging/log4cxx/trunk/src/properties.cpp URL: http://svn.apache.org/viewcvs/logging/log4cxx/trunk/src/properties.cpp?rev=385709&r1=385708&r2=385709&view=diff ============================================================================== --- logging/log4cxx/trunk/src/properties.cpp (original) +++ logging/log4cxx/trunk/src/properties.cpp Mon Mar 13 17:51:43 2006 @@ -328,7 +328,8 @@ void Properties::load(InputStreamPtr inStream) { Pool pool; - InputStreamReaderPtr lineReader = new InputStreamReader(inStream); + InputStreamReaderPtr lineReader( + new InputStreamReader(inStream, CharsetDecoder::getISOLatinDecoder())); LogString contents = lineReader->read(pool); properties.clear(); PropertyParser parser; Modified: logging/log4cxx/trunk/tests/src/filetestcase.cpp URL: http://svn.apache.org/viewcvs/logging/log4cxx/trunk/tests/src/filetestcase.cpp?rev=385709&r1=385708&r2=385709&view=diff ============================================================================== --- logging/log4cxx/trunk/tests/src/filetestcase.cpp (original) +++ logging/log4cxx/trunk/tests/src/filetestcase.cpp Mon Mar 13 17:51:43 2006 @@ -77,14 +77,6 @@ } } - void defaultWrite() { - File defFile; - Pool pool; - LogString greeting(LOG4CXX_STR("Hello, World")); - apr_status_t stat = defFile.write(greeting, pool); - CPPUNIT_ASSERT(stat != APR_SUCCESS); - } - #if LOG4CXX_HAS_WCHAR_T void wcharConstructor() {