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

12
src/util/CMakeLists.txt Normal file
View File

@@ -0,0 +1,12 @@
# 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
memory.c
string.c
math.c
)

19
src/util/math.c Normal file
View File

@@ -0,0 +1,19 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "math.h"
uint32_t mathNextPowTwo(uint32_t value) {
if (value == 0) return 1; // Handle zero case
value--;
value |= value >> 1;
value |= value >> 2;
value |= value >> 4;
value |= value >> 8;
value |= value >> 16;
return value + 1;
}

17
src/util/math.h Normal file
View File

@@ -0,0 +1,17 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
/**
* Finds the next power of two greater than or equal to the given value.
*
* @param value The value to find the next power of two for.
* @return The next power of two greater than or equal to the value.
*/
uint32_t mathNextPowTwo(uint32_t value);

96
src/util/memory.c Normal file
View File

@@ -0,0 +1,96 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "memory.h"
#include "assert/assert.h"
void * memoryAllocate(const size_t size) {
assertTrue(size > 0, "Cannot allocate 0 bytes of memory.");
void *ptr = malloc(size);
assertNotNull(ptr, "Memory allocation failed.");
return ptr;
}
void memoryFree(void *ptr) {
assertNotNull(ptr, "Cannot free NULL memory.");
free(ptr);
}
void memoryCopy(void *dest, const void *src, const size_t size) {
assertNotNull(dest, "Cannot copy to NULL memory.");
assertNotNull(src, "Cannot copy from NULL memory.");
assertTrue(size > 0, "Cannot copy 0 bytes of memory.");
assertTrue(dest != src, "Cannot copy memory to itself.");
memcpy(dest, src, size);
}
void memorySet(void *dest, const uint8_t value, const size_t size) {
assertNotNull(dest, "Cannot set NULL memory.");
assertTrue(size > 0, "Cannot set 0 bytes of memory.");
memset(dest, value, size);
}
void memoryZero(void *dest, const size_t size) {
memorySet(dest, 0, size);
}
void memoryCopyRangeSafe(
void *dest,
const void *start,
const void *end,
const size_t sizeMax
) {
assertFalse(start == end, "Start and end pointers are the same.");
assertTrue(end > start, "End pointer is not after start pointer.");
size_t copy = (size_t)end - (size_t)start;
assertTrue(copy <= sizeMax, "Size of memory to copy is too large.");
memoryCopy(dest, start, copy);
}
void memoryMove(void *dest, const void *src, const size_t size) {
assertNotNull(dest, "Cannot move to NULL memory.");
assertNotNull(src, "Cannot move from NULL memory.");
assertTrue(size > 0, "Cannot move 0 bytes of memory.");
assertTrue(dest != src, "Cannot move memory to itself.");
memmove(dest, src, size);
}
int_t memoryCompare(
const void *a,
const void *b,
const size_t size
) {
assertNotNull(a, "Cannot compare NULL memory.");
assertNotNull(b, "Cannot compare NULL memory.");
assertTrue(size > 0, "Cannot compare 0 bytes of memory.");
return memcmp(a, b, size);
}
void memoryReallocate(void **ptr, const size_t size) {
assertNotNull(ptr, "Cannot reallocate NULL pointer.");
assertTrue(size > 0, "Cannot reallocate to 0 bytes of memory.");
void *newPointer = memoryAllocate(size);
assertNotNull(newPointer, "Memory reallocation failed.");
memoryFree(*ptr);
*ptr = newPointer;
}
void memoryResize(void **ptr, const size_t oldSize, const size_t newSize) {
assertNotNull(ptr, "Cannot resize NULL pointer.");
if(newSize == oldSize) return;
if(oldSize == 0) return memoryReallocate(ptr, newSize);
assertTrue(newSize > oldSize, "New size must be greater than old size.");
void *newPointer = memoryAllocate(newSize);
assertNotNull(newPointer, "Memory resizing failed.");
memoryCopy(newPointer, *ptr, oldSize);
memoryFree(*ptr);
*ptr = newPointer;
}

107
src/util/memory.h Normal file
View File

