archivemuh

This commit is contained in:
2025-08-20 19:18:38 -05:00
parent 1411c2e96b
commit fbfcbe9578
173 changed files with 2802 additions and 13 deletions

10
src/error/CMakeLists.txt Normal file
View File

@@ -0,0 +1,10 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_TARGET_NAME}
PRIVATE
error.c
)

125
src/error/error.c Normal file
View File

@@ -0,0 +1,125 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "assert/assert.h"
#include "error.h"
#include "util/memory.h"
#include "util/string.h"
errorret_t errorThrowImpl(
errorstate_t *state,
errorcode_t code,
const char_t *file,
const char_t *function,
const int32_t line,
const char_t *message,
...
) {
assertNotNull(state, "Error state cannot be NULL");
assertTrue(code != ERROR_OK, "Error code must not be OK");
assertNotNull(file, "File cannot be NULL");
assertNotNull(function, "Function cannot be NULL");
assertTrue(line >= 0, "File pointer must be valid");
assertNotNull(message, "Message cannot be NULL");
memoryZero(state, sizeof(errorstate_t));
state->code = code;
// Format args.
va_list args;
va_start(args, message);
// Get length of formatted message
va_list argsCopy;
va_copy(argsCopy, args);
int32_t len = stringFormatVA(NULL, 0, message, argsCopy);
va_end(argsCopy);
// Create string to hold the formatted message
state->message = (char_t *)memoryAllocate(len + 1);
stringFormatVA(state->message, len + 1, message, args);
va_end(args);
// Format lines
len = stringFormat(NULL, 0, ERROR_LINE_FORMAT, file, line, function);
assertTrue(len >= 0, "Line formatting failed");
state->lines = (char_t *)memoryAllocate(len + 1);
stringFormat(state->lines, len + 1, ERROR_LINE_FORMAT, file, line, function);
return (errorret_t) {
.code = code,
.state = state
};
}
errorret_t errorOkImpl() {
return (errorret_t) {
.code = ERROR_OK,
.state = NULL
};
}
errorret_t errorChainImpl(
const errorret_t retval,
const char_t *file,
const char_t *function,
const int32_t line
) {
if (retval.code == ERROR_OK) return retval;
assertNotNull(retval.state, "Error state cannot be NULL");
assertNotNull(retval.state->message, "Message cannot be NULL");
// Create a new line string.
int32_t newLineLen = snprintf(NULL, 0, ERROR_LINE_FORMAT, file, line, function);
assertTrue(newLineLen >= 0, "Line formatting failed");
char_t *newLine = (char_t *)memoryAllocate(newLineLen + 1);
snprintf(newLine, newLineLen + 1, ERROR_LINE_FORMAT, file, line, function);
// Resize the existing lines to accommodate the new line
size_t existingLen = strlen(retval.state->lines);
memoryResize(
(void**)&retval.state->lines,
existingLen,
existingLen + newLineLen + 1
);
// Now append the new line to the existing lines
memoryCopy(
retval.state->lines + existingLen,
newLine,
newLineLen + 1
);
// Cleanup the temporary new line
memoryFree(newLine);
return retval;
}
void errorCatch(const errorret_t retval) {
if (retval.code == ERROR_OK) return;
assertNotNull(retval.state, "Error state cannot be NULL");
assertNotNull(retval.state->message, "Message cannot be NULL");
memoryFree((void*)retval.state->message);
}
errorret_t errorPrint(const errorret_t retval) {
if (retval.code == ERROR_OK) return retval;
assertNotNull(retval.state, "Error state cannot be NULL");
assertNotNull(retval.state->message, "Message cannot be NULL");
printf(
ERROR_PRINT_FORMAT,
retval.state->code,
retval.state->message,
retval.state->lines
);
return retval;
}

137
src/error/error.h Normal file
View File

@@ -0,0 +1,137 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
typedef uint8_t errorcode_t;
typedef struct {
errorcode_t code;
char_t *message;
char_t *lines;
} errorstate_t;
typedef struct {
errorcode_t code;
errorstate_t *state;
} errorret_t;
static const errorcode_t ERROR_OK = 0;
static const errorcode_t ERROR_NOT_OK = 1;
static const char_t *ERROR_PRINT_FORMAT = "Error (%d): %s\n%s";
static const char_t *ERROR_LINE_FORMAT = " at %s:%d in function %s\n";
static errorstate_t ERROR_STATE = {
.code = ERROR_OK,
.message = NULL,
.lines = NULL
};
/**
* Sets the error state with the provided code and message.
*
* @param state The error state to initialize.
* @param code The error code to set.
* @param file The file where the error occurred.
* @param function The function where the error occurred.
* @param line The line number where the error occurred.
* @param message The error message.
* @param args The arguments for the error message.
* @return The error code.
*/
errorret_t errorThrowImpl(
errorstate_t *state,
errorcode_t code,
const char_t *file,
const char_t *function,
const int32_t line,
const char_t *message,
...
);
/**
* Returns an error state with no error.
*
* @return An error state with code ERROR_OK.
*/
errorret_t errorOkImpl();
/**
* Chains an error state, allowing for error propagation.
*
* @param retval The return value containing the error state.
* @param file The file where the error occurred.
* @param function The function where the error occurred.
* @param line The line number where the error occurred.
* @return The error code if an error occurred, otherwise continues execution.
*/
errorret_t errorChainImpl(
const errorret_t retval,
const char_t *file,
const char_t *function,
const int32_t line
);
/**
* Catches an error and handles it.
*
* @param retval The return value containing the error state.
*/
void errorCatch(const errorret_t retval);
/**
* Prints the error state to the console.
*
* @param retval The return value containing the error state.
* @return Passed retval for chaining.
*/
errorret_t errorPrint(const errorret_t retval);
/**
* Throws an error with a formatted message.
*
* @param code The error code to throw.
* @param message The format string for the error message.
* @param ... Additional arguments for the format string.
* @return The error code.
*/
#define errorThrowWithCode(code, message, ... ) \
return errorThrowImpl(\
&ERROR_STATE, (code), __FILE__, __func__, __LINE__, (message), \
__VA_ARGS__ \
)
/**
* Throws an error with a default error code of ERROR_NOT_OK.
*
* @param message The format string for the error message.
* @param ... Additional arguments for the format string.
* @return The error code.
*/
#define errorThrow(message, ...) \
return errorThrowImpl(\
&ERROR_STATE, ERROR_NOT_OK, __FILE__, __func__, __LINE__, (message), \
__VA_ARGS__ \
)
/**
* Checks if a child method errored, and if it did, then send it up the chain.
* @param retval The return value containing the error state.
* @return The error code if an error occurred, otherwise continues execution.
*/
#define errorChain(retval) \
if ((retval).code != ERROR_OK) { \
return errorChainImpl(retval, __FILE__, __func__, __LINE__); \
}
/**
* Returns without an error.
*/
#define errorOk() \
return errorOkImpl()
// EOF