Changeset 3dfc083 for variable.c


Ignore:
Timestamp:
Jun 27, 2011, 4:28:08 AM (13 years ago)
Author:
Jason Gross <jgross@mit.edu>
Children:
eeba011
Parents:
705ea86
git-author:
Jason Gross <jgross@mit.edu> (06/27/11 01:28:53)
git-committer:
Jason Gross <jgross@mit.edu> (06/27/11 04:28:08)
Message:
Refactored owl_variable_dict_newvar_* to give accurate validsettings, and cleanup better

The cleanup no longer depends on the existing variable being the same
type, and is more less likely to leak, in particular when the variables
have custom delete functions.  Variables created from perl now have the
appropriate default validsettings.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • variable.c

    rce68f23 r3dfc083  
    572572int owl_variable_dict_add_from_list(owl_vardict *vd, owl_variable *variables_to_init)
    573573{
     574  int ret;
    574575  owl_variable *var, *cur;
    575576  for (var = variables_to_init; var->name != NULL; var++) {
     
    580581    cur->summary = g_strdup(var->summary);
    581582    cur->description = g_strdup(var->description);
    582     switch (cur->type) {
     583    if (var->type == OWL_VARIABLE_STRING)
     584      cur->pval_default = g_strdup(var->pval_default);
     585    ret = owl_variable_init_defaults(cur);
     586    if (ret != 0) {
     587      fprintf(stderr, "owl_variable_setup: invalid variable type\n");
     588      return ret;
     589    }
     590    owl_dict_insert_element(vd, cur->name, cur, NULL);
     591  }
     592  return 0;
     593}
     594int owl_variable_init_defaults(owl_variable *cur)
     595{
     596  switch (cur->type) {
    583597    case OWL_VARIABLE_OTHER:
    584598      cur->set_fn(cur, cur->pval_default);
     
    597611      if (!cur->delete_fn)
    598612        cur->delete_fn = owl_variable_delete_default;
    599       cur->pval_default = g_strdup(var->pval_default);
    600613      cur->set_fn(cur, cur->pval_default);
    601614      break;
     
    633646      break;
    634647    default:
    635       fprintf(stderr, "owl_variable_setup: invalid variable type\n");
    636       return(-2);
    637     }
    638     owl_dict_insert_element(vd, cur->name, cur, NULL);
     648      return -2;
    639649  }
    640650  return 0;
     
    646656}
    647657
    648 CALLER_OWN owl_variable *owl_variable_newvar(const char *name, const char *summary, const char *description)
    649 {
    650   owl_variable * var = g_new0(owl_variable, 1);
     658CALLER_OWN owl_variable *owl_variable_newvar(const char *name, const char *summary, const char *description, const char *validsettings, int type)
     659{
     660  owl_variable *var = g_new0(owl_variable, 1);
    651661  var->name = g_strdup(name);
    652662  var->summary = g_strdup(summary);
    653663  var->description = g_strdup(description);
     664  var->validsettings = validsettings;
     665  var->type = type;
    654666  return var;
    655667}
    656668
    657 void owl_variable_update(owl_variable *var, const char *summary, const char *desc) {
    658   g_free(var->summary);
    659   var->summary = g_strdup(summary);
    660   g_free(var->description);
    661   var->description = g_strdup(desc);
    662 }
    663 
    664 void owl_variable_dict_newvar_string(owl_vardict * vd, const char *name, const char *summ, const char * desc, const char * initval) {
    665   owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_STRING);
    666   if(old) {
    667     owl_variable_update(old, summ, desc);
    668     g_free(old->pval_default);
    669     old->pval_default = g_strdup(initval);
    670   } else {
    671     owl_variable * var = owl_variable_newvar(name, summ, desc);
    672     var->type = OWL_VARIABLE_STRING;
    673     var->pval_default = g_strdup(initval);
    674     var->set_fn = owl_variable_string_set_default;
    675     var->set_fromstring_fn = owl_variable_string_set_fromstring_default;
    676     var->get_fn = owl_variable_get_default;
    677     var->get_tostring_fn = owl_variable_string_get_tostring_default;
    678     var->delete_fn = owl_variable_delete_default;
    679     var->set_fn(var, initval);
    680     owl_variable_dict_add_variable(vd, var);
    681   }
    682 }
    683 
    684 void owl_variable_dict_newvar_int(owl_vardict * vd, const char *name, const char *summ, const char * desc, int initval) {
    685   owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_INT);
    686   if(old) {
    687     owl_variable_update(old, summ, desc);
    688     old->ival_default = initval;
    689   } else {
    690     owl_variable * var = owl_variable_newvar(name, summ, desc);
    691     var->type = OWL_VARIABLE_INT;
    692     var->ival_default = initval;
    693     var->validate_fn = owl_variable_int_validate_default;
    694     var->set_fn = owl_variable_int_set_default;
    695     var->set_fromstring_fn = owl_variable_int_set_fromstring_default;
    696     var->get_fn = owl_variable_get_default;
    697     var->get_tostring_fn = owl_variable_int_get_tostring_default;
    698     var->delete_fn = owl_variable_delete_default;
    699     var->val = g_new(int, 1);
    700     var->set_fn(var, &initval);
    701     owl_variable_dict_add_variable(vd, var);
    702   }
    703 }
    704 
    705 void owl_variable_dict_newvar_bool(owl_vardict * vd, const char *name, const char *summ, const char * desc, int initval) {
    706   owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_BOOL);
    707   if(old) {
    708     owl_variable_update(old, summ, desc);
    709     old->ival_default = initval;
    710   } else {
    711     owl_variable * var = owl_variable_newvar(name, summ, desc);
    712     var->type = OWL_VARIABLE_BOOL;
    713     var->ival_default = initval;
    714     var->validate_fn = owl_variable_bool_validate_default;
    715     var->set_fn = owl_variable_bool_set_default;
    716     var->set_fromstring_fn = owl_variable_bool_set_fromstring_default;
    717     var->get_fn = owl_variable_get_default;
    718     var->get_tostring_fn = owl_variable_bool_get_tostring_default;
    719     var->delete_fn = owl_variable_delete_default;
    720     var->val = g_new(int, 1);
    721     var->set_fn(var, &initval);
    722     owl_variable_dict_add_variable(vd, var);
    723   }
     669void owl_variable_dict_newvar_string(owl_vardict *vd, const char *name, const char *summ, const char *desc, const char *initval)
     670{
     671  owl_variable *var = owl_variable_newvar(name, summ, desc, "<string>", OWL_VARIABLE_STRING);
     672  var->pval_default = g_strdup(initval);
     673  owl_variable_init_defaults(var);
     674  owl_variable_dict_add_variable(vd, var);
     675}
     676
     677void owl_variable_dict_newvar_int(owl_vardict *vd, const char *name, const char *summ, const char *desc, int initval)
     678{
     679  owl_variable *var = owl_variable_newvar(name, summ, desc, "<int>", OWL_VARIABLE_INT);
     680  var->ival_default = initval;
     681  owl_variable_init_defaults(var);
     682  owl_variable_dict_add_variable(vd, var);
     683}
     684
     685void owl_variable_dict_newvar_bool(owl_vardict *vd, const char *name, const char *summ, const char *desc, bool initval)
     686{
     687  owl_variable *var = owl_variable_newvar(name, summ, desc, "on,off", OWL_VARIABLE_BOOL);
     688  var->ival_default = initval;
     689  owl_variable_init_defaults(var);
     690  owl_variable_dict_add_variable(vd, var);
    724691}
    725692
Note: See TracChangeset for help on using the changeset viewer.