From ce9e6a1c9a93738649894bf3a1575bcb25d90f31 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Thu, 20 Mar 2025 05:34:03 -0600 Subject: [PATCH] adding default standard types to config --- include/arc/std/config.h | 135 ++++++++++++++++++++++++++ src/std/config.c | 198 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 333 insertions(+) diff --git a/include/arc/std/config.h b/include/arc/std/config.h index 9bce5be..a6ddb9e 100644 --- a/include/arc/std/config.h +++ b/include/arc/std/config.h @@ -208,6 +208,141 @@ void ARC_Config_UnloadFromFile(ARC_Config *config, ARC_String *data); #define ARC_CONFIG_NUMBER_TAG 0x3A #define ARC_CONFIG_WHITESPACE 0x3B +/** + * @brief TODO: write this +*/ +void ARC_Config_InitStdTypes(ARC_Config *config); + +/** + * @brief +*/ +void ARC_ConfigType_BoolCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata); + +/** + * @brief +*/ +void ARC_ConfigType_BoolDestroyFn(void *type); + +/** + * @brief +*/ +void ARC_ConfigType_CharCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata); + +/** + * @brief +*/ +void ARC_ConfigType_CharDestroyFn(void *type); + +/** + * @brief +*/ +void ARC_ConfigType_Int8CopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata); + +/** + * @brief +*/ +void ARC_ConfigType_Int8DestroyFn(void *type); + +/** + * @brief +*/ +void ARC_ConfigType_Uint8CopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata); + +/** + * @brief +*/ +void ARC_ConfigType_Uint8DestroyFn(void *type); + +/** + * @brief +*/ +void ARC_ConfigType_Int16CopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata); + +/** + * @brief +*/ +void ARC_ConfigType_Int16DestroyFn(void *type); + +/** + * @brief +*/ +void ARC_ConfigType_Uint16CopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata); + +/** + * @brief +*/ +void ARC_ConfigType_Uint16DestroyFn(void *type); + +/** + * @brief +*/ +void ARC_ConfigType_Int32CopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata); + +/** + * @brief +*/ +void ARC_ConfigType_Int32DestroyFn(void *type); + +/** + * @brief +*/ +void ARC_ConfigType_Uint32CopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata); + +/** + * @brief +*/ +void ARC_ConfigType_Uint32DestroyFn(void *type); + +/** + * @brief +*/ +void ARC_ConfigType_Int64CopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata); + +/** + * @brief +*/ +void ARC_ConfigType_Int64DestroyFn(void *type); + +/** + * @brief +*/ +void ARC_ConfigType_Uint64CopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata); + +/** + * @brief +*/ +void ARC_ConfigType_Uint64DestroyFn(void *type); + +/** + * @brief +*/ +void ARC_ConfigType_FloatCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata); + +/** + * @brief +*/ +void ARC_ConfigType_FloatDestroyFn(void *type); + +/** + * @brief +*/ +void ARC_ConfigType_DoubleCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata); + +/** + * @brief +*/ +void ARC_ConfigType_DoubleDestroyFn(void *type); + +/** + * @brief +*/ +void ARC_ConfigType_StringCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata); + +/** + * @brief +*/ +void ARC_ConfigType_StringDestroyFn(void *type); + #ifdef __cplusplus } #endif diff --git a/src/std/config.c b/src/std/config.c index c04b1b8..6e013f6 100644 --- a/src/std/config.c +++ b/src/std/config.c @@ -760,3 +760,201 @@ void ARC_Config_UnloadFromFile(ARC_Config *config, ARC_String *path){ ARC_Parser_ParseFile(config->parser, path); } +void ARC_Config_InitStdTypes(ARC_Config *config){ + //ARC_Config_RegisterTypeWithCStr(config, "bool" , (ARC_ConfigType){ ARC_ConfigType_BoolCopyFn , ARC_ConfigType_BoolDestroyFn , NULL }); + //ARC_Config_RegisterTypeWithCStr(config, "int8" , (ARC_ConfigType){ ARC_ConfigType_Int8CopyFn , ARC_ConfigType_Int8DestroyFn , NULL }); + ARC_Config_RegisterTypeWithCStr(config, "uint8" , (ARC_ConfigType){ ARC_ConfigType_Uint8CopyFn , ARC_ConfigType_Uint8DestroyFn , NULL }); + //ARC_Config_RegisterTypeWithCStr(config, "int16" , (ARC_ConfigType){ ARC_ConfigType_Int16CopyFn , ARC_ConfigType_Int16DestroyFn , NULL }); + ARC_Config_RegisterTypeWithCStr(config, "uint16" , (ARC_ConfigType){ ARC_ConfigType_Uint16CopyFn, ARC_ConfigType_Uint16DestroyFn, NULL }); + //ARC_Config_RegisterTypeWithCStr(config, "int32" , (ARC_ConfigType){ ARC_ConfigType_Int32CopyFn , ARC_ConfigType_Int32DestroyFn , NULL }); + ARC_Config_RegisterTypeWithCStr(config, "uint32" , (ARC_ConfigType){ ARC_ConfigType_Uint32CopyFn, ARC_ConfigType_Uint32DestroyFn, NULL }); + //ARC_Config_RegisterTypeWithCStr(config, "int64" , (ARC_ConfigType){ ARC_ConfigType_Int64CopyFn , ARC_ConfigType_Int64DestroyFn , NULL }); + ARC_Config_RegisterTypeWithCStr(config, "uint64" , (ARC_ConfigType){ ARC_ConfigType_Uint64CopyFn, ARC_ConfigType_Uint64DestroyFn, NULL }); + //ARC_Config_RegisterTypeWithCStr(config, "float" , (ARC_ConfigType){ ARC_ConfigType_FloatCopyFn , ARC_ConfigType_FloatDestroyFn , NULL }); + //ARC_Config_RegisterTypeWithCStr(config, "double" , (ARC_ConfigType){ ARC_ConfigType_DoubleCopyFn, ARC_ConfigType_DoubleDestroyFn, NULL }); + ARC_Config_RegisterTypeWithCStr(config, "ARC_String", (ARC_ConfigType){ ARC_ConfigType_StringCopyFn, ARC_ConfigType_StringDestroyFn, NULL }); +} + +void ARC_ConfigType_BoolCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata){ + //TODO: write this +} + +void ARC_ConfigType_BoolDestroyFn(void *type){ + //TODO: write this +} + +//private function to make checking ints much easier +void ARC_ConfigType_IntNumberHelperCopyFn(void **type, ARC_ParserTagToken *parsedData, uint64_t maxSize){ + //TODO: write this +} + +//private function to make checking uints much easier +void ARC_ConfigType_UintNumberHelperCopyFn(void **type, ARC_ParserTagToken *parsedData, uint64_t maxSize){ + //go into the tag + ARC_ParserTagToken *childTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(parsedData->tagTokens, 0); + if(childTagToken->id != ARC_CONFIG_NUMBER_SIGN){ + arc_errno = ARC_ERRNO_DATA; + ARC_DEBUG_LOG_ERROR("ARC_ConfigType_UintNumberHelperCopyFn(type, parsedData, config), parsed data was not a "); + *type = NULL; + return; + } + + if(childTagToken->id == ARC_CONFIG_MINUS){ + arc_errno = ARC_ERRNO_DATA; + ARC_DEBUG_LOG_ERROR("ARC_ConfigType_UintNumberHelperCopyFn(type, parsedData, config), parsed data had a minus sign, uint numbers must be positive"); + *type = NULL; + return; + } + + //check if the first tag is a minus sign and create a string starting with that if it is + childTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(childTagToken->tagTokens, 0); + ARC_String *uintString; + ARC_String_Create(&uintString, NULL, 0); + + //get the number as a string + ARC_ParserData_HelperRecurseStringAdd(&uintString, parsedData); + + //TODO: check that this gives the right number + //get the length of the max size + uint32_t maxSizeLength = 0; + for(uint32_t size = maxSize; 0 < size; size /= 10){ + maxSizeLength++; + } + + //if the string is bigger than the possible size return NULL and error + if(uintString->length > maxSizeLength){ + arc_errno = ARC_ERRNO_DATA; + ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("ARC_ConfigType_UintNumberHelperCopyFn(type, parsedData, config), size \"%s\" was bigger than the max %lu", uintString->data, maxSize); + *type = NULL; + ARC_String_Destroy(uintString); + return; + } + + if(uintString->length == maxSize){ + //the max size of a uint64 is 8,446,744,073,709,551,615 so 19 will be big enough for the size + char maxuintCStr[19]; + sprintf(maxuintCStr, "%lu", maxSize); + + //check that the number is less than the max + int8_t stringIndex = maxSize; + for(int8_t index = maxSize + 1; index >= 0; index--, stringIndex--){ + if(uintString->data[stringIndex] > maxuintCStr[index]){ + arc_errno = ARC_ERRNO_DATA; + ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("ARC_ConfigType_UintNumberHelperCopyFn(type, parsedData, config), size \"%s\" was bigger than the max %lu", uintString->data, maxSize); + *type = NULL; + ARC_String_Destroy(uintString); + return; + } + + //if the number is smaller it is safe to stop checking + if(uintString->data[stringIndex] < maxuintCStr[index]){ + break; + } + } + } + + //copy the int value + *type = malloc(maxSizeLength); + uint64_t valueAsUint64 = ARC_String_ToUint64_t(uintString); + memcpy(*type, &valueAsUint64, maxSizeLength); + + //cleanup + ARC_String_Destroy(uintString); +} + +void ARC_ConfigType_Int8CopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata){ + ARC_ConfigType_IntNumberHelperCopyFn(type, parsedData, ~(uint8_t)0); +} + +void ARC_ConfigType_Int8DestroyFn(void *type){ + free((int8_t *)type); +} + +void ARC_ConfigType_Uint8CopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata){ + ARC_ConfigType_UintNumberHelperCopyFn(type, parsedData, ~(uint8_t)0); +} + +void ARC_ConfigType_Uint8DestroyFn(void *type){ + free((uint8_t *)type); +} + +void ARC_ConfigType_Int16CopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata){ + ARC_ConfigType_IntNumberHelperCopyFn(type, parsedData, ~(uint16_t)0); +} + +void ARC_ConfigType_Int16DestroyFn(void *type){ + free((int16_t *)type); +} + +void ARC_ConfigType_Uint16CopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata){ + ARC_ConfigType_UintNumberHelperCopyFn(type, parsedData, ~(uint16_t)0); +} + +void ARC_ConfigType_Uint16DestroyFn(void *type){ + free((uint16_t *)type); +} + +void ARC_ConfigType_Int32CopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata){ + ARC_ConfigType_IntNumberHelperCopyFn(type, parsedData, ~(uint32_t)0); +} + +void ARC_ConfigType_Int32DestroyFn(void *type){ + free((int32_t *)type); +} + +void ARC_ConfigType_Uint32CopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata){ + ARC_ConfigType_UintNumberHelperCopyFn(type, parsedData, ~(uint32_t)0); +} + +void ARC_ConfigType_Uint32DestroyFn(void *type){ + free((uint32_t *)type); +} + +void ARC_ConfigType_Int64CopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata){ + ARC_ConfigType_IntNumberHelperCopyFn(type, parsedData, ~(uint64_t)0); +} + +void ARC_ConfigType_Int64DestroyFn(void *type){ + free((int64_t *)type); +} + +void ARC_ConfigType_Uint64CopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata){ + ARC_ConfigType_UintNumberHelperCopyFn(type, parsedData, ~(uint64_t)0); +} + +void ARC_ConfigType_Uint64DestroyFn(void *type){ + free((uint64_t *)type); +} + +void ARC_ConfigType_FloatCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata){ + //TODO: write this +} + +void ARC_ConfigType_FloatDestroyFn(void *type){ + //TODO: write this +} + +void ARC_ConfigType_DoubleCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata){ + //TODO: write this +} + +void ARC_ConfigType_DoubleDestroyFn(void *type){ + //TODO: write this +} + +void ARC_ConfigType_StringCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata){ + if(parsedData->id != ARC_CONFIG_STRING){ + arc_errno = ARC_ERRNO_DATA; + ARC_DEBUG_LOG_ERROR("ARC_ConfigType_StringCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata), string was not passed in for ARC_String"); + type = NULL; + return; + } + + //get the string chars between the quotes + ARC_ParserTagToken *stringCharsTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(parsedData->tagTokens, 1); + ARC_ParserData_HelperRecurseStringAdd((ARC_String **)type, stringCharsTagToken); +} + +void ARC_ConfigType_StringDestroyFn(void *type){ + ARC_String_Destroy(type); +}