🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.2 KiB

  1. #include "config.h"
  2. #include "strutils.h"
  3. #include <cstdlib>
  4. #ifdef _WIN32
  5. #define WIN32_LEAN_AND_MEAN
  6. #include <windows.h>
  7. std::string wstr_to_utf8(const WCHAR *wstr)
  8. {
  9. std::string ret;
  10. int len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr);
  11. if(len > 0)
  12. {
  13. ret.resize(len);
  14. WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &ret[0], len, nullptr, nullptr);
  15. ret.pop_back();
  16. }
  17. return ret;
  18. }
  19. std::wstring utf8_to_wstr(const char *str)
  20. {
  21. std::wstring ret;
  22. int len = MultiByteToWideChar(CP_UTF8, 0, str, -1, nullptr, 0);
  23. if(len > 0)
  24. {
  25. ret.resize(len);
  26. MultiByteToWideChar(CP_UTF8, 0, str, -1, &ret[0], len);
  27. ret.pop_back();
  28. }
  29. return ret;
  30. }
  31. #endif
  32. namespace al {
  33. al::optional<std::string> getenv(const char *envname)
  34. {
  35. const char *str{std::getenv(envname)};
  36. if(str && str[0] != '\0')
  37. return al::make_optional<std::string>(str);
  38. return al::nullopt;
  39. }
  40. #ifdef _WIN32
  41. al::optional<std::wstring> getenv(const WCHAR *envname)
  42. {
  43. const WCHAR *str{_wgetenv(envname)};
  44. if(str && str[0] != L'\0')
  45. return al::make_optional<std::wstring>(str);
  46. return al::nullopt;
  47. }
  48. #endif
  49. } // namespace al