Changeset d536e72 for variable.c


Ignore:
Timestamp:
Jan 10, 2008, 1:02:06 AM (17 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, barnowl_perlaim, debian, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
44f32fb
Parents:
b10f340
Message:
Handle recreation of an existing variable better. Copy over the
default and docstrings, but preserve the old value.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • variable.c

    rad15610 rd536e72  
    455455
    456456int owl_variable_dict_setup(owl_vardict *vd) {
    457   owl_variable *cur;
     457  owl_variable *var, *cur;
    458458  if (owl_dict_create(vd)) return(-1);
    459   for (cur = variables_to_init; cur->name != NULL; cur++) {
     459  for (var = variables_to_init; var->name != NULL; var++) {
     460    cur = owl_malloc(sizeof(owl_variable));
     461    memcpy(cur, var, sizeof(owl_variable));
    460462    switch (cur->type) {
    461463    case OWL_VARIABLE_OTHER:
     
    520522void owl_variable_dict_add_variable(owl_vardict * vardict,
    521523                                    owl_variable * var) {
    522   owl_dict_insert_element(vardict, var->name, (void*)var, NULL);
     524  owl_dict_insert_element(vardict, var->name, (void*)var, (void(*)(void*))owl_variable_free);
    523525}
    524526
     
    532534}
    533535
     536void owl_variable_update(owl_variable *var, char *summary, char *desc) {
     537  if(var->summary) owl_free(var->summary);
     538  var->summary = owl_strdup(summary);
     539  if(var->description) owl_free(var->description);
     540  var->description = owl_strdup(desc);
     541}
     542
    534543void owl_variable_dict_newvar_string(owl_vardict * vd, char *name, char *summ, char * desc, char * initval) {
    535   owl_variable * var = owl_variable_newvar(name, summ, desc);
    536   var->type = OWL_VARIABLE_STRING;
    537   var->pval_default = owl_strdup(initval);
    538   var->set_fn = owl_variable_string_set_default;
    539   var->set_fromstring_fn = owl_variable_string_set_fromstring_default;
    540   var->get_fn = owl_variable_get_default;
    541   var->get_tostring_fn = owl_variable_string_get_tostring_default;
    542   var->free_fn = owl_variable_free_default;
    543   var->set_fn(var, initval);
    544   owl_variable_dict_add_variable(vd, var);
     544  owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_STRING);
     545  if(old) {
     546    owl_variable_update(old, summ, desc);
     547    if(old->pval_default) owl_free(old->pval_default);
     548    old->pval_default = owl_strdup(initval);
     549  } else {
     550    owl_variable * var = owl_variable_newvar(name, summ, desc);
     551    var->type = OWL_VARIABLE_STRING;
     552    var->pval_default = owl_strdup(initval);
     553    var->set_fn = owl_variable_string_set_default;
     554    var->set_fromstring_fn = owl_variable_string_set_fromstring_default;
     555    var->get_fn = owl_variable_get_default;
     556    var->get_tostring_fn = owl_variable_string_get_tostring_default;
     557    var->free_fn = owl_variable_free_default;
     558    var->set_fn(var, initval);
     559    owl_variable_dict_add_variable(vd, var);
     560  }
    545561}
    546562
    547563void owl_variable_dict_newvar_int(owl_vardict * vd, char *name, char *summ, char * desc, int initval) {
    548   owl_variable * var = owl_variable_newvar(name, summ, desc);
    549   var->type = OWL_VARIABLE_INT;
    550   var->ival_default = initval;
    551   var->validate_fn = owl_variable_int_validate_default;
    552   var->set_fn = owl_variable_int_set_default;
    553   var->set_fromstring_fn = owl_variable_int_set_fromstring_default;
    554   var->get_fn = owl_variable_get_default;
    555   var->get_tostring_fn = owl_variable_int_get_tostring_default;
    556   var->free_fn = owl_variable_free_default;
    557   var->val = owl_malloc(sizeof(int));
    558   var->set_fn(var, &initval);
    559   owl_variable_dict_add_variable(vd, var);
     564  owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_INT);
     565  if(old) {
     566    owl_variable_update(old, summ, desc);
     567    old->ival_default = initval;
     568  } else {
     569    owl_variable * var = owl_variable_newvar(name, summ, desc);
     570    var->type = OWL_VARIABLE_INT;
     571    var->ival_default = initval;
     572    var->validate_fn = owl_variable_int_validate_default;
     573    var->set_fn = owl_variable_int_set_default;
     574    var->set_fromstring_fn = owl_variable_int_set_fromstring_default;
     575    var->get_fn = owl_variable_get_default;
     576    var->get_tostring_fn = owl_variable_int_get_tostring_default;
     577    var->free_fn = owl_variable_free_default;
     578    var->val = owl_malloc(sizeof(int));
     579    var->set_fn(var, &initval);
     580    owl_variable_dict_add_variable(vd, var);
     581  }
    560582}
    561583
    562584void owl_variable_dict_newvar_bool(owl_vardict * vd, char *name, char *summ, char * desc, int initval) {
    563   owl_variable * var = owl_variable_newvar(name, summ, desc);
    564   var->type = OWL_VARIABLE_BOOL;
    565   var->ival_default = initval;
    566   var->validate_fn = owl_variable_bool_validate_default;
    567   var->set_fn = owl_variable_bool_set_default;
    568   var->set_fromstring_fn = owl_variable_bool_set_fromstring_default;
    569   var->get_fn = owl_variable_get_default;
    570   var->get_tostring_fn = owl_variable_bool_get_tostring_default;
    571   var->free_fn = owl_variable_free_default;
    572   var->val = owl_malloc(sizeof(int));
    573   var->set_fn(var, &initval);
    574   owl_variable_dict_add_variable(vd, var);
     585  owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_BOOL);
     586  if(old) {
     587    owl_variable_update(old, summ, desc);
     588    old->ival_default = initval;
     589  } else {
     590    owl_variable * var = owl_variable_newvar(name, summ, desc);
     591    var->type = OWL_VARIABLE_BOOL;
     592    var->ival_default = initval;
     593    var->validate_fn = owl_variable_bool_validate_default;
     594    var->set_fn = owl_variable_bool_set_default;
     595    var->set_fromstring_fn = owl_variable_bool_set_fromstring_default;
     596    var->get_fn = owl_variable_get_default;
     597    var->get_tostring_fn = owl_variable_bool_get_tostring_default;
     598    var->free_fn = owl_variable_free_default;
     599    var->val = owl_malloc(sizeof(int));
     600    var->set_fn(var, &initval);
     601    owl_variable_dict_add_variable(vd, var);
     602  }
    575603}
    576604
     
    590618void owl_variable_free(owl_variable *v) {
    591619  if (v->free_fn) v->free_fn(v);
     620  owl_free(v);
    592621}
    593622
     
    687716}
    688717
    689 /* returns a reference */
    690 void *owl_variable_get(owl_vardict *d, char *name, int require_type) {
     718owl_variable *owl_variable_get_var(owl_vardict *d, char *name, int require_type) {
    691719  owl_variable *v;
    692720  if (!name) return(NULL);
    693721  v = owl_dict_find_element(d, name);
    694722  if (v == NULL || !v->get_fn || v->type != require_type) return(NULL);
     723  return v;
     724}
     725
     726/* returns a reference */
     727void *owl_variable_get(owl_vardict *d, char *name, int require_type) {
     728  owl_variable *v = owl_variable_get_var(d, name, require_type);
     729  if(v == NULL) return NULL;
    695730  return v->get_fn(v);
    696731}
Note: See TracChangeset for help on using the changeset viewer.