commit 731ef31b053faaf69fe63c327f778d85dd4bfc1b
parent d5ba3d78e265548c23843ba5cafde2bda748db78
Author: cfillion <cfillion@users.noreply.github.com>
Date: Tue, 18 Feb 2020 10:20:43 -0500
correctly display download error messages containing non-English characters on Windows [#26]
Fixes #26
Diffstat:
3 files changed, 15 insertions(+), 5 deletions(-)
diff --git a/src/download.cpp b/src/download.cpp
@@ -21,6 +21,7 @@
#include "filesystem.hpp"
#include "hash.hpp"
#include "reapack.hpp"
+#include "win32.hpp"
#include <cassert>
@@ -152,16 +153,21 @@ bool Download::run()
headers = curl_slist_append(headers, "Cache-Control: no-cache");
curl_easy_setopt(ctx, CURLOPT_HTTPHEADER, headers);
- char errbuf[CURL_ERROR_SIZE] = "No error message";
- curl_easy_setopt(ctx, CURLOPT_ERRORBUFFER, errbuf);
+ std::string errbuf = "No error message";
+ errbuf.resize(CURL_ERROR_SIZE - 1, '\0');
+ curl_easy_setopt(ctx, CURLOPT_ERRORBUFFER, errbuf.data());
const CURLcode res = curl_easy_perform(ctx);
curl_slist_free_all(headers);
closeStream();
if(res != CURLE_OK) {
+#ifdef _WIN32
+ errbuf = Win32::ansi2utf8(errbuf);
+#endif
+
const std::string &err = String::format(
- "%s (%d): %s", curl_easy_strerror(res), res, errbuf);
+ "%s (%d): %s", curl_easy_strerror(res), res, errbuf.c_str());
setError({err, m_url});
return false;
}
diff --git a/src/reapack.cpp b/src/reapack.cpp
@@ -69,7 +69,7 @@ Path ReaPack::resourcePath()
#ifdef _WIN32
// convert from the current system codepage to UTF-8
if(atof(GetAppVersion()) < 5.70)
- return Win32::narrow(Win32::widen(GetResourcePath(), CP_ACP));
+ return Win32::ansi2utf8(GetResourcePath());
#endif
return {GetResourcePath()};
diff --git a/src/win32.hpp b/src/win32.hpp
@@ -35,10 +35,14 @@ namespace Win32 {
typedef wchar_t char_type;
std::wstring widen(const char *, UINT codepage = CP_UTF8);
- inline std::wstring widen(const std::string &str) { return widen(str.c_str(), CP_UTF8); }
+ inline std::wstring widen(const std::string &str, UINT codepage = CP_UTF8)
+ { return widen(str.c_str(), codepage); }
std::string narrow(const wchar_t *);
inline std::string narrow(const std::wstring &str) { return narrow(str.c_str()); }
+
+ inline std::string ansi2utf8(const char *str) { return narrow(widen(str, CP_ACP)); }
+ inline std::string ansi2utf8(const std::string &str) { return ansi2utf8(str.c_str()); }
#else
typedef char char_type;