From 01c6c4879ebc2c5d6faedd05043f03f98a9053a0 Mon Sep 17 00:00:00 2001 From: Enrico Forestieri Date: Thu, 20 Nov 2008 18:48:56 +0000 Subject: [PATCH] After a better understanding of the problem, restrict the fix for bugs 5216, 5280, and 5489 to the ISO-2022-JP encoding. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@27642 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/support/docstream.cpp | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/support/docstream.cpp b/src/support/docstream.cpp index 53d403b584..2fddca0320 100644 --- a/src/support/docstream.cpp +++ b/src/support/docstream.cpp @@ -86,32 +86,45 @@ protected: { #define WORKAROUND_ICONV_BUG 1 #if WORKAROUND_ICONV_BUG - // Due to a bug in some iconv versions, when the last char in - // the buffer is a wide char, it gets truncated (see bugs 5216, - // 5280, and also 5489). As a workaround, we append a null - // char and then remove it from output after the conversion. + // Due to a bug in some iconv versions, when the last char + // in the buffer is a wide char and the output encoding is + // ISO-2022-JP and we are going to switch to another encoding, + // the appropriate escape sequence for changing the character + // set is not output (see bugs 5216, 5280, and also 5489). + // As a workaround, we append a nul char in order to force + // a switch to ASCII, and then remove it from output after + // the conversion. intern_type * from_new = 0; - if (*(from_end - 1) >= 0x80) { + intern_type const * from_old = from; + size_t extra = 0; + if (*(from_end - 1) >= 0x80 && encoding_ == "ISO-2022-JP") { size_t len = from_end - from; from_new = new intern_type[len + 1]; memcpy(from_new, from, len * sizeof(intern_type)); from_new[len] = 0; from_end = from_new + len + 1; from = from_new; + extra = 1; } #endif size_t inbytesleft = (from_end - from) * sizeof(intern_type); size_t outbytesleft = (to_end - to) * sizeof(extern_type); +#if WORKAROUND_ICONV_BUG + outbytesleft += extra * sizeof(extern_type); +#endif from_next = from; to_next = to; result const retval = do_iconv(out_cd_, reinterpret_cast(&from_next), &inbytesleft, &to_next, &outbytesleft); #if WORKAROUND_ICONV_BUG - // Remove from output the null char that we inserted at the end + // Remove from output the nul char that we inserted at the end // of the input buffer in order to circumvent an iconv bug. - if (from_new) + if (from_new) { --to_next; + from_next = from_old + (from_next - from); + delete[] from_new; + } #endif if (retval == base::error) { fprintf(stderr, @@ -141,9 +154,6 @@ protected: fputc('\n', stderr); fflush(stderr); } -#if WORKAROUND_ICONV_BUG - delete[] from_new; -#endif return retval; } virtual result do_unshift(state_type &, extern_type * to, -- 2.39.2