Changeset 5f784ec for variable.c


Ignore:
Timestamp:
Sep 30, 2011, 8:12:24 AM (13 years ago)
Author:
Jason Gross <jgross@mit.edu>
Children:
c0e728a
Parents:
7803326
git-author:
Jason Gross <jgross@mit.edu> (07/12/11 18:29:38)
git-committer:
Jason Gross <jgross@mit.edu> (09/30/11 08:12:24)
Message:
Refactored perl variable creation code in preparation for ambiguous types

When we allow enums in perl, we can't be sure that recreation of an int
isn't actually changing it into an enum (though, really, variables
should only be recreated when you reload module code).  This makes
BarnOwl reinitilize all the fields, but keep the current value, if there
is one.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • variable.c

    rf271129 r5f784ec  
    568568int owl_variable_dict_add_from_list(owl_vardict *vd, owl_variable *variables_to_init)
    569569{
     570  int ret;
    570571  owl_variable *var, *cur;
    571572  for (var = variables_to_init; var->name != NULL; var++) {
     
    576577    cur->summary = g_strdup(var->summary);
    577578    cur->description = g_strdup(var->description);
    578     switch (cur->type) {
     579    if (cur->type == OWL_VARIABLE_STRING)
     580      cur->pval_default = g_strdup(var->pval_default);
     581    ret = owl_variable_init_defaults(cur);
     582    if (ret != 0) {
     583      fprintf(stderr, "owl_variable_setup: invalid variable type\n");
     584      return ret;
     585    }
     586    owl_dict_insert_element(vd, cur->name, cur, NULL);
     587  }
     588  return 0;
     589}
     590
     591int owl_variable_init_defaults(owl_variable *cur)
     592{
     593  switch (cur->type) {
    579594    case OWL_VARIABLE_OTHER:
    580595      cur->set_fn(cur, cur->pval_default);
     
    593608      if (!cur->delete_fn)
    594609        cur->delete_fn = owl_variable_delete_default;
    595       cur->pval_default = g_strdup(var->pval_default);
    596610      cur->set_fn(cur, cur->pval_default);
    597611      break;
     
    629643      break;
    630644    default:
    631       fprintf(stderr, "owl_variable_setup: invalid variable type\n");
    632       return(-2);
    633     }
    634     owl_dict_insert_element(vd, cur->name, cur, NULL);
     645      return -2;
    635646  }
    636647  return 0;
     
    642653}
    643654
    644 CALLER_OWN owl_variable *owl_variable_newvar(const char *name, const char *summary, const char *description)
     655CALLER_OWN owl_variable *owl_variable_newvar(const char *name, const char *summary, const char *description, const char *validsettings, int type)
    645656{
    646657  owl_variable * var = g_new0(owl_variable, 1);
     
    648659  var->summary = g_strdup(summary);
    649660  var->description = g_strdup(description);
     661  var->validsettings = validsettings;
     662  var->type = type;
    650663  return var;
    651664}
    652665
    653 void owl_variable_update(owl_variable *var, const char *summary, const char *desc) {
    654   g_free(var->summary);
    655   var->summary = g_strdup(summary);
    656   g_free(var->description);
    657   var->description = g_strdup(desc);
    658 }
    659 
    660666void owl_variable_dict_newvar_string(owl_vardict *vd, const char *name, const char *summ, const char *desc, const char *initval)
    661667{
    662668  owl_variable *old = owl_variable_get_var(vd, name);
    663   if (old && owl_variable_get_type(old) == OWL_VARIABLE_STRING) {
    664     owl_variable_update(old, summ, desc);
    665     g_free(old->pval_default);
    666     old->pval_default = g_strdup(initval);
    667   } else {
    668     owl_variable * var = owl_variable_newvar(name, summ, desc);
    669     var->type = OWL_VARIABLE_STRING;
    670     var->validsettings = "<string>";
    671     var->pval_default = g_strdup(initval);
    672     var->set_fn = owl_variable_string_set_default;
    673     var->set_fromstring_fn = owl_variable_string_set_fromstring_default;
    674     var->get_fn = owl_variable_get_default;
    675     var->get_tostring_fn = owl_variable_string_get_tostring_default;
    676     var->delete_fn = owl_variable_delete_default;
    677     var->set_fn(var, initval);
    678     owl_variable_dict_add_variable(vd, var);
    679   }
     669  char *oldval = NULL;
     670  if (old && owl_variable_get_type(old) == OWL_VARIABLE_STRING)
     671    oldval = owl_variable_get_tostring(old);
     672  owl_variable *var = owl_variable_newvar(name, summ, desc, "<string>", OWL_VARIABLE_STRING);
     673  var->pval_default = g_strdup(initval);
     674  owl_variable_init_defaults(var);
     675  if (old && owl_variable_get_type(old) == OWL_VARIABLE_STRING)
     676    var->set_fn(var, oldval);
     677  g_free(oldval);
     678  owl_variable_dict_add_variable(vd, var);
    680679}
    681680
     
    683682{
    684683  owl_variable *old = owl_variable_get_var(vd, name);
    685   if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT) {
    686     owl_variable_update(old, summ, desc);
    687     old->ival_default = initval;
    688   } else {
    689     owl_variable * var = owl_variable_newvar(name, summ, desc);
    690     var->type = OWL_VARIABLE_INT;
    691     var->validsettings = "<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   }
     684  int oldval;
     685  if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT)
     686    oldval = owl_variable_get_int(old);
     687  owl_variable *var = owl_variable_newvar(name, summ, desc, "<int>", OWL_VARIABLE_INT);
     688  var->ival_default = initval;
     689  owl_variable_init_defaults(var);
     690  if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT)
     691    var->set_fn(var, &oldval);
     692  owl_variable_dict_add_variable(vd, var);
    703693}
    704694
     
    706696{
    707697  owl_variable *old = owl_variable_get_var(vd, name);
    708   if (old && owl_variable_get_type(old) == OWL_VARIABLE_BOOL) {
    709     owl_variable_update(old, summ, desc);
    710     old->ival_default = initval;
    711   } else {
    712     owl_variable * var = owl_variable_newvar(name, summ, desc);
    713     var->type = OWL_VARIABLE_BOOL;
    714     var->validsettings = "on,off";
    715     var->ival_default = initval;
    716     var->validate_fn = owl_variable_bool_validate_default;
    717     var->set_fn = owl_variable_bool_set_default;
    718     var->set_fromstring_fn = owl_variable_bool_set_fromstring_default;
    719     var->get_fn = owl_variable_get_default;
    720     var->get_tostring_fn = owl_variable_bool_get_tostring_default;
    721     var->delete_fn = owl_variable_delete_default;
    722     var->val = g_new(int, 1);
    723     var->set_fn(var, &initval);
    724     owl_variable_dict_add_variable(vd, var);
    725   }
     698  bool oldval;
     699  if (old && owl_variable_get_type(old) == OWL_VARIABLE_BOOL)
     700    oldval = owl_variable_get_bool(old);
     701  owl_variable *var = owl_variable_newvar(name, summ, desc, "on,off", OWL_VARIABLE_BOOL);
     702  var->ival_default = initval;
     703  owl_variable_init_defaults(var);
     704  if (old && owl_variable_get_type(old) == OWL_VARIABLE_BOOL)
     705    var->set_fn(var, &oldval);
     706  owl_variable_dict_add_variable(vd, var);
    726707}
    727708
Note: See TracChangeset for help on using the changeset viewer.