misc
This commit is contained in:
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
project(sunshine_tools)
|
||||
|
||||
include_directories("${CMAKE_SOURCE_DIR}")
|
||||
include_directories(${CMAKE_SOURCE_DIR})
|
||||
|
||||
add_executable(dxgi-info dxgi.cpp)
|
||||
set_target_properties(dxgi-info PROPERTIES CXX_STANDARD 20)
|
||||
@@ -12,7 +12,7 @@ target_link_libraries(dxgi-info
|
||||
${PLATFORM_LIBRARIES})
|
||||
target_compile_options(dxgi-info PRIVATE ${SUNSHINE_COMPILE_OPTIONS})
|
||||
|
||||
add_executable(audio-info audio.cpp)
|
||||
add_executable(audio-info audio.cpp utils.cpp)
|
||||
set_target_properties(audio-info PROPERTIES CXX_STANDARD 20)
|
||||
target_link_libraries(audio-info
|
||||
${CMAKE_THREAD_LIBS_INIT}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
#define INITGUID
|
||||
#include "src/utility.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include <audioclient.h>
|
||||
#include <codecvt>
|
||||
@@ -46,8 +47,6 @@ namespace audio {
|
||||
|
||||
using handle_t = util::safe_ptr_v2<void, BOOL, CloseHandle>;
|
||||
|
||||
static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> converter;
|
||||
|
||||
class prop_var_t {
|
||||
public:
|
||||
prop_var_t() {
|
||||
@@ -206,7 +205,7 @@ namespace audio {
|
||||
// so we can take the first match as the current format to display.
|
||||
auto audio_client = make_audio_client(device, format);
|
||||
if (audio_client) {
|
||||
current_format = converter.from_bytes(format.name.data());
|
||||
current_format = from_utf8(format.name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
55
tools/utils.cpp
Normal file
55
tools/utils.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "utils.h"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
std::wstring from_utf8(const std::string_view &string) {
|
||||
// No conversion needed if the string is empty
|
||||
if (string.empty()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
// Get the output size required to store the string
|
||||
auto output_size = MultiByteToWideChar(CP_UTF8, 0, string.data(), string.size(), nullptr, 0);
|
||||
if (output_size == 0) {
|
||||
// auto winerr = GetLastError();
|
||||
// BOOST_LOG(error) << "Failed to get UTF-16 buffer size: "sv << winerr;
|
||||
return {};
|
||||
}
|
||||
|
||||
// Perform the conversion
|
||||
std::wstring output(output_size, L'\0');
|
||||
output_size = MultiByteToWideChar(CP_UTF8, 0, string.data(), string.size(), output.data(), output.size());
|
||||
if (output_size == 0) {
|
||||
// auto winerr = GetLastError();
|
||||
// BOOST_LOG(error) << "Failed to convert string to UTF-16: "sv << winerr;
|
||||
return {};
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
std::string to_utf8(const std::wstring_view &string) {
|
||||
// No conversion needed if the string is empty
|
||||
if (string.empty()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
// Get the output size required to store the string
|
||||
auto output_size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, string.data(), string.size(), nullptr, 0, nullptr, nullptr);
|
||||
if (output_size == 0) {
|
||||
// auto winerr = GetLastError();
|
||||
// BOOST_LOG(error) << "Failed to get UTF-8 buffer size: "sv << winerr;
|
||||
return {};
|
||||
}
|
||||
|
||||
// Perform the conversion
|
||||
std::string output(output_size, '\0');
|
||||
output_size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, string.data(), string.size(), output.data(), output.size(), nullptr, nullptr);
|
||||
if (output_size == 0) {
|
||||
// auto winerr = GetLastError();
|
||||
// BOOST_LOG(error) << "Failed to convert string to UTF-8: "sv << winerr;
|
||||
return {};
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
15
tools/utils.h
Normal file
15
tools/utils.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* @brief Convert a UTF-8 string into a UTF-16 wide string.
|
||||
* @param string The UTF-8 string.
|
||||
* @return The converted UTF-16 wide string.
|
||||
*/
|
||||
std::wstring from_utf8(const std::string_view &string);
|
||||
|
||||
/**
|
||||
* @brief Convert a UTF-16 wide string into a UTF-8 string.
|
||||
* @param string The UTF-16 wide string.
|
||||
* @return The converted UTF-8 string.
|
||||
*/
|
||||
std::string to_utf8(const std::wstring_view &string);
|
||||
Reference in New Issue
Block a user