Changeset 5445
- Timestamp:
- 03/20/08 14:56:41 (4 months ago)
- Files:
-
- libwired/trunk/libwired/misc/wi-config.c (modified) (12 diffs)
- libwired/trunk/libwired/misc/wi-config.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
libwired/trunk/libwired/misc/wi-config.c
r5404 r5445 50 50 wi_string_t *path; 51 51 wi_hash_t *types; 52 wi_hash_t *defaults; 52 53 wi_uinteger_t line; 53 54 … … 104 105 105 106 106 wi_config_t * wi_config_init_with_path(wi_config_t *config, wi_string_t *path, wi_hash_t *types ) {107 wi_config_t * wi_config_init_with_path(wi_config_t *config, wi_string_t *path, wi_hash_t *types, wi_hash_t *defaults) { 107 108 config->lock = wi_lock_init(wi_lock_alloc()); 108 109 config->path = wi_retain(path); 109 110 config->types = wi_retain(types); 110 111 config->changes = wi_set_init(wi_set_alloc()); 112 config->defaults = wi_retain(defaults); 111 113 112 114 return config; … … 155 157 156 158 config->values = wi_hash_init(wi_hash_alloc()); 159 160 if(config->defaults) 161 wi_hash_add_entries_from_hash(config->values, config->defaults); 157 162 158 163 while((string = wi_file_read_line(file))) { … … 181 186 _wi_config_log_error(config, name); 182 187 } 188 } else { 189 wi_error_set_libwired_error(WI_ERROR_SETTINGS_SYNTAXERROR); 190 191 _wi_config_log_error(config, string); 183 192 } 184 193 } … … 193 202 194 203 wi_boolean_t wi_config_write_file(wi_config_t *config) { 204 wi_enumerator_t *enumerator; 195 205 wi_file_t *file, *tmpfile; 196 206 wi_string_t *string, *name, *value; 207 wi_set_t *keys; 208 wi_boolean_t write; 197 209 198 210 file = wi_file_for_updating(config->path); … … 208 220 209 221 wi_lock_lock(config->lock); 222 223 keys = wi_autorelease(wi_copy(config->changes)); 210 224 211 225 while((string = wi_file_read_line(file))) { 212 if(wi_string_length(string) > 0 && !wi_string_has_prefix(string, WI_STR("#"))) { 226 if(wi_string_length(string) == 0) { 227 wi_file_write_format(tmpfile, WI_STR("\n")); 228 } 229 else if(wi_string_has_prefix(string, WI_STR("#"))) { 230 write = true; 231 string = wi_string_substring_from_index(string, 1); 232 213 233 if(_wi_config_parse_string(config, string, &name, &value)) { 214 if(!_wi_config_write_setting_to_file(config, name, tmpfile)) 215 wi_file_write_format(tmpfile, WI_STR("#%@\n"), string); 216 } else { 234 if(wi_set_contains_data(keys, name)) { 235 if(_wi_config_write_setting_to_file(config, name, tmpfile)) { 236 write = false; 237 238 wi_set_remove_data(keys, name); 239 } 240 } 241 } 242 243 if(write) 244 wi_file_write_format(tmpfile, WI_STR("#%@\n"), string); 245 } 246 else { 247 write = true; 248 249 if(_wi_config_parse_string(config, string, &name, &value)) { 250 if(wi_set_contains_data(keys, name)) { 251 if(_wi_config_write_setting_to_file(config, name, tmpfile)) { 252 write = false; 253 254 wi_set_remove_data(keys, name); 255 } 256 } 257 } 258 259 if(write) 217 260 wi_file_write_format(tmpfile, WI_STR("%@\n"), string); 218 }219 } else {220 wi_file_write_format(tmpfile, WI_STR("%@\n"), string);221 261 } 222 262 } 263 264 enumerator = wi_set_data_enumerator(keys); 265 266 while((name = wi_enumerator_next_data(enumerator))) 267 _wi_config_write_setting_to_file(config, name, tmpfile); 223 268 224 269 wi_set_remove_all_data(config->changes); … … 241 286 void wi_config_note_change(wi_config_t *config, wi_string_t *name) { 242 287 wi_set_add_data(config->changes, name); 288 } 289 290 291 292 void wi_config_clear_changes(wi_config_t *config) { 293 wi_set_remove_all_data(config->changes); 243 294 } 244 295 … … 265 316 array = wi_string_components_separated_by_string(string, WI_STR("=")); 266 317 267 if(wi_array_count(array) != 2) { 268 wi_error_set_libwired_error(WI_ERROR_SETTINGS_SYNTAXERROR); 269 270 _wi_config_log_error(config, string); 271 318 if(wi_array_count(array) != 2) 272 319 return false; 273 }274 320 275 321 *name = wi_string_by_deleting_surrounding_whitespace(WI_ARRAY(array, 0)); … … 392 438 struct passwd *user; 393 439 struct group *group; 440 wi_uinteger_t i, count; 394 441 395 442 if(!wi_hash_contains_key(config->types, name)) { … … 419 466 420 467 case WI_CONFIG_STRINGLIST: 421 if(wi_array_count(value) == 0) 422 return false; 423 424 wi_file_write_format(file, WI_STR("%@ = %@\n"), name, wi_array_first_data(value)); 425 426 wi_array_remove_data_at_index(value, 0); 468 count = wi_array_count(value); 469 470 for(i = 0; i < count; i++) 471 wi_file_write_format(file, WI_STR("%@ = %@\n"), name, WI_ARRAY(value, i)); 427 472 break; 428 473 … … 470 515 471 516 void wi_config_set_instance_for_name(wi_config_t *config, wi_runtime_instance_t *instance, wi_string_t *name) { 517 wi_runtime_instance_t *copy; 518 472 519 wi_lock_lock(config->lock); 473 520 … … 475 522 wi_set_add_data(config->changes, name); 476 523 477 wi_hash_set_data_for_key(config->values, instance, name); 524 copy = wi_copy(instance); 525 wi_hash_set_data_for_key(config->values, copy, name); 526 wi_release(copy); 478 527 479 528 wi_lock_unlock(config->lock); libwired/trunk/libwired/misc/wi-config.h
r5368 r5445 56 56 57 57 WI_EXPORT wi_config_t * wi_config_alloc(void); 58 WI_EXPORT wi_config_t * wi_config_init_with_path(wi_config_t *, wi_string_t *, wi_hash_t * );58 WI_EXPORT wi_config_t * wi_config_init_with_path(wi_config_t *, wi_string_t *, wi_hash_t *, wi_hash_t *); 59 59 60 60 WI_EXPORT wi_boolean_t wi_config_read_file(wi_config_t *); … … 62 62 63 63 WI_EXPORT void wi_config_note_change(wi_config_t *, wi_string_t *); 64 WI_EXPORT void wi_config_clear_changes(wi_config_t *); 64 65 WI_EXPORT wi_set_t * wi_config_changes(wi_config_t *); 65 66
