mirror of
https://github.com/AppleWin/AppleWin.git
synced 2025-08-15 18:27:29 +00:00
Save-state ANSI support: small refactor for new/delete char buffers (#838)
This commit is contained in:
@@ -456,49 +456,43 @@ void YamlSaveHelper::SaveString(const char* key, const char* value)
|
|||||||
if (value[0] == 0)
|
if (value[0] == 0)
|
||||||
value = "\"\"";
|
value = "\"\"";
|
||||||
|
|
||||||
LPWSTR pWcStr = NULL;
|
|
||||||
LPSTR pMbStr = NULL;
|
|
||||||
|
|
||||||
// libyaml supports UTF-8 and not accented ANSI characters (GH#838)
|
// libyaml supports UTF-8 and not accented ANSI characters (GH#838)
|
||||||
// . So convert ANSI to UTF-8, which is a 2-step process:
|
// . So convert ANSI to UTF-8, which is a 2-step process:
|
||||||
|
|
||||||
// 1) ANSI -> unicode
|
// 1) ANSI -> unicode
|
||||||
{
|
{
|
||||||
int wcStrSize = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, value, -1, NULL, 0);
|
int size = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, value, -1, NULL, 0);
|
||||||
if (wcStrSize == 0)
|
if (size == 0)
|
||||||
{
|
|
||||||
delete[] pWcStr; delete[] pMbStr;
|
|
||||||
throw std::string("Unable to convert to unicode: ") + std::string(value);
|
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, m_pWcStr, m_wcStrSize);
|
||||||
int res = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, value, -1, pWcStr, wcStrSize);
|
|
||||||
if (!res)
|
if (!res)
|
||||||
{
|
|
||||||
delete[] pWcStr; delete[] pMbStr;
|
|
||||||
throw std::string("Unable to convert to unicode: ") + std::string(value);
|
throw std::string("Unable to convert to unicode: ") + std::string(value);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 2) unicode -> UTF-8
|
// 2) unicode -> UTF-8
|
||||||
{
|
{
|
||||||
// NB. WC_ERR_INVALID_CHARS only defined when WIN_VER >= 0x600 - but stdafx.h defines it as 0x500
|
// 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);
|
int size = WideCharToMultiByte(CP_UTF8, 0/*WC_ERR_INVALID_CHARS*/, m_pWcStr, -1, NULL, 0, NULL, NULL);
|
||||||
if (mbStrSize == 0)
|
if (size == 0)
|
||||||
{
|
|
||||||
delete[] pWcStr; delete[] pMbStr;
|
|
||||||
throw std::string("Unable to convert to UTF-8: ") + std::string(value);
|
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*/, m_pWcStr, -1, m_pMbStr, m_mbStrSize, NULL, NULL);
|
||||||
int res = WideCharToMultiByte(CP_UTF8, 0/*WC_ERR_INVALID_CHARS*/, pWcStr, -1, pMbStr, mbStrSize, NULL, NULL);
|
|
||||||
if (!res)
|
if (!res)
|
||||||
{
|
|
||||||
delete [] pWcStr; delete [] pMbStr;
|
|
||||||
throw std::string("Unable to convert to UTF-8: ") + std::string(value);
|
throw std::string("Unable to convert to UTF-8: ") + std::string(value);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Save("%s: %s\n", key, pMbStr);
|
Save("%s: %s\n", key, m_pMbStr);
|
||||||
delete[] pWcStr; delete[] pMbStr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void YamlSaveHelper::SaveString(const char* key, const std::string & value)
|
void YamlSaveHelper::SaveString(const char* key, const std::string & value)
|
||||||
|
@@ -172,7 +172,11 @@ class YamlSaveHelper
|
|||||||
public:
|
public:
|
||||||
YamlSaveHelper(std::string pathname) :
|
YamlSaveHelper(std::string pathname) :
|
||||||
m_hFile(NULL),
|
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");
|
m_hFile = fopen(pathname.c_str(), "wt");
|
||||||
|
|
||||||
@@ -203,6 +207,9 @@ public:
|
|||||||
fprintf(m_hFile, "...\n");
|
fprintf(m_hFile, "...\n");
|
||||||
fclose(m_hFile);
|
fclose(m_hFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
delete[] m_pWcStr;
|
||||||
|
delete[] m_pMbStr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Save(const char* format, ...);
|
void Save(const char* format, ...);
|
||||||
@@ -271,4 +278,9 @@ private:
|
|||||||
int m_indent;
|
int m_indent;
|
||||||
static const UINT kMaxIndent = 50*2;
|
static const UINT kMaxIndent = 50*2;
|
||||||
char m_szIndent[kMaxIndent];
|
char m_szIndent[kMaxIndent];
|
||||||
|
|
||||||
|
LPWSTR m_pWcStr;
|
||||||
|
int m_wcStrSize;
|
||||||
|
LPSTR m_pMbStr;
|
||||||
|
int m_mbStrSize;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user