@@ -0,0 +1,107 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
/**
* Allocates memory.
*
* @param size The size of the memory to allocate.
* @return The allocated memory.
*/
void * memoryAllocate(const size_t size);
/**
* Frees memory.
*
* @param ptr The memory to free.
*/
void memoryFree(void *ptr);
/**
* Copies memory.
*
* @param dest The destination to copy to.
* @param src The source to copy from.
* @param size The size of the memory to copy.
*/
void memoryCopy(void *dest, const void *src, const size_t size);
/**
* Sets memory.
*
* @param dest The destination to set.
* @param value The value to set.
* @param size The size of the memory to set.
*/
void memorySet(void *dest, const uint8_t value, const size_t size);
/**
* Zeroes memory.
*
* @param dest The destination to zero.
* @param size The size of the memory to zero.
*/
void memoryZero(void *dest, const size_t size);
/**
* Copies memory, ensuring that the memory range is as expected, typically this
* is done if you're trying to reshape data in a buffer. Extremely useful in
* copying data to a shader buffer.
*
* @param dest The destination to copy to.
* @param start The start of the source to copy from.
* @param end The end of the source to copy from.
* @param sizeMax The maximum size of the memory to copy.
*/
void memoryCopyRangeSafe(
void *dest,
const void *start,
const void *end,
const size_t sizeMax
);
/**
* Moves memory.
*
* @param dest The destination to move to.
* @param src The source to move from.
* @param size The size of the memory to move.
*/
void memoryMove(void *dest, const void *src, const size_t size);
/**
* Compares memory.
*
* @param a The first memory to compare.
* @param b The second memory to compare.
* @param size The size of the memory to compare.
* @return 0 if the memory is equal, < 0 if a < b, > 0 if a > b.
*/
int_t memoryCompare(
const void *a,
const void *b,
const size_t size
);
/**
* Reallocates memory.
*
* @param ptr The pointer to the memory to reallocate.
* @param size The new size of the memory.
*/
void memoryReallocate(void **ptr, const size_t size);
/**
* Reallocates memory, but copies existing data to the new memory.
*
* @param ptr The pointer to the memory to resize.
* @param oldSize The old size of the memory.
* @param newSize The new size of the memory.
*/
void memoryResize(void **ptr, const size_t oldSize, const size_t newSize);

128
src/util/string.c Normal file
View File

@@ -0,0 +1,128 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "string.h"
#include "assert/assert.h"
#include "util/memory.h"
bool_t stringIsWhitespace(const char_t c) {
return isspace(c);
}
void stringCopy(char_t *dest, const char_t *src, const size_t destSize) {
assertNotNull(dest, "dest must not be NULL");
assertNotNull(src, "src must not be NULL");
assertTrue(destSize > 0, "destSize must be greater than 0");
assertStrLenMax(src, destSize, "src is too long");
memoryCopy(dest, src, strlen(src) + 1);
}
int stringCompare(const char_t *str1, const char_t *str2) {
assertNotNull(str1, "str1 must not be NULL");
assertNotNull(str2, "str2 must not be NULL");
return strcmp(str1, str2);
}
void stringTrim(char_t *str) {
assertNotNull(str, "str must not be NULL");
// Trim leading whitespace
char_t *start = str;
while(stringIsWhitespace(*start)) start++;
// Trim trailing whitespace
char_t *end = start + strlen(start) - 1;
while (end >= start && stringIsWhitespace(*end)) end--;
// Null-terminate the string
*(end + 1) = '\0';
// Move trimmed string to the original buffer
if (start != str) memmove(str, start, end - start + 2);
}
char_t * stringToken(char_t *str, const char_t *delim) {
assertNotNull(str, "str must not be NULL");
assertNotNull(delim, "delim must not be NULL");
return strtok(str, delim);
}
int32_t stringFormat(
char_t *dest,
const size_t destSize,
const char_t *format,
...
) {
va_list args;
va_start(args, format);
int32_t len = stringFormatVA(dest, destSize, format, args);
va_end(args);
return len;
}
int32_t stringFormatVA(
char_t *dest,
const size_t destSize,
const char_t *format,
const va_list args
) {
assertNotNull(format, "format must not be NULL");
if(dest == NULL) {
int32_t ret = vsnprintf(NULL, 0, format, args);
assertTrue(ret >= 0, "Failed to format string.");
return ret;
}
assertTrue(destSize > 0, "destSize must be greater than 0");
int32_t ret = vsnprintf(dest, destSize, format, args);
assertTrue(ret >= 0, "Failed to format string.");
assertTrue(ret < destSize, "Formatted string is too long.");
return ret;
}
bool_t stringToI32(const char_t *str, int32_t *out) {
assertNotNull(str, "str must not be NULL");
assertNotNull(out, "out must not be NULL");
char_t *endptr;
errno = 0;
long int result = strtol(str, &endptr, 10);
if (errno != 0 || *endptr != '\0') {
return false;
}
*out = (int32_t)result;
return true;
}
bool_t stringToI64(const char_t *str, int64_t *out) {
assertNotNull(str, "str must not be NULL");
assertNotNull(out, "out must not be NULL");
char_t *endptr;
errno = 0;
long long int result = strtoll(str, &endptr, 10);
if (errno != 0 || *endptr != '\0') {
return false;
}
*out = (int64_t)result;
return true;
}
bool_t stringToU16(const char_t *str, uint16_t *out) {
assertNotNull(str, "str must not be NULL");
assertNotNull(out, "out must not be NULL");
char_t *endptr;
errno = 0;
unsigned long int result = strtoul(str, &endptr, 10);
if (errno != 0 || *endptr != '\0' || result > UINT16_MAX) {
return false;
}
*out = (uint16_t)result;
return true;
}

