settings: Maintain order of sections and settings while enumerating

This commit is contained in:
Tobias Brunner
2014-05-15 11:28:08 +02:00
parent 2fbbea55c5
commit f5dd274ab8
4 changed files with 60 additions and 67 deletions
+11 -5
View File
@@ -80,18 +80,23 @@ static void kv_destroy(kv_t *kv, int idx, array_t *contents)
static bool section_purge(section_t *this, array_t *contents)
{
section_t *current;
int i;
int i, idx;
array_destroy_function(this->kv, (void*)kv_destroy, contents);
this->kv = NULL;
array_destroy(this->kv_order);
this->kv_order = NULL;
/* we ensure sections used as fallback, or configured with fallbacks (or
* having any such subsections) are not removed */
for (i = array_count(this->sections) - 1; i >= 0; i--)
for (i = array_count(this->sections_order) - 1; i >= 0; i--)
{
array_get(this->sections, i, &current);
if (section_purge(current, contents))
{
array_remove(this->sections, i, NULL);
array_remove(this->sections_order, i, NULL);
idx = array_bsearch(this->sections, current->name,
settings_section_find, NULL);
array_remove(this->sections, idx, NULL);
settings_section_destroy(current, contents);
}
}
@@ -758,7 +763,8 @@ static bool section_filter(hashtable_t *seen, section_t **in, char **out)
static enumerator_t *section_enumerator(section_t *section,
enumerator_data_t *data)
{
return enumerator_create_filter(array_create_enumerator(section->sections),
return enumerator_create_filter(
array_create_enumerator(section->sections_order),
(void*)section_filter, data->seen, NULL);
}
@@ -809,7 +815,7 @@ static bool kv_filter(hashtable_t *seen, kv_t **in, char **key,
*/
static enumerator_t *kv_enumerator(section_t *section, enumerator_data_t *data)
{
return enumerator_create_filter(array_create_enumerator(section->kv),
return enumerator_create_filter(array_create_enumerator(section->kv_order),
(void*)kv_filter, data->seen, NULL);
}
+14 -4
View File
@@ -75,7 +75,9 @@ static void kv_destroy(kv_t *kv, int idx, array_t *contents)
void settings_section_destroy(section_t *this, array_t *contents)
{
array_destroy_function(this->sections, (void*)section_destroy, contents);
array_destroy(this->sections_order);
array_destroy_function(this->kv, (void*)kv_destroy, contents);
array_destroy(this->kv_order);
array_destroy(this->fallbacks);
free(this->name);
free(this);
@@ -117,6 +119,7 @@ void settings_kv_add(section_t *section, kv_t *kv, array_t *contents)
{
array_insert_create(&section->kv, ARRAY_TAIL, kv);
array_sort(section->kv, settings_kv_sort, NULL);
array_insert_create(&section->kv_order, ARRAY_TAIL, kv);
}
else
{
@@ -139,6 +142,7 @@ void settings_section_add(section_t *parent, section_t *section,
{
array_insert_create(&parent->sections, ARRAY_TAIL, section);
array_sort(parent->sections, settings_section_sort, NULL);
array_insert_create(&parent->sections_order, ARRAY_TAIL, section);
}
else
{
@@ -156,19 +160,25 @@ void settings_section_extend(section_t *base, section_t *extension,
enumerator_t *enumerator;
section_t *section;
kv_t *kv;
int idx;
enumerator = array_create_enumerator(extension->sections);
enumerator = array_create_enumerator(extension->sections_order);
while (enumerator->enumerate(enumerator, (void**)&section))
{
array_remove_at(extension->sections, enumerator);
idx = array_bsearch(extension->sections, section->name,
settings_section_find, NULL);
array_remove(extension->sections, idx, NULL);
array_remove_at(extension->sections_order, enumerator);
settings_section_add(base, section, contents);
}
enumerator->destroy(enumerator);
enumerator = array_create_enumerator(extension->kv);
enumerator = array_create_enumerator(extension->kv_order);
while (enumerator->enumerate(enumerator, (void**)&kv))
{
array_remove_at(extension->kv, enumerator);
idx = array_bsearch(extension->kv, kv->key, settings_kv_find, NULL);
array_remove(extension->kv, idx, NULL);
array_remove_at(extension->kv_order, enumerator);
settings_kv_add(base, kv, contents);
}
enumerator->destroy(enumerator);
@@ -64,10 +64,20 @@ struct section_t {
*/
array_t *sections;
/**
* Subsections in original order, as section_t (pointer to obj in sections).
*/
array_t *sections_order;
/**
* Key value pairs, as kv_t.
*/
array_t *kv;
/**
* Key value pairs in original order, as kv_t (pointer to obj in kv).
*/
array_t *kv_order;
};
/**