Mercurial > minori
comparison dep/animia/src/util/win32.cc @ 152:8700806c2cc2
dep/animia: awesome new breaking changes!
I'm so tired
| author | Paper <mrpapersonic@gmail.com> | 
|---|---|
| date | Wed, 15 Nov 2023 02:34:59 -0500 | 
| parents | |
| children | cdf79282d647 | 
   comparison
  equal
  deleted
  inserted
  replaced
| 151:54744a48a7d7 | 152:8700806c2cc2 | 
|---|---|
| 1 #include "animia/util/win32.h" | |
| 2 | |
| 3 #include <windows.h> | |
| 4 #include <shlobj.h> /* SHGetKnownFolderPath */ | |
| 5 #include <subauth.h> /* UNICODE_STRING */ | |
| 6 | |
| 7 namespace animia::internal::win32 { | |
| 8 | |
| 9 std::string ToUtf8String(const std::wstring& string) { | |
| 10 const auto wctomb = [&string](LPSTR out, int size) -> int { | |
| 11 return ::WideCharToMultiByte(CP_UTF8, 0, string.c_str(), string.length(), out, size, nullptr, nullptr); | |
| 12 }; | |
| 13 | |
| 14 if (string.empty()) | |
| 15 return std::string(); | |
| 16 | |
| 17 | |
| 18 long size = ::WideCharToMultiByte(CP_UTF8, 0, string.c_str(), string.length(), nullptr, 0, nullptr, nullptr); | |
| 19 std::string ret = std::string(size, '\0'); | |
| 20 ::WideCharToMultiByte(CP_UTF8, 0, string.c_str(), string.length(), &ret.front(), ret.length(), nullptr, nullptr); | |
| 21 return ret; | |
| 22 } | |
| 23 | |
| 24 std::string ToUtf8String(const UNICODE_STRING& string) { | |
| 25 const auto wctomb = [&string](LPSTR out, int size) -> int { | |
| 26 return ::WideCharToMultiByte(CP_UTF8, 0, string.Buffer, string.Length, out, size, nullptr, nullptr); | |
| 27 }; | |
| 28 | |
| 29 if (string.Length <= 0) | |
| 30 return std::string(); | |
| 31 | |
| 32 long size = wctomb(nullptr, 0); | |
| 33 std::string ret = std::string(size, '\0'); | |
| 34 wctomb(&ret.front(), ret.length()); | |
| 35 return ret; | |
| 36 } | |
| 37 | |
| 38 std::wstring ToWstring(const std::string& string) { | |
| 39 const auto mbtowc = [&string](LPWSTR out, int size) -> int { | |
| 40 return ::MultiByteToWideChar(CP_UTF8, 0, string.c_str(), string.length(), out, size); | |
| 41 }; | |
| 42 | |
| 43 if (string.empty()) | |
| 44 return std::wstring(); | |
| 45 | |
| 46 long size = mbtowc(nullptr, 0); | |
| 47 std::wstring ret = std::wstring(size, L'\0'); | |
| 48 mbtowc(&ret.front(), ret.length()); | |
| 49 return ret; | |
| 50 } | |
| 51 | |
| 52 std::wstring GetFileNameFromPath(const std::wstring& path) { | |
| 53 const auto pos = path.find_last_of(L"/\\"); | |
| 54 return pos != std::wstring::npos ? path.substr(pos + 1) : path; | |
| 55 } | |
| 56 | |
| 57 std::wstring GetFileNameWithoutExtension(const std::wstring& filename) { | |
| 58 const auto pos = filename.find_last_of(L"."); | |
| 59 return pos != std::wstring::npos ? filename.substr(0, pos) : filename; | |
| 60 } | |
| 61 | |
| 62 static std::wstring GetSystemDirectory() { | |
| 63 PWSTR path_wch; | |
| 64 SHGetKnownFolderPath(FOLDERID_Windows, 0, NULL, &path_wch); | |
| 65 std::wstring path_wstr(path_wch); | |
| 66 CoTaskMemFree(path_wch); | |
| 67 return path_wstr; | |
| 68 } | |
| 69 | |
| 70 bool IsSystemDirectory(const std::string& path) { | |
| 71 std::wstring path_w = ToWstring(path); | |
| 72 ::CharUpperBuffW(&path_w.front(), path_w.length()); | |
| 73 | |
| 74 std::wstring windir = GetSystemDirectory(); | |
| 75 ::CharUpperBuffW(&windir.front(), windir.length()); | |
| 76 | |
| 77 /* wtf is 4? */ | |
| 78 return path_w.find(windir) == 4; | |
| 79 } | |
| 80 | |
| 81 bool IsSystemDirectory(std::wstring path) { | |
| 82 ::CharUpperBuffW(&path.front(), path.length()); | |
| 83 | |
| 84 std::wstring windir = GetSystemDirectory(); | |
| 85 ::CharUpperBuffW(&windir.front(), windir.length()); | |
| 86 | |
| 87 return path.find(windir) == 4; | |
| 88 } | |
| 89 | |
| 90 } | 