118
src/util/string.h Normal file
View File

@@ -0,0 +1,118 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
/**
* Determines if a character is whitespace.
*
* @param c The character to check.
* @return TRUE if the character is whitespace, FALSE otherwise.
*/
bool_t stringIsWhitespace(const char_t c);
/**
* Copies a string from src to dest, ensuring the dest string is null-terminated
* and does not exceed the specified size.
*
* @param dest The destination string.
* @param src The source string.
* @param destSize The size of the destination string exc. null terminator.
*/
void stringCopy(char_t *dest, const char_t *src, const size_t destSize);
/**
* Compares two strings.
*
* @param str1 The first string.
* @param str2 The second string.
* @return 0 if the strings are equal, -1 if str1 is less than str2, 1 if str1
* is greater than str2.
*/
int stringCompare(const char_t *str1, const char_t *str2);
/**
* Trims whitespace from the beginning and end of a string.
*
* @param str The string to trim.
*/
void stringTrim(char_t *str);
/**
* Gets the next token in a string using a delimiter.
* e.g. input: "Hello, World, Happy Monday!" with stringToken(input, ",") will
* return "Hello" then " World" then " Happy Monday!" on each subsequent call.
*
* @param str The string to split.
* @param delim The delimiter to split by.
* @return A pointer to the next token in the string.
*/
char_t * stringToken(char_t *str, const char_t *delim);
/**
* Formats a string.
*
* @param dest The destination string, or NULL to get the length of the
* formatted string.
* @param destSize The size of the destination string exc. null terminator, can
* be anything if dest is NULL.
* @param format The format string.
* @param ... The arguments to format.
* @return The number of characters written.
*/
int32_t stringFormat(
char_t *dest,
const size_t destSize,
const char_t *format,
...
);
/**
* Formats a string using a va_list.
*
* @param dest The destination string, or NULL to get the length of the
* formatted string.
* @param destSize The size of the destination string exc. null terminator, can
* be anything if dest is NULL.
* @param format The format string.
* @param args The va_list of arguments.
* @return The number of characters written.
*/
int32_t stringFormatVA(
char_t *dest,
const size_t destSize,
const char_t *format,
const va_list args
);
/**
* Converts a string to an integer.
*
* @param str The string to convert.
* @param out The output integer.
* @return TRUE if the conversion was successful, FALSE otherwise.
*/
bool_t stringToI32(const char_t *str, int32_t *out);
/**
* Converts a string to a signed 16-bit integer.
*
* @param str The string to convert.
* @param out The output signed integer.
* @return TRUE if the conversion was successful, FALSE otherwise.
*/
bool_t stringToI16(const char_t *str, int16_t *out);
/**
* Converts a string to an unsigned 16-bit integer.
*
* @param str The string to convert.
* @param out The output unsigned integer.
* @return TRUE if the conversion was successful, FALSE otherwise.
*/
bool_t stringToU16(const char_t *str, uint16_t *out);