Commit: e5c2daae9949170f982cbcfcd638f6194d9fcdf1
Parent: 6b9248dae6459f9b6f2da9489458025aca8b0ac2
Author: Randy Palamar
Date:   Wed,  3 Apr 2024 20:05:51 -0600
implement win32 platform layer
Diffstat:
| M | mc.c |  |  | 3 | ++- | 
| M | posix.c |  |  | 9 | ++++++++- | 
| A | win32.c |  |  | 56 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | 
3 files changed, 66 insertions(+), 2 deletions(-)
diff --git a/mc.c b/mc.c
@@ -74,7 +74,7 @@ static void die(const char *, ...);
 #if defined(__unix__) || defined(__APPLE__)
 #include "posix.c"
 #elif defined(_WIN32)
-#error Win32 is currently unsupported!
+#include "win32.c"
 #else
 #error Unsupported Platform!
 #endif
@@ -88,6 +88,7 @@ die(const char *fmt, ...)
 	vfprintf(stderr, fmt, ap);
 	va_end(ap);
 
+	os_pause();
 	exit(1);
 }
 
diff --git a/posix.c b/posix.c
@@ -4,9 +4,9 @@
 #define OS_READ     O_RDONLY
 #define OS_WRITE    (O_WRONLY | O_CREAT | O_TRUNC)
 #define OS_RW       O_RDWR
+#define OS_SEEK_BEG SEEK_SET
 #define OS_SEEK_CUR SEEK_CUR
 #define OS_SEEK_END SEEK_END
-#define OS_SEEK_SET SEEK_SET
 
 typedef int os_file;
 
@@ -44,3 +44,10 @@ os_get_core_count(void)
 {
 	return sysconf(_SC_NPROCESSORS_ONLN);
 }
+
+static void
+os_pause(void)
+{
+	/* not useful here since the user will run from command line */
+	return;
+}
diff --git a/win32.c b/win32.c
@@ -0,0 +1,56 @@
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+#define OS_READ     GENERIC_READ
+#define OS_WRITE    GENERIC_WRITE
+#define OS_RW       (GENERIC_READ | GENERIC_WRITE)
+#define OS_SEEK_BEG FILE_BEGIN
+#define OS_SEEK_CUR FILE_CURRENT
+#define OS_SEEK_END FILE_END
+
+typedef HANDLE os_file;
+
+static os_file
+os_open(s8 path, int flags)
+{
+	os_file f;
+	f = CreateFileA((char *)path.data, flags, 0, NULL,
+	                OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+	if (f == INVALID_HANDLE_VALUE)
+		die("rip can't open output file: %s\n", path.data);
+	return f;
+}
+
+static void
+os_write(os_file f, s8 s)
+{
+	if (WriteFile(f, s.data, s.len, 0, 0) == 0)
+		die("can't write to file\n");
+}
+
+static void
+os_close(os_file f)
+{
+	CloseHandle(f);
+}
+
+static void
+os_seek(os_file f, size off, int whence)
+{
+	SetFilePointer(f, off, 0, whence);
+}
+
+static u32
+os_get_core_count(void)
+{
+	SYSTEM_INFO sysinfo;
+	GetSystemInfo(&sysinfo);
+	return sysinfo.dwNumberOfProcessors;
+}
+
+static void
+os_pause(void)
+{
+	fputs("Press any key to close...", stdout);
+	fgetc(stdin);
+}