Commit 0f73f228 authored by mercury233's avatar mercury233 Committed by GitHub

update CopyString & CopyWideString (#2884)

parent 68d1ff85
...@@ -82,14 +82,18 @@ public: ...@@ -82,14 +82,18 @@ public:
return CopyWStr(src, dst, N); return CopyWStr(src, dst, N);
} }
template<size_t N> template<size_t N>
static void CopyString(const char* src, char(&dst)[N]) { static void CopyString(const char* src, char(&dst)[N], size_t len = N - 1) {
std::strncpy(dst, src, N - 1); if(len >= N)
dst[N - 1] = 0; len = N - 1;
std::strncpy(dst, src, len);
dst[len] = 0;
} }
template<size_t N> template<size_t N>
static void CopyWideString(const wchar_t* src, wchar_t(&dst)[N]) { static void CopyWideString(const wchar_t* src, wchar_t(&dst)[N], size_t len = N - 1) {
std::wcsncpy(dst, src, N - 1); if(len >= N)
dst[N - 1] = 0; len = N - 1;
std::wcsncpy(dst, src, len);
dst[len] = 0;
} }
static bool IsHighSurrogate(unsigned int c) { static bool IsHighSurrogate(unsigned int c) {
return (c >= 0xd800U && c <= 0xdbffU); return (c >= 0xd800U && c <= 0xdbffU);
......
...@@ -1234,11 +1234,8 @@ void Game::RefreshDeck(const wchar_t* deckpath, const std::function<void(const w ...@@ -1234,11 +1234,8 @@ void Game::RefreshDeck(const wchar_t* deckpath, const std::function<void(const w
} }
FileSystem::TraversalDir(deckpath, [additem](const wchar_t* name, bool isdir) { FileSystem::TraversalDir(deckpath, [additem](const wchar_t* name, bool isdir) {
if (!isdir && IsExtension(name, L".ydk")) { if (!isdir && IsExtension(name, L".ydk")) {
size_t len = std::wcslen(name);
wchar_t deckname[256]{}; wchar_t deckname[256]{};
size_t count = std::min(len - 4, sizeof deckname / sizeof deckname[0] - 1); BufferIO::CopyWideString(name, deckname, std::wcslen(name) - 4);
std::wcsncpy(deckname, name, count);
deckname[count] = 0;
additem(deckname); additem(deckname);
} }
}); });
......
...@@ -428,8 +428,7 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) { ...@@ -428,8 +428,7 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) {
wchar_t *dot = std::wcsrchr(open_file_name, L'.'); wchar_t *dot = std::wcsrchr(open_file_name, L'.');
if(dash && dot && !mywcsncasecmp(dot, L".ydk", 4)) { // full path if(dash && dot && !mywcsncasecmp(dot, L".ydk", 4)) { // full path
wchar_t deck_name[256]; wchar_t deck_name[256];
std::wcsncpy(deck_name, dash + 1, dot - dash - 1); BufferIO::CopyWideString(dash + 1, deck_name, dot - dash - 1);
deck_name[dot - dash - 1] = L'\0';
mainGame->ebDeckname->setText(deck_name); mainGame->ebDeckname->setText(deck_name);
mainGame->cbDBCategory->setSelected(-1); mainGame->cbDBCategory->setSelected(-1);
mainGame->cbDBDecks->setSelected(-1); mainGame->cbDBDecks->setSelected(-1);
...@@ -438,7 +437,7 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) { ...@@ -438,7 +437,7 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) {
mainGame->cbDBDecks->setEnabled(false); mainGame->cbDBDecks->setEnabled(false);
} else if(dash) { // has category } else if(dash) { // has category
wchar_t deck_name[256]; wchar_t deck_name[256];
std::wcsncpy(deck_name, dash + 1, 256); BufferIO::CopyWideString(dash + 1, deck_name);
for(size_t i = 0; i < mainGame->cbDBDecks->getItemCount(); ++i) { for(size_t i = 0; i < mainGame->cbDBDecks->getItemCount(); ++i) {
if(!std::wcscmp(mainGame->cbDBDecks->getItem(i), deck_name)) { if(!std::wcscmp(mainGame->cbDBDecks->getItem(i), deck_name)) {
BufferIO::CopyWideString(deck_name, mainGame->gameConf.lastdeck); BufferIO::CopyWideString(deck_name, mainGame->gameConf.lastdeck);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment