diff options
author | Vincent Richard <[email protected]> | 2005-01-01 11:32:23 +0000 |
---|---|---|
committer | Vincent Richard <[email protected]> | 2005-01-01 11:32:23 +0000 |
commit | fc1c6b08d191b3ee9964bf53ea95767bc54377ee (patch) | |
tree | 0e6a61589b60909b065523e5e8e1e0039f2fc93d /src/charset.cpp | |
parent | Fixed config file for Doxygen. (diff) | |
download | vmime-fc1c6b08d191b3ee9964bf53ea95767bc54377ee.tar.gz vmime-fc1c6b08d191b3ee9964bf53ea95767bc54377ee.zip |
Converted all C-style casts to C++-style casts + added unit test for utility::md5.
Diffstat (limited to 'src/charset.cpp')
-rw-r--r-- | src/charset.cpp | 52 |
1 files changed, 36 insertions, 16 deletions
diff --git a/src/charset.cpp b/src/charset.cpp index 4d2e005a..f8c8c50e 100644 --- a/src/charset.cpp +++ b/src/charset.cpp @@ -26,17 +26,26 @@ extern "C" { - #include <iconv.h> - #ifndef VMIME_BUILDING_DOC + #include <iconv.h> + // HACK: prototypes may differ depending on the compiler and/or system (the // second parameter may or may not be 'const'). This redeclaration is a hack // to have a common prototype "iconv_cast". - typedef size_t (*iconv_const_hack)(iconv_t cd, const char* * inbuf, - size_t *inbytesleft, char* * outbuf, size_t *outbytesleft); + class ICONV_HACK + { + public: + + ICONV_HACK(const char** ptr) : m_ptr(ptr) { } - #define iconv_const ((iconv_const_hack) iconv) + operator const char**() { return m_ptr; } + operator char**() { return const_cast <char**>(m_ptr); } + + private: + + const char** m_ptr; + }; #endif // VMIME_BUILDING_DOC } @@ -80,13 +89,23 @@ void charset::generate(utility::outputStream& os, const string::size_type /* max } +struct X +{ + X(const char**); + + operator const char**() { return x; } + operator char**() { return const_cast <char**>(x); } + + const char** x; +}; + void charset::convert(utility::inputStream& in, utility::outputStream& out, const charset& source, const charset& dest) { // Get an iconv descriptor const iconv_t cd = iconv_open(dest.getName().c_str(), source.getName().c_str()); - if (cd != (iconv_t) -1) + if (cd != reinterpret_cast <iconv_t>(-1)) { char inBuffer[5]; char outBuffer[32768]; @@ -97,14 +116,15 @@ void charset::convert(utility::inputStream& in, utility::outputStream& out, while (true) { // Fullfill the buffer - size_t inLength = (size_t) in.read(inBuffer + inPos, sizeof(inBuffer) - inPos) + inPos; + size_t inLength = static_cast <size_t>(in.read(inBuffer + inPos, sizeof(inBuffer) - inPos) + inPos); size_t outLength = sizeof(outBuffer); const char* inPtr = inBuffer; char* outPtr = outBuffer; // Convert input bytes - if (iconv_const(cd, &inPtr, &inLength, &outPtr, &outLength) == (size_t) -1) + if (iconv(cd, ICONV_HACK(&inPtr), &inLength, + &outPtr, &outLength) == static_cast <size_t>(-1)) { // Illegal input sequence or input sequence has no equivalent // sequence in the destination charset. @@ -118,7 +138,7 @@ void charset::convert(utility::inputStream& in, utility::outputStream& out, out.write("?", 1); // Skip a byte and leave unconverted bytes in the input buffer - std::copy((char*) inPtr + 1, inBuffer + sizeof(inBuffer), inBuffer); + std::copy(const_cast <char*>(inPtr + 1), inBuffer + sizeof(inBuffer), inBuffer); inPos = inLength - 1; } else @@ -127,7 +147,7 @@ void charset::convert(utility::inputStream& in, utility::outputStream& out, out.write(outBuffer, sizeof(outBuffer) - outLength); // Leave unconverted bytes in the input buffer - std::copy((char*) inPtr, inBuffer + sizeof(inBuffer), inBuffer); + std::copy(const_cast <char*>(inPtr), inBuffer + sizeof(inBuffer), inBuffer); inPos = inLength; prevIsInvalid = true; @@ -166,13 +186,13 @@ void charset::iconvert(const STRINGF& in, STRINGT& out, const charset& from, con typedef typename STRINGF::value_type ivt; typedef typename STRINGT::value_type ovt; - if (cd != (iconv_t) -1) + if (cd != reinterpret_cast <iconv_t>(-1)) { out.clear(); char buffer[65536]; - const char* inBuffer = (const char*) in.data(); + const char* inBuffer = static_cast <const char*>(in.data()); size_t inBytesLeft = in.length(); for ( ; inBytesLeft > 0 ; ) @@ -180,10 +200,10 @@ void charset::iconvert(const STRINGF& in, STRINGT& out, const charset& from, con size_t outBytesLeft = sizeof(buffer); char* outBuffer = buffer; - if (iconv_const(cd, &inBuffer, &inBytesLeft, - &outBuffer, &outBytesLeft) == (size_t) -1) + if (iconv(cd, ICONV_HACK(&inBuffer), &inBytesLeft, + &outBuffer, &outBytesLeft) == static_cast <size_t>(-1)) { - out += STRINGT((ovt*) buffer, sizeof(buffer) - outBytesLeft); + out += STRINGT(static_cast <ovt*>(buffer), sizeof(buffer) - outBytesLeft); // Ignore this "blocking" character and continue out += '?'; @@ -192,7 +212,7 @@ void charset::iconvert(const STRINGF& in, STRINGT& out, const charset& from, con } else { - out += STRINGT((ovt*) buffer, sizeof(buffer) - outBytesLeft); + out += STRINGT(static_cast <ovt*>(buffer), sizeof(buffer) - outBytesLeft); } } |