]> git.lyx.org Git - lyx.git/blobdiff - src/support/unicode.cpp
Remove unused macros USE_INCLUDED_STRING and STD_STRING_IS_GOOD
[lyx.git] / src / support / unicode.cpp
index d34f7c8e9080f5914d21d6097831517519a1b013..8e003437e3be72ae4b71aa61eab03e3904640af8 100644 (file)
@@ -3,7 +3,7 @@
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
- * \author Lars Gullik Bjønnes
+ * \author Lars Gullik Bjønnes
  *
  * Full author contact details are available in file CREDITS.
  *
 #include <iomanip>
 #include <ostream>
 #include <map>
+#include <string>
 
-using std::endl;
-using std::map;
-using std::make_pair;
-using std::string;
-using std::vector;
+using namespace std;
 
 namespace {
 
@@ -52,47 +49,47 @@ 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_;
 };
 
 
 IconvProcessor::IconvProcessor(char const * tocode, char const * fromcode)
-       : tocode_(tocode), fromcode_(fromcode),
-               pimpl_(new IconvProcessor::Private)
+       : 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 +97,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;
@@ -137,7 +134,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,9 +156,9 @@ 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
@@ -164,14 +166,14 @@ int IconvProcessor::convert(char const * buf, size_t buflen,
                                        *reinterpret_cast<unsigned char const *>(buf + i);
                                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
@@ -179,7 +181,7 @@ int IconvProcessor::convert(char const * buf, size_t buflen,
                                        *reinterpret_cast<unsigned char const *>(buf + i);
                                lyxerr << " 0x" << (unsigned int)b;
                        }
-                       lyxerr << std::dec << endl;
+                       lyxerr << dec << endl;
                        break;
                default:
                        lyxerr << "\tSome other error: " << errno << endl;
@@ -200,9 +202,7 @@ 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>();