aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVincent Richard <[email protected]>2005-10-19 11:25:57 +0000
committerVincent Richard <[email protected]>2005-10-19 11:25:57 +0000
commit08e1812b23106a2e27192dd56e95ba192fc58133 (patch)
treed644aff3d21c7493fcaf0ad0299394dcad77c773
parentFixed compilation problems on FreeBSD (thanks to Xin LI). (diff)
downloadvmime-08e1812b23106a2e27192dd56e95ba192fc58133.tar.gz
vmime-08e1812b23106a2e27192dd56e95ba192fc58133.zip
Charset converter.
-rw-r--r--ChangeLog5
-rw-r--r--SConstruct1
-rw-r--r--examples/example7.cpp7
-rw-r--r--src/charset.cpp145
4 files changed, 16 insertions, 142 deletions
diff --git a/ChangeLog b/ChangeLog
index 2da95d53..f9a4240d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,11 @@
VERSION 0.7.2cvs
================
+2005-10-19 Vincent Richard <[email protected]>
+
+ * charsetConverter.{hpp|cpp}: new object 'charsetConverter' for converting
+ between charsets (code moved from static functions in 'charset' class).
+
2005-10-16 Vincent Richard <[email protected]>
* SConstruct: fixed compilation problems on FreeBSD (thanks to Xin LI).
diff --git a/SConstruct b/SConstruct
index 95af6e2c..8dcb53da 100644
--- a/SConstruct
+++ b/SConstruct
@@ -82,6 +82,7 @@ libvmime_sources = [
'bodyPart.cpp', 'bodyPart.hpp',
'bodyPartAttachment.cpp', 'bodyPartAttachment.hpp',
'charset.cpp', 'charset.hpp',
+ 'charsetConverter.cpp', 'charsetConverter.hpp',
'component.cpp', 'component.hpp',
'constants.cpp', 'constants.hpp',
'contentDisposition.cpp', 'contentDisposition.hpp',
diff --git a/examples/example7.cpp b/examples/example7.cpp
index f8e35d64..921f0566 100644
--- a/examples/example7.cpp
+++ b/examples/example7.cpp
@@ -49,11 +49,12 @@ int main()
for (int i = 0 ; i < ef->getEncoderCount() ; ++i)
{
- const vmime::encoderFactory::registeredEncoder& enc = *ef->getEncoderAt(i);
+ vmime::ref <const vmime::encoderFactory::registeredEncoder>
+ enc = ef->getEncoderAt(i);
- std::cout << " * " << enc.getName() << std::endl;
+ std::cout << " * " << enc->getName() << std::endl;
- vmime::ref <vmime::encoder> e = enc.create();
+ vmime::ref <vmime::encoder> e = enc->create();
std::vector <vmime::string> props = e->getAvailableProperties();
diff --git a/src/charset.cpp b/src/charset.cpp
index fcf33542..b8ce9e70 100644
--- a/src/charset.cpp
+++ b/src/charset.cpp
@@ -27,6 +27,8 @@
#include "vmime/utility/stringUtils.hpp"
+#include "vmime/charsetConverter.hpp"
+
extern "C"
{
@@ -103,150 +105,15 @@ void charset::generate(utility::outputStream& os, const string::size_type /* max
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 != reinterpret_cast <iconv_t>(-1))
- {
- char inBuffer[32768];
- char outBuffer[32768];
- size_t inPos = 0;
-
- bool prevIsInvalid = false;
-
- while (true)
- {
- // Fullfill the buffer
- 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(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.
- if (prevIsInvalid)
- {
- // Write successfully converted bytes
- out.write(outBuffer, sizeof(outBuffer) - outLength);
-
- // Output a special character to indicate we don't known how to
- // convert the sequence at this position
- out.write("?", 1);
-
- // Skip a byte and leave unconverted bytes in the input buffer
- std::copy(const_cast <char*>(inPtr + 1), inBuffer + sizeof(inBuffer), inBuffer);
- inPos = inLength - 1;
- }
- else
- {
- // Write successfully converted bytes
- out.write(outBuffer, sizeof(outBuffer) - outLength);
-
- // Leave unconverted bytes in the input buffer
- std::copy(const_cast <char*>(inPtr), inBuffer + sizeof(inBuffer), inBuffer);
- inPos = inLength;
-
- if (errno != E2BIG)
- prevIsInvalid = true;
- }
- }
- else
- {
- // Write successfully converted bytes
- out.write(outBuffer, sizeof(outBuffer) - outLength);
-
- inPos = 0;
- prevIsInvalid = false;
- }
-
- // Check for end of data
- if (in.eof() && inPos == 0)
- break;
- }
-
- // Close iconv handle
- iconv_close(cd);
- }
- else
- {
- throw exceptions::charset_conv_error();
- }
-}
-
-
-template <class STRINGF, class STRINGT>
-void charset::iconvert(const STRINGF& in, STRINGT& out, const charset& from, const charset& to)
-{
- // Get an iconv descriptor
- const iconv_t cd = iconv_open(to.getName().c_str(), from.getName().c_str());
-
- typedef typename STRINGF::value_type ivt;
- typedef typename STRINGT::value_type ovt;
-
- if (cd != reinterpret_cast <iconv_t>(-1))
- {
- out.clear();
-
- char buffer[65536];
-
- const char* inBuffer = static_cast <const char*>(in.data());
- size_t inBytesLeft = in.length();
-
- for ( ; inBytesLeft > 0 ; )
- {
- size_t outBytesLeft = sizeof(buffer);
- char* outBuffer = buffer;
-
- if (iconv(cd, ICONV_HACK(&inBuffer), &inBytesLeft,
- &outBuffer, &outBytesLeft) == static_cast <size_t>(-1))
- {
- out += STRINGT(static_cast <ovt*>(buffer), sizeof(buffer) - outBytesLeft);
-
- // Ignore this "blocking" character and continue
- out += '?';
- ++inBuffer;
- --inBytesLeft;
- }
- else
- {
- out += STRINGT(static_cast <ovt*>(buffer), sizeof(buffer) - outBytesLeft);
- }
- }
-
- // Close iconv handle
- iconv_close(cd);
- }
- else
- {
- throw exceptions::charset_conv_error();
- }
-}
-
-
-#if VMIME_WIDE_CHAR_SUPPORT
-
-void charset::decode(const string& in, wstring& out, const charset& ch)
-{
- iconvert(in, out, ch, charset("WCHAR_T"));
+ charsetConverter conv(source, dest);
+ conv.convert(in, out);
}
-void charset::encode(const wstring& in, string& out, const charset& ch)
-{
- iconvert(in, out, charset("WCHAR_T"), ch);
-}
-
-#endif
-
-
void charset::convert(const string& in, string& out, const charset& source, const charset& dest)
{
- iconvert(in, out, source, dest);
+ charsetConverter conv(source, dest);
+ conv.convert(in, out);
}