Changeset 5445

Show
Ignore:
Timestamp:
03/20/08 14:56:41 (4 months ago)
Author:
morris
Message:

Add a concept of default settings

Handle config writes better

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libwired/trunk/libwired/misc/wi-config.c

    r5404 r5445  
    5050        wi_string_t                                             *path; 
    5151        wi_hash_t                                               *types; 
     52        wi_hash_t                                               *defaults; 
    5253        wi_uinteger_t                                   line; 
    5354         
     
    104105 
    105106 
    106 wi_config_t * wi_config_init_with_path(wi_config_t *config, wi_string_t *path, wi_hash_t *types) { 
     107wi_config_t * wi_config_init_with_path(wi_config_t *config, wi_string_t *path, wi_hash_t *types, wi_hash_t *defaults) { 
    107108        config->lock            = wi_lock_init(wi_lock_alloc()); 
    108109        config->path            = wi_retain(path); 
    109110        config->types           = wi_retain(types); 
    110111        config->changes         = wi_set_init(wi_set_alloc()); 
     112        config->defaults        = wi_retain(defaults); 
    111113         
    112114        return config; 
     
    155157         
    156158        config->values = wi_hash_init(wi_hash_alloc()); 
     159         
     160        if(config->defaults) 
     161                wi_hash_add_entries_from_hash(config->values, config->defaults); 
    157162         
    158163        while((string = wi_file_read_line(file))) { 
     
    181186                                        _wi_config_log_error(config, name); 
    182187                                } 
     188                        } else { 
     189                                wi_error_set_libwired_error(WI_ERROR_SETTINGS_SYNTAXERROR); 
     190                                 
     191                                _wi_config_log_error(config, string); 
    183192                        } 
    184193                } 
     
    193202 
    194203wi_boolean_t wi_config_write_file(wi_config_t *config) { 
     204        wi_enumerator_t         *enumerator; 
    195205        wi_file_t                       *file, *tmpfile; 
    196206        wi_string_t                     *string, *name, *value; 
     207        wi_set_t                        *keys; 
     208        wi_boolean_t            write; 
    197209         
    198210        file = wi_file_for_updating(config->path); 
     
    208220         
    209221        wi_lock_lock(config->lock); 
     222         
     223        keys = wi_autorelease(wi_copy(config->changes)); 
    210224 
    211225        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                         
    213233                        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) 
    217260                                wi_file_write_format(tmpfile, WI_STR("%@\n"), string); 
    218                         } 
    219                 } else { 
    220                         wi_file_write_format(tmpfile, WI_STR("%@\n"), string); 
    221261                } 
    222262        } 
     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); 
    223268         
    224269        wi_set_remove_all_data(config->changes); 
     
    241286void wi_config_note_change(wi_config_t *config, wi_string_t *name) { 
    242287        wi_set_add_data(config->changes, name); 
     288} 
     289 
     290 
     291 
     292void wi_config_clear_changes(wi_config_t *config) { 
     293        wi_set_remove_all_data(config->changes); 
    243294} 
    244295 
     
    265316        array = wi_string_components_separated_by_string(string, WI_STR("=")); 
    266317         
    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) 
    272319                return false; 
    273         } 
    274320         
    275321        *name = wi_string_by_deleting_surrounding_whitespace(WI_ARRAY(array, 0)); 
     
    392438        struct passwd                           *user; 
    393439        struct group                            *group; 
     440        wi_uinteger_t                           i, count; 
    394441         
    395442        if(!wi_hash_contains_key(config->types, name)) { 
     
    419466                         
    420467                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)); 
    427472                        break; 
    428473                         
     
    470515 
    471516void 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         
    472519        wi_lock_lock(config->lock); 
    473520         
     
    475522                wi_set_add_data(config->changes, name); 
    476523         
    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); 
    478527         
    479528        wi_lock_unlock(config->lock); 
  • libwired/trunk/libwired/misc/wi-config.h

    r5368 r5445  
    5656 
    5757WI_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 *); 
     58WI_EXPORT wi_config_t *                         wi_config_init_with_path(wi_config_t *, wi_string_t *, wi_hash_t *, wi_hash_t *); 
    5959 
    6060WI_EXPORT wi_boolean_t                          wi_config_read_file(wi_config_t *); 
     
    6262 
    6363WI_EXPORT void                                          wi_config_note_change(wi_config_t *, wi_string_t *); 
     64WI_EXPORT void                                          wi_config_clear_changes(wi_config_t *); 
    6465WI_EXPORT wi_set_t *                            wi_config_changes(wi_config_t *); 
    6566