Replace platform.h by more specific os.h and compiler.h

This commit is contained in:
bumbread 2022-06-06 21:49:19 +11:00
parent 605412d34c
commit b7c72f49ea
21 changed files with 164 additions and 127 deletions

View File

@ -1,45 +1,24 @@
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <_platform.h>
#include <stdio.h>
#include <_compiler.h>
#include <_os.h>
// TODO: use abort() to break
#if defined(_compiler_msvc)
#include <intrin.h>
#define brk __debugbreak
#else
#define brk __builtin_trap
#endif
#if defined(_os_win)
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
// TODO: make it work via printf
static void _assert_win32_prints(HANDLE conout, char const *str) {
DWORD written = 0;
WriteConsole(conout, str, strlen(str), &written, NULL);
}
extern void _assert_error(char *cond, char const *func, char const *file, char const *line) {
HANDLE conout = GetStdHandle(STD_OUTPUT_HANDLE);
if(conout != INVALID_HANDLE_VALUE) {
_assert_win32_prints(conout, "Assertion Failed: ");
_assert_win32_prints(conout, cond);
_assert_win32_prints(conout, "\n\tFile: ");
_assert_win32_prints(conout, file);
_assert_win32_prints(conout, "\n\tFunc: ");
_assert_win32_prints(conout, func);
_assert_win32_prints(conout, "\n\tLine: ");
_assert_win32_prints(conout, line);
extern void _assert_error(
char *cond,
char const *func,
char const *file,
char const *line
) {
printf("Assertion failed: %s\n", cond);
if(*func != 0) {
printf("\tFunction: %s\n", func);
}
else {
// TODO:
MessageBoxA(NULL,
"Assertion Failed Somewhere, good luck finding it!\n",
"Error",
MB_OK);
}
brk();
printf("\tFile: %s\n", file);
printf("\tLine: %s\n", line);
_compiler_brk();
}
#endif

View File

@ -2,7 +2,7 @@
#include <stdarg.h>
#include <ctype.h>
#include <_platform.h>
#include <_os.h>
#define __STDC_WANT_LIB_EXT1__ 1
#include <string.h>

View File

@ -195,7 +195,6 @@ size_t strlen(const char *s) {
size_t strnlen_s(const char *s, size_t maxsize) {
if (s == NULL) return 0;
size_t i = 0;
while (s[i] && i < maxsize) {
i++;

42
inc/_compiler.h Normal file
View File

@ -0,0 +1,42 @@
#pragma once
#if defined(_MSC_VER) && !defined(__clang__)
#define _compiler_msvc
#endif
#if defined(__GNUC__) && !defined(__clang__)
#define _compiler_gnu
#endif
#if defined(__clang__)
#define _compiler_clang
#endif
#if defined(__CUIKC__)
#define _compiler_cuik
#endif
#if !(defined(_compiler_msvc) \
|| defined(_compiler_gnu) \
|| defined(_compiler_cuik) \
|| defined(_compiler_clang))
#error "Unsupported Compiler"
#endif
#if defined(_compiler_msvc)
#include <intrin.h>
#define _compiler_brk __debugbreak
#elif defined(_compiler_gcc) || defined(_compiler_clang)
#define _compiler_brk __builtin_trap
#else
#error "_compiler_brk is not implemented for this compiler"
#endif
#if defined(_compiler_msvc)
#define _compiler_curfunc __FUNCTION__
#elif defined(_compiler_gcc) || defined(_compiler_clang)
#define _compiler_curfunc __func__
#else
#error "_compiler_curfunc not implemented for this compiler"
#endif

View File

@ -6,10 +6,3 @@
#define _con(a,b) a ## b
#if !defined(_func)
#if defined(_compiler_msvc)
#define _func __FUNCTION__
#else
#define _func __func__
#endif
#endif

15
inc/_os.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#if defined(_WIN32)
#define _os_win
#endif
#if defined(__linux__) && !defined(__ANDROID__)
#define _os_linux
#endif
#if !(defined(_os_win) \
|| defined(_os_linux))
#error "Unsupported OS"
#endif

View File

@ -1,54 +0,0 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
// Microsoft uses this to refer to the secure functions so we'll allow it
#ifdef __STDC_WANT_SECURE_LIB__
#define __STDC_WANT_LIB_EXT1__ 1
#endif
// Compiler Identification
#if defined(_MSC_VER) && !defined(__clang__)
#define _compiler_msvc
#endif
#if defined(__GNUC__) && !defined(__clang__)
#define _compiler_gnu
#endif
#if defined(__clang__)
#define _compiler_clang
#endif
#if defined(__CUIKC__)
#define _compiler_cuik
#endif
#if !(defined(_compiler_msvc) \
|| defined(_compiler_gnu) \
|| defined(_compiler_cuik) \
|| defined(_compiler_clang))
#error "Unsupported Compiler"
#endif
// OS Identification
#if defined(_WIN32)
#define _os_win
#endif
#if defined(__linux__) && !defined(__ANDROID__)
#define _os_linux
#endif
#if !(defined(_os_win) \
|| defined(_os_linux))
#error "Unsupported OS"
#endif
#ifdef __STDC_WANT_LIB_EXT1__
typedef int errno_t;
typedef size_t rsize_t;
#endif

View File

@ -1,8 +1,14 @@
#pragma once
#include "_compiler.h"
#include "_macros.h"
extern void _assert_error(char *cond, char const *func, char const *file, char const *line);
extern void _assert_error(
char *cond,
char const *func,
char const *file,
char const *line
);
#if defined(NDEBUG)
#define assert(ignore) ((void)0)
@ -11,7 +17,11 @@ extern void _assert_error(char *cond, char const *func, char const *file, char c
#define assert(condition) \
do { \
if(!(condition)) { \
_assert_error(#condition, _func, __FILE__, _str(__LINE__)); \
_assert_error( \
#condition, \
_compiler_curfunc, \
__FILE__, \
_str(__LINE__)); \
} \
} while(0)
#endif

View File

@ -1,2 +1,4 @@
#pragma once
#error "Complex Numbers aren't implemented"

View File

@ -1,4 +1,5 @@
#include "_platform.h"
#pragma once
#define EDOM 1
#define EILSEQ 2

View File

@ -1,3 +1,6 @@
#pragma once
typedef unsigned long fexcept_t;
typedef struct fenv_t fenv_t;

View File

@ -1,4 +1,5 @@
#include "_platform.h"
#pragma once
typedef float float_t;
typedef double double_t;

View File

@ -1,4 +1,5 @@
#include "_platform.h"
#pragma once
typedef int sig_atomic_t;

View File

@ -1,8 +1,23 @@
#include "_platform.h"
#pragma once
#include <stdint.h>
typedef struct FILE FILE;
typedef int64_t fpos_t;
#if !defined(__STDC_LIB_EXT1__)
#define __STDC_LIB_EXT1__
typedef int errno_t;
typedef size_t rsize_t;
#endif
#ifdef __STDC_WANT_SECURE_LIB__
#if !defined(__STDC_WANT_LIB_EXT1__)
#define __STDC_WANT_LIB_EXT1__ 1
#endif
#endif
#define _IOFBF 0x0000
#define _IOLBF 0x0040
#define _IONBF 0x0004
@ -75,10 +90,9 @@ int feof(FILE *stream);
int ferror(FILE *stream);
void perror(const char *s);
#ifdef __STDC_WANT_LIB_EXT1__
#define L_tmpnam_s L_tmpnam
#define TMP_MAX_S TMP_MAX
errno_t tmpfile_s(FILE * restrict * restrict streamptr);
errno_t tmpnam_s(char *s, rsize_t maxsize);
#if __STDC_WANT_LIB_EXT1__ == 1
#define L_tmpnam_s L_tmpnam
#define TMP_MAX_S TMP_MAX
errno_t tmpfile_s(FILE * restrict * restrict streamptr);
errno_t tmpnam_s(char *s, rsize_t maxsize);
#endif

View File

@ -1,9 +1,20 @@
#pragma once
#if !defined(NULL)
#define NULL ((void *)0)
#endif
#if !defined(__STDC_LIB_EXT1__)
#define __STDC_LIB_EXT1__
#endif
#ifdef __STDC_WANT_SECURE_LIB__
#if !defined(__STDC_WANT_LIB_EXT1__)
#define __STDC_WANT_LIB_EXT1__ 1
#endif
#endif
// typedef struct div_t {
// int quot;
// int rem;

View File

@ -5,6 +5,18 @@
#define NULL ((void *)0)
#endif
#if !defined(__STDC_LIB_EXT1__)
#define __STDC_LIB_EXT1__
typedef int errno_t;
typedef size_t rsize_t;
#endif
#if __STDC_WANT_SECURE_LIB__ == 1
#if !defined(__STDC_WANT_LIB_EXT1__)
#define __STDC_WANT_LIB_EXT1__ 1
#endif
#endif
// int _wcsicmp(const wchar_t *string1, const wchar_t *string2);
void *memcpy(void * restrict s1, const void * restrict s2, size_t n);
@ -31,15 +43,15 @@ char *strerror(int errnum);
size_t strlen(const char *s);
#if __STDC_WANT_LIB_EXT1__ == 1
int memcpy_s(void * restrict s1, size_t s1max, const void * restrict s2, size_t n);
int memmove_s(void *s1, size_t s1max, const void *s2, size_t n);
int strcpy_s(char * restrict s1, size_t s1max, const char * restrict s2);
int strncpy_s(char * restrict s1, size_t s1max,const char * restrict s2, size_t n);
int strcat_s(char * restrict s1, size_t s1max, const char * restrict s2);
int strncat_s(char * restrict s1, size_t s1max, const char * restrict s2, size_t n);
char *strtok_s(char * restrict s1, size_t * restrict s1max, const char * restrict s2, char ** restrict ptr);
int memset_s(void *s, size_t smax, int c, size_t n);
int strerror_s(char *s, size_t maxsize, int errnum);
size_t strerrorlen_s(int errnum);
size_t strnlen_s(const char *s, size_t maxsize);
errno_t memcpy_s(void * restrict s1, rsize_t s1max, const void * restrict s2, rsize_t n);
errno_t memmove_s(void *s1, rsize_t s1max, const void *s2, rsize_t n);
errno_t strcpy_s(char * restrict s1, rsize_t s1max, const char * restrict s2);
errno_t strncpy_s(char * restrict s1, rsize_t s1max,const char * restrict s2, rsize_t n);
errno_t strcat_s(char * restrict s1, rsize_t s1max, const char * restrict s2);
errno_t strncat_s(char * restrict s1, rsize_t s1max, const char * restrict s2, rsize_t n);
char *strtok_s(char * restrict s1, rsize_t * restrict s1max, const char * restrict s2, char ** restrict ptr);
errno_t memset_s(void *s, rsize_t smax, int c, rsize_t n);
errno_t strerror_s(char *s, rsize_t maxsize, errno_t errnum);
size_t strerrorlen_s(errno_t errnum);
size_t strnlen_s(const char *str, size_t strsz);
#endif

View File

@ -1,3 +1,6 @@
#pragma once
/*
these are type generic so they're macrod and use C11's _Generic

View File

@ -1,4 +1,6 @@
#pragma once
#include <time.h>
#define thread_local _Thread_local

View File

@ -1,4 +1,5 @@
#include "_platform.h"
#pragma once
typedef struct mbstate_t mbstate_t;
typedef uint16_t char16_t;

View File

@ -1,6 +1,7 @@
// On linux this will be UTF-32, on windows it's UTF-16
#include "_platform.h"
#pragma once
// On linux this will be UTF-32, on windows it's UTF-16 (or maybe UCS-2?)
typedef struct mbstate_t mbstate_t;
typedef wchar_t wint_t;

View File

@ -1,4 +1,5 @@
#include "_platform.h"
#pragma once
typedef int wint_t;
wctrans_t;