A bit more code cleanup
This commit is contained in:
@ -1,139 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "array.h"
|
||||
|
||||
void * arrayGet(size_t size, void *array, int32_t index) {
|
||||
return (char *)array + (index * (size/sizeof(char)));
|
||||
}
|
||||
|
||||
void arraySet(size_t size, void *array, int32_t index, void *data) {
|
||||
memcpy(arrayGet(size, array, index), data, size);
|
||||
}
|
||||
|
||||
void arrayShuffle(size_t size, void *array, int32_t arrayLength) {
|
||||
int32_t x, y;
|
||||
void *itemDest, *itemSource;
|
||||
void *temporary = malloc(size);
|
||||
|
||||
for(x = 0; x < arrayLength-1; x++) {
|
||||
// Select random element from remaining elements.
|
||||
y = randUint8Range(x, arrayLength);
|
||||
|
||||
itemDest = arrayGet(size, array, y);
|
||||
itemSource = arrayGet(size, array, x);
|
||||
|
||||
// Copy the data into temporary variable
|
||||
memcpy(temporary, itemDest, size);
|
||||
|
||||
// Take the item at this current index, and store from where we just took
|
||||
memcpy(itemDest, itemSource, size);
|
||||
|
||||
// Store the thing from where we just took into here.
|
||||
memcpy(itemSource, temporary, size);
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
free(temporary);
|
||||
}
|
||||
|
||||
int32_t arrayFind(size_t size, void *array, int32_t length, void *value) {
|
||||
int32_t i;
|
||||
void *item;
|
||||
|
||||
for(i = 0; i < length; i++) {
|
||||
// Get the item and compare.
|
||||
item = arrayGet(size, array, i);
|
||||
if(memcmp(item, value, size) == 0) return i;
|
||||
}
|
||||
|
||||
// No find.
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool arrayContains(size_t size, void *array, int32_t length, void *value) {
|
||||
int32_t i = arrayFind(size, array, length, value);
|
||||
return i != -1;
|
||||
}
|
||||
|
||||
void arrayCopy(size_t size, void *source, int32_t length, void *dest) {
|
||||
memcpy(dest, source, size * length);
|
||||
}
|
||||
|
||||
void arraySort(size_t size, void *array, int32_t length, arraysort_t *sort) {
|
||||
qsort(array, length, size, sort);
|
||||
}
|
||||
|
||||
// Common Sorters:
|
||||
|
||||
void arraySortInt32(int32_t *array, int32_t length) {
|
||||
arraySort(sizeof(int32_t), array, length, &_arraySorterInt32);
|
||||
}
|
||||
int32_t _arraySorterInt32(const void* left, const void* right) {
|
||||
return *((int32_t *)left) - *((int32_t *)right);
|
||||
}
|
||||
|
||||
void arraySortUint8(uint8_t *array, int32_t length) {
|
||||
arraySort(sizeof(uint8_t), array, length, &_arraySorterUint8);
|
||||
}
|
||||
int32_t _arraySorterUint8(const void* left, const void* right) {
|
||||
return *((uint8_t *)left) - *((uint8_t *)right);
|
||||
}
|
||||
|
||||
int32_t arrayFindString(char **array, int32_t arrayLength, char *value) {
|
||||
int32_t i;
|
||||
for(i = 0; i < arrayLength; i++) {
|
||||
if(strcmp(array[i], value) == 0) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void arrayRewind(size_t size, void *source, int32_t length, int32_t start,
|
||||
int32_t count
|
||||
) {
|
||||
if(count == -1) count = length - start;
|
||||
|
||||
// Create a temporary new array to house the data.
|
||||
void *temporary = malloc(size * count);
|
||||
|
||||
// Copy the data from the source to the temporary array.
|
||||
arrayCopy(size, arrayGet(size, source, start), count, temporary);
|
||||
|
||||
// Now copy the data back to the source array.
|
||||
arrayCopy(size, temporary, count, source);
|
||||
|
||||
// Cleanup the temporary array.
|
||||
free(temporary);
|
||||
}
|
||||
|
||||
void arraySplice(
|
||||
size_t size, void *array, int32_t start, int32_t count, int32_t length
|
||||
) {
|
||||
// Take the end of the array...
|
||||
int32_t takeStart = start + count;
|
||||
int32_t takeLength = length - takeStart;
|
||||
|
||||
void *temporary = malloc(size * takeLength);
|
||||
arrayCopy(size, arrayGet(size, array, takeStart), takeLength, temporary);
|
||||
|
||||
// Now copy it back into the original array
|
||||
arrayCopy(size, temporary, takeLength, arrayGet(size, array, start));
|
||||
free(temporary);
|
||||
}
|
||||
|
||||
|
||||
int32_t arraySum(
|
||||
size_t size, void *array, int32_t length, arraysumcallback_t *callback
|
||||
) {
|
||||
int32_t i, count;
|
||||
count = 0;
|
||||
for(i = 0; i < length; i++) {
|
||||
if(!callback(arrayGet(size, array, i))) continue;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
@ -1,173 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "rand.h"
|
||||
|
||||
/**
|
||||
* Definition of a callback that is used to sort an array.
|
||||
*
|
||||
* @param left The left element in the array.
|
||||
* @param right The right element in the array.
|
||||
* @return -1 for Left priority, 1 for Right and 0 for Equal.
|
||||
*/
|
||||
typedef int32_t arraysort_t(const void*, const void*);
|
||||
|
||||
/**
|
||||
* Callback used when calculating the sum of an array that match a state.
|
||||
* @param item Item within the array.
|
||||
* @return True if counted, otherwise false.
|
||||
*/
|
||||
typedef bool arraysumcallback_t(void*);
|
||||
|
||||
/**
|
||||
* Retreive the pointer to an elment within the array of unknown type.
|
||||
*
|
||||
* @param size Size of each element within the array.
|
||||
* @param array Array to get from.
|
||||
* @param index Index to get
|
||||
* @return Pointer to the item in the array.
|
||||
*/
|
||||
void * arrayGet(size_t size, void *array, int32_t index);
|
||||
|
||||
/**
|
||||
* Set an object into the array.
|
||||
*
|
||||
* @param size Size of the element to set.
|
||||
* @param array Array to set in to.
|
||||
* @param index Index to set into.
|
||||
* @param data Data to set into the array.
|
||||
* @return void*
|
||||
*/
|
||||
void arraySet(size_t size, void *array, int32_t index, void *data);
|
||||
|
||||
/**
|
||||
* Randomizes the contents of an array.
|
||||
*
|
||||
* @param size Size of each element within the array.
|
||||
* @param array Array to shuffle.
|
||||
* @param arrayLength Length of the array.
|
||||
*/
|
||||
void arrayShuffle(size_t size, void *array, int32_t arrayLength);
|
||||
|
||||
/**
|
||||
* Find the index within the array that matches the given pointer.
|
||||
*
|
||||
* @param size Size of each element within the array.
|
||||
* @param array Array to get from.
|
||||
* @param length Max length to check to.
|
||||
* @param value Pointer to the value to look for. This is memory compared.
|
||||
* @returns The index within the array that matches, or -1 if no match.
|
||||
*/
|
||||
int32_t arrayFind(size_t size, void *array, int32_t length, void *value);
|
||||
|
||||
/**
|
||||
* Compares each item in an array with the specified value, and returns true if
|
||||
* found within the array. This is a shorthand for arrayFind without an int32_t
|
||||
* being returned.
|
||||
*
|
||||
* @param size Size of each element within the array.
|
||||
* @param array Array to get from.
|
||||
* @param length Max length to check to.
|
||||
* @param value Pointer to the value to look for. This is memory compared.
|
||||
* @returns True if the array contains the value, otherwise false.
|
||||
*/
|
||||
bool arrayContains(size_t size, void *array, int32_t length, void *value);
|
||||
|
||||
/**
|
||||
* Copies the raw contents from the source array to the destination array.
|
||||
*
|
||||
* @param size Size of each element within the array.
|
||||
* @param source Source array.
|
||||
* @param length Length of the source array.
|
||||
* @param dest Destination array.
|
||||
*/
|
||||
void arrayCopy(size_t size, void *source, int32_t length, void *dest);
|
||||
|
||||
/**
|
||||
* Sort an array based on the result of an array sort callback.
|
||||
*
|
||||
* @param size Size of each element within the array.
|
||||
* @param array Array to sort.
|
||||
* @param length Max length to sort to.
|
||||
* @param sort Callback to use to sort.
|
||||
*/
|
||||
void arraySort(size_t size, void *array, int32_t length, arraysort_t *sort);
|
||||
|
||||
/**
|
||||
* Sort an int32 array.
|
||||
*
|
||||
* @param array Array to sort.
|
||||
* @param length Max length to sort.
|
||||
*/
|
||||
void arraySortInt32(int32_t *array, int32_t length);
|
||||
/** Internal int32_t array sorter. */
|
||||
int32_t _arraySorterInt32(const void *left, const void* right);
|
||||
|
||||
/**
|
||||
* Sort a uint8_t array.
|
||||
*
|
||||
* @param array Array to sort.
|
||||
* @param length Max Length to sort.
|
||||
*/
|
||||
void arraySortUint8(uint8_t *array, int32_t length);
|
||||
/** Internal uint8_t array sorter. */
|
||||
int32_t _arraySorterUint8(const void* left, const void* right);
|
||||
|
||||
/**
|
||||
* Find the index of a string within a string array. (Array of char pointers).
|
||||
*
|
||||
* @param array Array to check
|
||||
* @param arrayLength Length of th earray.
|
||||
* @param value The value to search for.
|
||||
* @return The index that the strings exists within the array.
|
||||
*/
|
||||
int32_t arrayFindString(char **array, int32_t arrayLength, char *value);
|
||||
|
||||
/**
|
||||
* Rewinds an array backwards. This is used to take an array that has some data
|
||||
* used towards the end of an array, but the data near the start is no longer
|
||||
* used. This will take the end data and put it back at the start.
|
||||
*
|
||||
* @param size The size of each element within the array.
|
||||
* @param source The array itself.
|
||||
* @param length The total length of the array.
|
||||
* @param start The first index within the array that you want to keep.
|
||||
* @param count Count of items after start to use. Use -1 to take the remaining.
|
||||
*/
|
||||
void arrayRewind(size_t size, void *source, int32_t length, int32_t start,
|
||||
int32_t count
|
||||
);
|
||||
|
||||
/**
|
||||
* Remove some elements from an array. This will cause the elements after the
|
||||
* removed items to be shifted down into their place.
|
||||
*
|
||||
* @param size Size of each element within the array.
|
||||
* @param array Array itself.
|
||||
* @param start The first index you want to remove.
|
||||
* @param count The count of elements to remove.
|
||||
* @param length The length of the total array.
|
||||
*/
|
||||
void arraySplice(
|
||||
size_t size, void *array, int32_t start, int32_t count, int32_t length
|
||||
);
|
||||
|
||||
/**
|
||||
* Sums the count of items that match a given filter in an array. The filter
|
||||
* is a callback method that the user can define.
|
||||
*
|
||||
* @param size Size of each element within the array.
|
||||
* @param array Array itself.
|
||||
* @param length Length of the array.
|
||||
* @param callback Callback that will return true/false when called.
|
||||
* @return The count of items that matched the callback invokation.
|
||||
*/
|
||||
int32_t arraySum(
|
||||
size_t size, void *array, int32_t length, arraysumcallback_t *callback
|
||||
);
|
@ -1,51 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "dictionary.h"
|
||||
|
||||
int32_t dictionaryGetIndex(char **keys, char *name, int32_t count) {
|
||||
int32_t i;
|
||||
for(i = 0; i < count; i++) {
|
||||
if(keys[i] == NULL) continue;
|
||||
if(strcmp(keys[i], name) == 0) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int32_t dictionaryGetOrAdd(
|
||||
char **keys, char *name, int32_t *count, int32_t stride, char *buffer
|
||||
) {
|
||||
int32_t i;
|
||||
int32_t firstFree = -1;
|
||||
|
||||
for(i = 0; i < *count; i++) {
|
||||
if(keys[i] == NULL) {
|
||||
if(firstFree == -1) firstFree = i;
|
||||
continue;
|
||||
}
|
||||
if(strcmp(keys[i], name) == 0) return i;
|
||||
}
|
||||
|
||||
if(firstFree == -1) {
|
||||
firstFree = *count;
|
||||
*count = *count + 1;
|
||||
}
|
||||
|
||||
keys[i] = buffer + (sizeof(char) * i * stride);
|
||||
memcpy(keys[i], name, strlen(name) + 1);
|
||||
return *count;
|
||||
}
|
||||
|
||||
void dictionaryRemoveByIndex(char **keys, int32_t i) {
|
||||
keys[i] = NULL;
|
||||
}
|
||||
|
||||
void dictionaryRemove(char **keys, char *name, int32_t count) {
|
||||
int32_t i = dictionaryGetIndex(keys, name, count);
|
||||
if(i == -1) return;
|
||||
keys[i] = NULL;
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
|
||||
/**
|
||||
* Retrieve the index of a given key from a dictionary.
|
||||
*
|
||||
* @param keys Array of strings.
|
||||
* @param name Key to search for.
|
||||
* @param count How long the array is.
|
||||
* @return The index of the key within the dictionary (or -1 if not found).
|
||||
*/
|
||||
int32_t dictionaryGetIndex(char **keys, char *name, int32_t *count);
|
||||
|
||||
/**
|
||||
* Retrieves or adds a key to the dictionary. Note that we take in the count as
|
||||
* a pointer to an integer because this will automatically be incremented if the
|
||||
* dictionary does not contain the string.
|
||||
*
|
||||
* @param keys Array of strings.
|
||||
* @param name Key to add/get.
|
||||
* @param count Pointer to an int32 that contains the count of items.
|
||||
* @param stride How many elements between each key within the char buffer.
|
||||
* @param buffer Char buffer where the key will be copied.
|
||||
* @return The index where the item was added.
|
||||
*/
|
||||
int32_t dictionaryGetOrAdd(
|
||||
char **keys, char *name, int32_t *count, int32_t stride, char *buffer
|
||||
);
|
||||
|
||||
/**
|
||||
* Removes a key from the dictionary.
|
||||
*
|
||||
* @param keys Array of strings.
|
||||
* @param i Index to remove.
|
||||
*/
|
||||
void dictionaryRemoveByIndex(char **keys, int32_t i);
|
||||
|
||||
/**
|
||||
* Remove an item from the dictionary by its key.
|
||||
*
|
||||
* @param keys Array of strings.
|
||||
* @param name Key to remove.
|
||||
* @param count Count of items within the dictionary.
|
||||
*/
|
||||
void dictionaryRemove(char **keys, char *name, int32_t count);
|
@ -1,49 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "dynarray.h"
|
||||
|
||||
void dynArrayInit(dynarray_t *array, size_t size, int32_t total) {
|
||||
array->total = total;
|
||||
array->size = size;
|
||||
array->length = 0;
|
||||
array->data = malloc(size * total);
|
||||
}
|
||||
|
||||
void * dynArrayGet(dynarray_t *array, int32_t i) {
|
||||
return arrayGet(array->size, array->data, i);
|
||||
}
|
||||
|
||||
void dynArraySet(dynarray_t *array, int32_t i, void *data) {
|
||||
array->length = mathMax(array->length, i+1);
|
||||
arraySet(array->size, array->data, i, data);
|
||||
}
|
||||
|
||||
int32_t dynArrayPush(dynarray_t *array, void *data) {
|
||||
int32_t i = array->length;
|
||||
dynArraySet(array, i, data);
|
||||
return i;
|
||||
}
|
||||
|
||||
int32_t dynArrayAdd(dynarray_t *array) {
|
||||
int32_t i = array->length;
|
||||
array->length++;
|
||||
return i;
|
||||
}
|
||||
|
||||
void dynArrayPop(dynarray_t *array) {
|
||||
array->length -= 1;
|
||||
}
|
||||
|
||||
void dynArraySplice(dynarray_t *array, int32_t start, int32_t count) {
|
||||
arraySplice(array->size, array->data, start, count, array->length);
|
||||
array->length -= count;
|
||||
}
|
||||
|
||||
void dynArrayDispose(dynarray_t *array) {
|
||||
free(array->data);
|
||||
}
|
@ -1,99 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "array.h"
|
||||
#include "math.h"
|
||||
|
||||
/** Custom Array Definition */
|
||||
typedef struct {
|
||||
/** The data storage */
|
||||
void *data;
|
||||
|
||||
/** Size of each element within the array */
|
||||
size_t size;
|
||||
|
||||
/** The count of elements currently in the array */
|
||||
int32_t length;
|
||||
|
||||
/** Total count of elements the array can hold */
|
||||
int32_t total;
|
||||
} dynarray_t;
|
||||
|
||||
/**
|
||||
* Create a dynamic array. Dynamic arrays are a more highlevel array style that
|
||||
* can offer great flexibility and ease-of-use, at the cost of memory, memory
|
||||
* fragmentation and some slight performance. Great for when you need something
|
||||
* like a traditional array, without the hassle of a linked list.
|
||||
*
|
||||
* @param array Array to initialize.
|
||||
* @param size Size of each element within the array.
|
||||
* @param total Total number of elements that the array can hold.
|
||||
*/
|
||||
void dynArrayInit(dynarray_t *array, size_t size, int32_t total);
|
||||
|
||||
/**
|
||||
* Returns the pointer to an index within the array.
|
||||
*
|
||||
* @param array Array to get from.
|
||||
* @param i Index to get
|
||||
* @return Pointer to that element.
|
||||
*/
|
||||
void * dynArrayGet(dynarray_t *array, int32_t i);
|
||||
|
||||
/**
|
||||
* Set something into the array.
|
||||
*
|
||||
* @param array Array to set into.
|
||||
* @param i Index to set into.
|
||||
* @param data Pointer to the data to set.
|
||||
*/
|
||||
void dynArraySet(dynarray_t *array, int32_t i, void *data);
|
||||
|
||||
/**
|
||||
* Push an element into the array.
|
||||
*
|
||||
* @param array Array to push in to.
|
||||
* @param data Pointer to the data to push.
|
||||
* @return The index that the data was pushed in to.
|
||||
*/
|
||||
int32_t dynArrayPush(dynarray_t *array, void *data);
|
||||
|
||||
/**
|
||||
* Create a new element in the array for you to use.
|
||||
*
|
||||
* @param array Array to add to.
|
||||
* @return The index that the item was added to.
|
||||
*/
|
||||
int32_t dynArrayAdd(dynarray_t *array);
|
||||
|
||||
/**
|
||||
* "Pops" (Removes) the last element from the array.
|
||||
*
|
||||
* @param array Array to pop.
|
||||
*/
|
||||
void dynArrayPop(dynarray_t *array);
|
||||
|
||||
/**
|
||||
* Removes elements from out of the array, and shuffles the remaining items.
|
||||
* This is useful for removing a specific item from an array and keeping the
|
||||
* holes out of the array. This will readjust the indexes of all elements that
|
||||
* appear after the spliced index.
|
||||
*
|
||||
* @param array Array to splice.
|
||||
* @param start First index to splice.
|
||||
* @param count Count of elements to splice
|
||||
*/
|
||||
void dynArraySplice(dynarray_t *array, int32_t start, int32_t count);
|
||||
|
||||
/**
|
||||
* Dispose a previously created dynamic array.
|
||||
*
|
||||
* @param array Dynamic array to dispose.
|
||||
*/
|
||||
void dynArrayDispose(dynarray_t *array);
|
@ -1,26 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
|
||||
/**
|
||||
* Create a flag definition.
|
||||
*
|
||||
* @param n The flag number.
|
||||
* @return The bitwise flag for that number.
|
||||
*/
|
||||
#define flagDefine(n) (1 << n)
|
||||
|
||||
/**
|
||||
* Turns a flag off in a state.
|
||||
*
|
||||
* @param state State to update.
|
||||
* @param flag Flag to turn off.
|
||||
* @return The updated state.
|
||||
*/
|
||||
#define flagOff(state, flag) (state & ~flag)
|
@ -1,138 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Msters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "list.h"
|
||||
|
||||
list_t * listCreate() {
|
||||
list_t *list = malloc(sizeof(list_t));
|
||||
if(list == NULL) return NULL;
|
||||
|
||||
list->start = NULL;
|
||||
list->end = NULL;
|
||||
list->size = 0;
|
||||
return list;
|
||||
}
|
||||
|
||||
listentry_t * listAdd(list_t *list, void *data) {
|
||||
//Create the list entry
|
||||
listentry_t *entry = malloc(sizeof(listentry_t));
|
||||
if(entry == NULL) return NULL;
|
||||
|
||||
entry->data = data;
|
||||
entry->next = NULL;
|
||||
entry->prev = NULL;
|
||||
|
||||
//Add it to the list
|
||||
listAddEntry(list, entry);
|
||||
|
||||
//Return the entry
|
||||
return entry;
|
||||
}
|
||||
|
||||
void listAddEntry(list_t *list, listentry_t *entry) {
|
||||
//Is this the first / only thing in the list?
|
||||
if(list->start == NULL) {
|
||||
list->start = entry;
|
||||
list->end = entry;
|
||||
} else {
|
||||
//Make the end's next be this entry, and this entry's prev the end, then
|
||||
//make this the new end
|
||||
entry->prev = list->end;
|
||||
list->end->next = entry;
|
||||
list->end = entry;
|
||||
}
|
||||
list->size++;
|
||||
}
|
||||
|
||||
void listRemove(list_t *list, void *data) {
|
||||
uint32_t i = 0;
|
||||
listentry_t *previous = list->start;
|
||||
|
||||
while(previous != NULL) {
|
||||
if(previous->data == data) {
|
||||
listRemoveEntry(list, previous, false);
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
previous = previous->next;
|
||||
}
|
||||
}
|
||||
|
||||
void listRemoveEntry(list_t *list, listentry_t *entry, bool freeData) {
|
||||
//Update next and prev
|
||||
if(entry->prev != NULL) entry->prev->next = entry->next;
|
||||
if(entry->next != NULL) entry->next->prev = entry->prev;
|
||||
|
||||
//Was this at the end?
|
||||
if(list->start == entry) list->start = entry->next;
|
||||
if(list->end == entry) list->end = entry->prev;
|
||||
|
||||
if(freeData) free(entry->data);
|
||||
free(entry);
|
||||
|
||||
list->size--;
|
||||
}
|
||||
|
||||
|
||||
listentry_t * listGetByIndex(list_t *list, uint32_t index) {
|
||||
if(index >= list->size) return NULL;
|
||||
|
||||
// TODO: We can probably make this more efficient by deciding which way we
|
||||
// should loop from based on the list size.
|
||||
|
||||
uint32_t i = 0;
|
||||
listentry_t *previous = list->start;
|
||||
|
||||
while(previous != NULL) {
|
||||
if(i == index) return previous;
|
||||
i++;
|
||||
previous = previous->next;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint32_t listGetIndex(list_t *list, void* data) {
|
||||
uint32_t i = 0;
|
||||
listentry_t *previous = list->start;
|
||||
|
||||
while(previous != NULL) {
|
||||
if(previous->data == data) return i;
|
||||
i++;
|
||||
previous = previous->next;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t listGetEntryIndex(list_t *list, listentry_t *entry) {
|
||||
uint32_t i = 0;
|
||||
listentry_t *previous = list->start;
|
||||
|
||||
while(previous != NULL) {
|
||||
if(previous == entry) return i;
|
||||
i++;
|
||||
previous = previous->next;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
void listDispose(list_t *list, bool freeData) {
|
||||
//Free all of the entries
|
||||
listentry_t *next = list->start;
|
||||
listentry_t *current;
|
||||
|
||||
while(next != NULL) {
|
||||
current = next;
|
||||
next = current->next;
|
||||
if(freeData) free(current->data);
|
||||
free(current);
|
||||
}
|
||||
|
||||
free(list);
|
||||
}
|
@ -1,101 +0,0 @@
|
||||
// Copyright (c) 2021 Dominic Msters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
|
||||
/**
|
||||
* Entry within a given linked list.
|
||||
* @param data* The pointer to the data that is within the entry.
|
||||
* @param prev* Pointer to the previous entry in the list.
|
||||
* @param next* Pointer to the next entry in the list.
|
||||
*/
|
||||
typedef struct listentry_t {
|
||||
void *data;
|
||||
struct listentry_t *prev;
|
||||
struct listentry_t *next;
|
||||
} listentry_t;
|
||||
|
||||
/**
|
||||
* Linked List of elements, Doubly Linked.
|
||||
* @param size The count of elements currently within the list
|
||||
* @param start* First element within the list.
|
||||
* @param end* Last element within the list.
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t size;
|
||||
listentry_t *start;
|
||||
listentry_t *end;
|
||||
} list_t;
|
||||
|
||||
/**
|
||||
* Creates a new linked list
|
||||
* @return Pointer to a new linked list.
|
||||
*/
|
||||
list_t * listCreate();
|
||||
|
||||
/**
|
||||
* Adds data to a linked list
|
||||
*
|
||||
* @param list* Pointer to a previously created linked list
|
||||
* @param data* Pointer to your data
|
||||
* @return A pointer to the new entry in the linked list that was created
|
||||
*/
|
||||
listentry_t * listAdd(list_t *list, void *data);
|
||||
|
||||
/**
|
||||
* Internal function
|
||||
*/
|
||||
void listAddEntry(list_t *list, listentry_t *entry);
|
||||
|
||||
/**
|
||||
* Remove data from a linked list.
|
||||
*
|
||||
* @param list Pointer to a previously created linked list.
|
||||
* @param data* Pointer to your data.
|
||||
*/
|
||||
void listRemove(list_t *list, void *data);
|
||||
|
||||
/**
|
||||
* Remove a list entry from the linked list.
|
||||
*
|
||||
* @param list* Pointer to a previously created linked list.
|
||||
* @param entry* Pointer to the entry within the linked list.
|
||||
* @param freeData If true the data of the listentry_t will be free()'d
|
||||
*/
|
||||
void listRemoveEntry(list_t *list, listentry_t *entry, bool freeData);
|
||||
|
||||
/**
|
||||
* Returns the entry at a given index. This method is costly.
|
||||
* @param list* Pointer to a previously created linked list
|
||||
* @param index Index of element within the linked list to remove
|
||||
* @return The entry at the index or NULL if outside the bounds.
|
||||
*/
|
||||
listentry_t * listGetByIndex(list_t *list, uint32_t index);
|
||||
|
||||
/**
|
||||
* Returns the index of data within the linked list. This method is costly.
|
||||
*
|
||||
* @param list* Pointer to a previously created linked list
|
||||
* @param data* Pointer to your data.
|
||||
* @return The index within the list the entry is in, or -1 if not found.
|
||||
*/
|
||||
uint32_t listGetIndex(list_t *list, void* data);
|
||||
|
||||
/**
|
||||
* Returns the index of an entry within the linked list. This method is costly.
|
||||
*
|
||||
* @param list* Pointer to a previously created linked list
|
||||
* @param entry* Pointer to the entry within the linked list.
|
||||
* @return The index within the list the entry is in, or -1 if not found.
|
||||
*/
|
||||
uint32_t listGetEntryIndex(list_t *list, listentry_t *entry);
|
||||
|
||||
/**
|
||||
* Dispose a previously created link list.
|
||||
* @param list* Pointer to a previously created linked list
|
||||
* @param freeData If true the data within each listentry_t will be free()'d
|
||||
*/
|
||||
void listDispose(list_t *list, bool freeData);
|
@ -1,83 +0,0 @@
|
||||
// Copyright (c) 2021 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
|
||||
#define MATH_PI ((float)M_PI)
|
||||
|
||||
/**
|
||||
* Returns the modulous a result for b. Consdiders negative numbers correctly.
|
||||
* @param a Number to modulo against. (a % b)
|
||||
* @param b Number to modulo with. (a % b)
|
||||
* @returns The modulo result.
|
||||
*/
|
||||
#define mathMod(a,b) ((a)%(b)+(b))%(b)
|
||||
|
||||
/**
|
||||
* Returns the modulous a result for b. Works for floating point numbers.
|
||||
* @param a Number to modulo against. (a % b)
|
||||
* @param b Number to modulo with. (a % b)
|
||||
* @returns The modulo result.
|
||||
*/
|
||||
#define mathModFloat(a, b) (float)fmod(a, b)
|
||||
|
||||
/**
|
||||
* Returns the maximum of two numbers.
|
||||
* @param a Number A.
|
||||
* @param b Number B.
|
||||
* @returns B if greater than A, otherwise B.
|
||||
*/
|
||||
#define mathMax(a,b) (a<b?b:a)
|
||||
|
||||
/**
|
||||
* Returns the minimum of two numbers.
|
||||
* @param a Number A.
|
||||
* @param b Number B.
|
||||
* @returns B if smaller than A, otherwise A.
|
||||
*/
|
||||
#define mathMin(a,b) (a>b?b:a)
|
||||
|
||||
/**
|
||||
* Clamp a number between two numbers.
|
||||
* @param val Value to clamp.
|
||||
* @param min The minimum number to clamp to.
|
||||
* @param max The maximum number to clamp to.
|
||||
*/
|
||||
#define mathClamp(val,min,max) mathMin(mathMax(val,min),max)
|
||||
|
||||
/**
|
||||
* Returns the absolute value. This will make -n equal n and y equal y.
|
||||
* @param n Number to abs.
|
||||
* @returns The absolute number.
|
||||
*/
|
||||
#define mathAbs(n) (n<0?-n:n)
|
||||
|
||||
/**
|
||||
* Convert degrees to radians.
|
||||
* @param n Degrees to convert.
|
||||
* @returns The number in radians.
|
||||
*/
|
||||
#define mathDeg2Rad(n) (n * MATH_PI / 180.0f)
|
||||
|
||||
/**
|
||||
* Convert radians to degrees.
|
||||
* @param n Radians to convert.
|
||||
* @returns The number in degrees.
|
||||
*/
|
||||
#define mathRad2Deg(n) (n * 180.0f / MATH_PI)
|
||||
|
||||
/**
|
||||
* Sign the given number, essentially clamps > 0 to 1, and < 0 to -1.
|
||||
* @param n Number to sign.
|
||||
* @returns The signed number.
|
||||
*/
|
||||
#define mathSign(n) (n>=0 ? 1.0f : -1.0f)
|
||||
|
||||
/**
|
||||
* Round a number down to the nearest whole number.
|
||||
* @param n Floating number to round down.
|
||||
* @returns The floor of the input.
|
||||
*/
|
||||
#define mathFloor(n) (floorf(n))
|
@ -1,23 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "mem.h"
|
||||
|
||||
void * memBufferResize(void *oldBuffer, int32_t oldSize, int32_t newSize,
|
||||
size_t size
|
||||
) {
|
||||
// Malloc a new buffer
|
||||
void *newBuffer = malloc(size * newSize);
|
||||
|
||||
// Copy old data
|
||||
memcpy(newBuffer, oldBuffer, size * oldSize);
|
||||
|
||||
// Clear old buffer
|
||||
free(oldBuffer);
|
||||
|
||||
return newBuffer;
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
|
||||
/**
|
||||
* Resizes a buffer to hold new amounts of data. Essentially a 3 step process of
|
||||
* - Malloc (new buffer)
|
||||
* - Memcpy (from old buffer)
|
||||
* - Free (old buffer)
|
||||
* - Return (new buffer)
|
||||
*
|
||||
* @param oldBuffer The old buffer.
|
||||
* @param oldSize The current size of the old buffer.
|
||||
* @param newSize Size of the new buffer.
|
||||
* @param size Size of the elements within the buffer.
|
||||
* @return The new buffer.
|
||||
*/
|
||||
void * memBufferResize(void *oldBuffer, int32_t oldSize, int32_t newSize,
|
||||
size_t size
|
||||
);
|
@ -1,56 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "math.h"
|
||||
|
||||
/**
|
||||
* Seed the random number generator
|
||||
* @param seed Seed to use for the seeded random number generator.
|
||||
*/
|
||||
#define randSeed(seed) (srand(seed))
|
||||
|
||||
/**
|
||||
* Generates a random int32_t.
|
||||
* @returns A random int32_t number.
|
||||
*/
|
||||
#define randInt32() ((int32_t)rand())
|
||||
|
||||
/**
|
||||
* Generates a random floating point number.
|
||||
* @returns A random floating point number.
|
||||
*/
|
||||
#define randFloat() (((float)randInt32()) * MATH_PI)
|
||||
|
||||
/**
|
||||
* Generates a random uint32_t
|
||||
* @returns A random uint32_t number.
|
||||
*/
|
||||
#define randUint32() (uint32_t)randInt32()
|
||||
|
||||
/**
|
||||
* Generates a random uint8_t
|
||||
* @returns A random uint8_t number.
|
||||
*/
|
||||
#define randUint8() (uint8_t)randInt32()
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Clamps a random number generation.
|
||||
* @param n Number that has been generated from the random.
|
||||
* @param min Minimum value to generate from. (Inclusive)
|
||||
* @param max Maximum value to generate to. (Exclusive)
|
||||
* @return Random number between min and max.
|
||||
*/
|
||||
#define randRange(n, min, max) (mathMod(n, (max-min)) + min)
|
||||
|
||||
#define randInt32Range(min, max) randRange(randInt32(), min, max)
|
||||
#define randFloatRange(min, max) ((float)fmod(randFloat(), max- min) + min)
|
||||
#define randUint32Range(min, max) randRange(randUint32(), min, max)
|
||||
#define randUint8Range(min, max) randRange(randUint8(), min, max)
|
@ -1,119 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "string.h"
|
||||
|
||||
int32_t stringHandlebarsBuffer(char *source,
|
||||
stringhandlebarvariable_t *variables, int32_t variableCount, char *buffer
|
||||
) {
|
||||
|
||||
int32_t i, l, n;
|
||||
char c;
|
||||
char keyBuffer[STRING_HANDLEBAR_KEY_MAXLENGTH];
|
||||
stringhandlebarvariable_t *variable;
|
||||
|
||||
// Start two counters. I holds source index, L holds target index.
|
||||
i = 0;
|
||||
l = 0;
|
||||
|
||||
while(true) {
|
||||
c = source[i];
|
||||
if(c == '\0') break;// Break on end of string.
|
||||
|
||||
// Look for {{, if not {{ then just treat as normal string.
|
||||
if(c != '{' || source[i + 1] != '{') {
|
||||
buffer[l] = c;
|
||||
i++;
|
||||
l++;
|
||||
continue;
|
||||
}
|
||||
|
||||
//Ignore those two chars
|
||||
i += 2;
|
||||
|
||||
// Skip space(s)
|
||||
while(true) {
|
||||
c = source[i];
|
||||
if(c != ' ') break;
|
||||
i++;
|
||||
}
|
||||
|
||||
// Get the key name
|
||||
n = 0;// Will hold the index within keyBuffer to copy chars into.
|
||||
while(true) {
|
||||
c = source[i];
|
||||
|
||||
// Don't overflow
|
||||
if(c == '\0') break;
|
||||
|
||||
// Skip spaces
|
||||
if(c == ' ') {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Handle end of key
|
||||
if(c == '}') {
|
||||
i++;
|
||||
if(source[i] == '}') i++;// For }} then skip the second }
|
||||
break;
|
||||
}
|
||||
|
||||
// Add to buffer.
|
||||
keyBuffer[n] = c;
|
||||
n++;
|
||||
i++;
|
||||
}
|
||||
|
||||
// Seal the keyBuffer string
|
||||
keyBuffer[n] = '\0';
|
||||
|
||||
// Now check each variable for a match.
|
||||
for(n = 0; n < variableCount; n++) {
|
||||
variable = variables + n;
|
||||
if(strcmp(keyBuffer, variable->key) != 0) continue;// Does the key match?
|
||||
|
||||
// Begin copying the variables' value into the string.
|
||||
n = 0;
|
||||
while(true) {
|
||||
if(variable->value[n] == '\0') break;// Handle end of variable value
|
||||
buffer[l] = variable->value[n];
|
||||
l++;
|
||||
n++;
|
||||
}
|
||||
// We found the value, so break.
|
||||
break;
|
||||
}
|
||||
|
||||
// Continue looking for next {{ variable }}...
|
||||
}
|
||||
|
||||
// Buffer has been fully cloned, seal the string.
|
||||
buffer[l] = '\0';
|
||||
|
||||
// Return the count of chars we wrote to the buffer. -1 due to null term.
|
||||
return l - 1;
|
||||
}
|
||||
|
||||
void stringStackInit(stringstack_t *stack) {
|
||||
stack->size = 0;
|
||||
}
|
||||
|
||||
void stringStackPush(stringstack_t *stack, char *string) {
|
||||
size_t offset = stack->size * STRING_STACK_STRING_SIZE;
|
||||
arrayCopy(
|
||||
sizeof(char), string,
|
||||
(int32_t)strlen(string) + 1,
|
||||
stack->strings + offset
|
||||
);
|
||||
stack->strings[stack->size] = stack->buffer + offset;
|
||||
stack->size++;
|
||||
}
|
||||
|
||||
void stringStackPop(stringstack_t *stack) {
|
||||
stack->size -= 1;
|
||||
}
|
@ -1,69 +0,0 @@
|
||||
// Copyright (c) 2021 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "array.h"
|
||||
|
||||
#define STRING_HANDLEBAR_KEY_MAXLENGTH 32
|
||||
|
||||
#define STRING_STACK_STRING_SIZE 256
|
||||
#define STRING_STACK_STRING_COUNT 128
|
||||
#define STRING_STACK_BUFFER STRING_STACK_STRING_SIZE * STRING_STACK_STRING_COUNT
|
||||
|
||||
/** Representation of a String Handlebar Variable */
|
||||
typedef struct {
|
||||
/** The key to use to replace in the source */
|
||||
char *key;
|
||||
/** The value to replace it with */
|
||||
char *value;
|
||||
} stringhandlebarvariable_t;
|
||||
|
||||
/** Definition of a string stack to push and pop strings from. */
|
||||
typedef struct {
|
||||
/** Raw char buffer, for holding strings */
|
||||
char buffer[STRING_STACK_BUFFER];
|
||||
/** Strings themselves */
|
||||
char *strings[STRING_STACK_STRING_COUNT];
|
||||
/** How many strings are on the stack */
|
||||
int32_t size;
|
||||
} stringstack_t;
|
||||
|
||||
/**
|
||||
* Replaces handlebars within the string with items from the list of variables.
|
||||
* Output of replacement is stored in the buffer provided. Handlebars within the
|
||||
* string should be in the format of; "{{ key }}", spaces within the bars will
|
||||
* be skipped. Variable keys cannot exceed STRING_HANDLEBAR_KEY_MAXLENGTH in
|
||||
* length.
|
||||
*
|
||||
* @param string String to parse.
|
||||
* @param variables Variables to use in the parsing.
|
||||
* @param variableCount How many variables in the array.
|
||||
* @param buffer Buffer to write characters in to.
|
||||
* @return The count of characters that were written to the buffer.
|
||||
*/
|
||||
int32_t stringHandlebarsBuffer(char *source,
|
||||
stringhandlebarvariable_t *variables, int32_t variableCount, char *buffer
|
||||
);
|
||||
|
||||
/**
|
||||
* Initializes a string stack for pushing strings on to.
|
||||
* @param stack Stack to initialize.
|
||||
*/
|
||||
void stringStackInit(stringstack_t *stack);
|
||||
|
||||
/**
|
||||
* Push a string onto the stack. The source string will be copied so you can
|
||||
* change the original pointer after this push operation.
|
||||
* @param stack Stack to push onto.
|
||||
* @param string String to copy and push.
|
||||
*/
|
||||
void stringStackPush(stringstack_t *stack, char *string);
|
||||
|
||||
/**
|
||||
* Pop an element off the end of a string stack.
|
||||
* @param stack Stack to pop from.
|
||||
*/
|
||||
void stringStackPop(stringstack_t *stack);
|
Reference in New Issue
Block a user