Merge pull request 'i2-console-input' (#9) from i2-console-input into main

Reviewed-on: #9
This commit is contained in:
Mineplay 2025-10-17 17:43:08 -05:00
commit 816535d980
5 changed files with 146 additions and 14 deletions

View file

@ -24,6 +24,10 @@
#include "../Utils/Error.h" #include "../Utils/Error.h"
IonyError iony_print(const char *text, long length); IonyError iony_console_print(const char *text, long length);
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);
#endif #endif

View file

@ -26,7 +26,8 @@
typedef enum { typedef enum {
IONY_ERROR_NONE = 0, IONY_ERROR_NONE = 0,
IONY_ERROR_INVALID_POINTER = 1, IONY_ERROR_INVALID_POINTER = 1,
IONY_ERROR_SYSTEM_CALL_FAILED = 2 IONY_ERROR_SYSTEM_CALL_FAILED = 2,
IONY_ERROR_HANDLE_NOT_FOUND = 3,
} IonyError; } IonyError;
#endif #endif

View file

@ -25,13 +25,17 @@
#if defined(__GNUC__) #if defined(__GNUC__)
#if defined(__x86_64__) #if defined(__x86_64__)
#define SYS_WRITE 1 #define IONY_SYS_READ 0
#define IONY_SYS_WRITE 1
#elif defined(__i386__) #elif defined(__i386__)
#define SYS_WRITE 4 #define IONY_SYS_READ 3
#define IONY_SYS_WRITE 4
#elif defined(__aarch64__) #elif defined(__aarch64__)
#define SYS_WRITE 64 #define IONY_SYS_READ 63
#define IONY_SYS_WRITE 64
#elif defined(__arm__) #elif defined(__arm__)
#define SYS_WRITE 4 #define IONY_SYS_READ 3
#define IONY_SYS_WRITE 4
#endif #endif
static inline long iony_system_call(long number, long argument1, long argument2, long argument3, long argument4, long argument5, long argument6) { static inline long iony_system_call(long number, long argument1, long argument2, long argument3, long argument4, long argument5, long argument6) {

View file

@ -20,33 +20,129 @@
* ----------------------------------------------------------------------------- * -----------------------------------------------------------------------------
*/ */
#include "../../Include/Iony/Core/Console.h" #include "../../Include/Iony/Core/Console.h"
#include "Iony/Utils/Error.h"
#if defined(_WIN32) #if defined(_WIN32)
#include <windows.h> #include <windows.h>
static HANDLE iony_standard_console_out = 0;
static HANDLE iony_standard_console_in = 0;
#elif defined(__GNUC__) #elif defined(__GNUC__)
#include "../../Include/Iony/Utils/System.h" #include "../../Include/Iony/Utils/System.h"
#endif #endif
IonyError iony_print(const char *text, const long length) { IonyError iony_console_print(const char *text, const long length) {
if (text == 0) { if (text == 0) {
return IONY_ERROR_INVALID_POINTER; return IONY_ERROR_INVALID_POINTER;
} }
#if defined(_WIN32) #if defined(_WIN32)
static HANDLE standard_console_out_handle = 0; if (iony_standard_console_out == 0) {
if (standard_console_out_handle == 0) { iony_standard_console_out = GetStdHandle(STD_OUTPUT_HANDLE);
standard_console_out_handle = GetStdHandle(STD_OUTPUT_HANDLE); if (iony_standard_console_out == INVALID_HANDLE_VALUE) {
return IONY_ERROR_HANDLE_NOT_FOUND;
}
} }
DWORD characters_written; 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; return IONY_ERROR_SYSTEM_CALL_FAILED;
} }
#elif defined(__GNUC__) #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; return IONY_ERROR_SYSTEM_CALL_FAILED;
} }
#endif #endif
return IONY_ERROR_NONE; return IONY_ERROR_NONE;
} }
IonyError iony_console_clear(void) {
#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;
}
#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, 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) < 0) {
return IONY_ERROR_SYSTEM_CALL_FAILED;
}
#endif
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;
}

View file

@ -23,7 +23,34 @@
#include <Iony/Core/Console.h> #include <Iony/Core/Console.h>
int main(void) { int main(void) {
if (iony_print("Hello, World!\n", 15) != IONY_ERROR_NONE) { if (iony_console_print("Press enter to continue...", 27) != IONY_ERROR_NONE) {
return -1;
}
char character = ' ';
if (iony_console_get_character(&character) != IONY_ERROR_NONE) {
return -1;
}
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; return -1;
} }