array added, needs fix for destroying

This commit is contained in:
herbglitch 2025-03-24 18:50:15 -06:00
parent a9ddb5aa00
commit e2c1a25cca

View file

@ -1,9 +1,12 @@
#include "arc/std/config.h" #include "arc/std/config.h"
#include "arc/std/parser/helpers.h" #include "arc/std/array.h"
#include "arc/std/bool.h" #include "arc/std/bool.h"
#include "arc/std/errno.h" #include "arc/std/errno.h"
#include "arc/std/hashtable.h" #include "arc/std/hashtable.h"
#include "arc/std/parser.h" #include "arc/std/parser.h"
#include "arc/std/vector.h"
#include "arc/std/parser/helpers.h"
#include "arc/std/vector/inline.h"
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
@ -338,10 +341,6 @@ void ARC_ConfigData_RunVariableLineTag(ARC_ParserTagToken *tagToken, ARC_Config
ARC_String_Create(&variableString, NULL, 0); ARC_String_Create(&variableString, NULL, 0);
ARC_ParserData_HelperRecurseStringAdd(&variableString, childTagToken); 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 //check if removing
if(config->load == ARC_False){ if(config->load == ARC_False){
ARC_Hashtable_Remove(config->currentGroup, (void *)variableString->data); 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'; nameVariableCStr[variableString->length] = '\0';
ARC_String_Destroy(variableString); 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 <value> //get <value>
childTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(tagToken->tagTokens, 9); childTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(tagToken->tagTokens, 9);
@ -387,9 +390,64 @@ void ARC_ConfigData_RunVariableLineTag(ARC_ParserTagToken *tagToken, ARC_Config
return; 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;
}
//<nestedValue> -> OPEN_CURLY_BRACE <whitespace> <valueArgs> <whitespace> 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);
//<valueArgs> -> <value> <whitespace> COMMA <whitespace> <valueArgs> | <value>
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 //passed the parsed value into the copy type function and set the destroy function
type->copyFn(&(typeData->data), childTagToken, config, type->userdata); type->copyFn(&(typeData->data), childTagToken, config, type->userdata);
typeData->destroyFn = type->destroyFn;
//add to the current group hashtable //add to the current group hashtable
ARC_Hashtable_Add(config->currentGroup, (void *)nameVariableCStr, (void *)typeData); ARC_Hashtable_Add(config->currentGroup, (void *)nameVariableCStr, (void *)typeData);