From 5e6ee54473cb286b57657326d4d8cedca0d51a15 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Fri, 20 Jan 2023 22:38:29 -0700 Subject: [PATCH] most config fixed, still need to do a bunch more testing --- include/arc/std/config.h | 2 +- include/arc/std/defaults/config.h | 36 +- include/arc/std/string.h | 28 +- src/graphics/sdl/config.c | 676 +++++++++++++++++------------- src/std/config.c | 83 ++-- src/std/defaults/config.c | 303 +++++++++---- src/std/string.c | 26 +- 7 files changed, 730 insertions(+), 424 deletions(-) diff --git a/include/arc/std/config.h b/include/arc/std/config.h index 69d0801..a7752a0 100644 --- a/include/arc/std/config.h +++ b/include/arc/std/config.h @@ -41,7 +41,7 @@ typedef uint8_t (* ARC_ConfigKeyRead)(ARC_Config* config, ARC_String *string, vo * @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 void (* ARC_ConfigKeyDelete)(ARC_Config* config, void *value); +typedef void (* ARC_ConfigKeyDelete)(ARC_Config* config, ARC_String *string, void *value); /** * @brief adds a usable key to ARC_Config diff --git a/include/arc/std/defaults/config.h b/include/arc/std/defaults/config.h index d830716..5e8b0e7 100644 --- a/include/arc/std/defaults/config.h +++ b/include/arc/std/defaults/config.h @@ -15,31 +15,35 @@ void ARC_Defaults_ConfigKey_Create(ARC_Config *config); 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_Char (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_Long (ARC_Config *config, ARC_String *string, void **value); +uint8_t ARC_ConfigKey_Read_Float (ARC_Config *config, ARC_String *string, void **value); +uint8_t ARC_ConfigKey_Read_Double (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); +// uint8_t ARC_ConfigKey_Read_StringArray(ARC_Config *config, ARC_String *string, 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); +void ARC_ConfigKey_Delete_Uint8_t (ARC_Config *config, ARC_String *string, void *value); +void ARC_ConfigKey_Delete_Int8_t (ARC_Config *config, ARC_String *string, void *value); +void ARC_ConfigKey_Delete_Uint16_t (ARC_Config *config, ARC_String *string, void *value); +void ARC_ConfigKey_Delete_Int16_t (ARC_Config *config, ARC_String *string, void *value); +void ARC_ConfigKey_Delete_Uint32_t (ARC_Config *config, ARC_String *string, void *value); +void ARC_ConfigKey_Delete_Int32_t (ARC_Config *config, ARC_String *string, void *value); +void ARC_ConfigKey_Delete_Uint64_t (ARC_Config *config, ARC_String *string, void *value); +void ARC_ConfigKey_Delete_Int64_t (ARC_Config *config, ARC_String *string, void *value); +void ARC_ConfigKey_Delete_Char (ARC_Config *config, ARC_String *string, void *value); +void ARC_ConfigKey_Delete_Int (ARC_Config *config, ARC_String *string, void *value); +void ARC_ConfigKey_Delete_Long (ARC_Config *config, ARC_String *string, void *value); +void ARC_ConfigKey_Delete_Float (ARC_Config *config, ARC_String *string, void *value); +void ARC_ConfigKey_Delete_Double (ARC_Config *config, ARC_String *string, void *value); +void ARC_ConfigKey_Delete_String (ARC_Config *config, ARC_String *string, void *value); +// void ARC_ConfigKey_Delete_StringArray(ARC_Config *config, ARC_String *string, void *value); #ifdef __cplusplus } diff --git a/include/arc/std/string.h b/include/arc/std/string.h index b68ac60..f17ad3b 100644 --- a/include/arc/std/string.h +++ b/include/arc/std/string.h @@ -24,6 +24,14 @@ typedef struct ARC_String { */ void ARC_String_Create(ARC_String **string, char *data, uint64_t length); +/** + * @brief creates ARC_String type with strinlen + * + * @param string ARC_String to create + * @param data cstring that will be stored in ARC_String + */ +void ARC_String_CreateWithStrlen(ARC_String **string, char *data); + /** * @brief destroys ARC_String type * @@ -89,12 +97,30 @@ uint8_t ARC_String_Alpha(ARC_String *string); /** * @brief converst substring from string to uint64_t * - * @param string string to convert to long + * @param string string to convert to uint64_t * * @return uint64_t converted number */ uint64_t ARC_String_ToUint64_t(ARC_String *string); +/** + * @brief converst substring from string to int64_t + * + * @param string string to convert to int64_t + * + * @return int64_t converted number +*/ +int64_t ARC_String_ToInt64_t(ARC_String *string); + +/** + * @brief converst substring from string to double + * + * @param string string to convert to double + * + * @return double converted number +*/ +double ARC_String_ToDouble(ARC_String *string); + /** * @brief takes a given string, and assigns index and length for position of first matching substring * diff --git a/src/graphics/sdl/config.c b/src/graphics/sdl/config.c index bdf825f..90e8223 100644 --- a/src/graphics/sdl/config.c +++ b/src/graphics/sdl/config.c @@ -19,28 +19,28 @@ SDL_Renderer *global_renderer; -int32_t ARC_Point_Read (ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value); -int32_t ARC_Rect_Read (ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value); -int32_t ARC_RectArray_Read (ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value); -int32_t ARC_SDL_Texture_Read(ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value); -int32_t ARC_Spritesheet_Read(ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value); -int32_t ARC_Sprite_Read (ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value); +uint8_t ARC_Point_Read (ARC_Config *config, ARC_String *string, void **value); +uint8_t ARC_Rect_Read (ARC_Config *config, ARC_String *string, void **value); +uint8_t ARC_RectArray_Read (ARC_Config *config, ARC_String *string, void **value); +uint8_t ARC_SDL_Texture_Read(ARC_Config *config, ARC_String *string, void **value); +uint8_t ARC_Spritesheet_Read(ARC_Config *config, ARC_String *string, void **value); +uint8_t ARC_Sprite_Read (ARC_Config *config, ARC_String *string, void **value); -int32_t ARC_Point_Delete (ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value); -int32_t ARC_Rect_Delete (ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value); -int32_t ARC_RectArray_Delete (ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value); -int32_t ARC_SDL_Texture_Delete(ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value); -int32_t ARC_Spritesheet_Delete(ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value); -int32_t ARC_Sprite_Delete (ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value); +void ARC_Point_Delete (ARC_Config *config, ARC_String *string, void *value); +void ARC_Rect_Delete (ARC_Config *config, ARC_String *string, void *value); +void ARC_RectArray_Delete (ARC_Config *config, ARC_String *string, void *value); +void ARC_SDL_Texture_Delete(ARC_Config *config, ARC_String *string, void *value); +void ARC_Spritesheet_Delete(ARC_Config *config, ARC_String *string, void *value); +void ARC_Sprite_Delete (ARC_Config *config, ARC_String *string, void *value); void ARC_GraphicsConfig_Init(ARC_Config *config, ARC_Renderer *renderer){ global_renderer = renderer->renderer; - ARC_ConfigKey_Add(config, (char *)"ARC_Point" , (ARC_ConfigKeyRead) ARC_Point_Read , (ARC_ConfigKeyDelete) ARC_Point_Delete ); - ARC_ConfigKey_Add(config, (char *)"ARC_Rect" , (ARC_ConfigKeyRead) ARC_Rect_Read , (ARC_ConfigKeyDelete) ARC_Rect_Delete ); - ARC_ConfigKey_Add(config, (char *)"ARC_Rect[]" , (ARC_ConfigKeyRead) ARC_RectArray_Read , (ARC_ConfigKeyDelete) ARC_RectArray_Delete ); - ARC_ConfigKey_Add(config, (char *)"SDL_Texture" , (ARC_ConfigKeyRead) ARC_SDL_Texture_Read, (ARC_ConfigKeyDelete) ARC_SDL_Texture_Delete); - ARC_ConfigKey_Add(config, (char *)"ARC_Spritesheet", (ARC_ConfigKeyRead) ARC_Spritesheet_Read, (ARC_ConfigKeyDelete) ARC_Spritesheet_Delete); - ARC_ConfigKey_Add(config, (char *)"ARC_Sprite" , (ARC_ConfigKeyRead) ARC_Sprite_Read , (ARC_ConfigKeyDelete) ARC_Sprite_Delete ); + ARC_Config_AddKeyCString(config, (char *)"ARC_Point" , 9, ARC_Point_Read , ARC_Point_Delete ); + ARC_Config_AddKeyCString(config, (char *)"ARC_Rect" , 8, ARC_Rect_Read , ARC_Rect_Delete ); + ARC_Config_AddKeyCString(config, (char *)"ARC_Rect[]" , 10, ARC_RectArray_Read , ARC_RectArray_Delete ); + ARC_Config_AddKeyCString(config, (char *)"SDL_Texture" , 11, ARC_SDL_Texture_Read, ARC_SDL_Texture_Delete); + ARC_Config_AddKeyCString(config, (char *)"ARC_Spritesheet", 15, ARC_Spritesheet_Read, ARC_Spritesheet_Delete); + ARC_Config_AddKeyCString(config, (char *)"ARC_Sprite" , 10, ARC_Sprite_Read , ARC_Sprite_Delete ); } int32_t ARC_SDL_Texture_Load(const char *path, SDL_Texture **texture){ @@ -59,100 +59,209 @@ int32_t ARC_SDL_Texture_Load(const char *path, SDL_Texture **texture){ return 0; } -int32_t ARC_Point_Read(ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value){ - ARC_StringSubstr_StripWhitespaceEnds((char *)data, subdata); - if(data[subdata->index] != '{' || data[subdata->index + subdata->length - 1] != '}'){ return ARC_ERRNO_DATA; } - subdata->index++; - subdata->length -= 2; +uint64_t getIndexAndErrorCheck(ARC_String *string, char *search, uint64_t searchLength){ + uint64_t separator = ARC_String_FindCString(string, ",", 1); - uint64_t split; - *value = malloc(sizeof(ARC_Rect)); + if(separator == ~(uint64_t)0){ + arc_errno = ARC_ERRNO_DATA; + } - //x - int32_t err = ARC_String_Find(((char *)data) + subdata->index, (char *)",", &split); - if(err){ return err; } - if(split == ~((uint64_t)0) || split > subdata->length){ return ARC_ERRNO_DATA; } - ARC_StringSubstr temp = { subdata->index, split }; - ((SDL_Point *) *value)->x = (int)ARC_String_ToUint64_t(data, &temp); + return separator; +} - //y - temp = (ARC_StringSubstr){ temp.index + split + 1, subdata->length - split - 1 }; - ((SDL_Point *) *value)->y = (int)ARC_String_ToUint64_t(data, &temp); +uint8_t ARC_Point_Read(ARC_Config *config, ARC_String *string, void **value){ + ARC_Config_Get(config, string, value); + if(*value){ + return 1; + } + if(string->data[0] != '{' || string->data[string->length - 1] != '}'){ + arc_errno = ARC_ERRNO_DATA; + return 0; + } + + uint64_t separator = getIndexAndErrorCheck(string, ",", 1); + if(arc_errno){ + return 0; + } + + ARC_String *xString, *yString; + ARC_String_CopySubstring(&xString, string, 1 , separator - 1 ); + ARC_String_CopySubstring(&yString, string, separator + 1, string->length - (separator + 2)); + + SDL_Point *point = (SDL_Point *)malloc(sizeof(SDL_Point)); + point->x = (int32_t)ARC_String_ToInt64_t(xString); + point->y = (int32_t)ARC_String_ToInt64_t(yString); + + ARC_String_Destroy(xString); + ARC_String_Destroy(yString); + + *value = point; return 0; } -int32_t ARC_Rect_Read(ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value){ - ARC_StringSubstr_StripWhitespaceEnds((char *)data, subdata); - if(data[subdata->index] != '{' || data[subdata->index + subdata->length - 1] != '}'){ return ARC_ERRNO_DATA; } - subdata->index++; - subdata->length -= 2; +uint8_t ARC_Rect_Read(ARC_Config *config, ARC_String *string, void **value){ + ARC_Config_Get(config, string, value); + if(*value){ + return 1; + } - uint64_t split; - *value = malloc(sizeof(ARC_Rect)); + if(string->data[0] != '{' || string->data[string->length - 1] != '}'){ + arc_errno = ARC_ERRNO_DATA; + return 0; + } + + ARC_String *current; + ARC_String_CopySubstring(¤t, string, 1, string->length - 2); + + ARC_String *temp, *tempStripped; + int32_t x, y, w, h; + int64_t separator; //x - int32_t err = ARC_String_Find(((char *)data) + subdata->index, (char *)",", &split); - if(err){ return err; } - if(split == ~((uint64_t)0) || split > subdata->length){ return ARC_ERRNO_DATA; } - ARC_StringSubstr temp = { subdata->index, split }; - ((ARC_Rect *) *value)->x = (int)ARC_String_ToUint64_t(data, &temp); - int32_t ttt = ((ARC_Rect *) *value)->x; + separator = getIndexAndErrorCheck(current, ",", 1); + if(arc_errno){ + return 0; + } + + ARC_String_CopySubstring(&temp, current, 0, separator - 1); + ARC_String_StripEndsWhitespace(temp, &tempStripped); + x = ARC_String_ToInt64_t(tempStripped); + ARC_String_Destroy(temp); + ARC_String_Destroy(tempStripped); + + temp = current; + ARC_String_CopySubstring(¤t, temp, separator + 1, temp->length - (separator + 1)); + ARC_String_Destroy(temp); //y - temp.index = subdata->index + split + 1; - err = ARC_String_Find(((char *)data) + temp.index, (char *)",", &split); - if(err){ return err; } - if(split == ~((uint64_t)0) || split > subdata->length){ return ARC_ERRNO_DATA; } - temp.length = split; - ((ARC_Rect *) *value)->y = (int)ARC_String_ToUint64_t(data, &temp); - ttt = ((ARC_Rect *) *value)->y; + separator = getIndexAndErrorCheck(current, ",", 1); + if(arc_errno){ + return 0; + } + + ARC_String_CopySubstring(&temp, current, 0, separator - 1); + ARC_String_StripEndsWhitespace(temp, &tempStripped); + y = ARC_String_ToInt64_t(tempStripped); + ARC_String_Destroy(temp); + ARC_String_Destroy(tempStripped); + + temp = current; + ARC_String_CopySubstring(¤t, temp, separator + 1, temp->length - (separator + 1)); + ARC_String_Destroy(temp); //w - temp.index = temp.index + split + 1; - err = ARC_String_Find(((char *)data) + temp.index, (char *)",", &split); - if(err){ return err; } - if(split == ~((uint64_t)0) || split > subdata->length){ return ARC_ERRNO_DATA; } - temp.length = split; - ((ARC_Rect *) *value)->w = (int)ARC_String_ToUint64_t(data, &temp); - ttt = ((ARC_Rect *) *value)->w; + separator = getIndexAndErrorCheck(current, ",", 1); + if(arc_errno){ + return 0; + } + + ARC_String_CopySubstring(&temp, current, 0, separator - 1); + ARC_String_StripEndsWhitespace(temp, &tempStripped); + w = ARC_String_ToInt64_t(tempStripped); + ARC_String_Destroy(temp); + ARC_String_Destroy(tempStripped); + + temp = current; + ARC_String_CopySubstring(¤t, temp, separator + 1, temp->length - (separator + 1)); + ARC_String_Destroy(temp); //h - temp = (ARC_StringSubstr){ temp.index + split + 1, subdata->length - split - 1 }; - ((ARC_Rect *) *value)->h = (int)ARC_String_ToUint64_t(data, &temp); - ttt = ((ARC_Rect *) *value)->h; + separator = current->length; + if(arc_errno){ + return 0; + } + ARC_String_CopySubstring(&temp, current, 0, separator); + ARC_String_StripEndsWhitespace(temp, &tempStripped); + h = ARC_String_ToInt64_t(tempStripped); + ARC_String_Destroy(temp); + ARC_String_Destroy(tempStripped); + ARC_String_Destroy(current); + + *value = malloc(sizeof(ARC_Rect)); + ((ARC_Rect *) *value)->x = x; + ((ARC_Rect *) *value)->y = y; + ((ARC_Rect *) *value)->w = w; + ((ARC_Rect *) *value)->h = h; return 0; } -int32_t ARC_RectArray_Read(ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value){ - ARC_StringSubstr_StripWhitespaceEnds((char *)data, subdata); - if(data[subdata->index] != '{' || data[subdata->index + subdata->length - 1] != '}'){ return ARC_ERRNO_DATA; } - subdata->index++; - subdata->length -= 2; +void ARC_RectArray_ReadRect(ARC_Config* config, ARC_String *stripped, uint64_t index, uint64_t length, uint64_t *arrayIndex, void **value){ + ARC_String *substr, *temp; + ARC_String_CopySubstring(&temp, stripped, index, length); + ARC_String_StripEndsWhitespace(temp, &substr); + ARC_String_Destroy(temp); - uint32_t arraySize = 1; - int32_t encapsulated = 0; - for(uint32_t i = subdata->index; i < subdata->index + subdata->length; i++){ - if(data[i] == '{'){ + // reading in reference + ARC_Rect *tempRect; + ARC_Config_Get(config, substr, (void **) &tempRect); + if(tempRect){ + ARC_String_Destroy(substr); + + ((ARC_Rect *)((ARC_Array *) *value)->data)[*arrayIndex] = *tempRect; + ++*arrayIndex; + + return; + } + + //reading in value + ARC_Rect_Read(config, substr, (void **) &tempRect); + if(arc_errno){ + ARC_String_Destroy(substr); + ARC_String_Destroy(stripped); + return; + } + + ((ARC_Rect *)((ARC_Array *) *value)->data)[*arrayIndex] = *tempRect; + ++*arrayIndex; + + ARC_Rect_Delete(config, substr, (void *)tempRect); + ARC_String_Destroy(substr); + if(arc_errno){ + return; + } +} + +uint8_t ARC_RectArray_Read(ARC_Config* config, ARC_String *string, void **value){ + ARC_Config_Get(config, string, value); + if(*value){ + return 1; + } + + if(string->data[0] != '{' || string->data[string->length - 1] != '}'){ + arc_errno = ARC_ERRNO_DATA; + return 0; + } + + ARC_String *temp, *stripped; + ARC_String_CopySubstring(&temp, string, 1, string->length - 2); + ARC_String_StripEndsWhitespace(temp, &stripped); + ARC_String_Destroy(temp); + + uint64_t arraySize = 1; + int64_t encapsulated = 0; + for(uint64_t i = 0; i < stripped->length; i++){ + if(stripped->data[i] == '{'){ encapsulated++; continue; } - if(data[i] == '}'){ + if(stripped->data[i] == '}'){ encapsulated--; continue; } - if(!encapsulated && data[i] == ','){ + if(!encapsulated && stripped->data[i] == ','){ arraySize++; } } if(encapsulated){ arc_errno = ARC_ERRNO_DATA; - ARC_DEBUG_LOG(arc_errno, "in ARC_RectArray_Read(config, data, subdata, value); after looping encapsulated was %d", encapsulated); - return arc_errno; + ARC_DEBUG_LOG(arc_errno, "in ARC_RectArray_Read(config, data, subdata, value); after looping encapsulated was %ld", encapsulated); + ARC_String_Destroy(stripped); + return 0; } *value = malloc(sizeof(ARC_Array)); @@ -160,286 +269,285 @@ int32_t ARC_RectArray_Read(ARC_Config* config, const char *data, ARC_StringSubst ((ARC_Array *) *value)->size = malloc(sizeof(uint32_t)); *((ARC_Array *) *value)->size = arraySize; - ARC_StringSubstr temp = { subdata->index, 0 }; + uint64_t index = 0; arraySize = 0; encapsulated = 0; - for(uint64_t i = subdata->index; i < subdata->index + subdata->length; i++){ - if(data[i] == '{'){ + for(uint64_t i = 0; i < stripped->length; i++){ + if(stripped->data[i] == '{'){ encapsulated++; continue; } - if(data[i] == '}'){ + if(stripped->data[i] == '}'){ encapsulated--; continue; } - if(!encapsulated && data[i] == ','){ - temp.length = i - temp.index; - - ARC_Rect *tempRect = (ARC_Rect *)ARC_Config_GetReference(config, (char *)data, &temp); - if(!tempRect){ - ARC_Rect_Read(config, data, &temp, (void **) &tempRect); - if(arc_errno){ - return arc_errno; - } - } - - ((ARC_Rect *)((ARC_Array *) *value)->data)[arraySize] = *tempRect; - - ARC_Rect_Delete(config, data, &temp, (void *)tempRect); + if(!encapsulated && stripped->data[i] == ','){ + ARC_RectArray_ReadRect(config, stripped, index, i - index, &arraySize, value); if(arc_errno){ - return arc_errno; + return 0; } - arraySize++; - temp = (ARC_StringSubstr){ i + 1, 0 }; - + index = i; + if(arraySize == *((ARC_Array *) *value)->size){ break; } } } - temp.length = (subdata->index + subdata->length) - temp.index; - ARC_Rect *tempRect = (ARC_Rect *)ARC_Config_GetReference(config, (char *)data, &temp); - if(!tempRect){ - int32_t ttt = ARC_Rect_Read(config, data, &temp, (void **) &tempRect); - if(arc_errno){ - return arc_errno; - } - } - - ((ARC_Rect *)((ARC_Array *) *value)->data)[arraySize] = *tempRect; - ARC_Rect ttt = ((ARC_Rect *)((ARC_Array *) *value)->data)[arraySize]; - - ARC_Rect_Delete(config, data, &temp, (void *)tempRect); - if(arc_errno){ - return arc_errno; - } + ARC_RectArray_ReadRect(config, stripped, index, stripped->length - index, &arraySize, value); + ARC_String_Destroy(stripped); return 0; } -int32_t ARC_SDL_Texture_Read(ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value){ - ARC_StringSubstr_StripWhitespaceEnds((char *)data, subdata); - ARC_StringSubstr_StripEnds((char *)data, (char *)"\"", subdata); - char path[subdata->length + 1]; - strncpy(path, data + subdata->index, subdata->length); - path[subdata->length] = 0; +uint8_t ARC_SDL_Texture_Read(ARC_Config* config, ARC_String *string, void **value){ + ARC_Config_Get(config, string, value); + if(*value){ + return 1; + } - return ARC_SDL_Texture_Load(path, (SDL_Texture **)value); + ARC_String *tempStr, *textureStr; + ARC_String_StripEndsWhitespace(string, &tempStr); + + ARC_String_CopySubstring(&textureStr, tempStr, 1, tempStr->length - 2); + ARC_String_Destroy(tempStr); + + ARC_SDL_Texture_Load(textureStr->data, (SDL_Texture **)value); + + ARC_String_Destroy(textureStr); + return 0; } -int32_t ARC_Spritesheet_Read(ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value){ - ARC_StringSubstr_StripWhitespaceEnds((char *)data, subdata); - if(data[subdata->index] != '{'){ - SDL_Texture *texture = (SDL_Texture *)ARC_Config_GetReference(config, (char *)data, subdata); - if(!texture && data[subdata->index] != '"'){ return ARC_ERRNO_DATA; } +void ARC_Spritesheet_ReadTexture(ARC_Config *config, ARC_String *string, uint32_t *size, void **value){ + SDL_Texture *texture; - if(!texture){ - int32_t err = ARC_SDL_Texture_Read(config, data, subdata, (void **)&texture); - if(err){ return ARC_ERRNO_DATA; } - } + ARC_String *tempStr, *textureStr; + ARC_String_StripEndsWhitespace(string, &tempStr); - *value = malloc(sizeof(ARC_Spritesheet)); - ((ARC_Spritesheet *) *value)->texture = texture; - ((ARC_Spritesheet *) *value)->size = NULL; - return 0; + //check for reference + ARC_Config_Get(config, tempStr, (void **)&texture); + if(!texture && (tempStr->data[0] != '"' || tempStr->data[string->length - 1] != '"')){ + arc_errno = ARC_ERRNO_DATA; } - if(data[subdata->index + subdata->length - 1] != '}'){ return ARC_ERRNO_DATA; } - subdata->index++; - subdata->length -= 2; //remove the starting { and ending } - - //Texture - uint64_t split; - int32_t err = ARC_String_Find(((char *)data) + subdata->index, (char *)",", &split); - if(err){ return err; } - if(split == ~((uint64_t)0) || split > subdata->length){ return ARC_ERRNO_DATA; } - - ARC_StringSubstr temp = { subdata->index, split }; - ARC_StringSubstr_StripWhitespaceEnds((char *)data, &temp); - SDL_Texture *texture = (SDL_Texture *)ARC_Config_GetReference(config, (char *)data, &temp); - if(!texture && data[temp.index] != '"'){ return ARC_ERRNO_DATA; } + ARC_String_CopySubstring(&textureStr, tempStr, 1, tempStr->length - 2); + ARC_String_Destroy(tempStr); + //try reading in the texture if(!texture){ - err = ARC_SDL_Texture_Read(config, data, &temp, (void **)&texture); - if(err){ return ARC_ERRNO_DATA; } + ARC_SDL_Texture_Read(config, string, (void **)&texture); + if(arc_errno){ + *value = NULL; + } } - //uint32_t size - temp = (ARC_StringSubstr){ subdata->index + split + 1, subdata->length - split - 1 }; - ARC_StringSubstr_StripWhitespaceEnds((char *)data, &temp); - uint32_t *size = (uint32_t *)ARC_Config_GetReference(config, (char *)data, &temp); - if(!size){ - ARC_ConfigKey_Read_Uint64_t(config, data, &temp, (void **)&size); - if(err){ return ARC_ERRNO_DATA; } - } + ARC_String_Destroy(textureStr); *value = malloc(sizeof(ARC_Spritesheet)); ((ARC_Spritesheet *) *value)->texture = texture; ((ARC_Spritesheet *) *value)->size = size; +} + +uint8_t ARC_Spritesheet_Read(ARC_Config* config, ARC_String *string, void **value){ + if(string->data[0] != '{' || string->data[string->length - 1] != '}'){ + ARC_Spritesheet_ReadTexture(config, string, NULL, value); + return 0; + } + + uint64_t split = getIndexAndErrorCheck(string, ",", 1); + if(arc_errno){ + return 0; + } + + ARC_String *temp, *textureStr, *sizeStr; + ARC_String_CopySubstring(&temp, string, 1, split - 1); + ARC_String_StripEndsWhitespace(temp, &textureStr); + ARC_String_Destroy(temp); + + ARC_String_CopySubstring(&temp, string, split + 1, string->length - (split + 1)); + ARC_String_StripEndsWhitespace(temp, &sizeStr); + ARC_String_Destroy(temp); + + uint32_t *size; + ARC_Config_Get(config, string, (void **)&size); + if(!size){ + ARC_ConfigKey_Read_Uint32_t(config, sizeStr, (void **)&size); + if(arc_errno){ + ARC_String_Destroy(sizeStr); + ARC_String_Destroy(textureStr); + return ARC_ERRNO_DATA; + } + } + + ARC_Spritesheet_ReadTexture(config, textureStr, size, value); + + ARC_String_Destroy(sizeStr); + ARC_String_Destroy(textureStr); return 0; } -int32_t ARC_Sprite_Read(ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value){ - ARC_StringSubstr_StripWhitespaceEnds((char *)data, subdata); - if(data[subdata->index] != '{' || data[subdata->index + subdata->length - 1] != '}'){ return ARC_ERRNO_DATA; } - subdata->index++; - subdata->length -= 2; //remove the starting { and ending } +uint8_t ARC_Sprite_Read(ARC_Config* config, ARC_String *string, void **value){ + ARC_Config_Get(config, string, value); + if(*value){ + return 1; + } - uint64_t split; - int32_t err = ARC_String_Find(((char *)data) + subdata->index, (char *)",", &split); - if(err){ return err; } - if(split == ~((uint64_t)0) || split > subdata->length){ return ARC_ERRNO_DATA; } + if(string->data[0] != '{' || string->data[string->length - 1] != '}'){ + ARC_Spritesheet_ReadTexture(config, string, NULL, value); + return 0; + } + + uint64_t split = getIndexAndErrorCheck(string, ",", 1); + if(arc_errno){ + return 0; + } + + ARC_String *temp, *spritesheetStr, *framesStr; + ARC_String_CopySubstring(&temp, string, 1, split - 2); + ARC_String_StripEndsWhitespace(temp, &spritesheetStr); + ARC_String_Destroy(temp); + + ARC_String_CopySubstring(&temp, string, split + 1, string->length - (split + 2)); + ARC_String_StripEndsWhitespace(temp, &framesStr); + ARC_String_Destroy(temp); //spritesheet - ARC_StringSubstr temp = { subdata->index, split }; - ARC_StringSubstr_StripWhitespaceEnds((char *)data, &temp); - ARC_Spritesheet *spritesheet = (ARC_Spritesheet *)ARC_Config_GetReference(config, (char *)data, &temp); + ARC_Spritesheet *spritesheet; + ARC_Config_Get(config, spritesheetStr, (void **)&spritesheet); if(!spritesheet){ - err = ARC_Spritesheet_Read(config, data, &temp, (void **)&spritesheet); - if(err){ return ARC_ERRNO_DATA; } - } - - //bounds - uint8_t isRectArray = 0; - temp = (ARC_StringSubstr){ subdata->index + split + 1, subdata->length - split - 1 }; - ARC_StringSubstr_StripWhitespaceEnds((char *)data, &temp); - ARC_Array *bounds = (ARC_Array *)ARC_Config_GetReference(config, (char *)data, &temp); - - if(!bounds){ - err = ARC_RectArray_Read(config, data, &temp, (void **)&bounds); - if(err){ return ARC_ERRNO_DATA; } - isRectArray = 1; - } - - //scale bounds on spritesheet size - // TODO: possible bug for sheets that use the same bounds - if(spritesheet->size){ - for(uint32_t i = 0; i < *bounds->size; i++){ - ((ARC_Rect *)bounds->data)[i].x *= *spritesheet->size; - ((ARC_Rect *)bounds->data)[i].y *= *spritesheet->size; - ((ARC_Rect *)bounds->data)[i].w *= *spritesheet->size; - ((ARC_Rect *)bounds->data)[i].h *= *spritesheet->size; + ARC_Spritesheet_Read(config, spritesheetStr, (void **)&spritesheet); + if(arc_errno){ + ARC_String_Destroy(spritesheetStr); + ARC_String_Destroy(framesStr ); + return 0; } } + //bounds + ARC_Array *frames; + ARC_Config_Get(config, framesStr, (void **)&frames); + + if(!frames){ + ARC_RectArray_Read(config, framesStr, (void **)&frames); + if(arc_errno){ + ARC_String_Destroy(spritesheetStr); + ARC_String_Destroy(framesStr ); + return 0; + } + } + + ARC_String_Destroy(spritesheetStr); + ARC_String_Destroy(framesStr ); + + //sprite *value = malloc(sizeof(ARC_Sprite)); ((ARC_Sprite *) *value)->frameIndex = malloc(sizeof(uint32_t)); ((ARC_Sprite *) *value)->spritesheet = spritesheet; - ((ARC_Sprite *) *value)->frames = bounds; + ((ARC_Sprite *) *value)->frames = frames; *((ARC_Sprite *) *value)->frameIndex = 0; - - ARC_Rect *ttt = (ARC_Rect *)bounds->data; - ARC_Rect ttf = ttt[0]; - if(isRectArray){ - free(bounds); + return 0; +} + +void ARC_Point_Delete(ARC_Config* config, ARC_String *string, void *value){ + free((ARC_Point *)value); +} + +void ARC_Rect_Delete(ARC_Config* config, ARC_String *string, void *value){ + free((ARC_Rect *)value); +} + +void ARC_RectArray_Delete(ARC_Config* config, ARC_String *string, void *value){ + free((ARC_Array *)value); +} + +void ARC_SDL_Texture_Delete(ARC_Config* config, ARC_String *string, void *value){ + SDL_DestroyTexture((SDL_Texture *) value); +} + + +void ARC_Spritesheet_Delete(ARC_Config* config, ARC_String *string, void *value){ + ARC_Spritesheet *sheetValue = (ARC_Spritesheet *)value; + + //check if read in as a Textrue reference + void *temp; + ARC_Config_Get(config, string, &temp); + if(temp){ + free(sheetValue); + return; } - return 0; -} - -int32_t ARC_Point_Delete(ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value){ - if((ARC_Point *)value){ free((ARC_Point *)value); } - return 0; -} - -int32_t ARC_Rect_Delete(ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value){ - if((ARC_Rect *)value){ free((ARC_Rect *)value); } - return 0; -} - -int32_t ARC_RectArray_Delete(ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value){ - if((ARC_Array *)value){ free((ARC_Array *)value); } - return 0; -} - -int32_t ARC_SDL_Texture_Delete(ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value){ - //if((SDL_Texture *) value){ SDL_DestroyTexture((SDL_Texture *) value); } - return 0; -} - -int32_t ARC_Spritesheet_Delete(ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value){ - ARC_Spritesheet *spritesheet = (ARC_Spritesheet *) value; - if(!data){ //there is no data, kill everything, most likely was called by a data type being destroyed - free(spritesheet); - return 0; + uint64_t split = getIndexAndErrorCheck(string, ",", 1); + if(arc_errno){ + free(sheetValue); + return; } - ARC_StringSubstr_StripWhitespaceEnds((char *)data, subdata); - if(data[subdata->index] != '{'){ - SDL_Texture *texture = (SDL_Texture *)ARC_Config_GetReference(config, (char *)data, subdata); - if(!texture){ - ARC_SDL_Texture_Delete(config, data, subdata, (void *)spritesheet->texture); - } + //check if texture and size are references + ARC_String *tempStr, *textureStr, *sizeStr; + ARC_String_CopySubstring(&tempStr, string, 1, split - 1); + ARC_String_StripEndsWhitespace(tempStr, &textureStr); + ARC_String_Destroy(tempStr); - // if(spritesheet){ - // free(spritesheet); - // } + ARC_String_CopySubstring(&tempStr, string, split + 1, string->length - (split + 1)); + ARC_String_StripEndsWhitespace(tempStr, &sizeStr); + ARC_String_Destroy(tempStr); - return 0; + ARC_Config_Get(config, sizeStr, (void **)&temp); + ARC_String_Destroy(sizeStr); + if(temp){ + free(sheetValue->size); } - uint64_t split; - int32_t err = ARC_String_Find(((char *)data) + subdata->index, (char *)",", &split); - if(err){ return err; } - if(split == ~((uint64_t)0) || split > subdata->length){ return ARC_ERRNO_DATA; } + ARC_Config_Get(config, textureStr, (void **)&temp); + ARC_String_Destroy(textureStr); + if(temp){ + free(sheetValue->size); + } - ARC_StringSubstr temp = { subdata->index, split }; - ARC_StringSubstr_StripWhitespaceEnds((char *)data, &temp); - SDL_Texture *texture = (SDL_Texture *)ARC_Config_GetReference(config, (char *)data, &temp); - if(!texture){ ARC_SDL_Texture_Delete(config, data, &temp, (void *)spritesheet->texture); } - - temp = (ARC_StringSubstr){ subdata->index + split + 1, subdata->length - split - 1 }; - ARC_StringSubstr_StripWhitespaceEnds((char *)data, &temp); - uint32_t *size = (uint32_t *)ARC_Config_GetReference(config, (char *)data, &temp); - if(!size){ ARC_ConfigKey_Delete_Uint64_t(config, data, &temp, (void *)spritesheet->size); } - - free(spritesheet); - return 0; + free(sheetValue); } -int32_t ARC_Sprite_Delete(ARC_Config* config, const char* data, ARC_StringSubstr *subdata, void *value){ - ARC_Sprite *sprite = (ARC_Sprite *) value; - if(!data){ - free(sprite); - return 0; +void ARC_Sprite_Delete(ARC_Config* config, ARC_String *string, void *value){ + ARC_Sprite *spriteValue = (ARC_Sprite *)value; + + //check if read in as a Textrue reference + void *temp; + uint64_t split = getIndexAndErrorCheck(string, ",", 1); + if(arc_errno){ + free(spriteValue); + return; } - ARC_StringSubstr_StripWhitespaceEnds((char *)data, subdata); - if(data[subdata->index] != '{' || data[subdata->index + subdata->length - 1] != '}'){ return ARC_ERRNO_DATA; } - subdata->index++; - subdata->length -= 2; //remove the starting { and ending } + //check if texture and size are references + ARC_String *tempStr, *spritesheetStr, *framesStr; + ARC_String_CopySubstring(&tempStr, string, 1, split - 1); + ARC_String_StripEndsWhitespace(tempStr, &spritesheetStr); + ARC_String_Destroy(tempStr); - uint64_t split; - int32_t err = ARC_String_Find(((char *)data) + subdata->index, (char *)",", &split); - if(err){ return err; } - if(split == ~((uint64_t)0) || split > subdata->length){ return ARC_ERRNO_DATA; } + ARC_String_CopySubstring(&tempStr, string, split + 1, string->length - (split + 1)); + ARC_String_StripEndsWhitespace(tempStr, &framesStr); + ARC_String_Destroy(tempStr); - //spritesheet - ARC_StringSubstr temp = { subdata->index, split }; - ARC_StringSubstr_StripWhitespaceEnds((char *)data, &temp); - ARC_Spritesheet *spritesheet = (ARC_Spritesheet *)ARC_Config_GetReference(config, (char *)data, &temp); - if(!spritesheet){ ARC_Spritesheet_Delete(config, data, &temp, (void *)sprite->spritesheet); } - - //bounds - temp = (ARC_StringSubstr){ subdata->index + split + 1, subdata->length - split - 1 }; - ARC_StringSubstr_StripWhitespaceEnds((char *)data, &temp); - SDL_Rect *bounds = (SDL_Rect *)ARC_Config_GetReference(config, (char *)data, &temp); - - if(!bounds){ - free(sprite->frames); + ARC_Config_Get(config, spritesheetStr, (void **)&temp); + ARC_String_Destroy(spritesheetStr); + if(temp){ + free(spriteValue->spritesheet); } - free(sprite->frameIndex); - free(sprite); - return 0; + ARC_Config_Get(config, framesStr, (void **)&temp); + ARC_String_Destroy(framesStr); + if(temp){ + free(spriteValue->frames); + } + + free(spriteValue); } #endif //ARC_SDL \ No newline at end of file diff --git a/src/std/config.c b/src/std/config.c index fef987a..6a6fc10 100644 --- a/src/std/config.c +++ b/src/std/config.c @@ -28,6 +28,11 @@ typedef struct ARC_ConfigTypeTemplate { void *data; } ARC_ConfigTypeTemplate; +typedef struct ARC_ConfigDeleteKeyArgs { + ARC_Config *config; + ARC_String *string; +} ARC_ConfigDeleteKeyArgs; + int8_t ARC_Config_KeyComp(void *key1, size_t *key1size, void *key2, size_t *key2size); void ARC_Config_CreateGroup(ARC_Config *config, ARC_String *name); @@ -76,12 +81,22 @@ void ARC_Config_Create(ARC_Config **config){ } void ARC_Config_Destroy(ARC_Config *config){ - ARC_Hashtable_Destroy(config->groups, ARC_Config_DestroyGroup, (void *)&config); - ARC_Hashtable_Destroy(config->keys , ARC_Config_RemoveKey , NULL ); + ARC_ConfigDeleteKeyArgs keyArgs = { + .config = config, + .string = NULL, + }; + + ARC_Hashtable_Destroy(config->groups, ARC_Config_DestroyGroup, (void *)&keyArgs); + ARC_Hashtable_Destroy(config->keys , ARC_Config_RemoveKey , NULL ); free(config); } +//TODO: fix NULL group void ARC_Config_SetGroup(ARC_Config *config, ARC_String *groupname){ + if(!config){ + return; + } + if(groupname == NULL){ ARC_Hashtable_Get(config->groups, (void *)" ", 1, (void **)&(config->currgroup)); return; @@ -107,11 +122,7 @@ void ARC_Config_SetGroup(ARC_Config *config, ARC_String *groupname){ void ARC_Config_Get(ARC_Config *config, ARC_String *keyname, void **value){ ARC_ConfigTypeTemplate *temp; - ARC_String *separator; - ARC_String_Create(&separator, "::", 2); - uint64_t length = ARC_String_Find(keyname, separator); - ARC_String_Destroy(separator); - + uint64_t length = ARC_String_FindCString(keyname, "::", 2); if(arc_errno){ //TODO: Debug info here *value = NULL; @@ -119,8 +130,12 @@ void ARC_Config_Get(ARC_Config *config, ARC_String *keyname, void **value){ } if(length != ~((uint64_t)0)){ - ARC_String *group; - ARC_String_CopySubstring(&group, keyname, 0, length); + length--; + ARC_String *group = NULL; + + if(length != 0){ + ARC_String_CopySubstring(&group, keyname, 0, length); + } ARC_Hashtable *currgroup = config->currgroup; ARC_Config_SetGroup(config, group); @@ -132,14 +147,22 @@ void ARC_Config_Get(ARC_Config *config, ARC_String *keyname, void **value){ ARC_String *name; ARC_String_CopySubstring(&name, keyname, length + 2, keyname->length - (length + 2)); - ARC_Hashtable_Get(config->currgroup, (void *)name->data, name->length, (void **)&temp); + ARC_String_Destroy(name); config->currgroup = currgroup; + if(group){ + ARC_String_Destroy(group); + } *value = temp->data; return; } + if(!keyname){ + *value = NULL; + return; + } + ARC_Hashtable_Get(config->currgroup, (void *)keyname->data, keyname->length, (void **)&temp); if(arc_errno || temp == NULL){ *value = NULL; @@ -163,12 +186,12 @@ void ARC_Config_SetKeyGroup(ARC_Config *config, ARC_String **data, uint8_t *comm } ARC_String *name, *temp; - ARC_String_CopySubstring(&temp, *data, index, nextIndex); + ARC_String_CopySubstring(&temp, *data, index, nextIndex - index - 1); ARC_String_StripEndsWhitespace(temp, &name); ARC_String_Destroy(temp); temp = *data; - ARC_String_RemoveSection(data, temp, nextIndex + 1, (*data)->length - (nextIndex + 1)); + ARC_String_CopySubstring(data, temp, nextIndex + 1, (*data)->length - (nextIndex + 1)); ARC_String_Destroy(temp); ARC_Config_Recurse(config, data, name, command); @@ -199,13 +222,19 @@ void ARC_Config_LoadFromKey(ARC_Config *config, ARC_String *keyType, ARC_String return; } - char *nameval = (char *)malloc(sizeof(char) * name->length); + char *nameval = (char *)malloc(sizeof(char) * name->length + 1); strncpy(nameval, name->data, name->length); + nameval[name->length] = '\0'; ARC_Hashtable_Add(config->currgroup, nameval, name->length, (void *)templateVal); } -void ARC_Config_UnloadFromKey(ARC_Config *config, ARC_String *keyType, ARC_String *name){ - ARC_Hashtable_Remove(config->currgroup, name->data, name->length, ARC_Config_DestroyGroupNode, &config); +void ARC_Config_UnloadFromKey(ARC_Config *config, ARC_String *keyType, ARC_String *name, ARC_String *value){ + ARC_ConfigDeleteKeyArgs keyArgs = { + .config = config, + .string = value, + }; + + ARC_Hashtable_Remove(config->currgroup, name->data, name->length, ARC_Config_DestroyGroupNode, &keyArgs); } void ARC_Config_GetNameAndValue(ARC_String *data, ARC_String **name, ARC_String **value){ @@ -223,7 +252,9 @@ void ARC_Config_GetNameAndValue(ARC_String *data, ARC_String **name, ARC_String ARC_String_StripEndsWhitespace(dataTemp, name); ARC_String_Destroy(dataTemp); - ARC_String_CopySubstring(value, data, index, data->length - index); + ARC_String_CopySubstring(&dataTemp, data, index, data->length - index); + ARC_String_StripEndsWhitespace(dataTemp, value); + ARC_String_Destroy(dataTemp); } void ARC_Config_Recurse(ARC_Config *config, ARC_String **data, ARC_String *groupstr, uint8_t *command){ @@ -268,6 +299,7 @@ void ARC_Config_Recurse(ARC_Config *config, ARC_String **data, ARC_String *group if(ARC_String_EqualsCString(keyType, "group", 5)){ ARC_Config_SetKeyGroup(config, data, command); ARC_String_Destroy(keyType); + config->currgroup = group; if(arc_errno){ return; } @@ -329,7 +361,7 @@ void ARC_Config_Recurse(ARC_Config *config, ARC_String **data, ARC_String *group // unload from key if(*command == ARC_CONFIG_FILE_IO_UNLOAD){ - ARC_Config_UnloadFromKey(config, keyType, name); + ARC_Config_UnloadFromKey(config, keyType, name, value); ARC_String_Destroy(keyType); ARC_String_Destroy(name ); @@ -354,13 +386,16 @@ void ARC_Config_Recurse(ARC_Config *config, ARC_String **data, ARC_String *group } void ARC_Config_StripComment(ARC_String *original, ARC_String **stripped, ARC_String *lineStart, ARC_String *lineEnd){ - ARC_String *current; + ARC_String *current = NULL; ARC_String_Copy(¤t, original); - uint64_t index = ARC_String_Find(current, lineStart); + uint64_t index = ARC_String_Find(original, lineStart); while(index != ~(uint64_t)0){ + ARC_String *commentString; + ARC_String_CopySubstring(&commentString, current, index + lineStart->length, current->length - (index + lineStart->length)); - uint64_t endIndex = ARC_String_Find(current, lineEnd); + uint64_t endIndex = ARC_String_Find(commentString, lineEnd); + ARC_String_Destroy(commentString); if(endIndex == ~(uint64_t)0){ ARC_DEBUG_ERR("ARC_Config_RemoveComments(original, commentRemoved); No newline found when stripping single line comment"); arc_errno = ARC_ERRNO_DATA; @@ -370,7 +405,7 @@ void ARC_Config_StripComment(ARC_String *original, ARC_String **stripped, ARC_St } ARC_String *currentTemp = current; - ARC_String_RemoveSection(¤t, currentTemp, index, endIndex - index); + ARC_String_RemoveSection(¤t, currentTemp, index, endIndex + lineStart->length + lineEnd->length); ARC_String_Destroy(currentTemp); index = ARC_String_Find(current, lineStart); @@ -487,7 +522,7 @@ void ARC_Config_FileIO(ARC_Config *config, ARC_String *path, uint8_t command){ return; } - char *tempData = (char *)malloc((char) data->length + 1); + char *tempData = (char *)malloc(sizeof(char) * (data->length + 1)); strncpy(tempData, data->data, data->length); tempData[data->length] = '\n'; @@ -547,9 +582,9 @@ void ARC_Config_DestroyGroupNode(ARC_HashtableNode *node, void *userdata){ ARC_ConfigTypeTemplate *temp = (ARC_ConfigTypeTemplate *)node->data; if(temp->Delete && temp->data && userdata){ - ARC_Config *config = (ARC_Config *)userdata; + ARC_ConfigDeleteKeyArgs *args = (ARC_ConfigDeleteKeyArgs *)userdata; - temp->Delete(config, temp->data); + temp->Delete(args->config, args->string, temp->data); } free(temp); diff --git a/src/std/defaults/config.c b/src/std/defaults/config.c index 8bae09c..11838d3 100644 --- a/src/std/defaults/config.c +++ b/src/std/defaults/config.c @@ -11,102 +11,182 @@ #include void ARC_Defaults_ConfigKey_Create(ARC_Config *config){ - ARC_Config_AddKeyCString(config, "uint8_t" , 7, ARC_ConfigKey_Read_Uint8_t , ARC_ConfigKey_Delete_Uint8_t ); - // ARC_Config_AddKeyCString(config, "int8_t" , ARC_ConfigKey_Read_Int8_t , ARC_ConfigKey_Delete_Int8_t ); - // ARC_Config_AddKeyCString(config, "char" , ARC_ConfigKey_Read_Char , ARC_ConfigKey_Delete_Char ); - // ARC_Config_AddKeyCString(config, "uint16_t", ARC_ConfigKey_Read_Uint16_t , ARC_ConfigKey_Delete_Uint16_t ); - // ARC_Config_AddKeyCString(config, "int16_t" , ARC_ConfigKey_Read_Int16_t , ARC_ConfigKey_Delete_Int16_t ); - // ARC_Config_AddKeyCString(config, "uint32_t", ARC_ConfigKey_Read_Uint32_t , ARC_ConfigKey_Delete_Uint32_t ); - // ARC_Config_AddKeyCString(config, "int32_t" , ARC_ConfigKey_Read_Int32_t , ARC_ConfigKey_Delete_Int32_t ); - // ARC_Config_AddKeyCString(config, "int" , ARC_ConfigKey_Read_Int , ARC_ConfigKey_Delete_Int ); - // ARC_Config_AddKeyCString(config, "uint64_t", ARC_ConfigKey_Read_Uint64_t , ARC_ConfigKey_Delete_Uint64_t ); - // ARC_Config_AddKeyCString(config, "int64_t" , ARC_ConfigKey_Read_Int64_t , ARC_ConfigKey_Delete_Int64_t ); - // ARC_Config_AddKeyCString(config, "long" , ARC_ConfigKey_Read_Long , ARC_ConfigKey_Delete_Long ); - // ARC_Config_AddKeyCString(config, "string" , ARC_ConfigKey_Read_String , ARC_ConfigKey_Delete_String ); + ARC_Config_AddKeyCString(config, "int8_t" , 6, ARC_ConfigKey_Read_Int8_t , ARC_ConfigKey_Delete_Int8_t ); + ARC_Config_AddKeyCString(config, "uint16_t", 8, ARC_ConfigKey_Read_Uint16_t , ARC_ConfigKey_Delete_Uint16_t ); + ARC_Config_AddKeyCString(config, "int16_t" , 7, ARC_ConfigKey_Read_Int16_t , ARC_ConfigKey_Delete_Int16_t ); + ARC_Config_AddKeyCString(config, "uint32_t", 8, ARC_ConfigKey_Read_Uint32_t , ARC_ConfigKey_Delete_Uint32_t ); + ARC_Config_AddKeyCString(config, "int32_t" , 7, ARC_ConfigKey_Read_Int32_t , ARC_ConfigKey_Delete_Int32_t ); + ARC_Config_AddKeyCString(config, "uint64_t", 8, ARC_ConfigKey_Read_Uint64_t , ARC_ConfigKey_Delete_Uint64_t ); + ARC_Config_AddKeyCString(config, "int64_t" , 7, ARC_ConfigKey_Read_Int64_t , ARC_ConfigKey_Delete_Int64_t ); + // ARC_Config_AddKeyCString(config, "char" , 4, ARC_ConfigKey_Read_Char , ARC_ConfigKey_Delete_Char ); + ARC_Config_AddKeyCString(config, "int" , 3, ARC_ConfigKey_Read_Int , ARC_ConfigKey_Delete_Int ); + ARC_Config_AddKeyCString(config, "long" , 4, ARC_ConfigKey_Read_Long , ARC_ConfigKey_Delete_Long ); + ARC_Config_AddKeyCString(config, "float" , 5, ARC_ConfigKey_Read_Float , ARC_ConfigKey_Delete_Float ); + ARC_Config_AddKeyCString(config, "double" , 6, ARC_ConfigKey_Read_Double , ARC_ConfigKey_Delete_Double ); + ARC_Config_AddKeyCString(config, "string" , 6, ARC_ConfigKey_Read_String , ARC_ConfigKey_Delete_String ); // ARC_Config_AddKeyCString(config, "string[]", ARC_ConfigKey_Read_StringArray, ARC_ConfigKey_Delete_StringArray); } uint8_t ARC_ConfigKey_Read_Uint8_t(ARC_Config* config, ARC_String *string, void **value){ - ARC_String *current; - ARC_String_StripEndsWhitespace(string, ¤t); - - ARC_Config_Get(config, current, value); + ARC_Config_Get(config, string, value); if(*value){ - ARC_String_Destroy(current); return 1; } *value = (uint8_t *) malloc(sizeof(uint8_t)); - *((uint8_t *)(*value)) = (uint8_t) ARC_String_ToUint64_t(current); - ARC_String_Destroy(current); + *((uint8_t *)(*value)) = (uint8_t) ARC_String_ToUint64_t(string); return 0; } -/* -void ARC_ConfigKey_Read_Int8_t(ARC_Config* config, ARC_String *string, void **value){ - *value = (int8_t *) malloc(sizeof(int8_t)); - *((int8_t *)(*value)) = (int8_t) ARC_String_ToUint64_t(string); -} - -void ARC_ConfigKey_Read_Char(ARC_Config* config, ARC_String *string, void **value){ - *value = (char *) malloc(sizeof(char)); - *((char *)(* value)) = (char) *(data + subdata->index); -} - -void ARC_ConfigKey_Read_Uint16_t(ARC_Config* config, ARC_String *string, void **value){ - *value = (uint16_t *) malloc(sizeof(uint16_t)); - *((uint16_t *)(*value)) = (uint16_t) ARC_String_ToUint64_t(data, subdata); -} - -void ARC_ConfigKey_Read_Int16_t(ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value){ - *value = (int16_t *) malloc(sizeof(int16_t)); - *((int16_t *)(*value)) = (int16_t) ARC_String_ToUint64_t(data, subdata); -} - -void ARC_ConfigKey_Read_Uint32_t(ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value){ - *value = (uint32_t *) malloc(sizeof(uint32_t)); - *((uint32_t *)(*value)) = (uint32_t) ARC_String_ToUint64_t(data, subdata); -} - -void ARC_ConfigKey_Read_Int32_t(ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value){ - *value = (int32_t *) malloc(sizeof(int32_t)); - *((int32_t *)(*value)) = (int32_t) ARC_String_ToUint64_t(data, subdata); -} - -void ARC_ConfigKey_Read_Int(ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value){ - *value = (int *) malloc(sizeof(int)); - *((int *)(*value)) = (int) ARC_String_ToUint64_t(data, subdata); -} - -void ARC_ConfigKey_Read_Uint64_t(ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value){ - *value = (uint64_t *) malloc(sizeof(uint64_t)); - *((uint64_t *)(*value)) = ARC_String_ToUint64_t(data, subdata); -} - -void ARC_ConfigKey_Read_Int64_t(ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value){ - *value = (int64_t *) malloc(sizeof(int64_t)); - *((int64_t *)(*value)) = (int64_t) ARC_String_ToUint64_t(data, subdata); -} - -void ARC_ConfigKey_Read_Long(ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value){ - *value = (long *) malloc(sizeof(long)); - *((long *)(*value)) = (long) ARC_String_ToUint64_t(data, subdata); -} - -void ARC_ConfigKey_Read_String(ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value){ - ARC_StringSubstr_StripWhitespaceEnds((char *)data, subdata); - if(data[subdata->index] != '"' || data[subdata->index + subdata->length - 1] != '"'){ - arc_errno = ARC_ERRNO_DATA; - return; +uint8_t ARC_ConfigKey_Read_Int8_t(ARC_Config* config, ARC_String *string, void **value){ + ARC_Config_Get(config, string, value); + if(*value){ + return 1; } - ARC_StringSubstr_StripEnds((char *)data, (char *)"\"", subdata); - *value = (char *) malloc(sizeof(char) * (subdata->length + 1)); - strncpy((char *)(*value), data + subdata->index, subdata->length); - ((char *)(*value))[subdata->length] = '\0'; + + *value = (int8_t *) malloc(sizeof(int8_t)); + *((int8_t *)(*value)) = (int8_t) ARC_String_ToInt64_t(string); + return 0; } +uint8_t ARC_ConfigKey_Read_Uint16_t(ARC_Config* config, ARC_String *string, void **value){ + ARC_Config_Get(config, string, value); + if(*value){ + return 1; + } + + *value = (uint16_t *) malloc(sizeof(uint16_t)); + *((uint16_t *)(*value)) = (uint16_t) ARC_String_ToUint64_t(string); + return 0; +} + +uint8_t ARC_ConfigKey_Read_Int16_t(ARC_Config* config, ARC_String *string, void **value){ + ARC_Config_Get(config, string, value); + if(*value){ + return 1; + } + + *value = (int16_t *) malloc(sizeof(int16_t)); + *((int16_t *)(*value)) = (int16_t) ARC_String_ToInt64_t(string); + return 0; +} + +uint8_t ARC_ConfigKey_Read_Uint32_t(ARC_Config* config, ARC_String *string, void **value){ + ARC_Config_Get(config, string, value); + if(*value){ + return 1; + } + + *value = (uint32_t *) malloc(sizeof(uint32_t)); + *((uint32_t *)(*value)) = (uint32_t) ARC_String_ToUint64_t(string); + return 0; +} + +uint8_t ARC_ConfigKey_Read_Int32_t(ARC_Config* config, ARC_String *string, void **value){ + ARC_Config_Get(config, string, value); + if(*value){ + return 1; + } + + *value = (int32_t *) malloc(sizeof(int32_t)); + *((int32_t *)(*value)) = (int32_t) ARC_String_ToInt64_t(string); + return 0; +} + +uint8_t ARC_ConfigKey_Read_Uint64_t(ARC_Config* config, ARC_String *string, void **value){ + ARC_Config_Get(config, string, value); + if(*value){ + return 1; + } + + *value = (uint64_t *) malloc(sizeof(uint64_t)); + *((uint64_t *)(*value)) = (uint64_t) ARC_String_ToUint64_t(string); + return 0; +} + +uint8_t ARC_ConfigKey_Read_Int64_t(ARC_Config* config, ARC_String *string, void **value){ + ARC_Config_Get(config, string, value); + if(*value){ + return 1; + } + + *value = (int64_t *) malloc(sizeof(int64_t)); + *((int64_t *)(*value)) = (int64_t) ARC_String_ToInt64_t(string); + return 0; +} + +uint8_t ARC_ConfigKey_Read_Char_t(ARC_Config* config, ARC_String *string, void **value){ + ARC_Config_Get(config, string, value); + if(*value){ + return 1; + } + + if(string->length != 1){ + arc_errno = ARC_ERRNO_DATA; + return 0; + } + + *value = (char *) malloc(sizeof(char)); + *((char *)(*value)) = string->data[0]; + return 0; +} + +uint8_t ARC_ConfigKey_Read_Int(ARC_Config* config, ARC_String *string, void **value){ + ARC_Config_Get(config, string, value); + if(*value){ + return 1; + } + + *value = (int *) malloc(sizeof(int)); + *((int *)(*value)) = (int) ARC_String_ToInt64_t(string); + return 0; +} + +uint8_t ARC_ConfigKey_Read_Long(ARC_Config* config, ARC_String *string, void **value){ + ARC_Config_Get(config, string, value); + if(*value){ + return 1; + } + + *value = (long *) malloc(sizeof(long)); + *((long *)(*value)) = (long) ARC_String_ToInt64_t(string); + return 0; +} + +uint8_t ARC_ConfigKey_Read_Float(ARC_Config* config, ARC_String *string, void **value){ + ARC_Config_Get(config, string, value); + if(*value){ + return 1; + } + + *value = (float *) malloc(sizeof(float)); + *((float *)(*value)) = (float) ARC_String_ToDouble(string); + return 0; +} + +uint8_t ARC_ConfigKey_Read_Double(ARC_Config* config, ARC_String *string, void **value){ + ARC_Config_Get(config, string, value); + if(*value){ + return 1; + } + + *value = (double *) malloc(sizeof(double)); + *((double *)(*value)) = (double) ARC_String_ToDouble(string); + return 0; +} + +uint8_t ARC_ConfigKey_Read_String(ARC_Config* config, ARC_String *string, void **value){ + ARC_Config_Get(config, string, value); + if(*value){ + return 1; + } + + ARC_String_Copy((ARC_String **)value, string); + return 0; +} +/* void ARC_ConfigKey_Read_StringArray(ARC_Config* config, const char *data, ARC_StringSubstr *subdata, void **value){ ARC_StringSubstr_StripWhitespaceEnds((char *)data, subdata); if(data[subdata->index] != '{' || data[subdata->index + subdata->length - 1] != '}'){ return ARC_ERRNO_DATA; } @@ -177,21 +257,60 @@ void ARC_ConfigKey_Read_StringArray(ARC_Config* config, const char *data, ARC_St } */ -void ARC_ConfigKey_Delete_Uint8_t(ARC_Config* config, void *value){ +void ARC_ConfigKey_Delete_Uint8_t(ARC_Config *config, ARC_String *string, void *value){ free((uint8_t *)value); } -// void ARC_ConfigKey_Delete_Int8_t (ARC_Config* config, void *value){ if((int8_t *)value){ free((int8_t *)value); } } -// void ARC_ConfigKey_Delete_Char (ARC_Config* config, void *value){ if((char *)value){ free((char *)value); } } -// void ARC_ConfigKey_Delete_Uint16_t (ARC_Config* config, void *value){ if((uint16_t *)value){ free((uint16_t *)value); } } -// void ARC_ConfigKey_Delete_Int16_t (ARC_Config* config, void *value){ if((int16_t *)value){ free((int16_t *)value); } } -// void ARC_ConfigKey_Delete_Uint32_t (ARC_Config* config, void *value){ if((uint32_t *)value){ free((uint32_t *)value); } } -// void ARC_ConfigKey_Delete_Int32_t (ARC_Config* config, void *value){ if((int32_t *)value){ free((int32_t *)value); } } -// void ARC_ConfigKey_Delete_Int (ARC_Config* config, void *value){ if((int *)value){ free((int *)value); } } -// void ARC_ConfigKey_Delete_Uint64_t (ARC_Config* config, void *value){ if((uint64_t *)value){ free((uint64_t *)value); } } -// void ARC_ConfigKey_Delete_Int64_t (ARC_Config* config, void *value){ if((int64_t *)value){ free((int64_t *)value); } } -// void ARC_ConfigKey_Delete_Long (ARC_Config* config, void *value){ if((long *)value){ free((long *)value); } } -// void ARC_ConfigKey_Delete_String (ARC_Config* config, void *value){ if((char *)value){ free((char *)value); } } -// void ARC_ConfigKey_Delete_StringArray(ARC_Config* config, void *value){ if((ARC_Array *)value){ free((ARC_Array *)value); } } +void ARC_ConfigKey_Delete_Int8_t(ARC_Config *config, ARC_String *string, void *value){ + free((int8_t *)value); +} + +void ARC_ConfigKey_Delete_Uint16_t(ARC_Config *config, ARC_String *string, void *value){ + free((uint16_t *)value); +} + +void ARC_ConfigKey_Delete_Int16_t(ARC_Config *config, ARC_String *string, void *value){ + free((int16_t *)value); +} + +void ARC_ConfigKey_Delete_Uint32_t(ARC_Config *config, ARC_String *string, void *value){ + free((uint32_t *)value); +} + +void ARC_ConfigKey_Delete_Int32_t(ARC_Config *config, ARC_String *string, void *value){ + free((int32_t *)value); +} + +void ARC_ConfigKey_Delete_Uint64_t(ARC_Config *config, ARC_String *string, void *value){ + free((uint64_t *)value); +} + +void ARC_ConfigKey_Delete_Int64_t(ARC_Config *config, ARC_String *string, void *value){ + free((int64_t *)value); +} + +void ARC_ConfigKey_Delete_Char(ARC_Config *config, ARC_String *string, void *value){ + free((char *)value); +} + +void ARC_ConfigKey_Delete_Int(ARC_Config *config, ARC_String *string, void *value){ + free((int *)value); +} + +void ARC_ConfigKey_Delete_Long(ARC_Config *config, ARC_String *string, void *value){ + free((long *)value); +} + +void ARC_ConfigKey_Delete_Float(ARC_Config *config, ARC_String *string, void *value){ + free((float *)value); +} + +void ARC_ConfigKey_Delete_Double(ARC_Config *config, ARC_String *string, void *value){ + free((double *)value); +} + +void ARC_ConfigKey_Delete_String(ARC_Config *config, ARC_String *string, void *value){ + ARC_String_Destroy((ARC_String *)value); +} #endif //ARC_DEFAULT_CONFIG diff --git a/src/std/string.c b/src/std/string.c index 6c0057f..3854af2 100644 --- a/src/std/string.c +++ b/src/std/string.c @@ -14,6 +14,15 @@ void ARC_String_Create(ARC_String **string, char *data, uint64_t length){ (*string)->data[length] = '\0'; } +void ARC_String_CreateWithStrlen(ARC_String **string, char *data){ + *string = (ARC_String *)malloc(sizeof(ARC_String)); + (*string)->length = strlen(data); + (*string)->data = (char *)malloc((*string)->length + 1); + + strncpy((*string)->data, data, (*string)->length); + (*string)->data[(*string)->length] = '\0'; +} + void ARC_String_Destroy(ARC_String *string){ free(string->data); free(string); @@ -108,13 +117,18 @@ uint8_t ARC_String_Alpha(ARC_String *string){ } uint64_t ARC_String_ToUint64_t(ARC_String *string){ - char temp[string->length + 1]; - strncpy(temp, string->data, string->length); - temp[string->length] = '\0'; - - return (uint64_t) strtoul(temp, NULL, 10); + return (uint64_t) strtoul(string->data, NULL, 10); } +int64_t ARC_String_ToInt64_t(ARC_String *string){ + return (int64_t) strtol(string->data, NULL, 10); +} + +double ARC_String_ToDouble(ARC_String *string){ + return strtod(string->data, NULL); +} + + uint64_t ARC_String_Find(ARC_String *string, ARC_String *substring){ if(!string || !substring){ arc_errno = ARC_ERRNO_NULL; @@ -128,7 +142,7 @@ uint64_t ARC_String_Find(ARC_String *string, ARC_String *substring){ uint64_t max = string->length - (substring->length - 1); for(uint64_t i = 0; max; i++, max--){ if(!strncmp(string->data + i, substring->data, substring->length)){ - return i + 1; + return i; } }