updated math config, added int to standard config, added bool to standard config

This commit is contained in:
herbglitch 2025-03-22 03:38:28 -06:00
parent ce9e6a1c9a
commit 0dab5f336b
5 changed files with 299 additions and 252 deletions

View file

@ -81,6 +81,7 @@ set(ARCHEUS_SOURCES
src/std/vector/inline.c src/std/vector/inline.c
src/math/circle.c src/math/circle.c
src/math/config.c
src/math/obround.c src/math/obround.c
src/math/point.c src/math/point.c
src/math/rectangle.c src/math/rectangle.c

View file

@ -6,18 +6,35 @@ extern "C" {
#endif #endif
#include <stdint.h> #include <stdint.h>
#include "arc/std/string.h"
#include "arc/std/config.h"
typedef struct ARC_Config ARC_Config; typedef struct ARC_Config ARC_Config;
//void ARC_MathConfig_Init(ARC_Config *config);
// /**
//uint8_t ARC_Point_Read (ARC_Config *config, ARC_String *string, void **value); * @brief
//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); void ARC_Config_InitMath(ARC_Config *config);
//
//void ARC_Point_Delete (ARC_Config *config, ARC_String *string, void *value); /**
//void ARC_Rect_Delete (ARC_Config *config, ARC_String *string, void *value); * @brief
//void ARC_RectArray_Delete(ARC_Config *config, ARC_String *string, void *value); */
void ARC_ConfigType_PointCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata);
/**
* @brief
*/
void ARC_ConfigType_PointDestroyFn(void *type);
/**
* @brief
*/
void ARC_ConfigType_RectCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata);
/**
* @brief
*/
void ARC_ConfigType_RectDestroyFn(void *type);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -211,7 +211,7 @@ void ARC_Config_UnloadFromFile(ARC_Config *config, ARC_String *data);
/** /**
* @brief TODO: write this * @brief TODO: write this
*/ */
void ARC_Config_InitStdTypes(ARC_Config *config); void ARC_Config_InitStd(ARC_Config *config);
/** /**
* @brief * @brief

View file

@ -8,269 +8,185 @@
#include "arc/math/point.h" #include "arc/math/point.h"
#include "arc/math/rectangle.h" #include "arc/math/rectangle.h"
// #define ARC_DEFAULT_CONFIG void ARC_Config_InitMath(ARC_Config *config){
#include "arc/std/defaults/config.h" ARC_Config_RegisterTypeWithCStr(config, "ARC_Point", (ARC_ConfigType){ ARC_ConfigType_PointCopyFn, ARC_ConfigType_PointDestroyFn, NULL });
ARC_Config_RegisterTypeWithCStr(config, "ARC_Rect" , (ARC_ConfigType){ ARC_ConfigType_RectCopyFn , ARC_ConfigType_RectDestroyFn , NULL });
void ARC_MathConfig_Init(ARC_Config *config){
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);
} }
uint64_t ARC_MathConfig_GetIndexAndErrorCheck(ARC_String *string, char *search, uint64_t searchLength){ void ARC_ConfigType_PointCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata){
uint64_t separator = ARC_String_FindCString(string, ",", 1); ARC_ParserTagToken *childTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(parsedData->tagTokens, 0);
if(childTagToken->id != ARC_CONFIG_NESTED_VALUE){
if(separator == ~(uint64_t)0){
arc_errno = ARC_ERRNO_DATA; arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR("ARC_ConfigType_PointCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata), nestedValue was not passed in for ARC_Point");
type = NULL;
return;
} }
return separator; //get the valueArgs
} ARC_ParserTagToken *valueArgsTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(childTagToken->tagTokens, 2);
uint8_t ARC_Point_Read(ARC_Config *config, ARC_String *string, void **value){ //if there is only a value with no comma break
ARC_Config_Get(config, string, value); if(ARC_Vector_GetSize(valueArgsTagToken->tagTokens) == 1){
if(*value){
return 1;
}
if(string->data[0] != '{' || string->data[string->length - 1] != '}'){
ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("in ARC_Point_Read(config, string, value); no matching curly braces: %s", string->data);
arc_errno = ARC_ERRNO_DATA; arc_errno = ARC_ERRNO_DATA;
return 0; ARC_DEBUG_LOG_ERROR("ARC_ConfigType_PointCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata), nested value { x } only had one value, point needs two { x, y }");
return;
} }
uint64_t separator = ARC_MathConfig_GetIndexAndErrorCheck(string, ",", 1); //get the first value
if(arc_errno){ ARC_ParserTagToken *valueTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(valueArgsTagToken->tagTokens, 0);
return 0; int32_t *pointTemp = NULL;
ARC_ConfigType_Int32CopyFn((void **)&pointTemp, valueTagToken, config, userdata);
if(pointTemp == NULL){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR("ARC_ConfigType_PointCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata), first parameter was not a int32");
return;
} }
ARC_String *xString, *yString; //copy the xpoint value to stack and free the pointer
ARC_String_CopySubstring(&xString, string, 1 , separator - 1 ); int32_t pointX = *pointTemp;
ARC_String_CopySubstring(&yString, string, separator + 1, string->length - (separator + 2)); free(pointTemp);
pointTemp = NULL;
//get the second value
valueArgsTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(valueArgsTagToken->tagTokens, 4);
//if there is an empty comma break
if(ARC_Vector_GetSize(valueArgsTagToken->tagTokens) > 1){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR("ARC_ConfigType_PointCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata), nested value { x, y, ... } had too many values, point needs two { x, y }");
return;
}
//get the second value
valueTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(valueArgsTagToken->tagTokens, 0);
ARC_ConfigType_Int32CopyFn((void **)&pointTemp, valueTagToken, config, userdata);
if(pointTemp == NULL){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR("ARC_ConfigType_PointCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata), second parameter was not a int32");
return;
}
//copy the last value and free the temp value
ARC_Point *point = (ARC_Point *)malloc(sizeof(ARC_Point)); ARC_Point *point = (ARC_Point *)malloc(sizeof(ARC_Point));
point->x = (int32_t)ARC_String_ToInt64_t(xString); *point = (ARC_Point){ pointX, *pointTemp };
point->y = (int32_t)ARC_String_ToInt64_t(yString); free(pointTemp);
ARC_String_Destroy(xString); //set the type value
ARC_String_Destroy(yString); *type = (void *)point;
*value = point;
return 0;
} }
uint8_t ARC_Rect_Read(ARC_Config *config, ARC_String *string, void **value){ void ARC_ConfigType_PointDestroyFn(void *type){
ARC_Config_Get(config, string, value); free((ARC_Point *)type);
if(*value){
return 1;
} }
if(string->data[0] != '{' || string->data[string->length - 1] != '}'){ void ARC_ConfigType_RectCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata){
ARC_ParserTagToken *childTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(parsedData->tagTokens, 0);
if(childTagToken->id != ARC_CONFIG_NESTED_VALUE){
arc_errno = ARC_ERRNO_DATA; arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("in ARC_Rect_Read(config, string, value); no matching curly braces: %s", string->data); ARC_DEBUG_LOG_ERROR("ARC_ConfigType_RectCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata), nestedValue was not passed in for ARC_Point");
return 0; type = NULL;
}
ARC_String *current;
ARC_String_CopySubstring(&current, string, 1, string->length - 2);
ARC_String *temp, *tempStripped;
int32_t x, y, w, h;
int64_t separator;
//x
separator = ARC_MathConfig_GetIndexAndErrorCheck(current, ",", 1);
if(arc_errno){
return 0;
}
ARC_String_CopySubstring(&temp, current, 0, separator - 1);
ARC_String_StripEndsWhitespace(&tempStripped, temp);
x = ARC_String_ToInt64_t(tempStripped);
ARC_String_Destroy(temp);
ARC_String_Destroy(tempStripped);
temp = current;
ARC_String_CopySubstring(&current, temp, separator + 1, temp->length - (separator + 1));
ARC_String_Destroy(temp);
//y
separator = ARC_MathConfig_GetIndexAndErrorCheck(current, ",", 1);
if(arc_errno){
return 0;
}
ARC_String_CopySubstring(&temp, current, 0, separator - 1);
ARC_String_StripEndsWhitespace(&tempStripped, temp);
y = ARC_String_ToInt64_t(tempStripped);
ARC_String_Destroy(temp);
ARC_String_Destroy(tempStripped);
temp = current;
ARC_String_CopySubstring(&current, temp, separator + 1, temp->length - (separator + 1));
ARC_String_Destroy(temp);
//w
separator = ARC_MathConfig_GetIndexAndErrorCheck(current, ",", 1);
if(arc_errno){
return 0;
}
ARC_String_CopySubstring(&temp, current, 0, separator - 1);
ARC_String_StripEndsWhitespace(&tempStripped, temp);
w = ARC_String_ToInt64_t(tempStripped);
ARC_String_Destroy(temp);
ARC_String_Destroy(tempStripped);
temp = current;
ARC_String_CopySubstring(&current, temp, separator + 1, temp->length - (separator + 1));
ARC_String_Destroy(temp);
//h
separator = current->length;
if(arc_errno){
return 0;
}
ARC_String_CopySubstring(&temp, current, 0, separator);
ARC_String_StripEndsWhitespace(&tempStripped, temp);
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;
}
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(&substr, temp);
ARC_String_Destroy(temp);
// 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; return;
} }
//reading in value //get the valueArgs
ARC_Rect_Read(config, substr, (void **) &tempRect); ARC_ParserTagToken *valueArgsTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(childTagToken->tagTokens, 2);
if(arc_errno){
ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("in ARC_RectArray_ReadRect(config, string, index, length, arrayIndex, value); failed to read rect: %s", substr->data); //if there is only a value with no comma break
ARC_String_Destroy(substr); if(ARC_Vector_GetSize(valueArgsTagToken->tagTokens) == 1){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR("ARC_ConfigType_RectCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata), nested value { x } only had one value, rect needs four { x, y, w, h }");
return; return;
} }
((ARC_Rect *)((ARC_Array *) *value)->data)[*arrayIndex] = *tempRect; //get the first value
++*arrayIndex; ARC_ParserTagToken *valueTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(valueArgsTagToken->tagTokens, 0);
int32_t *pointTemp = NULL;
ARC_Rect_Delete(config, substr, (void *)tempRect); ARC_ConfigType_Int32CopyFn((void **)&pointTemp, valueTagToken, config, userdata);
ARC_String_Destroy(substr); if(pointTemp == NULL){
}
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; arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("in ARC_RectArray_Read(config, string, value); no matching curly braces: %s", string->data); ARC_DEBUG_LOG_ERROR("ARC_ConfigType_PointCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata), first parameter was not a int32");
return 0; return;
} }
ARC_String *temp, *stripped; //copy the xpoint value to stack and free the pointer
ARC_String_CopySubstring(&temp, string, 1, string->length - 2); int32_t pointX = *pointTemp;
ARC_String_StripEndsWhitespace(&stripped, temp); free(pointTemp);
ARC_String_Destroy(temp); pointTemp = NULL;
uint64_t arraySize = 1; //get the second value
int64_t encapsulated = 0; valueArgsTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(valueArgsTagToken->tagTokens, 4);
for(uint64_t i = 0; i < stripped->length; i++){
if(stripped->data[i] == '{'){
encapsulated++;
continue;
}
if(stripped->data[i] == '}'){ //if there is only a value with no comma break
encapsulated--; if(ARC_Vector_GetSize(valueArgsTagToken->tagTokens) == 1){
continue;
}
if(!encapsulated && stripped->data[i] == ','){
arraySize++;
}
}
if(encapsulated){
arc_errno = ARC_ERRNO_DATA; arc_errno = ARC_ERRNO_DATA;
//TODO: Fix this for windows SMFH ARC_DEBUG_LOG_ERROR("ARC_ConfigType_RectCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata), nested value { x, y } only had two values, rect needs four { x, y, w, h }");
// ARC_DEBUG_LOG(arc_errno, "in ARC_RectArray_Read(config, data, subdata, value); after looping encapsulated was %ld", encapsulated); return;
ARC_String_Destroy(stripped);
return 0;
} }
*value = malloc(sizeof(ARC_Array)); //get the second value
((ARC_Array *) *value)->data = malloc(sizeof(ARC_Rect) * arraySize); valueTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(valueArgsTagToken->tagTokens, 0);
((ARC_Array *) *value)->size = arraySize; ARC_ConfigType_Int32CopyFn((void **)&pointTemp, valueTagToken, config, userdata);
if(pointTemp == NULL){
uint64_t index = 0; arc_errno = ARC_ERRNO_DATA;
arraySize = 0; ARC_DEBUG_LOG_ERROR("ARC_ConfigType_RectCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata), second parameter was not a int32");
encapsulated = 0; return;
for(uint64_t i = 0; i < stripped->length; i++){
if(stripped->data[i] == '{'){
encapsulated++;
continue;
} }
if(stripped->data[i] == '}'){ int32_t pointY = *pointTemp;
encapsulated--; free(pointTemp);
continue; pointTemp = NULL;
//get the third value
valueArgsTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(valueArgsTagToken->tagTokens, 4);
//if there is an empty comma break
if(ARC_Vector_GetSize(valueArgsTagToken->tagTokens) == 1){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR("ARC_ConfigType_RectCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata), nested value { x, y, w } only had three values, rect needs four { x, y, w, h }");
return;
} }
if(!encapsulated && stripped->data[i] == ','){ //get the third value
ARC_RectArray_ReadRect(config, stripped, index, i - index, &arraySize, value); valueTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(valueArgsTagToken->tagTokens, 0);
if(arc_errno){ ARC_ConfigType_Int32CopyFn((void **)&pointTemp, valueTagToken, config, userdata);
return 0; if(pointTemp == NULL){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR("ARC_ConfigType_RectCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata), third parameter was not a int32");
return;
} }
index = i + 1; int32_t pointW = *pointTemp;
free(pointTemp);
pointTemp = NULL;
if(arraySize == ((ARC_Array *) *value)->size){ //get the fourth value
break; valueArgsTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(valueArgsTagToken->tagTokens, 4);
}
} //if there is an empty comma break
if(ARC_Vector_GetSize(valueArgsTagToken->tagTokens) > 1){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR("ARC_ConfigType_RectCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata), nested value { x, y, w, h, ... } had too many values, rect needs four { x, y, w, h }");
return;
} }
if(arraySize != ((ARC_Array *) *value)->size){ //get the fourth value
ARC_RectArray_ReadRect(config, stripped, index, stripped->length - index, &arraySize, value); valueTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(valueArgsTagToken->tagTokens, 0);
} ARC_ConfigType_Int32CopyFn((void **)&pointTemp, valueTagToken, config, userdata);
ARC_String_Destroy(stripped); if(pointTemp == NULL){
return 0; arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR("ARC_ConfigType_RectCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata), fourth parameter was not a int32");
return;
} }
void ARC_Point_Delete(ARC_Config* config, ARC_String *string, void *value){ //copy the last value and free the temp value
free((ARC_Point *)value); ARC_Rect *point = (ARC_Rect *)malloc(sizeof(ARC_Point));
*point = (ARC_Rect){ pointX, pointY, pointW, *pointTemp };
free(pointTemp);
//set the type value
*type = (void *)point;
} }
void ARC_Rect_Delete(ARC_Config* config, ARC_String *string, void *value){ void ARC_ConfigType_RectDestroyFn(void *type){
free((ARC_Rect *)value); free((ARC_Rect *)type);
}
void ARC_RectArray_Delete(ARC_Config* config, ARC_String *string, void *value){
//TODO free value->data
free((ARC_Array *)value);
} }

View file

@ -566,7 +566,7 @@ void ARC_Config_Create(ARC_Config **config){
"<type> -> <variable>\n" "<type> -> <variable>\n"
"<value> -> <variable> | <numberSign> | <string> | <nestedValue>\n" "<value> -> <variable> | <numberSign> | <string> | <nestedValue>\n"
"<nestedValue> -> OPEN_CURLY_BRACE <whitespace> <valueArgs> <whitespace> CLOSE_CURLY_BRACE\n" "<nestedValue> -> OPEN_CURLY_BRACE <whitespace> <valueArgs> <whitespace> CLOSE_CURLY_BRACE\n"
"<valueArgs> -> <value> COMMA <valueArgs> | <value>\n" "<valueArgs> -> <value> <whitespace> COMMA <whitespace> <valueArgs> | <value>\n"
"<variable> -> ALPHA_UPPER_CHAR <variableName> | ALPHA_LOWER_CHAR <variableName> | UNDERSCORE <variableName>\n" "<variable> -> ALPHA_UPPER_CHAR <variableName> | ALPHA_LOWER_CHAR <variableName> | UNDERSCORE <variableName>\n"
"<variableName> -> <variableChar> <variableName> | LAMBDA\n" "<variableName> -> <variableChar> <variableName> | LAMBDA\n"
@ -760,15 +760,15 @@ void ARC_Config_UnloadFromFile(ARC_Config *config, ARC_String *path){
ARC_Parser_ParseFile(config->parser, path); ARC_Parser_ParseFile(config->parser, path);
} }
void ARC_Config_InitStdTypes(ARC_Config *config){ void ARC_Config_InitStd(ARC_Config *config){
//ARC_Config_RegisterTypeWithCStr(config, "bool" , (ARC_ConfigType){ ARC_ConfigType_BoolCopyFn , ARC_ConfigType_BoolDestroyFn , NULL }); 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, "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, "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, "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, "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, "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, "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, "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, "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, "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, "double" , (ARC_ConfigType){ ARC_ConfigType_DoubleCopyFn, ARC_ConfigType_DoubleDestroyFn, NULL });
@ -776,16 +776,129 @@ void ARC_Config_InitStdTypes(ARC_Config *config){
} }
void ARC_ConfigType_BoolCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata){ void ARC_ConfigType_BoolCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata){
//TODO: write this //go into the <variable> tag
ARC_ParserTagToken *childTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(parsedData->tagTokens, 0);
if(childTagToken->id != ARC_CONFIG_VARIABLE){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR("ARC_ConfigType_BoolNumberHelperCopyFn(type, parsedData, config), parsed data was not a <numberSign>");
*type = NULL;
return;
}
//get the value as a string
ARC_String *value;
ARC_String_Create(&value, NULL, 0);
ARC_ParserData_HelperRecurseStringAdd(&value, childTagToken);
if(ARC_String_EqualsCStringWithStrlen(value, "true") == ARC_True){
ARC_Bool *boolValue = (ARC_Bool *)malloc(sizeof(ARC_Bool));
*boolValue = ARC_True;
ARC_String_Destroy(value);
*type = boolValue;
return;
}
if(ARC_String_EqualsCStringWithStrlen(value, "false") == ARC_True){
ARC_Bool *boolValue = (ARC_Bool *)malloc(sizeof(ARC_Bool));
*boolValue = ARC_False;
ARC_String_Destroy(value);
*type = boolValue;
return;
}
//error if true or false was not passed in
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("ARC_ConfigType_BoolNumberHelperCopyFn(type, parsedData, config), bool can only be \"true\" or \"false\" but was given \"%s\"", value->data);
ARC_String_Destroy(value);
*type = NULL;
} }
void ARC_ConfigType_BoolDestroyFn(void *type){ void ARC_ConfigType_BoolDestroyFn(void *type){
//TODO: write this free((ARC_Bool *)type);
} }
//private function to make checking ints much easier //private function to make checking ints much easier
void ARC_ConfigType_IntNumberHelperCopyFn(void **type, ARC_ParserTagToken *parsedData, uint64_t maxSize){ void ARC_ConfigType_IntNumberHelperCopyFn(void **type, ARC_ParserTagToken *parsedData, uint64_t maxSize){
//TODO: write this //go into the <numberSign> 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_IntNumberHelperCopyFn(type, parsedData, config), parsed data was not a <numberSign>");
*type = NULL;
return;
}
//get the max positive size
uint64_t maxPositiveSize = maxSize / 2;
//set the max size to its max for positive or negative
int64_t maxSignSize = maxSize / 2;
if(childTagToken->id != ARC_CONFIG_NUMBER_SIGN){
maxSize *= 1;
maxSize--;
}
//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 *intString;
ARC_String_Create(&intString, NULL, 0);
//get the number as a string
ARC_ParserData_HelperRecurseStringAdd(&intString, parsedData);
//TODO: check that this gives the right number
//get the length of the max size
uint32_t maxSizeLength = 0;
for(uint32_t size = maxPositiveSize; 0 < size; size /= 10){
maxSizeLength++;
}
if(childTagToken->id != ARC_CONFIG_NUMBER_SIGN){
maxSizeLength++;
}
//if the string is bigger than the possible size return NULL and error
if(intString->length > maxSizeLength){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("ARC_ConfigType_IntNumberHelperCopyFn(type, parsedData, config), size \"%s\" was bigger or smaller than than the max %lu or min %lu", intString->data, maxPositiveSize, (maxPositiveSize * -1) - 1);
*type = NULL;
ARC_String_Destroy(intString);
return;
}
if(intString->length == maxSize){
//the max size of a uint64 is -9,223,372,036,854,775,808 so 20 will be big enough for the size
char maxintCStr[20];
sprintf(maxintCStr, "%ld", maxSignSize);
printf("value %s\n", maxintCStr);
//check that the number is less than the max
int8_t stringIndex = maxSize;
for(int8_t index = maxSize + 1; index >= 0; index--, stringIndex--){
if(intString->data[stringIndex] > maxintCStr[index]){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("ARC_ConfigType_IntNumberHelperCopyFn(type, parsedData, config), size \"%s\" was bigger than the max of the signed value %lu", intString->data, maxSize);
*type = NULL;
ARC_String_Destroy(intString);
return;
}
//if the number is smaller it is safe to stop checking
if(intString->data[stringIndex] < maxintCStr[index]){
break;
}
}
}
//copy the int value
*type = malloc(maxSizeLength);
uint64_t valueAsUint64 = ARC_String_ToInt64_t(intString);
memcpy(*type, &valueAsUint64, maxSizeLength);
//cleanup
ARC_String_Destroy(intString);
} }
//private function to make checking uints much easier //private function to make checking uints much easier
@ -831,8 +944,8 @@ void ARC_ConfigType_UintNumberHelperCopyFn(void **type, ARC_ParserTagToken *pars
} }
if(uintString->length == maxSize){ 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 //the max size of a uint64 is 8,446,744,073,709,551,615 so 20 will be big enough for the size
char maxuintCStr[19]; char maxuintCStr[20];
sprintf(maxuintCStr, "%lu", maxSize); sprintf(maxuintCStr, "%lu", maxSize);
//check that the number is less than the max //check that the number is less than the max