From 39eb052853ea9c6eb71ea70499e86457a4936d39 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 17 Oct 2025 14:06:53 -0500 Subject: [PATCH] feat(console output): added error handling to print function --- Include/Iony/Core/Console.h | 2 +- Include/Iony/Utils/Error.h | 1 + Src/Core/Console.c | 15 +++++++-------- Tests/Main.c | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Include/Iony/Core/Console.h b/Include/Iony/Core/Console.h index 246ac68..73d07d8 100644 --- a/Include/Iony/Core/Console.h +++ b/Include/Iony/Core/Console.h @@ -24,6 +24,6 @@ #include "../Utils/Error.h" -IonyError iony_print(char *text); +IonyError iony_print(char *text, long length); #endif diff --git a/Include/Iony/Utils/Error.h b/Include/Iony/Utils/Error.h index 4c45b41..e5e4d2a 100644 --- a/Include/Iony/Utils/Error.h +++ b/Include/Iony/Utils/Error.h @@ -26,6 +26,7 @@ typedef enum { IONY_ERROR_NONE = 0, IONY_ERROR_INVALID_POINTER = 1, + IONY_ERROR_SYSTEM_CALL_FAILED = 2 } IonyError; #endif diff --git a/Src/Core/Console.c b/Src/Core/Console.c index 9e43e1d..fe469d7 100644 --- a/Src/Core/Console.c +++ b/Src/Core/Console.c @@ -27,23 +27,22 @@ #include "../../Include/Iony/Utils/System.h" #endif -IonyError iony_print(char *text) { +IonyError iony_print(char *text, long length) { if (text == 0) { return IONY_ERROR_INVALID_POINTER; } - long text_length = 0; - while (text[text_length] != '\0') { - text_length += 1; - } - #if defined(_WIN32) static HANDLE standard_console_out_handle = GetStdHandle(STD_OUTPUT_HANDLE); DWORD characters_written; - WriteFile(standard_console_out_handle, text, text_length, &characters_written, 0); + if (!WriteFile(standard_console_out_handle, text, length, &characters_written, 0)) { + return IONY_ERROR_SYSTEM_CALL_FAILED; + } #elif defined(__GNUC__) - iony_system_call(SYS_WRITE, 1, (long)text, text_length, 0, 0, 0); + if (iony_system_call(SYS_WRITE, 1, (long)text, length, 0, 0, 0) < 0) { + return IONY_ERROR_SYSTEM_CALL_FAILED; + } #endif return IONY_ERROR_NONE; diff --git a/Tests/Main.c b/Tests/Main.c index 97f2c4c..357c7c9 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -23,7 +23,7 @@ #include int main(void) { - if (iony_print("Hello, World!\n") != IONY_ERROR_NONE) { + if (iony_print("Hello, World!\n", 15) != IONY_ERROR_NONE) { return -1; }