diff --git a/code/assert.c b/code/assert.c index 323f943..00ddc71 100644 --- a/code/assert.c +++ b/code/assert.c @@ -1,45 +1,24 @@ #include #include #include -#include <_platform.h> +#include + +#include <_compiler.h> +#include <_os.h> // TODO: use abort() to break -#if defined(_compiler_msvc) -#include -#define brk __debugbreak -#else -#define brk __builtin_trap -#endif -#if defined(_os_win) -#define WIN32_LEAN_AND_MEAN -#include - -// 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 \ No newline at end of file diff --git a/code/stdio.c b/code/stdio.c index 814e54c..1db2ec8 100644 --- a/code/stdio.c +++ b/code/stdio.c @@ -2,7 +2,7 @@ #include #include -#include <_platform.h> +#include <_os.h> #define __STDC_WANT_LIB_EXT1__ 1 #include diff --git a/code/string.c b/code/string.c index 3f58a51..938cfef 100644 --- a/code/string.c +++ b/code/string.c @@ -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++; diff --git a/inc/_compiler.h b/inc/_compiler.h new file mode 100644 index 0000000..41c74f3 --- /dev/null +++ b/inc/_compiler.h @@ -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 + #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 diff --git a/inc/_macros.h b/inc/_macros.h index 962f0ac..0f4e977 100644 --- a/inc/_macros.h +++ b/inc/_macros.h @@ -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 diff --git a/inc/_os.h b/inc/_os.h new file mode 100644 index 0000000..cc49a99 --- /dev/null +++ b/inc/_os.h @@ -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 diff --git a/inc/_platform.h b/inc/_platform.h deleted file mode 100644 index a0742a0..0000000 --- a/inc/_platform.h +++ /dev/null @@ -1,54 +0,0 @@ -#pragma once -#include -#include - -// 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 - diff --git a/inc/assert.h b/inc/assert.h index 119d665..d4bb0f5 100644 --- a/inc/assert.h +++ b/inc/assert.h @@ -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 diff --git a/inc/complex.h b/inc/complex.h index 05e77a7..70a21d5 100644 --- a/inc/complex.h +++ b/inc/complex.h @@ -1,2 +1,4 @@ +#pragma once + #error "Complex Numbers aren't implemented" diff --git a/inc/errno.h b/inc/errno.h index 50f3328..54aea48 100644 --- a/inc/errno.h +++ b/inc/errno.h @@ -1,4 +1,5 @@ -#include "_platform.h" + +#pragma once #define EDOM 1 #define EILSEQ 2 diff --git a/inc/fenv.h b/inc/fenv.h index 135ba10..19b3154 100644 --- a/inc/fenv.h +++ b/inc/fenv.h @@ -1,3 +1,6 @@ + +#pragma once + typedef unsigned long fexcept_t; typedef struct fenv_t fenv_t; diff --git a/inc/math.h b/inc/math.h index 1b398fa..22ece91 100644 --- a/inc/math.h +++ b/inc/math.h @@ -1,4 +1,5 @@ -#include "_platform.h" + +#pragma once typedef float float_t; typedef double double_t; diff --git a/inc/signal.h b/inc/signal.h index 03a3551..c3016c8 100644 --- a/inc/signal.h +++ b/inc/signal.h @@ -1,4 +1,5 @@ -#include "_platform.h" + +#pragma once typedef int sig_atomic_t; diff --git a/inc/stdio.h b/inc/stdio.h index 17ca341..b2f59a9 100644 --- a/inc/stdio.h +++ b/inc/stdio.h @@ -1,8 +1,23 @@ -#include "_platform.h" + +#pragma once + +#include 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 diff --git a/inc/stdlib.h b/inc/stdlib.h index 9cb01a5..3ca5496 100644 --- a/inc/stdlib.h +++ b/inc/stdlib.h @@ -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; diff --git a/inc/string.h b/inc/string.h index fa2dfc1..e9f0f0d 100644 --- a/inc/string.h +++ b/inc/string.h @@ -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 diff --git a/inc/tgmath.h b/inc/tgmath.h index 6bf9533..13e302a 100644 --- a/inc/tgmath.h +++ b/inc/tgmath.h @@ -1,3 +1,6 @@ + +#pragma once + /* these are type generic so they're macrod and use C11's _Generic diff --git a/inc/threads.h b/inc/threads.h index 293a063..fc65d53 100644 --- a/inc/threads.h +++ b/inc/threads.h @@ -1,4 +1,6 @@ +#pragma once + #include #define thread_local _Thread_local diff --git a/inc/uchar.h b/inc/uchar.h index 4517405..66bb9d5 100644 --- a/inc/uchar.h +++ b/inc/uchar.h @@ -1,4 +1,5 @@ -#include "_platform.h" + +#pragma once typedef struct mbstate_t mbstate_t; typedef uint16_t char16_t; diff --git a/inc/wchar.h b/inc/wchar.h index 710bc34..73557d9 100644 --- a/inc/wchar.h +++ b/inc/wchar.h @@ -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; diff --git a/inc/wctype.h b/inc/wctype.h index c4dda4f..220c748 100644 --- a/inc/wctype.h +++ b/inc/wctype.h @@ -1,4 +1,5 @@ -#include "_platform.h" + +#pragma once typedef int wint_t; wctrans_t;