From fc1c6b08d191b3ee9964bf53ea95767bc54377ee Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Sat, 1 Jan 2005 11:32:23 +0000 Subject: Converted all C-style casts to C++-style casts + added unit test for utility::md5. --- src/charset.cpp | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) (limited to 'src/charset.cpp') 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 - #ifndef VMIME_BUILDING_DOC + #include + // 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 (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 (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 (-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 (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 (-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 (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 (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 (-1)) { out.clear(); char buffer[65536]; - const char* inBuffer = (const char*) in.data(); + const char* inBuffer = static_cast (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 (-1)) { - out += STRINGT((ovt*) buffer, sizeof(buffer) - outBytesLeft); + out += STRINGT(static_cast (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 (buffer), sizeof(buffer) - outBytesLeft); } } -- cgit v1.2.3