From e2c1a25cca826beb172a2cc90fe41578b8e4625c Mon Sep 17 00:00:00 2001 From: herbglitch Date: Mon, 24 Mar 2025 18:50:15 -0600 Subject: [PATCH] array added, needs fix for destroying --- src/std/config.c | 70 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 64 insertions(+), 6 deletions(-) diff --git a/src/std/config.c b/src/std/config.c index 8d89660..7ad6868 100644 --- a/src/std/config.c +++ b/src/std/config.c @@ -1,9 +1,12 @@ #include "arc/std/config.h" -#include "arc/std/parser/helpers.h" +#include "arc/std/array.h" #include "arc/std/bool.h" #include "arc/std/errno.h" #include "arc/std/hashtable.h" #include "arc/std/parser.h" +#include "arc/std/vector.h" +#include "arc/std/parser/helpers.h" +#include "arc/std/vector/inline.h" #include #include #include @@ -338,10 +341,6 @@ void ARC_ConfigData_RunVariableLineTag(ARC_ParserTagToken *tagToken, ARC_Config ARC_String_Create(&variableString, NULL, 0); ARC_ParserData_HelperRecurseStringAdd(&variableString, childTagToken); - //check if the value is an array - childTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(tagToken->tagTokens, 5); - //ARC_Bool isArray = (ARC_Bool)(ARC_Vector_GetSize(childTagToken->tagTokens) != 0); - //check if removing if(config->load == ARC_False){ ARC_Hashtable_Remove(config->currentGroup, (void *)variableString->data); @@ -365,6 +364,10 @@ void ARC_ConfigData_RunVariableLineTag(ARC_ParserTagToken *tagToken, ARC_Config nameVariableCStr[variableString->length] = '\0'; ARC_String_Destroy(variableString); + //check if the value is an array + childTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(tagToken->tagTokens, 5); + ARC_Bool isArray = (ARC_Bool)(ARC_Vector_GetSize(childTagToken->tagTokens) != 0); + //get childTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(tagToken->tagTokens, 9); @@ -387,9 +390,64 @@ void ARC_ConfigData_RunVariableLineTag(ARC_ParserTagToken *tagToken, ARC_Config return; } + //set the type's destroy function + typeData->destroyFn = type->destroyFn; + + //if the value is an array loop through all the nested value's args + if(isArray == ARC_True){ + ARC_ParserTagToken *nestedValueTagToken = ARC_Vector_Get(childTagToken->tagTokens, 0); + if(nestedValueTagToken->id != ARC_CONFIG_NESTED_VALUE){ + ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("ARC_ConfigData_RunVariableLineTag(tagToken, config), variable \"%s\" was set to be an array but did not pass in a nested value { ... }", nameVariableCStr); + return; + } + + // -> OPEN_CURLY_BRACE CLOSE_CURLY_BRACE + ARC_ParserTagToken *valueArgsTagToken = ARC_Vector_Get(nestedValueTagToken->tagTokens, 2); + + //create a temporary vector to read in the array + ARC_VectorInline *typeVector; + ARC_VectorInline_Create(&typeVector, sizeof(void *), NULL, NULL); + + // -> COMMA | + while(valueArgsTagToken->id == ARC_CONFIG_VALUE_ARGS){ + ARC_ParserTagToken *valueTagToken = ARC_Vector_Get(valueArgsTagToken->tagTokens, 0); + + //copy the type and store it in the vector + void *typeData = NULL; + type->copyFn(&typeData, valueTagToken, config, type->userdata); + ARC_VectorInline_Add(typeVector, typeData); + + //if this value args was the last one break + if(ARC_Vector_GetSize(valueArgsTagToken->tagTokens) == 1){ + break; + } + + //get the next valueArgs + valueArgsTagToken = ARC_Vector_Get(valueArgsTagToken->tagTokens, 4); + } + + //copy the data in an ARC_Array + ARC_Array typeVectorArray = ARC_VectorInline_GetData(typeVector); + ARC_Array *array = (ARC_Array *)malloc(sizeof(ARC_Array)); + array->size = typeVectorArray.size; + array->data = NULL; + + if(typeVectorArray.size != 0){ + //copy the vector into the array's data + array->data = (void **)malloc(sizeof(void *) * typeVectorArray.size); + memcpy(array->data, typeVectorArray.data, typeVectorArray.size); + } + + //add the array to the group hashtable + ARC_Hashtable_Add(config->currentGroup, (void *)nameVariableCStr, (void *)array); + + //cleanup + ARC_VectorInline_Destroy(typeVector); + return; + } + //passed the parsed value into the copy type function and set the destroy function type->copyFn(&(typeData->data), childTagToken, config, type->userdata); - typeData->destroyFn = type->destroyFn; //add to the current group hashtable ARC_Hashtable_Add(config->currentGroup, (void *)nameVariableCStr, (void *)typeData);