void wi_test_set_mutation(void) { wi_mutable_set_t *set1, *set2; set1 = wi_set_init_with_capacity_and_callbacks(wi_mutable_set_alloc(), 0, true, wi_set_null_callbacks); WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 0U, ""); WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("foo")), 0U, ""); WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("bar")), 0U, ""); wi_mutable_set_add_data(set1, WI_STR("foo")); wi_mutable_set_add_data(set1, WI_STR("bar")); WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 2U, ""); WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("foo")), 1U, ""); WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("bar")), 1U, ""); wi_mutable_set_add_data(set1, WI_STR("foo")); wi_mutable_set_add_data(set1, WI_STR("bar")); WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 2U, ""); WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("foo")), 2U, ""); WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("bar")), 2U, ""); wi_mutable_set_remove_data(set1, WI_STR("foo")); wi_mutable_set_remove_data(set1, WI_STR("bar")); WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 2U, ""); WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("foo")), 1U, ""); WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("bar")), 1U, ""); wi_mutable_set_remove_data(set1, WI_STR("foo")); wi_mutable_set_remove_data(set1, WI_STR("bar")); WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 0U, ""); wi_mutable_set_add_data(set1, WI_STR("foo")); wi_mutable_set_add_data(set1, WI_STR("bar")); set2 = wi_mutable_copy(set1); wi_mutable_set_add_data(set2, WI_STR("baz")); wi_mutable_set_set_set(set1, set2); WI_TEST_ASSERT_EQUAL_INSTANCES(set1, set2, ""); wi_mutable_set_remove_all_data(set1); WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 0U, ""); wi_mutable_set_add_data_from_array(set1, wi_array_with_data(WI_STR("foo"), WI_STR("bar"), NULL)); WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 2U, ""); WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("foo")), 1U, ""); WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("bar")), 1U, ""); }
void wi_config_clear_changes(wi_config_t *config) { wi_mutable_set_remove_all_data(config->changes); }
wi_boolean_t wi_config_write_file(wi_config_t *config) { wi_enumerator_t *enumerator; wi_file_t *file, *tmpfile; wi_string_t *string, *name, *value; wi_mutable_set_t *keys; wi_boolean_t write; file = wi_file_for_updating(config->path); if(!file) { wi_log_err(WI_STR("Could not open %@: %m"), config->path); return false; } tmpfile = wi_file_temporary_file(); wi_lock_lock(config->lock); keys = wi_autorelease(wi_mutable_copy(config->changes)); while((string = wi_file_read_line(file))) { if(wi_string_length(string) == 0) { wi_file_write_format(tmpfile, WI_STR("\n")); } else if(wi_string_has_prefix(string, WI_STR("#"))) { write = true; string = wi_string_substring_from_index(string, 1); if(_wi_config_parse_string(config, string, &name, &value)) { if(wi_set_contains_data(keys, name)) { if(_wi_config_write_setting_to_file(config, name, tmpfile)) { write = false; wi_mutable_set_remove_data(keys, name); } } } if(write) wi_file_write_format(tmpfile, WI_STR("#%@\n"), string); } else { write = true; if(_wi_config_parse_string(config, string, &name, &value)) { if(wi_set_contains_data(keys, name)) { if(_wi_config_write_setting_to_file(config, name, tmpfile)) { write = false; wi_mutable_set_remove_data(keys, name); } } } if(write) wi_file_write_format(tmpfile, WI_STR("%@\n"), string); } } enumerator = wi_set_data_enumerator(keys); while((name = wi_enumerator_next_data(enumerator))) _wi_config_write_setting_to_file(config, name, tmpfile); wi_mutable_set_remove_all_data(config->changes); wi_lock_unlock(config->lock); wi_file_truncate(file, 0); wi_file_seek(tmpfile, 0); while((string = wi_file_read_line(tmpfile))) wi_file_write_format(file, WI_STR("%@\n"), string); return true; }