diff options
author | John Ankarström <john@ankarstrom.se> | 2022-08-03 21:57:27 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-08-03 21:57:27 +0200 |
commit | 04daecdd21e86e7556a9462380096530228cd6ca (patch) | |
tree | ed2a8134532607687c165c4a13d6f6c020c42d18 /c/wcharptr.cpp | |
parent | 32bb4696396e48521614d00e5b8f6e6586822f31 (diff) | |
download | EpisodeBrowser-04daecdd21e86e7556a9462380096530228cd6ca.tar.gz |
Split common.h to util.h, wcharptr.h and win.h.
Diffstat (limited to 'c/wcharptr.cpp')
-rw-r--r-- | c/wcharptr.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/c/wcharptr.cpp b/c/wcharptr.cpp new file mode 100644 index 0000000..e406ed1 --- /dev/null +++ b/c/wcharptr.cpp @@ -0,0 +1,64 @@ +#include <utility> +#include <windows.h> + +#include "wcharptr.h" +#include "win.h" + +wchar_ptr::wchar_ptr() noexcept {} + +wchar_ptr::wchar_ptr(wchar_t* const s) noexcept : m_p(s) {} + +wchar_ptr& wchar_ptr::operator=(wchar_t* const s) noexcept +{ + if (m_p != s) { + delete m_p; + m_p = s; + } + return *this; +} + +wchar_ptr::wchar_ptr(wchar_ptr&& other) noexcept + : m_p(std::exchange(other.m_p, nullptr)) {} + +wchar_ptr& wchar_ptr::operator=(wchar_ptr&& other) noexcept +{ + std::swap(m_p, other.m_p); + return *this; +} + +wchar_ptr::operator wchar_t*() noexcept +{ + return m_p; +} + +wchar_t* wchar_ptr::release() noexcept +{ + wchar_t* p2 = m_p; + m_p = nullptr; + return p2; +} + +wchar_ptr::~wchar_ptr() noexcept +{ + delete m_p; +} + +wchar_ptr wchar_ptr::from_narrow(const char* const src, const int cp) +{ + int cbMultiByte = strlen(src)+1; + int cchWideChar = MultiByteToWideChar(cp, 0, src, cbMultiByte, NULL, 0); + wchar_t* dst = new wchar_t[cchWideChar]; + if (!MultiByteToWideChar(cp, 0, src, cbMultiByte, dst, cchWideChar)) { + delete dst; + throw Win32Error(); + } + return dst; +} + +wchar_ptr wchar_ptr::copy(const wchar_t* const src) +{ + const int cb = wcslen(src)+1; + wchar_t* dst = new wchar_t[cb]; + memcpy(dst, src, cb*sizeof(wchar_t)); + return dst; +} |