Path.cpp
Go to the documentation of this file.
1 #if defined(_WIN32) && defined(_MSC_VER)
2 
3  #include "utility/Path.hpp"
4 
5  #include <codecvt>
6  #include <cwchar>
7  #include <locale>
8 
9  #include "utility/spdlog-fmt.hpp"
10 
11 namespace dai {
12 std::wstring Path::convert_utf8_to_wide(const std::string& utf8string) {
13  // #pragma warning(suppress : 4996)
14  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
15  return converter.from_bytes(utf8string);
16 }
17 
18 std::string Path::u8string() const {
19  // #pragma warning(suppress : 4996)
20  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
21  return converter.to_bytes(_nativePath);
22 }
23 
24 std::string Path::string() const {
25  std::mbstate_t state = std::mbstate_t();
26  const wchar_t* pNativePath = _nativePath.c_str();
27  const size_t len = std::wcsrtombs(nullptr, &pNativePath, 0, &state);
28  if(len == static_cast<std::size_t>(-1)) {
29  // alternate std::system_error(static_cast<int>(ERROR_NO_UNICODE_TRANSLATION), std::system_category())
30  throw std::range_error(dai::utility::path_convert_err);
31  }
32  std::string mbstr(len + 1, 0); // +1 for terminating null placed by wcsrtombs()
33  const size_t retVal = std::wcsrtombs(&mbstr[0], &pNativePath, mbstr.size(), &state);
34  mbstr.resize(len); // remove the extra terminating null
35  return mbstr;
36 }
37 
38 } // namespace dai
39 
40 #endif
dai::Path::u8string
std::string u8string() const
Get path in utf-8.
Definition: Path.hpp:164
dai::utility::path_convert_err
static constexpr char path_convert_err[]
Definition: spdlog-fmt.hpp:16
dai::Path::string
std::string string() const
Get path in native-encoding string; no conversion.
Definition: Path.hpp:145
Path.hpp
dai::Path::_nativePath
string_type _nativePath
Definition: Path.hpp:199
spdlog-fmt.hpp
dai
Definition: CameraExposureOffset.hpp:6


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19