From b5de96b237e5535ab408c08a13c85e23f8c16770 Mon Sep 17 00:00:00 2001 From: bumbread Date: Fri, 3 Jun 2022 10:41:48 +1100 Subject: [PATCH] Merge in ctype --- code/assert.c | 11 +++- code/ctype.c | 92 ++++++++++++++++++++++++++++++++ code/entry_windows.c | 1 + inc/_macros.h | 13 +++++ inc/_platform.h | 10 +--- inc/assert.h | 22 ++------ inc/errno.h | 4 +- inc/stdlib.h | 124 +++++++++++++++++++++---------------------- inc/string.h | 2 + makefile | 23 +++++--- 10 files changed, 202 insertions(+), 100 deletions(-) create mode 100644 code/ctype.c create mode 100644 inc/_macros.h diff --git a/code/assert.c b/code/assert.c index 146e326..cd95f43 100644 --- a/code/assert.c +++ b/code/assert.c @@ -3,6 +3,14 @@ #include #include <_platform.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 @@ -22,7 +30,7 @@ WriteConsole(conout, str, _assert_strlen(str), &written, NULL); } - extern void _assert_print(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) { HANDLE conout = GetStdHandle(STD_OUTPUT_HANDLE); if(conout != INVALID_HANDLE_VALUE) { @@ -42,5 +50,6 @@ "Error", MB_OK); } + brk(); } #endif \ No newline at end of file diff --git a/code/ctype.c b/code/ctype.c new file mode 100644 index 0000000..1d8bffe --- /dev/null +++ b/code/ctype.c @@ -0,0 +1,92 @@ + +// TODO: table-driven? +// TODO: other locales + +#include +#include + +#define in_range(low, v, high) ((low <= v) && (v <= high)) + +int isalnum(int c) +{ + return isalpha(c) || isdigit(c); +} + +int isalpha(int c) +{ + return islower(c) || isupper(c); +} + +int isblank(int c) +{ + return c == ' ' || c == '\t'; +} + +int iscntrl(int c) +{ + return in_range('\x00', c, '\x1f') || c == '\x7f'; +} + +int isdigit(int c) +{ + return in_range('0', c, '9'); +} + +int isgraph(int c) +{ + return isprint(c) && (c != ' '); +} + +int islower(int c) +{ + return in_range('a', c, 'z'); +} + +int isprint(int c) +{ + return in_range(' ', c, '\x7e'); +} + +int ispunct(int c) +{ + return in_range('\x21', c, '\x2f') + || in_range('\x3a', c, '\x40') + || in_range('\x5b', c, '\x60') + || in_range('\x7b', c, '\x7e'); +} + +int isspace(int c) +{ + return in_range('\x09', c, '\x0d') + || c == ' '; +} + +int isupper(int c) +{ + return in_range('A', c, 'Z'); +} + +int isxdigit(int c) +{ + return in_range('0', c, '9') + || in_range('a', c, 'f') + || in_range('A', c, 'F'); +} + +int tolower(int c) +{ + if(isupper(c)) { + return c-'A'+'a'; + } + else return c; +} + +int toupper(int c) +{ + if(islower(c)) { + return c-'a'+'A'; + } + else return c; +} + + diff --git a/code/entry_windows.c b/code/entry_windows.c index a457d5c..7de915f 100644 --- a/code/entry_windows.c +++ b/code/entry_windows.c @@ -1,3 +1,4 @@ + #include <_platform.h> #include diff --git a/inc/_macros.h b/inc/_macros.h new file mode 100644 index 0000000..dc9df71 --- /dev/null +++ b/inc/_macros.h @@ -0,0 +1,13 @@ + +#pragma once + +#define _str_(x) #x +#define _str(x) _str_(x) + +#if !defined(_func) + #if defined(_compiler_msvc) + #define _func __FUNCTION__ + #else + #define _func __func__ + #endif +#endif diff --git a/inc/_platform.h b/inc/_platform.h index 208cf68..ad2ab2a 100644 --- a/inc/_platform.h +++ b/inc/_platform.h @@ -32,14 +32,6 @@ #error "Unsupported Compiler" #endif -#if !defined(_func) - #if defined(_compiler_msvc) - #define _func __FUNCTION__ - #else - #define _func __func__ - #endif -#endif - // OS Identification #if defined(_WIN32) @@ -58,4 +50,4 @@ #ifdef __STDC_WANT_LIB_EXT1__ typedef int errno_t; typedef size_t rsize_t; -#endif \ No newline at end of file +#endif diff --git a/inc/assert.h b/inc/assert.h index 11f2361..c519463 100644 --- a/inc/assert.h +++ b/inc/assert.h @@ -1,20 +1,9 @@ -#if !defined(_assert_h) -#define _assert_h -#include "_platform.h" +#pragma once -extern void _assert_print(char *cond, char const *func, char const *file, char const *line); +#include "_macros.h" -// TODO: use abort() to break -#if defined(_compiler_msvc) - #include - #define _compiler_brk __debugbreak -#else - #define _compiler_brk __builtin_trap -#endif - -#define _str_(x) #x -#define _str(x) _str_(x) +extern void _assert_error(char *cond, char const *func, char const *file, char const *line); #if defined(NDEBUG) #define assert(ignore) ((void)0) @@ -23,10 +12,7 @@ extern void _assert_print(char *cond, char const *func, char const *file, char c #define assert(condition) \ do { \ if(!(condition)) { \ - _assert_print(#condition, _func, __FILE__, _str(__LINE__)); \ - _compiler_brk(); \ + _assert_error(#condition, _func, __FILE__, _str(__LINE__)); \ } \ } while(0) #endif - -#endif \ No newline at end of file diff --git a/inc/errno.h b/inc/errno.h index bc9ca5b..a294129 100644 --- a/inc/errno.h +++ b/inc/errno.h @@ -4,5 +4,5 @@ #define EILSEQ 2 #define ERANGE 3 -// TODO: implement this -#define errno 0 +// TODO: +//_Thread_local int errno; diff --git a/inc/stdlib.h b/inc/stdlib.h index ae32cea..9f4bb3b 100644 --- a/inc/stdlib.h +++ b/inc/stdlib.h @@ -1,71 +1,71 @@ #include "_platform.h" -typedef struct div_t { - int quot; - int rem; -} div_t; +// typedef struct div_t { +// int quot; +// int rem; +// } div_t; -typedef struct ldiv_t { - long quot; - long rem; -} ldiv_t; +// typedef struct ldiv_t { +// long quot; +// long rem; +// } ldiv_t; -typedef struct lldiv_t { - long long quot; - long long rem; -} lldiv_t; +// typedef struct lldiv_t { +// long long quot; +// long long rem; +// } lldiv_t; -#define EXIT_FAILURE 1 -#define EXIT_SUCCESS 0 +// #define EXIT_FAILURE 1 +// #define EXIT_SUCCESS 0 -#define RAND_MAX 65536 -#define MB_CUR_MAX 5 +// #define RAND_MAX 65536 +// #define MB_CUR_MAX 5 -double atof(const char *nptr); -int atoi(const char *nptr); -long int atol(const char *nptr); -long long int atoll(const char *nptr); -double strtod(const char * restrict nptr, char ** restrict endptr); -float strtof(const char * restrict nptr, char ** restrict endptr); -long double strtold(const char * restrict nptr, char ** restrict endptr); -long int strtol(const char * restrict nptr, char ** restrict endptr, int base); -long long int strtoll(const char * restrict nptr, char ** restrict endptr, int base); -unsigned long int strtoul(const char * restrict nptr, char ** restrict endptr, int base); -unsigned long long int strtoull(const char * restrict nptr, char ** restrict endptr, int base); -int rand(void); -void srand(unsigned int seed); -void *aligned_alloc(size_t alignment, size_t size); -void *calloc(size_t nmemb, size_t size); -void free(void *ptr); -void *malloc(size_t size); -void *realloc(void *ptr, size_t size); -_Noreturn void abort(void); -int atexit(void (*func)(void)); -int at_quick_exit(void (*func)(void)); -_Noreturn void exit(int status); -_Noreturn void _Exit(int status); -char *getenv(const char *name); -_Noreturn void quick_exit(int status); -int system(const char *string); -void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); -void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); -int abs(int j); -long int labs(long int j); -long long int llabs(long long int j); -div_t div(int numer, int denom); -ldiv_t ldiv(long int numer, long int denom); -lldiv_t lldiv(long long int numer, long long int denom); -int mblen(const char *s, size_t n); -int mbtowc(wchar_t * restrict pwc, const char * restrict s, size_t n); -int wctomb(char *s, wchar_t wchar); -size_t mbstowcs(wchar_t * restrict pwcs, const char * restrict s, size_t n); -size_t wcstombs(char * restrict s, const wchar_t * restrict pwcs, size_t n); +// double atof(const char *nptr); +// int atoi(const char *nptr); +// long int atol(const char *nptr); +// long long int atoll(const char *nptr); +// double strtod(const char * restrict nptr, char ** restrict endptr); +// float strtof(const char * restrict nptr, char ** restrict endptr); +// long double strtold(const char * restrict nptr, char ** restrict endptr); +// long int strtol(const char * restrict nptr, char ** restrict endptr, int base); +// long long int strtoll(const char * restrict nptr, char ** restrict endptr, int base); +// unsigned long int strtoul(const char * restrict nptr, char ** restrict endptr, int base); +// unsigned long long int strtoull(const char * restrict nptr, char ** restrict endptr, int base); +// int rand(void); +// void srand(unsigned int seed); +// void *aligned_alloc(size_t alignment, size_t size); +// void *calloc(size_t nmemb, size_t size); +// void free(void *ptr); +// void *malloc(size_t size); +// void *realloc(void *ptr, size_t size); +// _Noreturn void abort(void); +// int atexit(void (*func)(void)); +// int at_quick_exit(void (*func)(void)); +// _Noreturn void exit(int status); +// _Noreturn void _Exit(int status); +// char *getenv(const char *name); +// _Noreturn void quick_exit(int status); +// int system(const char *string); +// void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); +// void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); +// int abs(int j); +// long int labs(long int j); +// long long int llabs(long long int j); +// div_t div(int numer, int denom); +// ldiv_t ldiv(long int numer, long int denom); +// lldiv_t lldiv(long long int numer, long long int denom); +// int mblen(const char *s, size_t n); +// int mbtowc(wchar_t * restrict pwc, const char * restrict s, size_t n); +// int wctomb(char *s, wchar_t wchar); +// size_t mbstowcs(wchar_t * restrict pwcs, const char * restrict s, size_t n); +// size_t wcstombs(char * restrict s, const wchar_t * restrict pwcs, size_t n); -#ifdef __STDC_WANT_LIB_EXT1__ - typedef void (*constraint_handler_t)(const char * restrict msg, void * restrict ptr, errno_t error); +// #ifdef __STDC_WANT_LIB_EXT1__ +// typedef void (*constraint_handler_t)(const char * restrict msg, void * restrict ptr, errno_t error); - constraint_handler_t set_constraint_handler_s(constraint_handler_t handler); - void abort_handler_s(const char * restrict msg, void * restrict ptr, errno_t error); - void ignore_handler_s(const char * restrict msg, void * restrict ptr, errno_t error); - errno_t getenv_s(size_t * restrict len, char * restrict value, rsize_t maxsize, const char * restrict name); -#endif +// constraint_handler_t set_constraint_handler_s(constraint_handler_t handler); +// void abort_handler_s(const char * restrict msg, void * restrict ptr, errno_t error); +// void ignore_handler_s(const char * restrict msg, void * restrict ptr, errno_t error); +// errno_t getenv_s(size_t * restrict len, char * restrict value, rsize_t maxsize, const char * restrict name); +// #endif diff --git a/inc/string.h b/inc/string.h index 45b2c87..201bc70 100644 --- a/inc/string.h +++ b/inc/string.h @@ -1,5 +1,7 @@ #include "_platform.h" +int _wcsicmp(const wchar_t *string1, const wchar_t *string2); + void *memcpy(void * restrict s1, const void * restrict s2, size_t n); void *memmove(void *s1, const void *s2, size_t n); char *strcpy(char * restrict s1, const char * restrict s2); diff --git a/makefile b/makefile index 7429823..80f36ca 100644 --- a/makefile +++ b/makefile @@ -1,10 +1,10 @@ -GNUFLAGS=-g -gcodeview -Werror -Wall -Iinc -Icode -CLFLAGS=/Zi /I:inc /link /incremental:no /subsystem:windows /nodefaultlib kernel32.lib +# TODO: make shell variable-based path +CLFLAGS=/X /GS- /Iinc /Icode $(shell test\inctoarg) -CC=clang -CFLAGS=$(GNUFLAGS) -LDFLAGS=/nologo /nodefaultlib /entry:mainCRTStartup +CC=clang-cl +CFLAGS=$(CLFLAGS) +LDFLAGS=/nologo /nodefaultlib /entry:mainCRTStartup /subsystem:console SRC_DIR := code OBJ_DIR := build @@ -14,7 +14,14 @@ OBJ_FILES := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.obj,$(SRC_FILES)) ciabatta.lib: $(OBJ_FILES) lib $(LDFLAGS) /out:$@ $^ -$(OBJ_DIR)/%.obj: $(SRC_DIR)/%.c - $(CC) $(CFLAGS) -c -o $@ $< +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c + $(CC) -c -o $@ $< $(CFLAGS) -.PHONY: ciabatta.lib +clean: + del /F /Q build\* + del /F /Q lib\ciabatta.lib + +inctoarg: + cl test\inctoarg.c + +.PHONY: inctoarg ciabatta.lib