mongoose/src/arch_win32.h

119 lines
2.6 KiB
C
Raw Normal View History

2020-12-05 19:26:32 +08:00
#pragma once
#if MG_ARCH == MG_ARCH_WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#endif
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
2020-12-05 19:26:32 +08:00
#if defined(_MSC_VER) && _MSC_VER < 1700
#define __func__ ""
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
typedef unsigned char uint8_t;
2021-01-25 18:08:15 +08:00
typedef char int8_t;
2020-12-05 19:26:32 +08:00
typedef unsigned short uint16_t;
2021-01-25 18:08:15 +08:00
typedef short int16_t;
2020-12-05 19:26:32 +08:00
typedef unsigned int uint32_t;
2021-01-25 18:08:15 +08:00
typedef int int32_t;
2021-01-26 20:16:58 +08:00
typedef enum { false = 0, true = 1 } bool;
2020-12-05 19:26:32 +08:00
#else
#include <stdbool.h>
#include <stdint.h>
2020-12-22 17:44:59 +08:00
#include <ws2tcpip.h>
2020-12-05 19:26:32 +08:00
#endif
#include <winsock2.h>
// Protect from calls like std::snprintf in app code
// See https://github.com/cesanta/mongoose/issues/1047
#ifndef __cplusplus
2020-12-05 19:26:32 +08:00
#define snprintf _snprintf
#define vsnprintf _vsnprintf
#define strdup(x) _strdup(x)
#endif
typedef unsigned suseconds_t;
2020-12-05 19:26:32 +08:00
typedef int socklen_t;
#define MG_DIRSEP '\\'
#ifndef PATH_MAX
#define PATH_MAX MAX_PATH
#endif
2020-12-05 19:26:32 +08:00
#ifndef EINPROGRESS
#define EINPROGRESS WSAEINPROGRESS
#endif
#ifndef EWOULDBLOCK
#define EWOULDBLOCK WSAEWOULDBLOCK
#endif
2021-02-11 23:03:22 +08:00
#define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX)
2021-07-23 05:46:33 +08:00
#define fopen(a, b) mg_fopen((a), (b))
2021-07-24 17:35:48 +08:00
// Later windows define _stati64 macro, older do not
#ifdef _stati64
#define stat _stat64
#define _stat64(a, b) mg_stat((a), (b))
#else
#define stat _stati64
#define _stati64(a, b) mg_stat((a), (b))
#endif
2020-12-05 19:26:32 +08:00
#ifndef va_copy
#ifdef __va_copy
#define va_copy __va_copy
#else
#define va_copy(x, y) (x) = (y)
#endif
#endif
#ifndef S_ISDIR
#define S_ISDIR(x) (((x) &_S_IFMT) == _S_IFDIR)
#endif
#define MG_INT64_FMT "%I64d"
2021-07-23 05:46:33 +08:00
static __inline FILE *mg_fopen(const char *path, const char *mode) {
2021-07-24 17:35:48 +08:00
wchar_t b1[MAX_PATH], b2[10];
2021-07-23 05:46:33 +08:00
MultiByteToWideChar(CP_UTF8, 0, path, -1, b1, sizeof(b1) / sizeof(b1[0]));
MultiByteToWideChar(CP_UTF8, 0, mode, -1, b2, sizeof(b2) / sizeof(b2[0]));
return _wfopen(b1, b2);
}
2021-07-24 17:35:48 +08:00
static __inline int mg_stat(const char *path, struct stat *st) {
wchar_t tmp[MAX_PATH];
MultiByteToWideChar(CP_UTF8, 0, path, -1, tmp, sizeof(tmp) / sizeof(tmp[0]));
return _wstati64(tmp, st);
}
2021-04-29 16:50:33 +08:00
// https://lgtm.com/rules/2154840805/ -gmtime, localtime, ctime and asctime
static __inline struct tm *gmtime_r(time_t *t, struct tm *tm) {
(void) tm;
return gmtime(t);
}
static __inline struct tm *localtime_r(time_t *t, struct tm *tm) {
(void) tm;
return localtime(t);
}
2020-12-05 19:26:32 +08:00
#endif