updated math config, added int to standard config, added bool to standard config
This commit is contained in:
parent
ce9e6a1c9a
commit
0dab5f336b
5 changed files with 299 additions and 252 deletions
|
|
@ -8,269 +8,185 @@
|
|||
#include "arc/math/point.h"
|
||||
#include "arc/math/rectangle.h"
|
||||
|
||||
// #define ARC_DEFAULT_CONFIG
|
||||
#include "arc/std/defaults/config.h"
|
||||
|
||||
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);
|
||||
void ARC_Config_InitMath(ARC_Config *config){
|
||||
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 });
|
||||
}
|
||||
|
||||
uint64_t ARC_MathConfig_GetIndexAndErrorCheck(ARC_String *string, char *search, uint64_t searchLength){
|
||||
uint64_t separator = ARC_String_FindCString(string, ",", 1);
|
||||
|
||||
if(separator == ~(uint64_t)0){
|
||||
void ARC_ConfigType_PointCopyFn(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_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){
|
||||
ARC_Config_Get(config, string, value);
|
||||
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);
|
||||
//if there is only a value with no comma break
|
||||
if(ARC_Vector_GetSize(valueArgsTagToken->tagTokens) == 1){
|
||||
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);
|
||||
if(arc_errno){
|
||||
return 0;
|
||||
//get the first value
|
||||
ARC_ParserTagToken *valueTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(valueArgsTagToken->tagTokens, 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;
|
||||
ARC_String_CopySubstring(&xString, string, 1 , separator - 1 );
|
||||
ARC_String_CopySubstring(&yString, string, separator + 1, string->length - (separator + 2));
|
||||
//copy the xpoint value to stack and free the pointer
|
||||
int32_t pointX = *pointTemp;
|
||||
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));
|
||||
point->x = (int32_t)ARC_String_ToInt64_t(xString);
|
||||
point->y = (int32_t)ARC_String_ToInt64_t(yString);
|
||||
*point = (ARC_Point){ pointX, *pointTemp };
|
||||
free(pointTemp);
|
||||
|
||||
ARC_String_Destroy(xString);
|
||||
ARC_String_Destroy(yString);
|
||||
|
||||
*value = point;
|
||||
return 0;
|
||||
//set the type value
|
||||
*type = (void *)point;
|
||||
}
|
||||
|
||||
uint8_t ARC_Rect_Read(ARC_Config *config, ARC_String *string, void **value){
|
||||
ARC_Config_Get(config, string, value);
|
||||
if(*value){
|
||||
return 1;
|
||||
}
|
||||
void ARC_ConfigType_PointDestroyFn(void *type){
|
||||
free((ARC_Point *)type);
|
||||
}
|
||||
|
||||
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_DEBUG_LOG_ERROR_WITH_VARIABLES("in ARC_Rect_Read(config, string, value); no matching curly braces: %s", string->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
|
||||
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(¤t, 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(¤t, 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(¤t, 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;
|
||||
|
||||
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");
|
||||
type = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
//reading in value
|
||||
ARC_Rect_Read(config, substr, (void **) &tempRect);
|
||||
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);
|
||||
ARC_String_Destroy(substr);
|
||||
//get the valueArgs
|
||||
ARC_ParserTagToken *valueArgsTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(childTagToken->tagTokens, 2);
|
||||
|
||||
//if there is only a value with no 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 } only had one value, rect needs four { x, y, w, h }");
|
||||
return;
|
||||
}
|
||||
|
||||
((ARC_Rect *)((ARC_Array *) *value)->data)[*arrayIndex] = *tempRect;
|
||||
++*arrayIndex;
|
||||
|
||||
ARC_Rect_Delete(config, substr, (void *)tempRect);
|
||||
ARC_String_Destroy(substr);
|
||||
}
|
||||
|
||||
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] != '}'){
|
||||
//get the first value
|
||||
ARC_ParserTagToken *valueTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(valueArgsTagToken->tagTokens, 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_WITH_VARIABLES("in ARC_RectArray_Read(config, string, value); no matching curly braces: %s", string->data);
|
||||
return 0;
|
||||
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 *temp, *stripped;
|
||||
ARC_String_CopySubstring(&temp, string, 1, string->length - 2);
|
||||
ARC_String_StripEndsWhitespace(&stripped, temp);
|
||||
ARC_String_Destroy(temp);
|
||||
//copy the xpoint value to stack and free the pointer
|
||||
int32_t pointX = *pointTemp;
|
||||
free(pointTemp);
|
||||
pointTemp = NULL;
|
||||
|
||||
uint64_t arraySize = 1;
|
||||
int64_t encapsulated = 0;
|
||||
for(uint64_t i = 0; i < stripped->length; i++){
|
||||
if(stripped->data[i] == '{'){
|
||||
encapsulated++;
|
||||
continue;
|
||||
}
|
||||
//get the second value
|
||||
valueArgsTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(valueArgsTagToken->tagTokens, 4);
|
||||
|
||||
if(stripped->data[i] == '}'){
|
||||
encapsulated--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(!encapsulated && stripped->data[i] == ','){
|
||||
arraySize++;
|
||||
}
|
||||
}
|
||||
|
||||
if(encapsulated){
|
||||
//if there is only a value with no comma break
|
||||
if(ARC_Vector_GetSize(valueArgsTagToken->tagTokens) == 1){
|
||||
arc_errno = ARC_ERRNO_DATA;
|
||||
//TODO: Fix this for windows SMFH
|
||||
// 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;
|
||||
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 }");
|
||||
return;
|
||||
}
|
||||
|
||||
*value = malloc(sizeof(ARC_Array));
|
||||
((ARC_Array *) *value)->data = malloc(sizeof(ARC_Rect) * arraySize);
|
||||
((ARC_Array *) *value)->size = arraySize;
|
||||
|
||||
uint64_t index = 0;
|
||||
arraySize = 0;
|
||||
encapsulated = 0;
|
||||
for(uint64_t i = 0; i < stripped->length; i++){
|
||||
if(stripped->data[i] == '{'){
|
||||
encapsulated++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(stripped->data[i] == '}'){
|
||||
encapsulated--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(!encapsulated && stripped->data[i] == ','){
|
||||
ARC_RectArray_ReadRect(config, stripped, index, i - index, &arraySize, value);
|
||||
if(arc_errno){
|
||||
return 0;
|
||||
}
|
||||
|
||||
index = i + 1;
|
||||
|
||||
if(arraySize == ((ARC_Array *) *value)->size){
|
||||
break;
|
||||
}
|
||||
}
|
||||
//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_RectCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata), second parameter was not a int32");
|
||||
return;
|
||||
}
|
||||
|
||||
if(arraySize != ((ARC_Array *) *value)->size){
|
||||
ARC_RectArray_ReadRect(config, stripped, index, stripped->length - index, &arraySize, value);
|
||||
int32_t pointY = *pointTemp;
|
||||
free(pointTemp);
|
||||
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;
|
||||
}
|
||||
ARC_String_Destroy(stripped);
|
||||
return 0;
|
||||
|
||||
//get the third 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_RectCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata), third parameter was not a int32");
|
||||
return;
|
||||
}
|
||||
|
||||
int32_t pointW = *pointTemp;
|
||||
free(pointTemp);
|
||||
pointTemp = NULL;
|
||||
|
||||
//get the fourth 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, h, ... } had too many values, rect needs four { x, y, w, h }");
|
||||
return;
|
||||
}
|
||||
|
||||
//get the fourth 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_RectCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata), fourth parameter was not a int32");
|
||||
return;
|
||||
}
|
||||
|
||||
//copy the last value and free the temp 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_Point_Delete(ARC_Config* config, ARC_String *string, void *value){
|
||||
free((ARC_Point *)value);
|
||||
void ARC_ConfigType_RectDestroyFn(void *type){
|
||||
free((ARC_Rect *)type);
|
||||
}
|
||||
|
||||
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){
|
||||
//TODO free value->data
|
||||
free((ARC_Array *)value);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue