most config fixed, still need to do a bunch more testing
This commit is contained in:
parent
2b2e40921d
commit
5e6ee54473
7 changed files with 730 additions and 424 deletions
|
|
@ -28,6 +28,11 @@ typedef struct ARC_ConfigTypeTemplate {
|
|||
void *data;
|
||||
} ARC_ConfigTypeTemplate;
|
||||
|
||||
typedef struct ARC_ConfigDeleteKeyArgs {
|
||||
ARC_Config *config;
|
||||
ARC_String *string;
|
||||
} ARC_ConfigDeleteKeyArgs;
|
||||
|
||||
int8_t ARC_Config_KeyComp(void *key1, size_t *key1size, void *key2, size_t *key2size);
|
||||
|
||||
void ARC_Config_CreateGroup(ARC_Config *config, ARC_String *name);
|
||||
|
|
@ -76,12 +81,22 @@ void ARC_Config_Create(ARC_Config **config){
|
|||
}
|
||||
|
||||
void ARC_Config_Destroy(ARC_Config *config){
|
||||
ARC_Hashtable_Destroy(config->groups, ARC_Config_DestroyGroup, (void *)&config);
|
||||
ARC_Hashtable_Destroy(config->keys , ARC_Config_RemoveKey , NULL );
|
||||
ARC_ConfigDeleteKeyArgs keyArgs = {
|
||||
.config = config,
|
||||
.string = NULL,
|
||||
};
|
||||
|
||||
ARC_Hashtable_Destroy(config->groups, ARC_Config_DestroyGroup, (void *)&keyArgs);
|
||||
ARC_Hashtable_Destroy(config->keys , ARC_Config_RemoveKey , NULL );
|
||||
free(config);
|
||||
}
|
||||
|
||||
//TODO: fix NULL group
|
||||
void ARC_Config_SetGroup(ARC_Config *config, ARC_String *groupname){
|
||||
if(!config){
|
||||
return;
|
||||
}
|
||||
|
||||
if(groupname == NULL){
|
||||
ARC_Hashtable_Get(config->groups, (void *)" ", 1, (void **)&(config->currgroup));
|
||||
return;
|
||||
|
|
@ -107,11 +122,7 @@ void ARC_Config_SetGroup(ARC_Config *config, ARC_String *groupname){
|
|||
void ARC_Config_Get(ARC_Config *config, ARC_String *keyname, void **value){
|
||||
ARC_ConfigTypeTemplate *temp;
|
||||
|
||||
ARC_String *separator;
|
||||
ARC_String_Create(&separator, "::", 2);
|
||||
uint64_t length = ARC_String_Find(keyname, separator);
|
||||
ARC_String_Destroy(separator);
|
||||
|
||||
uint64_t length = ARC_String_FindCString(keyname, "::", 2);
|
||||
if(arc_errno){
|
||||
//TODO: Debug info here
|
||||
*value = NULL;
|
||||
|
|
@ -119,8 +130,12 @@ void ARC_Config_Get(ARC_Config *config, ARC_String *keyname, void **value){
|
|||
}
|
||||
|
||||
if(length != ~((uint64_t)0)){
|
||||
ARC_String *group;
|
||||
ARC_String_CopySubstring(&group, keyname, 0, length);
|
||||
length--;
|
||||
ARC_String *group = NULL;
|
||||
|
||||
if(length != 0){
|
||||
ARC_String_CopySubstring(&group, keyname, 0, length);
|
||||
}
|
||||
|
||||
ARC_Hashtable *currgroup = config->currgroup;
|
||||
ARC_Config_SetGroup(config, group);
|
||||
|
|
@ -132,14 +147,22 @@ void ARC_Config_Get(ARC_Config *config, ARC_String *keyname, void **value){
|
|||
|
||||
ARC_String *name;
|
||||
ARC_String_CopySubstring(&name, keyname, length + 2, keyname->length - (length + 2));
|
||||
|
||||
ARC_Hashtable_Get(config->currgroup, (void *)name->data, name->length, (void **)&temp);
|
||||
ARC_String_Destroy(name);
|
||||
|
||||
config->currgroup = currgroup;
|
||||
if(group){
|
||||
ARC_String_Destroy(group);
|
||||
}
|
||||
*value = temp->data;
|
||||
return;
|
||||
}
|
||||
|
||||
if(!keyname){
|
||||
*value = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
ARC_Hashtable_Get(config->currgroup, (void *)keyname->data, keyname->length, (void **)&temp);
|
||||
if(arc_errno || temp == NULL){
|
||||
*value = NULL;
|
||||
|
|
@ -163,12 +186,12 @@ void ARC_Config_SetKeyGroup(ARC_Config *config, ARC_String **data, uint8_t *comm
|
|||
}
|
||||
|
||||
ARC_String *name, *temp;
|
||||
ARC_String_CopySubstring(&temp, *data, index, nextIndex);
|
||||
ARC_String_CopySubstring(&temp, *data, index, nextIndex - index - 1);
|
||||
ARC_String_StripEndsWhitespace(temp, &name);
|
||||
ARC_String_Destroy(temp);
|
||||
|
||||
temp = *data;
|
||||
ARC_String_RemoveSection(data, temp, nextIndex + 1, (*data)->length - (nextIndex + 1));
|
||||
ARC_String_CopySubstring(data, temp, nextIndex + 1, (*data)->length - (nextIndex + 1));
|
||||
ARC_String_Destroy(temp);
|
||||
|
||||
ARC_Config_Recurse(config, data, name, command);
|
||||
|
|
@ -199,13 +222,19 @@ void ARC_Config_LoadFromKey(ARC_Config *config, ARC_String *keyType, ARC_String
|
|||
return;
|
||||
}
|
||||
|
||||
char *nameval = (char *)malloc(sizeof(char) * name->length);
|
||||
char *nameval = (char *)malloc(sizeof(char) * name->length + 1);
|
||||
strncpy(nameval, name->data, name->length);
|
||||
nameval[name->length] = '\0';
|
||||
ARC_Hashtable_Add(config->currgroup, nameval, name->length, (void *)templateVal);
|
||||
}
|
||||
|
||||
void ARC_Config_UnloadFromKey(ARC_Config *config, ARC_String *keyType, ARC_String *name){
|
||||
ARC_Hashtable_Remove(config->currgroup, name->data, name->length, ARC_Config_DestroyGroupNode, &config);
|
||||
void ARC_Config_UnloadFromKey(ARC_Config *config, ARC_String *keyType, ARC_String *name, ARC_String *value){
|
||||
ARC_ConfigDeleteKeyArgs keyArgs = {
|
||||
.config = config,
|
||||
.string = value,
|
||||
};
|
||||
|
||||
ARC_Hashtable_Remove(config->currgroup, name->data, name->length, ARC_Config_DestroyGroupNode, &keyArgs);
|
||||
}
|
||||
|
||||
void ARC_Config_GetNameAndValue(ARC_String *data, ARC_String **name, ARC_String **value){
|
||||
|
|
@ -223,7 +252,9 @@ void ARC_Config_GetNameAndValue(ARC_String *data, ARC_String **name, ARC_String
|
|||
ARC_String_StripEndsWhitespace(dataTemp, name);
|
||||
ARC_String_Destroy(dataTemp);
|
||||
|
||||
ARC_String_CopySubstring(value, data, index, data->length - index);
|
||||
ARC_String_CopySubstring(&dataTemp, data, index, data->length - index);
|
||||
ARC_String_StripEndsWhitespace(dataTemp, value);
|
||||
ARC_String_Destroy(dataTemp);
|
||||
}
|
||||
|
||||
void ARC_Config_Recurse(ARC_Config *config, ARC_String **data, ARC_String *groupstr, uint8_t *command){
|
||||
|
|
@ -268,6 +299,7 @@ void ARC_Config_Recurse(ARC_Config *config, ARC_String **data, ARC_String *group
|
|||
if(ARC_String_EqualsCString(keyType, "group", 5)){
|
||||
ARC_Config_SetKeyGroup(config, data, command);
|
||||
ARC_String_Destroy(keyType);
|
||||
config->currgroup = group;
|
||||
if(arc_errno){
|
||||
return;
|
||||
}
|
||||
|
|
@ -329,7 +361,7 @@ void ARC_Config_Recurse(ARC_Config *config, ARC_String **data, ARC_String *group
|
|||
|
||||
// unload from key
|
||||
if(*command == ARC_CONFIG_FILE_IO_UNLOAD){
|
||||
ARC_Config_UnloadFromKey(config, keyType, name);
|
||||
ARC_Config_UnloadFromKey(config, keyType, name, value);
|
||||
|
||||
ARC_String_Destroy(keyType);
|
||||
ARC_String_Destroy(name );
|
||||
|
|
@ -354,13 +386,16 @@ void ARC_Config_Recurse(ARC_Config *config, ARC_String **data, ARC_String *group
|
|||
}
|
||||
|
||||
void ARC_Config_StripComment(ARC_String *original, ARC_String **stripped, ARC_String *lineStart, ARC_String *lineEnd){
|
||||
ARC_String *current;
|
||||
ARC_String *current = NULL;
|
||||
ARC_String_Copy(¤t, original);
|
||||
|
||||
uint64_t index = ARC_String_Find(current, lineStart);
|
||||
uint64_t index = ARC_String_Find(original, lineStart);
|
||||
while(index != ~(uint64_t)0){
|
||||
ARC_String *commentString;
|
||||
ARC_String_CopySubstring(&commentString, current, index + lineStart->length, current->length - (index + lineStart->length));
|
||||
|
||||
uint64_t endIndex = ARC_String_Find(current, lineEnd);
|
||||
uint64_t endIndex = ARC_String_Find(commentString, lineEnd);
|
||||
ARC_String_Destroy(commentString);
|
||||
if(endIndex == ~(uint64_t)0){
|
||||
ARC_DEBUG_ERR("ARC_Config_RemoveComments(original, commentRemoved); No newline found when stripping single line comment");
|
||||
arc_errno = ARC_ERRNO_DATA;
|
||||
|
|
@ -370,7 +405,7 @@ void ARC_Config_StripComment(ARC_String *original, ARC_String **stripped, ARC_St
|
|||
}
|
||||
|
||||
ARC_String *currentTemp = current;
|
||||
ARC_String_RemoveSection(¤t, currentTemp, index, endIndex - index);
|
||||
ARC_String_RemoveSection(¤t, currentTemp, index, endIndex + lineStart->length + lineEnd->length);
|
||||
ARC_String_Destroy(currentTemp);
|
||||
|
||||
index = ARC_String_Find(current, lineStart);
|
||||
|
|
@ -487,7 +522,7 @@ void ARC_Config_FileIO(ARC_Config *config, ARC_String *path, uint8_t command){
|
|||
return;
|
||||
}
|
||||
|
||||
char *tempData = (char *)malloc((char) data->length + 1);
|
||||
char *tempData = (char *)malloc(sizeof(char) * (data->length + 1));
|
||||
strncpy(tempData, data->data, data->length);
|
||||
tempData[data->length] = '\n';
|
||||
|
||||
|
|
@ -547,9 +582,9 @@ void ARC_Config_DestroyGroupNode(ARC_HashtableNode *node, void *userdata){
|
|||
|
||||
ARC_ConfigTypeTemplate *temp = (ARC_ConfigTypeTemplate *)node->data;
|
||||
if(temp->Delete && temp->data && userdata){
|
||||
ARC_Config *config = (ARC_Config *)userdata;
|
||||
ARC_ConfigDeleteKeyArgs *args = (ARC_ConfigDeleteKeyArgs *)userdata;
|
||||
|
||||
temp->Delete(config, temp->data);
|
||||
temp->Delete(args->config, args->string, temp->data);
|
||||
}
|
||||
|
||||
free(temp);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue