feat(console output): added error handling to print function

This commit is contained in:
Mineplay 2025-10-17 14:06:53 -05:00
parent a627d5ead5
commit 39eb052853
4 changed files with 10 additions and 10 deletions

View file

@ -24,6 +24,6 @@
#include "../Utils/Error.h" #include "../Utils/Error.h"
IonyError iony_print(char *text); IonyError iony_print(char *text, long length);
#endif #endif

View file

@ -26,6 +26,7 @@
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
} IonyError; } IonyError;
#endif #endif

View file

@ -27,23 +27,22 @@
#include "../../Include/Iony/Utils/System.h" #include "../../Include/Iony/Utils/System.h"
#endif #endif
IonyError iony_print(char *text) { IonyError iony_print(char *text, long length) {
if (text == 0) { if (text == 0) {
return IONY_ERROR_INVALID_POINTER; return IONY_ERROR_INVALID_POINTER;
} }
long text_length = 0;
while (text[text_length] != '\0') {
text_length += 1;
}
#if defined(_WIN32) #if defined(_WIN32)
static HANDLE standard_console_out_handle = GetStdHandle(STD_OUTPUT_HANDLE); static HANDLE standard_console_out_handle = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD characters_written; 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__) #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 #endif
return IONY_ERROR_NONE; return IONY_ERROR_NONE;

View file

@ -23,7 +23,7 @@
#include <Iony/Core/Console.h> #include <Iony/Core/Console.h>
int main(void) { int main(void) {
if (iony_print("Hello, World!\n") != IONY_ERROR_NONE) { if (iony_print("Hello, World!\n", 15) != IONY_ERROR_NONE) {
return -1; return -1;
} }