]> git.lyx.org Git - lyx.git/blobdiff - src/support/unicode.cpp
Remove ugly multiple definition hack
[lyx.git] / src / support / unicode.cpp
index 74c7b6fdc34be98765189a58a59d78667789f28a..95415a5538e45ad11a4ed4695c5b277ae79024c3 100644 (file)
 
 #include <config.h>
 
-#include "unicode.h"
-
-#include "debug.h"
+#include "support/unicode.h"
+#include "support/debug.h"
+#include "support/mutex.h"
 
 #include <iconv.h>
 
 #include <boost/cstdint.hpp>
 
 #include <cerrno>
-#include <iomanip>
 #include <map>
+#include <ostream>
+//Needed in MSVC
+#include <string>
+
 
-using std::endl;
-using std::map;
-using std::make_pair;
-using std::string;
-using std::vector;
+using namespace std;
 
 namespace {
 
@@ -52,47 +51,49 @@ namespace lyx {
 static const iconv_t invalid_cd = (iconv_t)(-1);
 
 
-struct IconvProcessor::Private {
-       Private(): cd(invalid_cd) {}
-       ~Private()
+struct IconvProcessor::Impl
+{
+       Impl(string const & to, string const & from)
+               : cd(invalid_cd), tocode_(to), fromcode_(from)
+       {}
+
+       ~Impl()
        {
-               if (cd != invalid_cd) {
-                       if (iconv_close(cd) == -1) {
-                               lyxerr << "Error returned from iconv_close("
-                                      << errno << ")" << endl;
-                       }
-               }
+               if (cd != invalid_cd && iconv_close(cd) == -1)
+                               LYXERR0("Error returned from iconv_close(" << errno << ")");
        }
+
        iconv_t cd;
+       string tocode_;
+       string fromcode_;
+
+       Mutex mutex_; // iconv() is not thread save, see #7240
 };
 
 
-IconvProcessor::IconvProcessor(char const * tocode,
-               char const * fromcode): tocode_(tocode), fromcode_(fromcode),
-               pimpl_(new IconvProcessor::Private)
+IconvProcessor::IconvProcessor(char const * tocode, char const * fromcode)
+       : pimpl_(new IconvProcessor::Impl(tocode, fromcode))
 {
 }
 
 
 IconvProcessor::IconvProcessor(IconvProcessor const & other)
-       : tocode_(other.tocode_), fromcode_(other.fromcode_),
-         pimpl_(new IconvProcessor::Private)
+       : pimpl_(new IconvProcessor::Impl(other.pimpl_->tocode_, other.pimpl_->fromcode_))
 {
 }
 
 
-IconvProcessor & IconvProcessor::operator=(IconvProcessor const & other)
+IconvProcessor::~IconvProcessor()
 {
-       if (&other == this)
-               return *this;
-       tocode_ = other.tocode_;
-       fromcode_ = other.fromcode_;
-       pimpl_.reset(new Private);
-       return *this;
+       delete pimpl_;
 }
 
 
-IconvProcessor::~IconvProcessor() {}
+void IconvProcessor::operator=(IconvProcessor const & other)
+{
+       if (&other != this)
+               pimpl_ = new Impl(other.pimpl_->tocode_, other.pimpl_->fromcode_);
+}
 
 
 bool IconvProcessor::init()
@@ -100,15 +101,15 @@ bool IconvProcessor::init()
        if (pimpl_->cd != invalid_cd)
                return true;
 
-       pimpl_->cd = iconv_open(tocode_.c_str(), fromcode_.c_str());
+       pimpl_->cd = iconv_open(pimpl_->tocode_.c_str(), pimpl_->fromcode_.c_str());
        if (pimpl_->cd != invalid_cd)
                return true;
 
        lyxerr << "Error returned from iconv_open" << endl;
        switch (errno) {
                case EINVAL:
-                       lyxerr << "EINVAL The conversion from " << fromcode_
-                               << " to " << tocode_
+                       lyxerr << "EINVAL The conversion from " << pimpl_->fromcode_
+                               << " to " << pimpl_->tocode_
                                << " is not supported by the implementation."
                                << endl;
                        break;
@@ -123,6 +124,8 @@ bool IconvProcessor::init()
 int IconvProcessor::convert(char const * buf, size_t buflen,
                char * outbuf, size_t maxoutsize)
 {
+       Mutex::Locker lock(&pimpl_->mutex_);
+
        if (buflen == 0)
                return 0;
 
@@ -137,7 +140,12 @@ int IconvProcessor::convert(char const * buf, size_t buflen,
 
        int res = iconv(pimpl_->cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
 
-       //lyxerr << std::dec;
+       // flush out remaining data. This is needed because iconv sometimes
+       // holds back chars in the stream, waiting for a combination character
+       // (see e.g. http://sources.redhat.com/bugzilla/show_bug.cgi?id=1124)
+       iconv(pimpl_->cd, NULL, NULL, &outbuf, &outbytesleft);
+
+       //lyxerr << dec;
        //lyxerr << "Inbytesleft: " << inbytesleft << endl;
        //lyxerr << "Outbytesleft: " << outbytesleft << endl;
 
@@ -154,32 +162,32 @@ int IconvProcessor::convert(char const * buf, size_t buflen,
                case EILSEQ:
                        lyxerr << "EILSEQ An invalid multibyte sequence"
                                << " has been encountered in the input.\n"
-                               << "When converting from " << fromcode_
-                               << " to " << tocode_ << ".\n";
-                       lyxerr << "Input:" << std::hex;
+                               << "When converting from " << pimpl_->fromcode_
+                               << " to " << pimpl_->tocode_ << ".\n";
+                       lyxerr << "Input:" << hex;
                        for (size_t i = 0; i < buflen; ++i) {
                                // char may be signed, avoid output of
                                // something like 0xffffffc2
                                boost::uint32_t const b =
                                        *reinterpret_cast<unsigned char const *>(buf + i);
-                               lyxerr << " 0x" << b;
+                               lyxerr << " 0x" << (unsigned int)b;
                        }
-                       lyxerr << std::dec << endl;
+                       lyxerr << dec << endl;
                        break;
                case EINVAL:
                        lyxerr << "EINVAL An incomplete multibyte sequence"
                                << " has been encountered in the input.\n"
-                               << "When converting from " << fromcode_
-                               << " to " << tocode_ << ".\n";
-                       lyxerr << "Input:" << std::hex;
+                               << "When converting from " << pimpl_->fromcode_
+                               << " to " << pimpl_->tocode_ << ".\n";
+                       lyxerr << "Input:" << hex;
                        for (size_t i = 0; i < buflen; ++i) {
                                // char may be signed, avoid output of
                                // something like 0xffffffc2
                                boost::uint32_t const b =
                                        *reinterpret_cast<unsigned char const *>(buf + i);
-                               lyxerr << " 0x" << b;
+                               lyxerr << " 0x" << (unsigned int)b;
                        }
-                       lyxerr << std::dec << endl;
+                       lyxerr << dec << endl;
                        break;
                default:
                        lyxerr << "\tSome other error: " << errno << endl;
@@ -195,14 +203,24 @@ int IconvProcessor::convert(char const * buf, size_t buflen,
 }
 
 
+std::string IconvProcessor::from() const
+{
+       return pimpl_->fromcode_;
+}
+
+
+std::string IconvProcessor::to() const
+{
+       return pimpl_->tocode_;
+}
+
+
 namespace {
 
 
 template<typename RetType, typename InType>
 vector<RetType>
-iconv_convert(IconvProcessor & processor,
-             InType const * buf,
-             size_t buflen)
+iconv_convert(IconvProcessor & processor, InType const * buf, size_t buflen)
 {
        if (buflen == 0)
                return vector<RetType>();
@@ -210,17 +228,21 @@ iconv_convert(IconvProcessor & processor,
        char const * inbuf = reinterpret_cast<char const *>(buf);
        size_t inbytesleft = buflen * sizeof(InType);
 
-       size_t const outsize = 32768;
-       static char out[outsize];
-       char * outbuf = out;
+       static std::vector<char> outbuf(32768);
+       // The number of UCS4 code points in buf is at most inbytesleft.
+       // The output encoding will use at most
+       // max_encoded_bytes(pimpl_->tocode_) per UCS4 code point.
+       size_t maxoutbufsize = max_encoded_bytes(processor.to()) * inbytesleft;
+       if (outbuf.size() < maxoutbufsize)
+               outbuf.resize(maxoutbufsize);
 
-       int bytes = processor.convert(inbuf, inbytesleft, outbuf, outsize);
+       int bytes = processor.convert(inbuf, inbytesleft, &outbuf[0], outbuf.size());
        if (bytes <= 0)
                // Conversion failed
                // FIXME Maybe throw an exception and handle that in the caller?
                return vector<RetType>();
 
-       RetType const * tmp = reinterpret_cast<RetType const *>(out);
+       RetType const * tmp = reinterpret_cast<RetType const *>(&outbuf[0]);
        return vector<RetType>(tmp, tmp + bytes / sizeof(RetType));
 }
 
@@ -345,4 +367,34 @@ void ucs4_to_multibytes(char_type ucs4, vector<char> & out,
                out.clear();
 }
 
+int max_encoded_bytes(std::string const & encoding)
+{
+       // FIXME: this information should be transferred to lib/encodings
+       // UTF8 uses at most 4 bytes to represent one UCS4 code point
+       // (see RFC 3629). RFC 2279 specifies 6 bytes, but that
+       // information is outdated, and RFC 2279 has been superseded by
+       // RFC 3629.
+       // The CJK encodings use (different) multibyte representation as well.
+       // All other encodings encode one UCS4 code point in one byte
+       // (and can therefore only encode a subset of UCS4)
+       // Furthermore, all encodings that use shifting (like SJIS) do not work with
+       // iconv_codecvt_facet.
+       if (encoding == "UTF-8" ||
+           encoding == "GB" ||
+           encoding == "EUC-TW")
+               return 4;
+       else if (encoding == "EUC-JP")
+               return 3;
+       else if (encoding == "ISO-2022-JP")
+               return 8;
+       else if (encoding == "BIG5" ||
+                encoding == "EUC-KR" ||
+                encoding == "EUC-CN" ||
+                encoding == "SJIS" ||
+                encoding == "GBK")
+               return 2;
+       else
+               return 1;
+}
+
 } // namespace lyx