[go: up one dir, main page]

Menu

Diff of /console.c [000000] .. [r1]  Maximize  Restore

Switch to side-by-side view

--- a
+++ b/console.c
@@ -0,0 +1,46 @@
+#include <windows.h>
+#include <stdio.h>
+#include <conio.h>
+#include <fcntl.h>
+#include <io.h>
+
+#pragma warning(disable: 4311)
+
+static BOOL created_console = FALSE;
+
+// returns TRUE if created a console, or FALSE if one already exists (1 console/process)
+BOOL console_create(void)
+{
+	int hCrt;
+	FILE *hf;
+	HANDLE in, out;
+
+	if(!AllocConsole()) return(FALSE);
+	in = GetStdHandle(STD_INPUT_HANDLE);
+	out = GetStdHandle(STD_OUTPUT_HANDLE);
+	hCrt = _open_osfhandle((int) in, _O_RDONLY);
+	hf = _fdopen(hCrt, "r");
+	*stdin = *hf;
+	setvbuf(stdin, NULL, _IONBF, 0);
+	hCrt = _open_osfhandle((int) out, _O_TEXT);
+	hf = _fdopen(hCrt, "w");
+	*stdout = *stderr = *hf;
+	setvbuf(stdout, NULL, _IONBF, 0);
+	setvbuf(stderr, NULL, _IONBF, 0);
+	created_console = TRUE;
+	return(TRUE);
+}
+
+void console_key(char message[])
+{
+	if(created_console) {
+		printf("%s", message);
+		_getch();
+	}
+}
+
+void console_destroy(void)
+{
+	FreeConsole();
+	created_console = FALSE;
+}