1212#include < Windows.h>
1313#else
1414#include < iconv.h>
15+ #include < errno.h>
1516#endif
1617
1718enum Encodings
@@ -71,7 +72,7 @@ class EncodingHelper
7172
7273 return std::string (utf8Str.begin (), utf8Str.end ());
7374#else
74- char * inputCodePage;
75+ std::string inputCodePage;
7576
7677 switch (inputEncoding)
7778 {
@@ -85,7 +86,7 @@ class EncodingHelper
8586 case W1251: inputCodePage = " WINDOWS-1251" ; break ;
8687 }
8788
88- iconv_t handle = iconv_open (inputCodePage, " UTF8" );
89+ iconv_t handle = iconv_open (" UTF8" , inputCodePage. c_str () );
8990
9091 if (handle == (iconv_t )-1 )
9192 {
@@ -112,15 +113,26 @@ class EncodingHelper
112113
113114 if (iconv (handle, &inBuf, &inBytesLeft, &outBuf, &outBytesLeft) == (size_t )-1 )
114115 {
116+ std::string err;
117+
118+ if (errno == E2BIG)
119+ err = " There is not sufficient room at *outbuf." ;
120+ else if (errno == EILSEQ)
121+ err = " An invalid multibyte sequence has been encountered in the input." ;
122+ else if (errno == EINVAL)
123+ err = " An incomplete multibyte sequence has been encountered in the input." ;
124+ else
125+ err = " Generic Error" ;
126+
127+ logprintf (" \n\n ChatBot UTF8 conversion iconv failed: %s\n " , err.c_str ());
115128 delete[] output;
116129 iconv_close (handle);
117- logprintf (" \n\n ChatBot UTF8 conversion error: iconv failed\n " );
118130 return " " ;
119131 }
120132
121133 iconv_close (handle);
122134
123- std::string result (outBuf , strlen (outBuf ));
135+ std::string result (output , strlen (output ));
124136 delete[] output;
125137
126138 return result;
@@ -171,7 +183,7 @@ class EncodingHelper
171183
172184 return std::string (encStr.begin (), encStr.end ());
173185#else
174- char * outputCodePage;
186+ std::string outputCodePage;
175187
176188 switch (outputEncoding)
177189 {
@@ -185,7 +197,7 @@ class EncodingHelper
185197 case W1251: outputCodePage = " WINDOWS-1251" ; break ;
186198 }
187199
188- iconv_t handle = iconv_open (" UTF8" , outputCodePage );
200+ iconv_t handle = iconv_open (outputCodePage. c_str (), " UTF8" );
189201
190202 if (handle == (iconv_t )-1 )
191203 {
@@ -212,15 +224,27 @@ class EncodingHelper
212224
213225 if (iconv (handle, &inBuf, &inBytesLeft, &outBuf, &outBytesLeft) == (size_t )-1 )
214226 {
227+ std::string err;
228+
229+ if (errno == E2BIG)
230+ err = " There is not sufficient room at *outbuf." ;
231+ else if (errno == EILSEQ)
232+ err = " An invalid multibyte sequence has been encountered in the input." ;
233+ else if (errno == EINVAL)
234+ err = " An incomplete multibyte sequence has been encountered in the input." ;
235+ else
236+ err = " Generic Error" ;
237+
238+ logprintf (" \n\n ChatBot WideByte conversion iconv failed: %s\n " , err.c_str ());
239+
215240 delete[] output;
216241 iconv_close (handle);
217- logprintf (" \n\n ChatBot WideByte conversion error: iconv failed\n " );
218242 return " " ;
219243 }
220244
221245 iconv_close (handle);
222246
223- std::string result (outBuf , strlen (outBuf ));
247+ std::string result (output , strlen (output ));
224248 delete[] output;
225249
226250 return result;
0 commit comments