u/windows: Add files and u_winerror string format helper

This commit is contained in:
Jakob Bornecrantz 2022-11-17 15:22:34 +00:00
parent 812cbd222f
commit 57820394f4
3 changed files with 103 additions and 0 deletions

View file

@ -110,6 +110,11 @@ target_link_libraries(
aux_math
)
# Only uses normal Windows libraries, doesn't add anything extra.
if(WIN32)
target_sources(aux_util PRIVATE u_windows.c u_windows.h)
endif()
# Is basically used everywhere, unavoidable.
if(XRT_HAVE_SYSTEM_CJSON)
target_link_libraries(aux_util PUBLIC cJSON::cJSON)

View file

@ -0,0 +1,61 @@
// Copyright 2022, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0
/*!
* @file
* @brief Various helpers for doing Windows specific things.
* @author Jakob Bornecrantz <jakob@collabora.com>
*
* @ingroup aux_os
*/
#include "xrt/xrt_windows.h"
#include "util/u_windows.h"
#include "assert.h"
/*
*
* 'Exported' functions.
*
*/
const char *
u_winerror(char *s, size_t size, DWORD err, bool remove_end)
{
DWORD dSize = (DWORD)size;
assert(dSize == size);
BOOL bRet;
bRet = FormatMessageA( //
FORMAT_MESSAGE_FROM_SYSTEM, //
NULL, //
err, //
LANG_SYSTEM_DEFAULT, //
s, //
dSize, //
NULL); //
if (!bRet) {
s[0] = 0;
}
if (!remove_end) {
return s;
}
// Remove newline and period from message.
size = strnlen_s(s, size);
for (size_t i = size; i-- > 0;) {
switch (s[i]) {
case '.':
case '\n':
case '\r': //
s[i] = '\0';
continue;
default: break;
}
break;
}
return s;
}

View file

@ -0,0 +1,37 @@
// Copyright 2022, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0
/*!
* @file
* @brief Various helpers for doing Windows specific things.
* @author Jakob Bornecrantz <jakob@collabora.com>
*
* @ingroup aux_os
*/
#pragma once
#include "xrt/xrt_compiler.h"
#include "xrt/xrt_windows.h"
#ifdef __cplusplus
extern "C" {
#endif
/*!
* This function formats a Windows error number, as returned by `GetLastError`,
* and writes it into the given buffer.
*
* @param buffer Buffer to format the error into.
* @param size Size of the given buffer.
* @param err Error number to format a string for.
* @param remove_end Removes and trailing `\n`, `\r` and `.` characters.
*/
const char *
u_winerror(char *buffer, size_t size, DWORD err, bool remove_end);
#ifdef __cplusplus
}
#endif