Eliminate explicit _WIN32 checks

This commit is contained in:
Sergey Lyubka 2022-02-12 14:33:43 +00:00
parent 3e8e2ab513
commit 3e085ccbaa
4 changed files with 22 additions and 22 deletions

View File

@ -756,7 +756,7 @@ static int p_stat(const char *path, size_t *size, time_t *mtime) {
LOG(LL_ERROR, ("stat() API is not supported. %p %p %p", path, size, mtime));
return 0;
#else
#if defined(_WIN32)
#if MG_ARCH == MG_ARCH_WIN32
struct _stati64 st;
wchar_t tmp[PATH_MAX];
MultiByteToWideChar(CP_UTF8, 0, path, -1, tmp, sizeof(tmp) / sizeof(tmp[0]));
@ -771,7 +771,7 @@ static int p_stat(const char *path, size_t *size, time_t *mtime) {
#endif
}
#ifdef _WIN32
#if MG_ARCH == MG_ARCH_WIN32
struct dirent {
char d_name[MAX_PATH];
};
@ -898,7 +898,7 @@ static void p_list(const char *dir, void (*fn)(const char *, void *),
static void *p_open(const char *path, int flags) {
const char *mode = flags == MG_FS_READ ? "rb" : "a+b";
#ifdef _WIN32
#if MG_ARCH == MG_ARCH_WIN32
wchar_t b1[PATH_MAX], b2[10];
MultiByteToWideChar(CP_UTF8, 0, path, -1, b1, sizeof(b1) / sizeof(b1[0]));
MultiByteToWideChar(CP_UTF8, 0, mode, -1, b2, sizeof(b2) / sizeof(b2[0]));
@ -2741,7 +2741,7 @@ void mg_mgr_free(struct mg_mgr *mgr) {
void mg_mgr_init(struct mg_mgr *mgr) {
memset(mgr, 0, sizeof(*mgr));
#if defined(_WIN32) && MG_ENABLE_WINSOCK
#if MG_ARCH == MG_ARCH_WIN32 && MG_ENABLE_WINSOCK
// clang-format off
{ WSADATA data; WSAStartup(MAKEWORD(2, 2), &data); }
// clang-format on
@ -3105,7 +3105,7 @@ struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, const char *url,
#if MG_ENABLE_SOCKET
#if defined(_WIN32) && MG_ENABLE_WINSOCK
#if MG_ARCH == MG_ARCH_WIN32 && MG_ENABLE_WINSOCK
#define MG_SOCK_ERRNO WSAGetLastError()
#ifndef SO_EXCLUSIVEADDRUSE
#define SO_EXCLUSIVEADDRUSE ((int) (~SO_REUSEADDR))
@ -3177,7 +3177,7 @@ static bool mg_sock_would_block(void) {
#ifndef WINCE
|| err == EAGAIN || err == EINTR
#endif
#if defined(_WIN32) && MG_ENABLE_WINSOCK
#if MG_ARCH == MG_ARCH_WIN32 && MG_ENABLE_WINSOCK
|| err == WSAEINTR || err == WSAEWOULDBLOCK
#endif
;
@ -3271,7 +3271,7 @@ bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
}
static void mg_set_non_blocking_mode(SOCKET fd) {
#if defined(_WIN32) && MG_ENABLE_WINSOCK
#if MG_ARCH == MG_ARCH_WIN32 && MG_ENABLE_WINSOCK
unsigned long on = 1;
ioctlsocket(fd, FIONBIO, &on);
#elif MG_ARCH == MG_ARCH_FREERTOS_TCP
@ -3304,7 +3304,7 @@ static SOCKET mg_open_listener(const char *url, struct mg_addr *addr) {
(void) on;
if ((fd = socket(af, type, proto)) != INVALID_SOCKET &&
#if (!defined(_WIN32) || !defined(SO_EXCLUSIVEADDRUSE)) && \
#if (MG_ARCH != MG_ARCH_WIN32 || !defined(SO_EXCLUSIVEADDRUSE)) && \
(!defined(LWIP_SOCKET) || (defined(LWIP_SOCKET) && SO_REUSE == 1))
// 1. SO_RESUSEADDR is not enabled on Windows because the semantics of
// SO_REUSEADDR on UNIX and Windows is different. On Windows,
@ -3319,7 +3319,7 @@ static SOCKET mg_open_listener(const char *url, struct mg_addr *addr) {
// but won't work! (setsockopt will return EINVAL)
!setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) &&
#endif
#if defined(_WIN32) && defined(SO_EXCLUSIVEADDRUSE) && !defined(WINCE)
#if MG_ARCH == MG_ARCH_WIN32 && defined(SO_EXCLUSIVEADDRUSE) && !defined(WINCE)
// "Using SO_REUSEADDR and SO_EXCLUSIVEADDRUSE"
//! setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *) &on, sizeof(on))
//! &&
@ -3430,7 +3430,7 @@ static void setsockopts(struct mg_connection *c) {
int idle = 60;
setsockopt(FD(c), IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(idle));
#endif
#if !defined(_WIN32) && !defined(__QNX__)
#if MG_ARCH != MG_ARCH_WIN32 && !defined(__QNX__)
{
int cnt = 3, intvl = 20;
setsockopt(FD(c), IPPROTO_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt));
@ -3499,7 +3499,7 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
if (MG_SOCK_ERRNO != EAGAIN)
#endif
LOG(LL_ERROR, ("%lu accept failed, errno %d", lsn->id, MG_SOCK_ERRNO));
#if (!defined(_WIN32) && (MG_ARCH != MG_ARCH_FREERTOS_TCP))
#if ((MG_ARCH != MG_ARCH_WIN32) && (MG_ARCH != MG_ARCH_FREERTOS_TCP))
} else if ((long) fd >= FD_SETSIZE) {
LOG(LL_ERROR, ("%ld > %ld", (long) fd, (long) FD_SETSIZE));
closesocket(fd);

View File

@ -15,7 +15,7 @@ static int p_stat(const char *path, size_t *size, time_t *mtime) {
LOG(LL_ERROR, ("stat() API is not supported. %p %p %p", path, size, mtime));
return 0;
#else
#if defined(_WIN32)
#if MG_ARCH == MG_ARCH_WIN32
struct _stati64 st;
wchar_t tmp[PATH_MAX];
MultiByteToWideChar(CP_UTF8, 0, path, -1, tmp, sizeof(tmp) / sizeof(tmp[0]));
@ -30,7 +30,7 @@ static int p_stat(const char *path, size_t *size, time_t *mtime) {
#endif
}
#ifdef _WIN32
#if MG_ARCH == MG_ARCH_WIN32
struct dirent {
char d_name[MAX_PATH];
};
@ -157,7 +157,7 @@ static void p_list(const char *dir, void (*fn)(const char *, void *),
static void *p_open(const char *path, int flags) {
const char *mode = flags == MG_FS_READ ? "rb" : "a+b";
#ifdef _WIN32
#if MG_ARCH == MG_ARCH_WIN32
wchar_t b1[PATH_MAX], b2[10];
MultiByteToWideChar(CP_UTF8, 0, path, -1, b1, sizeof(b1) / sizeof(b1[0]));
MultiByteToWideChar(CP_UTF8, 0, mode, -1, b2, sizeof(b2) / sizeof(b2[0]));

View File

@ -148,7 +148,7 @@ void mg_mgr_free(struct mg_mgr *mgr) {
void mg_mgr_init(struct mg_mgr *mgr) {
memset(mgr, 0, sizeof(*mgr));
#if defined(_WIN32) && MG_ENABLE_WINSOCK
#if MG_ARCH == MG_ARCH_WIN32 && MG_ENABLE_WINSOCK
// clang-format off
{ WSADATA data; WSAStartup(MAKEWORD(2, 2), &data); }
// clang-format on

View File

@ -9,7 +9,7 @@
#include "util.h"
#if MG_ENABLE_SOCKET
#if defined(_WIN32) && MG_ENABLE_WINSOCK
#if MG_ARCH == MG_ARCH_WIN32 && MG_ENABLE_WINSOCK
#define MG_SOCK_ERRNO WSAGetLastError()
#ifndef SO_EXCLUSIVEADDRUSE
#define SO_EXCLUSIVEADDRUSE ((int) (~SO_REUSEADDR))
@ -81,7 +81,7 @@ static bool mg_sock_would_block(void) {
#ifndef WINCE
|| err == EAGAIN || err == EINTR
#endif
#if defined(_WIN32) && MG_ENABLE_WINSOCK
#if MG_ARCH == MG_ARCH_WIN32 && MG_ENABLE_WINSOCK
|| err == WSAEINTR || err == WSAEWOULDBLOCK
#endif
;
@ -175,7 +175,7 @@ bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
}
static void mg_set_non_blocking_mode(SOCKET fd) {
#if defined(_WIN32) && MG_ENABLE_WINSOCK
#if MG_ARCH == MG_ARCH_WIN32 && MG_ENABLE_WINSOCK
unsigned long on = 1;
ioctlsocket(fd, FIONBIO, &on);
#elif MG_ARCH == MG_ARCH_FREERTOS_TCP
@ -208,7 +208,7 @@ static SOCKET mg_open_listener(const char *url, struct mg_addr *addr) {
(void) on;
if ((fd = socket(af, type, proto)) != INVALID_SOCKET &&
#if (!defined(_WIN32) || !defined(SO_EXCLUSIVEADDRUSE)) && \
#if (MG_ARCH != MG_ARCH_WIN32 || !defined(SO_EXCLUSIVEADDRUSE)) && \
(!defined(LWIP_SOCKET) || (defined(LWIP_SOCKET) && SO_REUSE == 1))
// 1. SO_RESUSEADDR is not enabled on Windows because the semantics of
// SO_REUSEADDR on UNIX and Windows is different. On Windows,
@ -223,7 +223,7 @@ static SOCKET mg_open_listener(const char *url, struct mg_addr *addr) {
// but won't work! (setsockopt will return EINVAL)
!setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) &&
#endif
#if defined(_WIN32) && defined(SO_EXCLUSIVEADDRUSE) && !defined(WINCE)
#if MG_ARCH == MG_ARCH_WIN32 && defined(SO_EXCLUSIVEADDRUSE) && !defined(WINCE)
// "Using SO_REUSEADDR and SO_EXCLUSIVEADDRUSE"
//! setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *) &on, sizeof(on))
//! &&
@ -334,7 +334,7 @@ static void setsockopts(struct mg_connection *c) {
int idle = 60;
setsockopt(FD(c), IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(idle));
#endif
#if !defined(_WIN32) && !defined(__QNX__)
#if MG_ARCH != MG_ARCH_WIN32 && !defined(__QNX__)
{
int cnt = 3, intvl = 20;
setsockopt(FD(c), IPPROTO_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt));
@ -403,7 +403,7 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
if (MG_SOCK_ERRNO != EAGAIN)
#endif
LOG(LL_ERROR, ("%lu accept failed, errno %d", lsn->id, MG_SOCK_ERRNO));
#if (!defined(_WIN32) && (MG_ARCH != MG_ARCH_FREERTOS_TCP))
#if ((MG_ARCH != MG_ARCH_WIN32) && (MG_ARCH != MG_ARCH_FREERTOS_TCP))
} else if ((long) fd >= FD_SETSIZE) {
LOG(LL_ERROR, ("%ld > %ld", (long) fd, (long) FD_SETSIZE));
closesocket(fd);