From aa247b3d4ba1e5e6329163214e04694b5b20d88c Mon Sep 17 00:00:00 2001 From: bumbread Date: Tue, 7 Jun 2022 13:57:57 +1100 Subject: [PATCH] Separate out platform dependent stuff --- code/os/win/io.c | 15 +++++++++++++++ code/stdio.c | 33 ++++++--------------------------- inc/_os.h | 4 ++++ inc/stdio.h | 14 +++++++++++--- 4 files changed, 36 insertions(+), 30 deletions(-) create mode 100644 code/os/win/io.c diff --git a/code/os/win/io.c b/code/os/win/io.c new file mode 100644 index 0000000..795659a --- /dev/null +++ b/code/os/win/io.c @@ -0,0 +1,15 @@ + +#define WIN32_LEAN_AND_MEAN +#include + +// It's just mapped directly to HANDLE +struct FILE { + int unused; +}; + +void _file_write(void* ctx, size_t n, const char str[]) { + DWORD written = 0; + WriteFile((HANDLE) ctx, str, n, &written, NULL); +} + + diff --git a/code/stdio.c b/code/stdio.c index 1db2ec8..4ffde74 100644 --- a/code/stdio.c +++ b/code/stdio.c @@ -7,6 +7,8 @@ #define __STDC_WANT_LIB_EXT1__ 1 #include +#include <_os.h> + typedef void(*OutputFunc)(void* ctx, size_t n, const char str[]); /////////////////////////////////////////////// @@ -18,29 +20,6 @@ typedef void(*OutputFunc)(void* ctx, size_t n, const char str[]); #define FMT_STRLEN_S(s, maxsize) strnlen_s(s, maxsize) #include "printf.h" -/////////////////////////////////////////////// -// Platform dependent -/////////////////////////////////////////////// -#if defined(_os_win) -#define WIN32_LEAN_AND_MEAN -#include - -// It's just mapped directly to HANDLE -struct FILE { - int unused; -}; - -static void file_write(void* ctx, size_t n, const char str[]) { - DWORD written = 0; - WriteFile((HANDLE) ctx, str, n, &written, NULL); -} -#else -#error "TODO: Implement this" -#endif - -/////////////////////////////////////////////// -// Platform indenpendent -/////////////////////////////////////////////// typedef struct { size_t used, capacity; char* string; @@ -61,12 +40,12 @@ static void string_write(void *ctx, size_t n, const char *restrict str) { } int fprintf(FILE *file, const char *restrict fmt, ...) { - CALL_PRINTF(fmt_print_char, file, file_write, fmt); + CALL_PRINTF(fmt_print_char, file, _file_write, fmt); return result; } int printf(const char *restrict fmt, ...) { - CALL_PRINTF(fmt_print_char, stdout, file_write, fmt); + CALL_PRINTF(fmt_print_char, stdout, _file_write, fmt); return result; } @@ -83,11 +62,11 @@ int sprintf(char *restrict s, const char *restrict fmt, ...) { } int vfprintf(FILE *file, const char *restrict fmt, va_list args) { - return fmt_print_char(file, file_write, fmt, args); + return fmt_print_char(file, _file_write, fmt, args); } int vprintf(const char *restrict fmt, va_list args) { - return fmt_print_char(stdout, file_write, fmt, args); + return fmt_print_char(stdout, _file_write, fmt, args); } int vsnprintf(char *restrict s, size_t n, const char *restrict fmt, va_list args) { diff --git a/inc/_os.h b/inc/_os.h index cc49a99..253bdc6 100644 --- a/inc/_os.h +++ b/inc/_os.h @@ -13,3 +13,7 @@ || defined(_os_linux)) #error "Unsupported OS" #endif + +// OS-dependent IO Functions +void _file_write(void* ctx, size_t n, const char str[]); +typedef struct FILE FILE; diff --git a/inc/stdio.h b/inc/stdio.h index b2f59a9..e867634 100644 --- a/inc/stdio.h +++ b/inc/stdio.h @@ -6,6 +6,10 @@ typedef struct FILE FILE; typedef int64_t fpos_t; +#if !defined(NULL) + #define NULL ((void *)0) +#endif + #if !defined(__STDC_LIB_EXT1__) #define __STDC_LIB_EXT1__ typedef int errno_t; @@ -29,9 +33,9 @@ typedef int64_t fpos_t; #define FOPEN_MAX 20 #ifdef _os_win -#define FILENAME_MAX 260 + #define FILENAME_MAX 260 #else -#define FILENAME_MAX 4096 + #define FILENAME_MAX 4096 #endif #define L_tmpnam FILENAME_MAX @@ -40,7 +44,11 @@ typedef int64_t fpos_t; #define SEEK_END 2 #define SEEK_SET 0 -#define TMP_MAX INT_MAX +#ifdef _os_win + #define TMP_MAX SHORT_MAX +#else + #define TMP_MAX INT_MAX +#endif extern FILE *stdout, *stderr, *stdin;