From a35716e34e2682c7087915514a1387ec03b4d4e3 Mon Sep 17 00:00:00 2001 From: bumbread Date: Thu, 4 Aug 2022 20:47:38 +1100 Subject: [PATCH] headers (2) --- inc/assert.h | 2 +- inc/inttypes.h | 10 +++++++--- inc/stdbool.h | 14 ++++++++++++++ inc/stddef.h | 14 +++++++++++--- inc/stdio.h | 24 +++++++++++++++++++++--- inc/string.h | 8 ++++++-- inc/threads.h | 49 +++++++++++++++++++++++++++--------------------- inc/time.h | 48 +++++++++++++++++++++++++++-------------------- inc/uchar.h | 11 +++++++---- inc/wchar.h | 51 +++++++++++++++++++++++++++++++++++++++----------- inc/wctype.h | 2 +- 11 files changed, 164 insertions(+), 69 deletions(-) create mode 100644 inc/stdbool.h diff --git a/inc/assert.h b/inc/assert.h index 893cd39..da8c44e 100644 --- a/inc/assert.h +++ b/inc/assert.h @@ -23,7 +23,7 @@ void _Noreturn _assert( #define assert(c) if (!(c)) __builtin_trap() #elif _MSC_VER #define assert(c) if (!(c)) __debugbreak() - // TODO: Handle Cuik, and if such handling is not required remove this comment + // TODO: Handle Cuik, and if such handling is not required remove this comment #else // In debug mode there shouldn't be any optimizations so this should // work as a simple way to cause a trap. diff --git a/inc/inttypes.h b/inc/inttypes.h index 81534ff..63fbd0f 100644 --- a/inc/inttypes.h +++ b/inc/inttypes.h @@ -1,9 +1,7 @@ #pragma once -#include -#include -#include +#include #define PRIi8 "hhi" #define PRIu8 "hhu" @@ -256,6 +254,12 @@ #define SCNxPTR "lx" #endif +#if defined(_WIN32) + typedef unsigned short wchar_t; +#else + typedef int wchar_t; +#endif + typedef struct imaxdiv_t imaxdiv_t; struct imaxdiv_t { intmax_t quot; diff --git a/inc/stdbool.h b/inc/stdbool.h new file mode 100644 index 0000000..d7fa82b --- /dev/null +++ b/inc/stdbool.h @@ -0,0 +1,14 @@ + +#pragma once + +#if !defined(bool) + #define bool _Bool +#endif + +#if !defined(true) + #define true 1 +#endif + +#if !defined(false) + #define false 0 +#endif diff --git a/inc/stddef.h b/inc/stddef.h index 0a936e3..60579c2 100644 --- a/inc/stddef.h +++ b/inc/stddef.h @@ -9,9 +9,17 @@ #define NULL ((void *)0) #endif -#define bool _Bool -#define true 1 -#define false 0 +#if !defined(bool) + #define bool _Bool +#endif + +#if !defined(true) + #define true 1 +#endif + +#if !defined(false) + #define false 0 +#endif #define offsetof(st, m) ((size_t)((char *)&((st *)0)->m - (char *)0)) #define alignas _Alignas diff --git a/inc/stdio.h b/inc/stdio.h index cd15be8..fe7925e 100644 --- a/inc/stdio.h +++ b/inc/stdio.h @@ -2,9 +2,27 @@ #pragma once #include -#include -#include -#include + +#if !defined(NULL) + #define NULL ((void *)0) +#endif + +#if defined(_WIN32) + typedef unsigned long long size_t; +#else + typedef unsigned long size_t; +#endif + +#if !defined(_mbstate_t_defined) + #define _mbstate_t_defined + typedef struct mbstate_t mbstate_t; + struct mbstate_t { + union { + unsigned short leftover; + unsigned short high_surrogate; + }; + }; +#endif typedef struct FILE FILE; diff --git a/inc/string.h b/inc/string.h index 85766a1..92d1393 100644 --- a/inc/string.h +++ b/inc/string.h @@ -1,12 +1,16 @@ #pragma once -#include - #if !defined(NULL) #define NULL ((void *)0) #endif +#if defined(_WIN32) + typedef unsigned long long size_t; +#else + typedef unsigned long size_t; +#endif + #if !defined(__STDC_LIB_EXT1__) #define __STDC_LIB_EXT1__ typedef int errno_t; diff --git a/inc/threads.h b/inc/threads.h index f50274b..5e527ef 100644 --- a/inc/threads.h +++ b/inc/threads.h @@ -1,7 +1,5 @@ #pragma once -#include -#include #include #if defined(_WIN32) @@ -13,29 +11,38 @@ #define ONCE_FLAG_INIT 1 #define TSS_DTOR_ITERATIONS 32 +#if !defined(_timespec_defined) + #define _timespec_defined + typedef unsigned long long time_t; + struct timespec { + time_t tv_sec; + long tv_nsec; + }; +#endif + #if defined(_WIN32) -typedef struct cnd_t { - int idk_yet; -} cnd_t; + typedef struct cnd_t { + int idk_yet; + } cnd_t; -typedef struct thrd_t { - void* handle; -} thrd_t; + typedef struct thrd_t { + void* handle; + } thrd_t; -typedef struct tss_t { - int idk_yet; -} tss_t; + typedef struct tss_t { + int idk_yet; + } tss_t; -typedef struct mtx_t { - int type; - // Done to handle recursive mutexes - unsigned long recursion; - unsigned long owner; - atomic_int counter; - void* semaphore; -} mtx_t; + typedef struct mtx_t { + int type; + // Done to handle recursive mutexes + unsigned long recursion; + unsigned long owner; + atomic_int counter; + void* semaphore; + } mtx_t; #else -#error "Not implemented" + #error "Not implemented" #endif typedef void(*tss_dtor_t) (void*); @@ -85,11 +92,11 @@ int thrd_detach (thrd_t thr); int thrd_equal (thrd_t thr0, thrd_t thr1); int thrd_join (thrd_t thr, int *res); void thrd_yield (void); -_Noreturn void thrd_exit(int res); int thrd_sleep( const struct timespec *duration, struct timespec *remaining ); +_Noreturn void thrd_exit(int res); int tss_create(tss_t *key, tss_dtor_t dtor); void tss_delete(tss_t key); diff --git a/inc/time.h b/inc/time.h index a210a5f..e04460a 100644 --- a/inc/time.h +++ b/inc/time.h @@ -3,32 +3,38 @@ #include #if !defined(NULL) -#define NULL ((void *)0) + #define NULL ((void *)0) #endif #define TIME_UTC 1 #define CLOCKS_PER_SEC ((clock_t)1000000) -typedef uint64_t clock_t; -typedef uint64_t time_t; +typedef unsigned long long clock_t; +typedef unsigned long long time_t; -struct timespec { - time_t tv_sec; - long tv_nsec; -}; +#if !defined(_timespec_defined) + #define _timespec_defined + struct timespec { + time_t tv_sec; + long tv_nsec; + }; +#endif -struct tm { - int tm_sec; - int tm_min; - int tm_hour; - int tm_mon; - int tm_mday; // Days passed within 1st of current month - int tm_year; // Years since year 1900 - int tm_wday; // Days passed since sunday - int tm_yday; // Days passed since Jan 1 - int tm_isdst; -}; +#if !defined(_tm_defined) + #define _tm_defined + struct tm { + int tm_sec; + int tm_min; + int tm_hour; + int tm_mon; + int tm_mday; + int tm_year; + int tm_wday; + int tm_yday; + int tm_isdst; + }; +#endif clock_t clock (void); time_t time (time_t *timer); @@ -48,8 +54,10 @@ size_t strftime( ); #ifdef __STDC_WANT_LIB_EXT1__ + errno_t asctime_s(char *s, rsize_t maxsize, const struct tm *timeptr); -errno_t ctime_s(char *s, rsize_t maxsize, const time_t *timer); -struct tm *gmtime_s(const time_t * restrict timer, struct tm * restrict result); +errno_t ctime_s (char *s, rsize_t maxsize, const time_t *timer); +struct tm *gmtime_s (const time_t * restrict timer, struct tm * restrict result); struct tm *localtime_s(const time_t * restrict timer, struct tm * restrict result); + #endif diff --git a/inc/uchar.h b/inc/uchar.h index f645214..e4d591f 100644 --- a/inc/uchar.h +++ b/inc/uchar.h @@ -1,8 +1,11 @@ #pragma once -#include -#include +#if defined(_WIN32) + typedef unsigned long long size_t; +#else + typedef unsigned long size_t; +#endif #if !defined(_mbstate_t_defined) #define _mbstate_t_defined @@ -15,8 +18,8 @@ }; #endif -typedef uint_least16_t char16_t; -typedef uint_least32_t char32_t; +typedef unsigned short char16_t; +typedef unsigned int char32_t; size_t mbrtoc16( char16_t *restrict pc16, diff --git a/inc/wchar.h b/inc/wchar.h index 4079e87..580e306 100644 --- a/inc/wchar.h +++ b/inc/wchar.h @@ -1,19 +1,39 @@ #pragma once -#include -#include -#include -#include -#include +#include +#include + +#if !defined(WEOF) + #define WEOF -1 +#endif + +#if !defined(NULL) + #define NULL ((void *)0) +#endif #if defined(_WIN32) + #if !defined(WCHAR_MIN) + #define WCHAR_MIN 0x0000 + #endif + #if !defined(WCHAR_MAX) + #define WCHAR_MAX 0xffff + #endif typedef unsigned short wchar_t; #else + #if !defined(WCHAR_MIN) + #define WCHAR_MIN -0x80000000 + #endif + #if !defined(WCHAR_MAX) + #define WCHAR_MAX +0x7fffffff + #endif typedef int wchar_t; #endif typedef int wint_t; +typedef int wint_t; +typedef int (*wctrans_t)(wint_t wc); +typedef int (*wctype_t)(wint_t wc); #if !defined(_mbstate_t_defined) #define _mbstate_t_defined @@ -26,14 +46,23 @@ typedef int wint_t; }; #endif -// #define WCHAR_MIN 0x0000 -// #define WCHAR_MAX 0xffff - - -#ifndef WEOF - #define WEOF -1 +#if !defined(_tm_defined) + #define _tm_defined + struct tm { + int tm_sec; + int tm_min; + int tm_hour; + int tm_mon; + int tm_mday; + int tm_year; + int tm_wday; + int tm_yday; + int tm_isdst; + }; #endif +typedef struct FILE FILE; + // String length size_t wcslen(const wchar_t *s); diff --git a/inc/wctype.h b/inc/wctype.h index 488cd5a..01cd6ca 100644 --- a/inc/wctype.h +++ b/inc/wctype.h @@ -5,7 +5,7 @@ typedef int wint_t; typedef int (*wctrans_t)(wint_t wc); typedef int (*wctype_t)(wint_t wc); -#ifndef WEOF +#if !defined(WEOF) #define WEOF 0 #endif