From 49f58a1c2b7219cf9641c0f5ecc5246da3e58c7d Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 17 Oct 2025 17:05:10 -0500 Subject: [PATCH 1/5] feat(console input): added get character function --- Include/Iony/Core/Console.h | 2 ++ Include/Iony/Utils/System.h | 12 ++++++++---- Src/Core/Console.c | 30 ++++++++++++++++++++++++++++-- Tests/Main.c | 5 +++++ 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/Include/Iony/Core/Console.h b/Include/Iony/Core/Console.h index d52719b..09af028 100644 --- a/Include/Iony/Core/Console.h +++ b/Include/Iony/Core/Console.h @@ -26,4 +26,6 @@ IonyError iony_print(const char *text, long length); +IonyError iony_get_character(char *character); + #endif diff --git a/Include/Iony/Utils/System.h b/Include/Iony/Utils/System.h index 71c8a66..fa85951 100644 --- a/Include/Iony/Utils/System.h +++ b/Include/Iony/Utils/System.h @@ -25,13 +25,17 @@ #if defined(__GNUC__) #if defined(__x86_64__) -#define SYS_WRITE 1 +#define IONY_SYS_READ 0 +#define IONY_SYS_WRITE 1 #elif defined(__i386__) -#define SYS_WRITE 4 +#define IONY_SYS_READ 3 +#define IONY_SYS_WRITE 4 #elif defined(__aarch64__) -#define SYS_WRITE 64 +#define IONY_SYS_READ 63 +#define IONY_SYS_WRITE 64 #elif defined(__arm__) -#define SYS_WRITE 4 +#define IONY_SYS_READ 3 +#define IONY_SYS_WRITE 4 #endif static inline long iony_system_call(long number, long argument1, long argument2, long argument3, long argument4, long argument5, long argument6) { diff --git a/Src/Core/Console.c b/Src/Core/Console.c index a4b351e..df69d0b 100644 --- a/Src/Core/Console.c +++ b/Src/Core/Console.c @@ -20,9 +20,12 @@ * ----------------------------------------------------------------------------- */ #include "../../Include/Iony/Core/Console.h" +#include "Iony/Utils/Error.h" #if defined(_WIN32) #include + +static HANDLE iony_standard_console_in = 0; #elif defined(__GNUC__) #include "../../Include/Iony/Utils/System.h" #endif @@ -37,13 +40,36 @@ IonyError iony_print(const char *text, const long length) { if (standard_console_out_handle == 0) { standard_console_out_handle = GetStdHandle(STD_OUTPUT_HANDLE); } - + DWORD characters_written; if (!WriteFile(standard_console_out_handle, text, length, &characters_written, 0)) { return IONY_ERROR_SYSTEM_CALL_FAILED; } #elif defined(__GNUC__) - if (iony_system_call(SYS_WRITE, 1, (long)text, length, 0, 0, 0) < 0) { + if (iony_system_call(IONY_SYS_WRITE, 1, (long)text, length, 0, 0, 0) < 0) { + return IONY_ERROR_SYSTEM_CALL_FAILED; + } + #endif + + return IONY_ERROR_NONE; +} + +IonyError iony_get_character(char *character) { + if (character == 0) { + return IONY_ERROR_INVALID_POINTER; + } + + #if defined(_WIN32) + if (iony_standard_console_in == 0) { + iony_standard_console_in = GetStdHandle(STD_INPUT_HANDLE); + } + + DWORD characters_read; + if (!ReadFile(iony_standard_console_in, character, 1, &characters_read, 0)) { + return IONY_ERROR_SYSTEM_CALL_FAILED; + } + #elif defined(__GNUC__) + if (iony_system_call(IONY_SYS_READ, 0, (long)character, 1, 0, 0, 0)) { return IONY_ERROR_SYSTEM_CALL_FAILED; } #endif diff --git a/Tests/Main.c b/Tests/Main.c index 357c7c9..18d4eff 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -27,5 +27,10 @@ int main(void) { return -1; } + char character = ' '; + if (iony_get_character(&character) != IONY_ERROR_NONE) { + return -1; + } + return 0; } \ No newline at end of file From 98129b3260d9ccc25cc451dc73591587ccf8cdd6 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 17 Oct 2025 17:06:23 -0500 Subject: [PATCH 2/5] refactor(console input): changed name of console functions to include console --- Include/Iony/Core/Console.h | 4 ++-- Src/Core/Console.c | 4 ++-- Tests/Main.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Include/Iony/Core/Console.h b/Include/Iony/Core/Console.h index 09af028..afaf2b5 100644 --- a/Include/Iony/Core/Console.h +++ b/Include/Iony/Core/Console.h @@ -24,8 +24,8 @@ #include "../Utils/Error.h" -IonyError iony_print(const char *text, long length); +IonyError iony_console_print(const char *text, long length); -IonyError iony_get_character(char *character); +IonyError iony_console_get_character(char *character); #endif diff --git a/Src/Core/Console.c b/Src/Core/Console.c index df69d0b..e443122 100644 --- a/Src/Core/Console.c +++ b/Src/Core/Console.c @@ -30,7 +30,7 @@ static HANDLE iony_standard_console_in = 0; #include "../../Include/Iony/Utils/System.h" #endif -IonyError iony_print(const char *text, const long length) { +IonyError iony_console_print(const char *text, const long length) { if (text == 0) { return IONY_ERROR_INVALID_POINTER; } @@ -54,7 +54,7 @@ IonyError iony_print(const char *text, const long length) { return IONY_ERROR_NONE; } -IonyError iony_get_character(char *character) { +IonyError iony_console_get_character(char *character) { if (character == 0) { return IONY_ERROR_INVALID_POINTER; } diff --git a/Tests/Main.c b/Tests/Main.c index 18d4eff..eff138d 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -23,12 +23,12 @@ #include int main(void) { - if (iony_print("Hello, World!\n", 15) != IONY_ERROR_NONE) { + if (iony_console_print("Hello, World!\n", 15) != IONY_ERROR_NONE) { return -1; } char character = ' '; - if (iony_get_character(&character) != IONY_ERROR_NONE) { + if (iony_console_get_character(&character) != IONY_ERROR_NONE) { return -1; } From c74b7364f23399642543a8969adbf43b2597dd22 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 17 Oct 2025 17:24:39 -0500 Subject: [PATCH 3/5] feat(console input): implemented clear console function --- Include/Iony/Core/Console.h | 1 + Include/Iony/Utils/Error.h | 3 ++- Src/Core/Console.c | 51 +++++++++++++++++++++++++++++++++---- Tests/Main.c | 5 ++++ 4 files changed, 54 insertions(+), 6 deletions(-) diff --git a/Include/Iony/Core/Console.h b/Include/Iony/Core/Console.h index afaf2b5..a66a10c 100644 --- a/Include/Iony/Core/Console.h +++ b/Include/Iony/Core/Console.h @@ -25,6 +25,7 @@ #include "../Utils/Error.h" IonyError iony_console_print(const char *text, long length); +IonyError iony_console_clear(); IonyError iony_console_get_character(char *character); diff --git a/Include/Iony/Utils/Error.h b/Include/Iony/Utils/Error.h index e5e4d2a..9812407 100644 --- a/Include/Iony/Utils/Error.h +++ b/Include/Iony/Utils/Error.h @@ -26,7 +26,8 @@ typedef enum { IONY_ERROR_NONE = 0, IONY_ERROR_INVALID_POINTER = 1, - IONY_ERROR_SYSTEM_CALL_FAILED = 2 + IONY_ERROR_SYSTEM_CALL_FAILED = 2, + IONY_ERROR_HANDLE_NOT_FOUND = 3, } IonyError; #endif diff --git a/Src/Core/Console.c b/Src/Core/Console.c index e443122..0eeb640 100644 --- a/Src/Core/Console.c +++ b/Src/Core/Console.c @@ -25,6 +25,7 @@ #if defined(_WIN32) #include +static HANDLE iony_standard_console_out = 0; static HANDLE iony_standard_console_in = 0; #elif defined(__GNUC__) #include "../../Include/Iony/Utils/System.h" @@ -36,13 +37,15 @@ IonyError iony_console_print(const char *text, const long length) { } #if defined(_WIN32) - static HANDLE standard_console_out_handle = 0; - if (standard_console_out_handle == 0) { - standard_console_out_handle = GetStdHandle(STD_OUTPUT_HANDLE); + if (iony_standard_console_out == 0) { + iony_standard_console_out = GetStdHandle(STD_OUTPUT_HANDLE); + if (iony_standard_console_out == INVALID_HANDLE_VALUE) { + return IONY_ERROR_HANDLE_NOT_FOUND; + } } DWORD characters_written; - if (!WriteFile(standard_console_out_handle, text, length, &characters_written, 0)) { + if (!WriteFile(iony_standard_console_out, text, length, &characters_written, 0)) { return IONY_ERROR_SYSTEM_CALL_FAILED; } #elif defined(__GNUC__) @@ -54,6 +57,41 @@ IonyError iony_console_print(const char *text, const long length) { return IONY_ERROR_NONE; } +IonyError iony_console_clear() { + #if defined(_WIN32) + if (iony_standard_console_out == 0) { + iony_standard_console_out = GetStdHandle(STD_OUTPUT_HANDLE); + if (iony_standard_console_out == INVALID_HANDLE_VALUE) { + return IONY_ERROR_HANDLE_NOT_FOUND; + } + } + + CONSOLE_SCREEN_BUFFER_INFO console_info; + DWORD characters_written, cell_count; + COORD cursor_coords = { 0, 0 }; + if (!GetConsoleScreenBufferInfo(iony_standard_console_out, &console_info)) { + return IONY_ERROR_SYSTEM_CALL_FAILED; + } + + cell_count = console_info.dwSize.X * console_info.dwSize.Y; + if (!FillConsoleOutputCharacterA(iony_standard_console_out, ' ', cell_count, cursor_coords, &characters_written)) { + return IONY_ERROR_SYSTEM_CALL_FAILED; + } + + if (!FillConsoleOutputAttribute(iony_standard_console_out, console_info.wAttributes, cell_count, cursor_coords, &characters_written)) { + return IONY_ERROR_SYSTEM_CALL_FAILED; + } + + SetConsoleCursorPosition(iony_standard_console_out, cursor_coords); + #elif defined(__GNUC__) + const char clear[] = "\033[2J\033[H"; + if (iony_system_call(IONY_SYS_WRITE, 1, (long)clear, sizeof(clear) - 1, 0, 0, 0) < 0) { + return IONY_ERROR_SYSTEM_CALL_FAILED; + } + #endif + return IONY_ERROR_NONE; +} + IonyError iony_console_get_character(char *character) { if (character == 0) { return IONY_ERROR_INVALID_POINTER; @@ -62,6 +100,9 @@ IonyError iony_console_get_character(char *character) { #if defined(_WIN32) if (iony_standard_console_in == 0) { iony_standard_console_in = GetStdHandle(STD_INPUT_HANDLE); + if (iony_standard_console_in == INVALID_HANDLE_VALUE) { + return IONY_ERROR_HANDLE_NOT_FOUND; + } } DWORD characters_read; @@ -69,7 +110,7 @@ IonyError iony_console_get_character(char *character) { return IONY_ERROR_SYSTEM_CALL_FAILED; } #elif defined(__GNUC__) - if (iony_system_call(IONY_SYS_READ, 0, (long)character, 1, 0, 0, 0)) { + if (iony_system_call(IONY_SYS_READ, 0, (long)character, 1, 0, 0, 0) < 0) { return IONY_ERROR_SYSTEM_CALL_FAILED; } #endif diff --git a/Tests/Main.c b/Tests/Main.c index eff138d..327a0fa 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -32,5 +32,10 @@ int main(void) { return -1; } + if (iony_console_clear() != IONY_ERROR_NONE) { + + return -1; + } + return 0; } \ No newline at end of file From e72a78543e6d023cf3feb62889cd83f65ff352b9 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 17 Oct 2025 17:37:14 -0500 Subject: [PATCH 4/5] feat(console input): implemented get line function --- Include/Iony/Core/Console.h | 1 + Src/Core/Console.c | 29 +++++++++++++++++++++++++++++ Tests/Main.c | 21 +++++++++++++++++++-- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/Include/Iony/Core/Console.h b/Include/Iony/Core/Console.h index a66a10c..5a0a050 100644 --- a/Include/Iony/Core/Console.h +++ b/Include/Iony/Core/Console.h @@ -28,5 +28,6 @@ IonyError iony_console_print(const char *text, long length); IonyError iony_console_clear(); IonyError iony_console_get_character(char *character); +IonyError iony_console_get_line(char *character_string, long max_length, long *characters_read); #endif diff --git a/Src/Core/Console.c b/Src/Core/Console.c index 0eeb640..4d77523 100644 --- a/Src/Core/Console.c +++ b/Src/Core/Console.c @@ -117,3 +117,32 @@ IonyError iony_console_get_character(char *character) { return IONY_ERROR_NONE; } + +IonyError iony_console_get_line(char *character_string, const long max_length, long *characters_read) { + if (character_string == 0) { + return IONY_ERROR_INVALID_POINTER; + } + + #if defined(_WIN32) + if (iony_standard_console_in == 0) { + iony_standard_console_in = GetStdHandle(STD_INPUT_HANDLE); + if (iony_standard_console_in == INVALID_HANDLE_VALUE) { + return IONY_ERROR_HANDLE_NOT_FOUND; + } + } + + DWORD characters_read; + if (!ReadFile(iony_standard_console_in, character_string, max_length, characters_read, 0)) { + return IONY_ERROR_SYSTEM_CALL_FAILED; + } + #elif defined(__GNUC__) + long result = iony_system_call(IONY_SYS_READ, 0, (long)character_string, max_length, 0, 0, 0); + if (result < 0) { + return IONY_ERROR_SYSTEM_CALL_FAILED; + } + + *characters_read = result; + #endif + + return IONY_ERROR_NONE; +} diff --git a/Tests/Main.c b/Tests/Main.c index 327a0fa..e4ea0fc 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -23,7 +23,7 @@ #include int main(void) { - if (iony_console_print("Hello, World!\n", 15) != IONY_ERROR_NONE) { + if (iony_console_print("Press enter to continue...", 27) != IONY_ERROR_NONE) { return -1; } @@ -33,7 +33,24 @@ int main(void) { } if (iony_console_clear() != IONY_ERROR_NONE) { - + return -1; + } + + if (iony_console_print("Enter your name: ", 18) != IONY_ERROR_NONE) { + return -1; + } + + char name_input[256]; + long characters_read = 0; + if (iony_console_get_line(name_input, 256, &characters_read) != IONY_ERROR_NONE) { + return -1; + } + + if (iony_console_print("Your name is ", 14) != IONY_ERROR_NONE) { + return -1; + } + + if (iony_console_print(name_input, characters_read) != IONY_ERROR_NONE) { return -1; } From b13018b7cd538b0921401b2f5e7d81d0d3c0910d Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 17 Oct 2025 17:38:56 -0500 Subject: [PATCH 5/5] refactor(console input): added missing void to clear console function --- Include/Iony/Core/Console.h | 2 +- Src/Core/Console.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Include/Iony/Core/Console.h b/Include/Iony/Core/Console.h index 5a0a050..a52ae21 100644 --- a/Include/Iony/Core/Console.h +++ b/Include/Iony/Core/Console.h @@ -25,7 +25,7 @@ #include "../Utils/Error.h" IonyError iony_console_print(const char *text, long length); -IonyError iony_console_clear(); +IonyError iony_console_clear(void); IonyError iony_console_get_character(char *character); IonyError iony_console_get_line(char *character_string, long max_length, long *characters_read); diff --git a/Src/Core/Console.c b/Src/Core/Console.c index 4d77523..b2f2ce8 100644 --- a/Src/Core/Console.c +++ b/Src/Core/Console.c @@ -57,7 +57,7 @@ IonyError iony_console_print(const char *text, const long length) { return IONY_ERROR_NONE; } -IonyError iony_console_clear() { +IonyError iony_console_clear(void) { #if defined(_WIN32) if (iony_standard_console_out == 0) { iony_standard_console_out = GetStdHandle(STD_OUTPUT_HANDLE);