1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#include "error.h"
static __thread char errorbuf[NNWT_ERROR_STR_MAXLEN];
#ifdef NAUNET_ON_WINDOWS
static __thread wchar_t errorbufw[NNWT_ERROR_STR_MAXLEN];
#endif
char *nn_strerror(s32 errnum)
{
#ifdef NAUNET_ON_WINDOWS
strerror_s(errorbuf, NNWT_ERROR_STR_MAXLEN, errnum);
#else
#if (_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE
if (strerror_r(errnum, errorbuf, NNWT_ERROR_STR_MAXLEN) != 0) errorbuf[0] = '\0';
#else
return strerror_r(errnum, errorbuf, NNWT_ERROR_STR_MAXLEN);
#endif
#endif
return errorbuf;
}
#ifdef NAUNET_ON_WINDOWS
#include "../winwrap.h"
#include <wchar.h>
// https://stackoverflow.com/a/46104456
char *nn_win32_error_message(s32 err)
{
DWORD ret = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
0,
errorbufw,
NNWT_ERROR_STR_MAXLEN,
NULL);
if (!ret) {
return NULL;
}
errorbufw[ret] = L'\0';
mbstate_t mbstate = { 0 };
wchar_t *wc = errorbufw;
size_t length = wcsrtombs(NULL, (const wchar_t **)&wc, 0, &mbstate);
if (length == (size_t)-1) {
return NULL;
}
length = wcsrtombs(errorbuf, (const wchar_t **)&wc, length, &mbstate);
al_assert(length < NNWT_ERROR_STR_MAXLEN);
errorbuf[length] = L'\0';
return errorbuf;
}
#endif
|