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