Separate out platform dependent stuff

This commit is contained in:
bumbread 2022-06-07 13:57:57 +11:00
parent 7bdca7eb73
commit aa247b3d4b
4 changed files with 36 additions and 30 deletions

15
code/os/win/io.c Normal file
View File

@ -0,0 +1,15 @@
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
// 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);
}

View File

@ -7,6 +7,8 @@
#define __STDC_WANT_LIB_EXT1__ 1
#include <string.h>
#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 <Windows.h>
// 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) {

View File

@ -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;

View File

@ -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;