config and string reworked, hashtable updated to use arc_errno

This commit is contained in:
herbglitch 2023-01-17 01:59:08 -07:00
parent 0bbce28469
commit f8d987da8e
12 changed files with 1121 additions and 657 deletions

View file

@ -13,6 +13,11 @@ extern "C" {
*/
typedef struct ARC_ECS ARC_ECS;
/**
* @brief entity type for ARC_ECS
*/
typedef uint64_t ARC_ECSEntity;
/**
* @brief creates ARC_Config type
*

View file

@ -3,6 +3,9 @@
#include <stdint.h>
/**
* @brief a type that holds an array of data and its size
*/
typedef struct ARC_Array {
uint32_t *size;
void *data;

View file

@ -9,10 +9,6 @@ extern "C" {
#include "arc/std/string.h"
#include <stdint.h>
#ifndef ARC_HOME_PATH
#define ARC_HOME_PATH "./res/"
#endif //ARC_HOME_PATH
#define ARC_KEY_BUCKET_SIZE 0x20
#define ARC_GROUP_BUCKET_SIZE 0x20
#define ARC_GROUP_DATA_BUCKET_SIZE 0x20
@ -25,56 +21,85 @@ typedef struct ARC_Config ARC_Config;
/**
* @brief a function to read a key from string to a ARC_ConfigTypeTemplate
*
* @param config ARC_Config to store data to
* @param data string of what is to be read in
* @param subdata location of stubstring in data for what is to be read in
* @param value value of read in variable
* @param config ARC_Config to store data to
* @param string ARC_String of data that is being read in
* @param value value that is read in
*
* @note use ARC_Config_StoreValue(ARC_Config *config, ARC_String *name, void *value); to store a value to the config
* if there is an error, set arc_errno
*
* @return 0 if value not a reference, 1 if value is a reference
*/
typedef int32_t (* ARC_ConfigKeyRead)(ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value);
typedef uint8_t (* ARC_ConfigKeyRead)(ARC_Config* config, ARC_String *string, void **value);
/**
* @brief a function to delete a value from a key in ARC_Config
*
* @param config ARC_Config that can be used to check for references in data
* @param data string of what is going to be deleted (used to check if value is a reference)
* @param subdata location of substring in data for what is going to be deleted (used to check if value is a reference)
* @param value pointer of data to be deleted
*
* @note this function can be NULL if memory does not need to be cleaned for this type
* if there is an error, set arc_errno
*/
typedef int32_t (* ARC_ConfigKeyDelete)(ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value);
typedef void (* ARC_ConfigKeyDelete)(ARC_Config* config, void *value);
/**
* @brief adds a usable key to ARC_Config
*
* @param config ARC_Config we are adding keys to
* @param config ARC_Config to add keys to
* @param type string of key type
* @param keyRead function for reading/creating key from string
* @param keyDelete function for deleting stored key
*
* @return 0 on sucess, ARC_ERRNO_ on fail
*/
int32_t ARC_ConfigKey_Add(ARC_Config *config, char *type, ARC_ConfigKeyRead keyRead, ARC_ConfigKeyDelete keyDelete);
void ARC_Config_AddKey(ARC_Config *config, ARC_String *type, ARC_ConfigKeyRead keyRead, ARC_ConfigKeyDelete keyDelete);
/**
* @brief adds a key from a cstring
* @param config ARC_Config to add keys to
* @param type cstring of key type
* @param length length of cstring
* @param keyRead function for reading/creating key from string
* @param keyDelete function for deleting stored key
*/
void ARC_Config_AddKeyCString(ARC_Config *config, const char *type, uint64_t length, ARC_ConfigKeyRead keyRead, ARC_ConfigKeyDelete keyDelete);
/**
* @brief external callback to add keys to config
*/
typedef int32_t (* ARC_ConfigKey_AddFunc)(ARC_Config *config);
typedef void (* ARC_ConfigKey_AddFunc)(ARC_Config *config);
/**
* @brief creates ARC_Config type
*
* @param config ARC_Config we are initializing
* @param keysAdd callback to add ConfigKeys to config->keys, can be NULL
*
* @return 0 on sucess, ARC_ERRNO_ on fail
* @param config ARC_Config to initialize
*/
int32_t ARC_Config_Create(ARC_Config **config, ARC_ConfigKey_AddFunc keysAdd);
void ARC_Config_Create(ARC_Config **config);
/**
* @brief destroys ARC_Config type
*
* @return 0 on sucess, ARC_ERRNO_ on fail
*/
int32_t ARC_Config_Destroy(ARC_Config *config);
void ARC_Config_Destroy(ARC_Config *config);
/**
* @brief sets current group in config
*
* @note ARC_Config_Get will use this set group
*
* @param config ARC_Config we are setting current group in
* @param groupname name of group that will be set
*/
void ARC_Config_SetGroup(ARC_Config *config, ARC_String *groupname);
/**
* @brief get a value from a given keyname
*
* @note name may be prefaced with <group>:: to specify group
*
* @param config ARC_Config to get value from
* @param keyname name of key to get from config
* @param value data retrieved from config
*/
void ARC_Config_Get(ARC_Config *config, ARC_String *keyname, void **value);
/**
* @brief commands that can be used in ARC_Config_FileIO
@ -87,48 +112,8 @@ int32_t ARC_Config_Destroy(ARC_Config *config);
*
* @param config ARC_Config where io operations will take place
* @param path file path for io
*
* @return 0 on sucess, ARC_ERRNO_ on fail
*/
int32_t ARC_Config_FileIO(ARC_Config *config, const char *path, uint8_t command);
/**
* @brief sets current group in config
*
* @note ARC_Config_Get will use this set group
*
* @param config ARC_Config we are setting current group in
* @param groupname name of group that will be set
*
* @return 0 on sucess, ARC_ERRNO_ on fail
*/
int32_t ARC_Config_SetGroup(ARC_Config *config, char *groupname);
/**
* @brief get a value from a given keyname
*
* @note name may be prefaced with <group>:: to specify group
*
* @param config ARC_Config to get value from
* @param keyname name of key to get from config
* @param value data retrieved from config
*
* @return 0 on sucess, ARC_ERRNO_ on fail
*/
int32_t ARC_Config_Get(ARC_Config *config, char *keyname, void **value);
/**
* @brief get a reference value from a given substring
*
* @note this function is meant to help with creation and deletion functions for types
*
* @param config ARC_Config to get value from
* @param data string that holds the substring that will be used
* @param subdata location of stubstring in data for what is to be read in
*
* @return a valid pointer on sucess, NULL on fail
*/
void *ARC_Config_GetReference(ARC_Config *config, char *data, ARC_StringSubstr *subdata);
void ARC_Config_FileIO(ARC_Config *config, ARC_String *path, uint8_t command);
#ifdef __cplusplus
}

View file

@ -11,35 +11,35 @@ extern "C" {
#include "arc/std/string.h"
typedef struct ARC_Config ARC_Config;
int32_t ARC_Defaults_ConfigKey_Create(ARC_Config *config);
void ARC_Defaults_ConfigKey_Create(ARC_Config *config);
int32_t ARC_ConfigKey_Read_Uint8_t (ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value);
int32_t ARC_ConfigKey_Read_Int8_t (ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value);
//int32_t ARC_ConfigKey_Read_Char (ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value);
int32_t ARC_ConfigKey_Read_Uint16_t (ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value);
int32_t ARC_ConfigKey_Read_Int16_t (ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value);
int32_t ARC_ConfigKey_Read_Uint32_t (ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value);
int32_t ARC_ConfigKey_Read_Int32_t (ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value);
int32_t ARC_ConfigKey_Read_Int (ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value);
int32_t ARC_ConfigKey_Read_Uint64_t (ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value);
int32_t ARC_ConfigKey_Read_Int64_t (ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value);
int32_t ARC_ConfigKey_Read_Long (ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value);
int32_t ARC_ConfigKey_Read_String (ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value);
int32_t ARC_ConfigKey_Read_StringArray(ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value);
uint8_t ARC_ConfigKey_Read_Uint8_t (ARC_Config *config, ARC_String *string, void **value);
uint8_t ARC_ConfigKey_Read_Int8_t (ARC_Config *config, ARC_String *string, void **value);
uint8_t ARC_ConfigKey_Read_Char (ARC_Config *config, ARC_String *string, void **value);
uint8_t ARC_ConfigKey_Read_Uint16_t (ARC_Config *config, ARC_String *string, void **value);
uint8_t ARC_ConfigKey_Read_Int16_t (ARC_Config *config, ARC_String *string, void **value);
uint8_t ARC_ConfigKey_Read_Uint32_t (ARC_Config *config, ARC_String *string, void **value);
uint8_t ARC_ConfigKey_Read_Int32_t (ARC_Config *config, ARC_String *string, void **value);
uint8_t ARC_ConfigKey_Read_Int (ARC_Config *config, ARC_String *string, void **value);
uint8_t ARC_ConfigKey_Read_Uint64_t (ARC_Config *config, ARC_String *string, void **value);
uint8_t ARC_ConfigKey_Read_Int64_t (ARC_Config *config, ARC_String *string, void **value);
uint8_t ARC_ConfigKey_Read_Long (ARC_Config *config, ARC_String *string, void **value);
uint8_t ARC_ConfigKey_Read_String (ARC_Config *config, ARC_String *string, void **value);
uint8_t ARC_ConfigKey_Read_StringArray(ARC_Config *config, ARC_String *string, void **value);
int32_t ARC_ConfigKey_Delete_Uint8_t (ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value);
int32_t ARC_ConfigKey_Delete_Int8_t (ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value);
int32_t ARC_ConfigKey_Delete_Char (ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value);
int32_t ARC_ConfigKey_Delete_Uint16_t (ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value);
int32_t ARC_ConfigKey_Delete_Int16_t (ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value);
int32_t ARC_ConfigKey_Delete_Uint32_t (ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value);
int32_t ARC_ConfigKey_Delete_Int32_t (ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value);
int32_t ARC_ConfigKey_Delete_Int (ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value);
int32_t ARC_ConfigKey_Delete_Uint64_t (ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value);
int32_t ARC_ConfigKey_Delete_Int64_t (ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value);
int32_t ARC_ConfigKey_Delete_Long (ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value);
int32_t ARC_ConfigKey_Delete_String (ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value);
int32_t ARC_ConfigKey_Delete_StringArray(ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value);
void ARC_ConfigKey_Delete_Uint8_t (ARC_Config *config, void *value);
void ARC_ConfigKey_Delete_Int8_t (ARC_Config *config, void *value);
void ARC_ConfigKey_Delete_Char (ARC_Config *config, void *value);
void ARC_ConfigKey_Delete_Uint16_t (ARC_Config *config, void *value);
void ARC_ConfigKey_Delete_Int16_t (ARC_Config *config, void *value);
void ARC_ConfigKey_Delete_Uint32_t (ARC_Config *config, void *value);
void ARC_ConfigKey_Delete_Int32_t (ARC_Config *config, void *value);
void ARC_ConfigKey_Delete_Int (ARC_Config *config, void *value);
void ARC_ConfigKey_Delete_Uint64_t (ARC_Config *config, void *value);
void ARC_ConfigKey_Delete_Int64_t (ARC_Config *config, void *value);
void ARC_ConfigKey_Delete_Long (ARC_Config *config, void *value);
void ARC_ConfigKey_Delete_String (ARC_Config *config, void *value);
void ARC_ConfigKey_Delete_StringArray(ARC_Config *config, void *value);
#ifdef __cplusplus
}

View file

@ -31,10 +31,8 @@ struct ARC_HashtableNode {
* @param key value to hash
* @param keysize should be sizeof(key) before key is a void ptr
* @param hashval value of hash, does not need to be within range of buckets
*
* @return 0 on sucess, ARC_ERRNO_ on fail
*/
typedef int32_t (* ARC_Hashtable_Hash)(void *key, size_t *keysize, uint32_t *hashval);
typedef void (* ARC_Hashtable_Hash)(void *key, size_t *keysize, uint32_t *hashval);
/**
* @brief key comparison function ptr
@ -42,19 +40,17 @@ typedef int32_t (* ARC_Hashtable_Hash)(void *key, size_t *keysize, uint32_t *has
* @param key1 first key
* @param key2 second key
*
* @return 0 on sucess
* @return 0 when keys match
*/
typedef int32_t (* ARC_Hashtable_KeyCompare)(void *key1, size_t *key1size, void *key2, size_t *key2size);
typedef int8_t (* ARC_Hashtable_KeyCompare)(void *key1, size_t *key1size, void *key2, size_t *key2size);
/**
* @brief callback to allow memory freeing of nodes
*
* @param node node to be destroyed
* @param userdata any data the user wants to access in the callback
*
* @return 0 on sucess, ARC_ERRNO_ on fail
*/
typedef int32_t (* ARC_HashtableNode_DestroyExternal)(ARC_HashtableNode *node, void *userdata);
typedef void (* ARC_HashtableNode_DestroyExternal)(ARC_HashtableNode *node, void *userdata);
/**
* @brief cteates ARC_Hashtable type
@ -72,10 +68,8 @@ void ARC_Hashtable_Create(ARC_Hashtable **htable, uint32_t bucketsize, ARC_Hasht
* @param htable htable that will be destroyed
* @param external function to allow external freeing of nodes, can be NULL
* @param userdata any data the user wants access to in the callback
*
* @return 0 on sucess, ARC_ERRNO_ on fail
*/
int32_t ARC_Hashtable_Destroy(ARC_Hashtable *htable, ARC_HashtableNode_DestroyExternal external, void *userdata);
void ARC_Hashtable_Destroy(ARC_Hashtable *htable, ARC_HashtableNode_DestroyExternal external, void *userdata);
/**
* @brief adds value to hastable
@ -84,10 +78,8 @@ int32_t ARC_Hashtable_Destroy(ARC_Hashtable *htable, ARC_HashtableNode_DestroyEx
* @param key key for node that is being added
* @param keysize sizeof key before it is passed into a void *
* @param data data for node that is being added
*
* @return 0 on sucess, ARC_ERRNO_ on fail
*/
int32_t ARC_Hashtable_Add(ARC_Hashtable *htable, void *key, size_t keysize, void *data);
void ARC_Hashtable_Add(ARC_Hashtable *htable, void *key, size_t keysize, void *data);
/**
* @brief gets value from hashtable by key
@ -96,10 +88,8 @@ int32_t ARC_Hashtable_Add(ARC_Hashtable *htable, void *key, size_t keysize, void
* @param key key to get value from table
* @param keysize sizeof key before it is passed into a void *
* @param data data retrieved from table
*
* @return 0 on sucess, ARC_ERRNO_ on fail
*/
int32_t ARC_Hashtable_Get(ARC_Hashtable *htable, void *key, size_t keysize, void **data);
void ARC_Hashtable_Get(ARC_Hashtable *htable, void *key, size_t keysize, void **data);
/**
* @brief removes value from hashtable
@ -109,10 +99,8 @@ int32_t ARC_Hashtable_Get(ARC_Hashtable *htable, void *key, size_t keysize, void
* @param keysize sizeof key before it is passed into a void *
* @param external function to allow external freeing of data, can be NULL
* @param userdata any data the user wants access to in the callback
*
* @return 0 on sucess, ARC_ERRNO_ on fail
*/
int32_t ARC_Hashtable_Remove(ARC_Hashtable *htable, void *key, size_t keysize, ARC_HashtableNode_DestroyExternal external, void *userdata);
void ARC_Hashtable_Remove(ARC_Hashtable *htable, void *key, size_t keysize, ARC_HashtableNode_DestroyExternal external, void *userdata);
#ifdef __cplusplus
}

View file

@ -6,6 +6,7 @@ extern "C" {
#endif
#include <stdint.h>
#include "arc/std/string.h"
/**
* @brief get string and size from file
@ -13,11 +14,8 @@ extern "C" {
* @param path a string to path of target file
* @param data pointer to where string will be created
* this will need to be freed once done using it
* @param size size of string
*
* @return int 0 on success, ARC_ERRNO_ on fail
*/
int32_t ARC_IO_FileToStr(const char *path, char **data, uint64_t *size);
void ARC_IO_FileToStr(ARC_String *path, ARC_String **data);
#ifdef __cplusplus
}

View file

@ -10,92 +10,168 @@ extern "C" {
/**
* @brief substring position within a string
*/
typedef struct ARC_StringSubstr {
uint64_t index;
uint64_t length;
} ARC_StringSubstr;
typedef struct ARC_String {
char *data;
uint64_t length;
} ARC_String;
/**
* @brief substring position within a string, stored as 4 bytes
*/
typedef struct ARC_StringSubstr32_t {
uint32_t index;
uint32_t length;
} ARC_StringSubstr32_t;
* @brief creates ARC_String type
*
* @param string ARC_String to create
* @param data cstring that will be stored in ARC_String
* @param length length of ARC_String
*/
void ARC_String_Create(ARC_String **string, char *data, uint64_t length);
/**
* @brief substring position within a string, stored as 1 byte
* @brief destroys ARC_String type
*
* @param string string that will be destroyed
*/
void ARC_String_Destroy(ARC_String *string);
/**
* @brief copy a ARC_String
*
* @param copy copy of oldString, will be set to NULL on error
* @param original original string that is being copied
*/
typedef struct ARC_StringSubstr8_t {
uint8_t index;
uint8_t length;
} ARC_StringSubstr8_t;
void ARC_String_Copy(ARC_String **copy, ARC_String *original);
/**
* @brief copy a subtring from a givin ARC_String
*
* @param substring new coppied substring, will be null on error
* @param original string to copy substring from
* @param start starting index in relation on original
* @param length length of substring that is being created
*/
void ARC_String_CopySubstring(ARC_String **substring, ARC_String *original, uint64_t start, uint64_t length);
/**
* @brief copy a subtring from a givin ARC_String
*
* @param newString string that doesn't have substring in it, will be null on error
* @param original string to remove substring from
* @param substring substring to remove
*/
void ARC_String_RemoveSubstring(ARC_String **newString, ARC_String *original, ARC_String *substring);
/**
* @brief checks if two strings are the same
*
* @param first string to check against second
* @param second string to check against first
*
* @return 1 if match, 0 if they don't match
*/
uint8_t ARC_String_Equals(ARC_String *first, ARC_String *second);
/**
* @brief check if ARC_String and cstring match
*
* @param string ARC_string to check
* @param cstring cstring to check
* @param length length of cstring
*/
uint8_t ARC_String_EqualsCString(ARC_String *string, const char *cstring, uint64_t length);
/**
* @brief checks if string is alphabetic
*
* @param val string to check
* @param length length of string to check
* @param string string to check
*
* @return 1 if alphabetic, 0 if not alphabetic
*/
uint8_t ARC_String_Alpha(char *val, uint64_t length);
uint8_t ARC_String_Alpha(ARC_String *string);
/**
* @brief converst substring from string to uint64_t
*
* @param data string to get substring from
* @param substr substring to convert to long
*
*
* @param string string to convert to long
*
* @return uint64_t converted number
*/
uint64_t ARC_String_ToUint64_t(const char *data, ARC_StringSubstr *substr);
uint64_t ARC_String_ToUint64_t(ARC_String *string);
/**
* @brief takes a given string, and assigns index and length for position of first matching substring
*
* @param data the string to find substring in
* @param substr the string to find
* @param index the index of substring within the string will be ~uint64_t if not found
* @param string the string that will be searched
* @param substr substring to find within string
*
* @return int ARC_ERRNO_ error code
* @return ~(uint64_t)0 on error, anything else on success
*/
int32_t ARC_String_Find(char *data, char *substr, uint64_t *index);
uint64_t ARC_String_Find(ARC_String *string, ARC_String *substring);
/**
* @brief takes given cstring and gives position of first matching
*
* @param string the string that will be searched
* @param cstring the cstring to find within string
* @param length the length of cstring
*
* @return ~(uint64_t)0 on error, anything else on success
*/
uint64_t ARC_String_FindCString(ARC_String *string, const char *cstring, uint64_t length);
/**
* @brief takes a given string, and assigns index and length for position of last matching substring
*
* @param data the string to find substring in
* @param substr the string to find
* @param index the index of substring within the string will be ~uint64_t if not found
* @param string the string that will be searched
* @param substr substring to find within string
*
* @return int ARC_ERRNO_ error code
* @return ~(uint64_t)0 on error, anything else on success
*/
int32_t ARC_String_FindBack(char *data, char *substr, uint64_t *index);
uint64_t ARC_String_FindBack(ARC_String *string, ARC_String *substring);
/**
* @brief strips the ends based on a given substing
* @brief strips the ends based on a given char
*
* @param data the string to find the substring in
* @param substr the substring to strip ends by, defaults to " " if NULL
* @param subdata the substring of data, will use given substring data, or strlen if length == 0
* also will hold the return values
*
* @return int ARC_ERRNO_ error code
* @param original the string which whill have the matching char stripped from
* @param stripped where to store the string which has witespace stripped
* will be null if there is an error
* @param charToStrip the char that will be stripped from the ends
*/
int32_t ARC_StringSubstr_StripEnds(char *data, char *substr, ARC_StringSubstr *subdata);
void ARC_String_StripEnds(ARC_String *original, ARC_String **stripped, char charToStrip);
/**
* @brief strips the ends based on a given substing
* @brief strips whitespace from a ARC_String
*
* @param data the string to find the substring in
* @param substr the substring to strip ends by, defaults to " " if NULL
* @param subdata the substring of data, will use given substring data, or strlen if length == 0
* also will hold the return values
*
* @return int ARC_ERRNO_ error code
* @param original the string which whill have whitespace stripped from
* @param stripped where to store the string which has witespace stripped
* will be null if there is an error
*/
int32_t ARC_StringSubstr_StripWhitespaceEnds(char *data, ARC_StringSubstr *subdata);
void ARC_String_StripWhitespace(ARC_String *original, ARC_String **stripped);
/**
* @brief strips the whitespace from the ends of a string
*
* @param original the string which whill have the whitespace stripped from its ends
* @param stripped where to store the string which has witespace stripped from the ends
* will be null if there is an error
*/
void ARC_String_StripEndsWhitespace(ARC_String *original, ARC_String **stripped);
/**
* @brief merges two strings together
*
* @param first first part of string to combine
* @param second second part of string to combine
* @param combined new ARC_String of combined strings frist + second
*/
void ARC_String_Merge(ARC_String *first, ARC_String *second, ARC_String **combined);
/**
* @brief copy a subtring from a givin ARC_String
*
* @param newString new string without specified section, will be NULL on error
* @param original string to remove section from
* @param removeIndex starting index in relation on original of what is to be removed
* @param removeLength length of section that is being removed
*/
void ARC_String_RemoveSection(ARC_String **newString, ARC_String *original, uint64_t removeIndex, uint64_t removeLength);
#ifdef __cplusplus
}