Save-state ANSI support: small refactor for new/delete char buffers (#838)

This commit is contained in:
tomcw 2020-09-25 21:25:46 +01:00
parent 09a5ac4761
commit b9d80a0317
2 changed files with 30 additions and 24 deletions

View File

@ -456,49 +456,43 @@ void YamlSaveHelper::SaveString(const char* key, const char* value)
if (value[0] == 0)
value = "\"\"";
LPWSTR pWcStr = NULL;
LPSTR pMbStr = NULL;
// libyaml supports UTF-8 and not accented ANSI characters (GH#838)
// . So convert ANSI to UTF-8, which is a 2-step process:
// 1) ANSI -> unicode
{
int wcStrSize = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, value, -1, NULL, 0);
if (wcStrSize == 0)
{
delete[] pWcStr; delete[] pMbStr;
int size = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, value, -1, NULL, 0);
if (size == 0)
throw std::string("Unable to convert to unicode: ") + std::string(value);
if (size > m_wcStrSize)
{
delete[] m_pWcStr;
m_pWcStr = new WCHAR[size];
m_wcStrSize = size;
}
pWcStr = new WCHAR[wcStrSize];
int res = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, value, -1, pWcStr, wcStrSize);
int res = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, value, -1, m_pWcStr, m_wcStrSize);
if (!res)
{
delete[] pWcStr; delete[] pMbStr;
throw std::string("Unable to convert to unicode: ") + std::string(value);
}
}
// 2) unicode -> UTF-8
{
// NB. WC_ERR_INVALID_CHARS only defined when WIN_VER >= 0x600 - but stdafx.h defines it as 0x500
int mbStrSize = WideCharToMultiByte(CP_UTF8, 0/*WC_ERR_INVALID_CHARS*/, pWcStr, -1, NULL, 0, NULL, NULL);
if (mbStrSize == 0)
{
delete[] pWcStr; delete[] pMbStr;
int size = WideCharToMultiByte(CP_UTF8, 0/*WC_ERR_INVALID_CHARS*/, m_pWcStr, -1, NULL, 0, NULL, NULL);
if (size == 0)
throw std::string("Unable to convert to UTF-8: ") + std::string(value);
if (size > m_mbStrSize)
{
delete[] m_pMbStr;
m_pMbStr = new char[size];
m_mbStrSize = size;
}
pMbStr = new char[mbStrSize];
int res = WideCharToMultiByte(CP_UTF8, 0/*WC_ERR_INVALID_CHARS*/, pWcStr, -1, pMbStr, mbStrSize, NULL, NULL);
int res = WideCharToMultiByte(CP_UTF8, 0/*WC_ERR_INVALID_CHARS*/, m_pWcStr, -1, m_pMbStr, m_mbStrSize, NULL, NULL);
if (!res)
{
delete [] pWcStr; delete [] pMbStr;
throw std::string("Unable to convert to UTF-8: ") + std::string(value);
}
}
Save("%s: %s\n", key, pMbStr);
delete[] pWcStr; delete[] pMbStr;
Save("%s: %s\n", key, m_pMbStr);
}
void YamlSaveHelper::SaveString(const char* key, const std::string & value)

View File

@ -172,7 +172,11 @@ class YamlSaveHelper
public:
YamlSaveHelper(std::string pathname) :
m_hFile(NULL),
m_indent(0)
m_indent(0),
m_pWcStr(NULL),
m_wcStrSize(0),
m_pMbStr(NULL),
m_mbStrSize(0)
{
m_hFile = fopen(pathname.c_str(), "wt");
@ -203,6 +207,9 @@ public:
fprintf(m_hFile, "...\n");
fclose(m_hFile);
}
delete[] m_pWcStr;
delete[] m_pMbStr;
}
void Save(const char* format, ...);
@ -271,4 +278,9 @@ private:
int m_indent;
static const UINT kMaxIndent = 50*2;
char m_szIndent[kMaxIndent];
LPWSTR m_pWcStr;
int m_wcStrSize;
LPSTR m_pMbStr;
int m_mbStrSize;
};