Move key management to the Mutator class
It really belongs there.
This commit is contained in:
parent
f58ade761d
commit
f38c35cbae
36
src/config.c
36
src/config.c
|
@ -123,45 +123,25 @@ int config_set(Config * config, char const * section, char const * variable,
|
|||
/* create a new section */
|
||||
if((mutator = mutator_new()) == NULL)
|
||||
return -1;
|
||||
if((p = string_new(section)) == NULL
|
||||
|| mutator_set(config, p, mutator) != 0)
|
||||
if(mutator_set(config, section, mutator) != 0)
|
||||
{
|
||||
string_delete(p);
|
||||
mutator_delete(mutator);
|
||||
return -1;
|
||||
}
|
||||
p = NULL;
|
||||
}
|
||||
/* check if a value was already set for this variable */
|
||||
else if((p = mutator_get(mutator, variable)) != NULL)
|
||||
{
|
||||
if(value != NULL && (newvalue = string_new(value)) == NULL)
|
||||
return -1;
|
||||
if(mutator_set(mutator, variable, newvalue) != 0)
|
||||
{
|
||||
string_delete(newvalue);
|
||||
return -1;
|
||||
}
|
||||
/* free the former value */
|
||||
string_delete(p);
|
||||
return 0;
|
||||
}
|
||||
else if(value == NULL)
|
||||
else if((p = mutator_get(mutator, variable)) == NULL && value == NULL)
|
||||
/* there is nothing to do */
|
||||
return 0;
|
||||
if((p = string_new(variable)) == NULL)
|
||||
return -1;
|
||||
if(value != NULL && (newvalue = string_new(value)) == NULL)
|
||||
{
|
||||
string_delete(p);
|
||||
return -1;
|
||||
}
|
||||
/* set the new value */
|
||||
if(mutator_set(mutator, p, newvalue) != 0)
|
||||
if(mutator_set(mutator, variable, newvalue) != 0)
|
||||
{
|
||||
string_delete(newvalue);
|
||||
string_delete(p);
|
||||
return -1;
|
||||
}
|
||||
/* free the former value */
|
||||
string_delete(p);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -376,18 +356,16 @@ static void _delete_foreach(String const * key, void * value, void * data)
|
|||
String * section = (String *)key;
|
||||
Mutator * mutator = value;
|
||||
|
||||
/* free the values */
|
||||
mutator_foreach(mutator, _delete_foreach_section, NULL);
|
||||
string_delete(section);
|
||||
mutator_delete(mutator);
|
||||
}
|
||||
|
||||
static void _delete_foreach_section(String const * key, void * value,
|
||||
void * data)
|
||||
{
|
||||
String * k = (String *)key;
|
||||
String * v = value;
|
||||
|
||||
string_delete(k);
|
||||
string_delete(v);
|
||||
}
|
||||
|
||||
|
|
|
@ -35,6 +35,7 @@ Mutator * mutator_new(void)
|
|||
/* mutator_delete */
|
||||
void mutator_delete(Mutator * mutator)
|
||||
{
|
||||
mutator_reset(mutator);
|
||||
hash_delete(mutator);
|
||||
}
|
||||
|
||||
|
@ -62,9 +63,29 @@ void * mutator_get(Mutator * mutator, String const * key)
|
|||
int mutator_set(Mutator * mutator, String const * key, void * value)
|
||||
{
|
||||
int ret;
|
||||
String * k;
|
||||
String * oldk;
|
||||
|
||||
if((ret = hash_set(mutator, key, value)) != 0)
|
||||
if(value == NULL)
|
||||
{
|
||||
/* look for the former key */
|
||||
if((oldk = hash_get_key(mutator, key)) == NULL)
|
||||
/* there is nothing to do */
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
oldk = NULL;
|
||||
/* allocate the key */
|
||||
if((k = string_new(key)) == NULL)
|
||||
return -1;
|
||||
if((ret = hash_set(mutator, k, value)) != 0)
|
||||
{
|
||||
error_set("%s: %s", key, "Could not set the value");
|
||||
string_delete(k);
|
||||
}
|
||||
else
|
||||
/* free the former key if removed */
|
||||
string_delete(oldk);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -77,7 +98,18 @@ void mutator_foreach(Mutator * mutator, MutatorForeach func, void * data)
|
|||
|
||||
|
||||
/* mutator_reset */
|
||||
static void _reset_foreach(String const * key, void * value, void * data);
|
||||
|
||||
int mutator_reset(Mutator * mutator)
|
||||
{
|
||||
/* free the keys */
|
||||
mutator_foreach(mutator, _reset_foreach, NULL);
|
||||
return hash_reset(mutator);
|
||||
}
|
||||
|
||||
static void _reset_foreach(String const * key, void * value, void * data)
|
||||
{
|
||||
String * k = (String *)key;
|
||||
|
||||
string_delete(k);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user