feat(console input): implemented get line function
This commit is contained in:
parent
c74b7364f2
commit
e72a78543e
3 changed files with 49 additions and 2 deletions
|
|
@ -28,5 +28,6 @@ IonyError iony_console_print(const char *text, long length);
|
||||||
IonyError iony_console_clear();
|
IonyError iony_console_clear();
|
||||||
|
|
||||||
IonyError iony_console_get_character(char *character);
|
IonyError iony_console_get_character(char *character);
|
||||||
|
IonyError iony_console_get_line(char *character_string, long max_length, long *characters_read);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -117,3 +117,32 @@ IonyError iony_console_get_character(char *character) {
|
||||||
|
|
||||||
return IONY_ERROR_NONE;
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
21
Tests/Main.c
21
Tests/Main.c
|
|
@ -23,7 +23,7 @@
|
||||||
#include <Iony/Core/Console.h>
|
#include <Iony/Core/Console.h>
|
||||||
|
|
||||||
int main(void) {
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -33,7 +33,24 @@ int main(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (iony_console_clear() != IONY_ERROR_NONE) {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue