Commit 3380dbb4 authored by captainwong's avatar captainwong

update

parent 3751fb7a
.vscode/
# Compiled Object files
*.slo
*.lo
......
#pragma once
#include <memory>
#include <functional>
namespace jlib {
//template <typename T>
//struct deleter : public std::default_delete<T>
//{};
template <typename T>
class auto_free
{
public:
typedef std::function<void(T*)> deleter;
explicit auto_free(T* data, deleter dter = {})
: data_(data), deleter_(dter)
{}
~auto_free() {
if (data_ && deleter_) {
deleter_(data_);
}
}
protected:
T* data_ = nullptr;
deleter deleter_ = nullptr;
};
}
#pragma once
#include <vector>
#include <string>
namespace jlib {
template <class V, class T>
bool is_contain(const V& v, const T& t) {
for (auto i : v) { if (i == t) { return true; } }
return false;
}
template <class V>
std::vector<std::wstring> get_other(const V& v, const std::wstring& t) {
std::vector<std::wstring> ret = {};
for (auto i : v) { if (i != t) { ret.push_back(t); } }
return ret;
}
template <class V>
std::vector<std::string> get_other(const V& v, const std::string& t) {
std::vector<std::string> ret = {};
for (auto i : v) { if (i != t) { ret.push_back(t); } }
return ret;
}
}
......@@ -4,27 +4,51 @@
namespace jlib{
inline bool toClipboard(HWND hwnd, const std::string &s)
inline bool to_clipboard(const std::wstring &s)
{
if (!OpenClipboard(hwnd)) return false;
if (!EmptyClipboard()) return false;
HGLOBAL hg = GlobalAlloc(GMEM_DDESHARE, s.size() + 1);
if (!hg) {
CloseClipboard();
return false;
}
LPVOID hMem = GlobalLock(hg);
if (!hMem) {
CloseClipboard();
return false;
}
memcpy(hMem, s.c_str(), s.size() + 1);
GlobalUnlock(hg);
SetClipboardData(CF_TEXT, hg);
CloseClipboard();
GlobalFree(hg);
return true;
if (!OpenClipboard(nullptr)) return false;
if (!EmptyClipboard()) return false;
size_t sz = (s.length() + 1) * 2;
HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, sz);
if (!hg) {
CloseClipboard();
return false;
}
LPVOID hMem = GlobalLock(hg);
if (!hMem) {
CloseClipboard();
return false;
}
memset(hMem, 0, sz);
memcpy(hMem, s.data(), s.size() * 2);
GlobalUnlock(hg);
SetClipboardData(CF_UNICODETEXT, hg);
CloseClipboard();
GlobalFree(hg);
return true;
}
inline std::wstring from_clipboard()
{
std::wstring text;
do {
if (!OpenClipboard(nullptr))
break;
HANDLE hData = GetClipboardData(CF_UNICODETEXT);
if (hData == nullptr)
break;
auto pszText = static_cast<const wchar_t*>(GlobalLock(hData));
if (pszText == nullptr)
break;
text = pszText;
GlobalUnlock(hData);
CloseClipboard();
} while (false);
return text;
}
}
\ No newline at end of file
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