Changeset ca54fd6 for variable.c


Ignore:
Timestamp:
Jul 3, 2011, 8:42:55 PM (10 years ago)
Author:
Jason Gross <jgross@mit.edu>
Branches:
master, release-1.8, release-1.9
Children:
ca749a9
Parents:
bc1d648
git-author:
Jason Gross <jgross@mit.edu> (06/27/11 01:14:13)
git-committer:
Jason Gross <jgross@mit.edu> (07/03/11 20:42:55)
Message:
Pass owl_variable * around instead of owl_vardict *.

This allows (forces) functions to do their own NULL- and type- checking
on variables.  The functions in varsubs.c get less checking, but if
these auto-generated functions are failing, something more serious is
probably wrong.

The owl_variable_get_* functions type-check their arguments, and
owl_function_error if the argument is the wrong type (it's a programmer
error and we should make some noise).
File:
1 edited

Legend:

Unmodified
Added
Removed
  • variable.c

    rbc1d648 rca54fd6  
    665665{
    666666  owl_variable *old = owl_variable_get_var(vd, name);
    667   if (old && old->type == OWL_VARIABLE_STRING) {
     667  if (old && owl_variable_get_type(old) == OWL_VARIABLE_STRING) {
    668668    owl_variable_update(old, summ, desc);
    669669    g_free(old->pval_default);
     
    686686{
    687687  owl_variable *old = owl_variable_get_var(vd, name);
    688   if (old && old->type == OWL_VARIABLE_INT) {
     688  if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT) {
    689689    owl_variable_update(old, summ, desc);
    690690    old->ival_default = initval;
     
    708708{
    709709  owl_variable *old = owl_variable_get_var(vd, name);
    710   if (old && old->type == OWL_VARIABLE_BOOL) {
     710  if (old && owl_variable_get_type(old) == OWL_VARIABLE_BOOL) {
    711711    owl_variable_update(old, summ, desc);
    712712    old->ival_default = initval;
     
    753753
    754754
     755const char *owl_variable_get_name(const owl_variable *v)
     756{
     757  return v->name;
     758}
     759
    755760const char *owl_variable_get_description(const owl_variable *v) {
    756761  return v->description;
     
    769774}
    770775
     776int owl_variable_get_type(const owl_variable *v)
     777{
     778  return v->type;
     779}
     780
    771781/* functions for getting and setting variable values */
    772782
    773783/* returns 0 on success, prints a status msg if msg is true */
    774 int owl_variable_set_fromstring(owl_vardict *d, const char *name, const char *value, int msg, int requirebool) {
    775   owl_variable *v;
     784int owl_variable_set_fromstring(owl_variable *v, const char *value, int msg) {
    776785  char *tostring;
    777   if (!name) return(-1);
    778   v = owl_dict_find_element(d, name);
    779   if (v == NULL) {
    780     if (msg) owl_function_error("Unknown variable %s", name);
    781     return -1;
    782   }
    783786  if (!v->set_fromstring_fn) {
    784     if (msg) owl_function_error("Variable %s is read-only", name);
     787    if (msg) owl_function_error("Variable %s is read-only", owl_variable_get_name(v));
    785788    return -1;   
    786789  }
    787   if (requirebool && v->type!=OWL_VARIABLE_BOOL) {
    788     if (msg) owl_function_error("Variable %s is not a boolean", name);
    789     return -1;   
    790   }
    791790  if (0 != v->set_fromstring_fn(v, value)) {
    792     if (msg) owl_function_error("Unable to set %s (must be %s)", name,
    793                                   owl_variable_get_validsettings(v));
     791    if (msg) owl_function_error("Unable to set %s (must be %s)", owl_variable_get_name(v),
     792                                owl_variable_get_validsettings(v));
    794793    return -1;
    795794  }
    796795  if (msg && v->get_tostring_fn) {
    797796    tostring = v->get_tostring_fn(v, v->get_fn(v));
    798     owl_function_makemsg("%s = '%s'", name, tostring);
     797    owl_function_makemsg("%s = '%s'", owl_variable_get_name(v), tostring);
    799798    g_free(tostring);
    800799  }   
     
    802801}
    803802 
    804 int owl_variable_set_string(owl_vardict *d, const char *name, const char *newval) {
    805   owl_variable *v;
    806   if (!name) return(-1);
    807   v = owl_dict_find_element(d, name);
    808   if (v == NULL || !v->set_fn) return(-1);
    809   if (v->type!=OWL_VARIABLE_STRING) return(-1);
     803int owl_variable_set_string(owl_variable *v, const char *newval)
     804{
     805  if (v->type != OWL_VARIABLE_STRING) return -1;
    810806  return v->set_fn(v, newval);
    811807}
    812808 
    813 int owl_variable_set_int(owl_vardict *d, const char *name, int newval) {
    814   owl_variable *v;
    815   if (!name) return(-1);
    816   v = owl_dict_find_element(d, name);
    817   if (v == NULL || !v->set_fn) return(-1);
    818   if (v->type!=OWL_VARIABLE_INT && v->type!=OWL_VARIABLE_BOOL) return(-1);
     809int owl_variable_set_int(owl_variable *v, int newval)
     810{
     811  if (v->type != OWL_VARIABLE_INT && v->type != OWL_VARIABLE_BOOL) return -1;
    819812  return v->set_fn(v, &newval);
    820813}
    821814 
    822 int owl_variable_set_bool_on(owl_vardict *d, const char *name) {
    823   return owl_variable_set_int(d,name,1);
    824 }
    825 
    826 int owl_variable_set_bool_off(owl_vardict *d, const char *name) {
    827   return owl_variable_set_int(d,name,0);
    828 }
    829 
    830 CALLER_OWN char *owl_variable_get_tostring(const owl_vardict *d, const char *name)
    831 {
    832   owl_variable *v;
    833   if (!name) return NULL;
    834   v = owl_dict_find_element(d, name);
    835   if (v == NULL || !v->get_tostring_fn) return NULL;
     815int owl_variable_set_bool_on(owl_variable *v)
     816{
     817  if (v->type != OWL_VARIABLE_BOOL) return -1;
     818  return owl_variable_set_int(v, true);
     819}
     820
     821int owl_variable_set_bool_off(owl_variable *v)
     822{
     823  if (v->type != OWL_VARIABLE_BOOL) return -1;
     824  return owl_variable_set_int(v, false);
     825}
     826
     827CALLER_OWN char *owl_variable_get_tostring(const owl_variable *v)
     828{
    836829  return v->get_tostring_fn(v, v->get_fn(v));
    837830}
    838831
    839 CALLER_OWN char *owl_variable_get_default_tostring(const owl_vardict *d, const char *name)
    840 {
    841   owl_variable *v;
    842   if (!name) return NULL;
    843   v = owl_dict_find_element(d, name);
    844   if (v == NULL || !v->get_tostring_fn) return NULL;
     832CALLER_OWN char *owl_variable_get_default_tostring(const owl_variable *v)
     833{
    845834  if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
    846835    return v->get_tostring_fn(v, &(v->ival_default));
     
    860849
    861850/* returns a reference */
    862 const void *owl_variable_get(const owl_vardict *d, const char *name)
    863 {
    864   const owl_variable *v = owl_variable_get_var(d, name);
    865   if(v == NULL) return NULL;
     851const void *owl_variable_get(const owl_variable *v)
     852{
    866853  return v->get_fn(v);
    867854}
    868855
     856const char *owl_variable_get_string(const owl_variable *v)
     857{
     858  if (owl_variable_get_type(v) != OWL_VARIABLE_STRING) {
     859    owl_function_error("Variable '%s' is not a string.", owl_variable_get_name(v));
     860    return NULL;
     861  }
     862  return owl_variable_get(v);
     863}
     864
    869865/* returns a reference */
    870 const char *owl_variable_get_string(const owl_vardict *d, const char *name) {
    871   /* TODO: (Decide whether or not to) type check? */
    872   return owl_variable_get(d, name);
    873 }
    874 
    875 /* returns a reference */
    876 const void *owl_variable_get_other(const owl_vardict *d, const char *name) {
    877   /* TODO: (Decide whether or not to) type check? */
    878   return owl_variable_get(d, name);
    879 }
    880 
    881 int owl_variable_get_int(const owl_vardict *d, const char *name) {
    882   /* TODO: (Decide whether or not to) type check? */
    883   const int *pi = owl_variable_get(d, name);
     866const void *owl_variable_get_other(const owl_variable *v)
     867{
     868  if (owl_variable_get_type(v) != OWL_VARIABLE_OTHER) {
     869    owl_function_error("Variable '%s' is not type other.", owl_variable_get_name(v));
     870    return NULL;
     871  }
     872  return owl_variable_get(v);
     873}
     874
     875int owl_variable_get_int(const owl_variable *v)
     876{
     877  if (owl_variable_get_type(v) != OWL_VARIABLE_INT) {
     878    owl_function_error("Variable '%s' is an int.", owl_variable_get_name(v));
     879    return -1;
     880  }
     881  const int *pi = owl_variable_get(v);
    884882  if (!pi) return -1;
    885883  return *pi;
    886884}
    887885
    888 int owl_variable_get_bool(const owl_vardict *d, const char *name) {
    889   /* TODO: (Decide whether or not to) type check? */
    890   const int *pi = owl_variable_get(d, name);
     886int owl_variable_get_bool(const owl_variable *v)
     887{
     888  if (owl_variable_get_type(v) != OWL_VARIABLE_BOOL) {
     889    owl_function_error("Variable '%s' is a boolean.", owl_variable_get_name(v));
     890    return -1;
     891  }
     892  const int *pi = owl_variable_get(v);
    891893  if (!pi) return -1;
    892894  return *pi;
    893895}
    894896
    895 void owl_variable_describe(const owl_vardict *d, const char *name, owl_fmtext *fm) {
    896   char *default_buf;
    897   owl_variable *v;
    898 
    899   if (!name
    900       || (v = owl_dict_find_element(d, name)) == NULL
    901       || !v->get_fn) {
    902     owl_fmtext_appendf_normal(fm, "     No such variable '%s'\n", name);
    903     return;
    904   }
    905   if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
    906     default_buf = v->get_tostring_fn(v, &(v->ival_default));
    907   } else {
    908     default_buf = v->get_tostring_fn(v, v->pval_default);
    909   }
     897void owl_variable_describe(const owl_variable *v, owl_fmtext *fm)
     898{
     899  char *default_buf = owl_variable_get_default_tostring(v);
     900
    910901  owl_fmtext_appendf_normal(fm, OWL_TABSTR "%-20s - %s (default: '%s')\n",
    911                             v->name,
     902                            owl_variable_get_name(v),
    912903                            owl_variable_get_summary(v), default_buf);
    913904  g_free(default_buf);
    914905}
    915906
    916 void owl_variable_get_help(const owl_vardict *d, const char *name, owl_fmtext *fm) {
     907void owl_variable_get_help(const owl_variable *v, owl_fmtext *fm) {
    917908  char *tostring;
    918   owl_variable *v;
    919 
    920   if (!name
    921       || (v = owl_dict_find_element(d, name)) == NULL
    922       || !v->get_fn) {
    923     owl_fmtext_append_normal(fm, "No such variable...\n");
    924     return;
    925   }
    926909
    927910  owl_fmtext_append_bold(fm, "OWL VARIABLE\n\n");
    928911  owl_fmtext_append_normal(fm, OWL_TABSTR);
    929   owl_fmtext_append_normal(fm, name);
     912  owl_fmtext_append_normal(fm, owl_variable_get_name(v));
    930913  owl_fmtext_append_normal(fm, " - ");
    931   owl_fmtext_append_normal(fm, v->summary);
     914  owl_fmtext_append_normal(fm, owl_variable_get_summary(v));
    932915  owl_fmtext_append_normal(fm, "\n\n");
    933916
    934917  owl_fmtext_append_normal(fm, "Current:        ");
    935   tostring = owl_variable_get_tostring(d, name);
     918  tostring = owl_variable_get_tostring(v);
    936919  owl_fmtext_append_normal(fm, tostring);
    937920  g_free(tostring);
     
    939922
    940923
    941   if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
    942     tostring = v->get_tostring_fn(v, &(v->ival_default));
    943   } else {
    944     tostring = v->get_tostring_fn(v, v->pval_default);
    945   }
     924  tostring = owl_variable_get_default_tostring(v);
    946925  owl_fmtext_append_normal(fm, "Default:        ");
    947926  owl_fmtext_append_normal(fm, tostring);
Note: See TracChangeset for help on using the changeset viewer.