]> git.lyx.org Git - lyx.git/blobdiff - src/support/unicode.C
* src/support/unicode.C
[lyx.git] / src / support / unicode.C
index fcb47ca56d8c3b45d5deb621ac58438e72018385..0ddeadffbc9c3e8aa02f3422c0ad1333c8158d9a 100644 (file)
 
 using std::endl;
 
+namespace {
+
+#ifdef WORDS_BIGENDIAN
+       char const * utf16_codeset = "UTF16-BE";
+#else
+       char const * utf16_codeset = "UTF16-LE";
+#endif
+
+}
+
+
 namespace lyx {
 
 #ifdef WORDS_BIGENDIAN
        char const * ucs4_codeset = "UCS-4BE";
-       char const * ucs2_codeset = "UCS-2BE";
 #else
        char const * ucs4_codeset = "UCS-4LE";
-       char const * ucs2_codeset = "UCS-2LE";
 #endif
 
 static const iconv_t invalid_cd = (iconv_t)(-1);
@@ -194,6 +203,10 @@ iconv_convert(IconvProcessor & processor,
        char * outbuf = out;
 
        int bytes = processor.convert(inbuf, inbytesleft, outbuf, outsize);
+       if (bytes <= 0)
+               // Conversion failed
+               // FIXME Maybe throw an exception and handle that in the caller?
+               return std::vector<RetType>();
 
        RetType const * tmp = reinterpret_cast<RetType const *>(out);
        return std::vector<RetType>(tmp, tmp + bytes / sizeof(RetType));
@@ -219,52 +232,18 @@ utf8_to_ucs4(char const * utf8str, size_t ls)
 }
 
 
-lyx::char_type
-ucs2_to_ucs4(unsigned short c)
-{
-       return ucs2_to_ucs4(&c, 1)[0];
-}
-
-
-std::vector<lyx::char_type>
-ucs2_to_ucs4(std::vector<unsigned short> const & ucs2str)
+std::vector<char_type>
+utf16_to_ucs4(unsigned short const * s, size_t ls)
 {
-       if (ucs2str.empty())
-               return std::vector<lyx::char_type>();
-
-       return ucs2_to_ucs4(&ucs2str[0], ucs2str.size());
-}
-
-
-std::vector<lyx::char_type>
-ucs2_to_ucs4(unsigned short const * ucs2str, size_t ls)
-{
-       static IconvProcessor processor(ucs4_codeset, ucs2_codeset);
-       return iconv_convert<lyx::char_type>(processor, ucs2str, ls);
-}
-
-
-unsigned short
-ucs4_to_ucs2(lyx::char_type c)
-{
-       return ucs4_to_ucs2(&c, 1)[0];
-}
-
-
-std::vector<unsigned short>
-ucs4_to_ucs2(std::vector<lyx::char_type> const & ucs4str)
-{
-       if (ucs4str.empty())
-               return std::vector<unsigned short>();
-
-       return ucs4_to_ucs2(&ucs4str[0], ucs4str.size());
+       static IconvProcessor processor(ucs4_codeset, utf16_codeset);
+       return iconv_convert<char_type>(processor, s, ls);
 }
 
 
 std::vector<unsigned short>
-ucs4_to_ucs2(lyx::char_type const * s, size_t ls)
+ucs4_to_utf16(char_type const * s, size_t ls)
 {
-       static IconvProcessor processor(ucs2_codeset, ucs4_codeset);
+       static IconvProcessor processor(utf16_codeset, ucs4_codeset);
        return iconv_convert<unsigned short>(processor, s, ls);
 }