Skip to content

Commit cd8babc

Browse files
committed
Encoding System on Linux with iconv
1 parent 773c833 commit cd8babc

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ bld/
2727
# Uncomment if you have tasks that create the project's static files in wwwroot
2828
#wwwroot/
2929

30+
.vscode/
31+
3032
# MSTest test Results
3133
[Tt]est[Rr]esult*/
3234
[Bb]uild[Ll]og.*

src/ChatBotHelper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ std::string ChatBotHelper::GetBotAnswer(const ChatBotParams& params, nlohmann::j
182182
if (!error.empty())
183183
return response.dump(4).c_str();
184184
}
185-
catch (std::exception)
185+
catch (...)
186186
{
187187
//errore non trovato
188188
}
@@ -201,7 +201,7 @@ std::string ChatBotHelper::GetBotAnswer(const ChatBotParams& params, nlohmann::j
201201

202202
return answer;
203203
}
204-
catch (std::exception exc)
204+
catch (std::exception &exc)
205205
{
206206
logprintf("ChatBot Plugin Exception GetBotAnswer(): %s\n", exc.what());
207207
logprintf("Chatbot Plugin Exception Response:\n%s", response.dump(4).c_str());

src/EncodingHelper.hpp

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <Windows.h>
1313
#else
1414
#include <iconv.h>
15+
#include <errno.h>
1516
#endif
1617

1718
enum 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\nChatBot UTF8 conversion iconv failed: %s\n", err.c_str());
115128
delete[] output;
116129
iconv_close(handle);
117-
logprintf("\n\nChatBot 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\nChatBot WideByte conversion iconv failed: %s\n", err.c_str());
239+
215240
delete[] output;
216241
iconv_close(handle);
217-
logprintf("\n\nChatBot 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

Comments
 (0)