Changeset ca54fd6


Ignore:
Timestamp:
Jul 3, 2011, 8:42:55 PM (13 years ago)
Author:
Jason Gross <jgross@mit.edu>
Branches:
master, release-1.10, 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).
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • commands.c

    re6d7e4e rca54fd6  
    15721572  int  silent=0;
    15731573  int requirebool=0;
     1574  owl_variable *v;
    15741575
    15751576  if (argc == 1) {
     
    15941595    return NULL;
    15951596  }
    1596   owl_variable_set_fromstring(owl_global_get_vardict(&g), var, val, !silent, requirebool);
     1597
     1598  v = owl_variable_get_var(owl_global_get_vardict(&g), var);
     1599  if (v == NULL) {
     1600    if (!silent) owl_function_error("Unknown variable '%s'", var);
     1601  } else if (requirebool && owl_variable_get_type(v) != OWL_VARIABLE_BOOL) {
     1602    if (!silent) owl_function_error("Variable '%s' is not a boolean", var);
     1603  } else {
     1604    owl_variable_set_fromstring(v, val, !silent);
     1605  }
    15971606  return NULL;
    15981607}
     
    16001609char *owl_command_unset(int argc, const char *const *argv, const char *buff)
    16011610{
     1611  owl_variable *v;
    16021612  const char *var, *val;
    16031613  int  silent=0;
     
    16141624    return NULL;
    16151625  }
    1616   owl_variable_set_fromstring(owl_global_get_vardict(&g), var, val, !silent, 1);
     1626
     1627  v = owl_variable_get_var(owl_global_get_vardict(&g), var);
     1628  if (v == NULL) {
     1629    if (!silent) owl_function_error("Unknown variable '%s'", var);
     1630  } else if (owl_variable_get_type(v) != OWL_VARIABLE_BOOL) {
     1631    if (!silent) owl_function_error("Variable '%s' is not a boolean", var);
     1632  } else {
     1633    owl_variable_set_fromstring(v, val, !silent);
     1634  }
    16171635  return NULL;
    16181636}
     
    16221640  const char *var;
    16231641  char *value;
     1642  const owl_variable *v;
    16241643
    16251644  if (argc==1) {
     
    16331652  var=argv[1];
    16341653   
    1635   value = owl_variable_get_tostring(owl_global_get_vardict(&g), var);
    1636   if (value) {
     1654  v = owl_variable_get_var(owl_global_get_vardict(&g), var);
     1655  if (v) {
     1656    value = owl_variable_get_tostring(v);
    16371657    owl_function_makemsg("%s = '%s'", var, value);
    16381658    g_free(value);
     
    25052525}
    25062526
    2507 char *owl_command_getvar(int argc, const char *const *argv, const char *buff)
    2508 {
     2527CALLER_OWN char *owl_command_getvar(int argc, const char *const *argv, const char *buff)
     2528{
     2529  const owl_variable *v;
    25092530  if (argc != 2) {
    25102531    owl_function_makemsg("Wrong number of arguments for %s", argv[0]);
    25112532    return NULL;
    25122533  }
    2513   return owl_variable_get_tostring(owl_global_get_vardict(&g), argv[1]);
     2534  v = owl_variable_get_var(owl_global_get_vardict(&g), argv[1]);
     2535  if (v == NULL) return NULL;
     2536  return owl_variable_get_tostring(v);
    25142537}
    25152538
  • functions.c

    rb8a3e00 rca54fd6  
    15721572void owl_function_printallvars(void)
    15731573{
     1574  const owl_variable *v;
    15741575  const char *name;
    15751576  char *var;
     
    15851586    if (name && name[0]!='_') {
    15861587      g_string_append_printf(str, "\n%-20s = ", name);
    1587       var = owl_variable_get_tostring(owl_global_get_vardict(&g), name);
     1588      v = owl_variable_get_var(owl_global_get_vardict(&g), name);
     1589      var = owl_variable_get_tostring(v);
    15881590      if (var) {
    15891591        g_string_append(str, var);
     
    16011603void owl_function_show_variables(void)
    16021604{
     1605  const owl_variable *v;
    16031606  GPtrArray *varnames;
    16041607  owl_fmtext fm; 
     
    16131616    varname = varnames->pdata[i];
    16141617    if (varname && varname[0]!='_') {
    1615       owl_variable_describe(owl_global_get_vardict(&g), varname, &fm);
     1618      v = owl_variable_get_var(owl_global_get_vardict(&g), varname);
     1619      owl_variable_describe(v, &fm);
    16161620    }
    16171621  }
     
    16231627void owl_function_show_variable(const char *name)
    16241628{
     1629  const owl_variable *v;
    16251630  owl_fmtext fm; 
    16261631
    16271632  owl_fmtext_init_null(&fm);
    1628   owl_variable_get_help(owl_global_get_vardict(&g), name, &fm);
     1633  v = owl_variable_get_var(owl_global_get_vardict(&g), name);
     1634  if (v)
     1635    owl_variable_get_help(v, &fm);
     1636  else
     1637    owl_fmtext_append_normal(&fm, "No such variable...\n");
    16291638  owl_function_popless_fmtext(&fm);
    16301639  owl_fmtext_cleanup(&fm);
  • help.c

    rce68f23 rca54fd6  
    44void owl_help(void)
    55{
     6  const owl_variable *v;
    67  owl_fmtext fm;
    78  const char *varname;
     
    133134    varname = varnames->pdata[i];
    134135    if (varname && varname[0]!='_') {
    135       owl_variable_describe(owl_global_get_vardict(&g), varname, &fm);
     136      v = owl_variable_get_var(owl_global_get_vardict(&g), varname);
     137      owl_variable_describe(v, &fm);
    136138    }
    137139  }
  • stubgen.pl

    rfd03b12 rca54fd6  
    1414    if ($vartype =~ /^BOOL/) {
    1515        print "void owl_global_set_${altvarname}_on(owl_global *g) {\n";
    16         print "  owl_variable_set_bool_on(&g->vars, \"$varname\");\n}\n";
     16        print "  owl_variable_set_bool_on(owl_variable_get_var(&g->vars, \"$varname\"));\n}\n";
    1717        print "void owl_global_set_${altvarname}_off(owl_global *g) {\n";
    18         print "  owl_variable_set_bool_off(&g->vars, \"$varname\");\n}\n";
     18        print "  owl_variable_set_bool_off(owl_variable_get_var(&g->vars, \"$varname\"));\n}\n";
    1919        print "int owl_global_is_$altvarname(const owl_global *g) {\n";
    20         print "  return owl_variable_get_bool(&g->vars, \"$varname\");\n}\n";
     20        print "  return owl_variable_get_bool(owl_variable_get_var(&g->vars, \"$varname\"));\n}\n";
    2121    } elsif ($vartype =~ /^PATH/ or $vartype =~ /^STRING/) {
    2222        print "void owl_global_set_$altvarname(owl_global *g, const char *text) {\n";
    23         print "  owl_variable_set_string(&g->vars, \"$varname\", text);\n}\n";
     23        print "  owl_variable_set_string(owl_variable_get_var(&g->vars, \"$varname\"), text);\n}\n";
    2424        print "const char *owl_global_get_$altvarname(const owl_global *g) {\n";
    25         print "  return owl_variable_get_string(&g->vars, \"$varname\");\n}\n";
     25        print "  return owl_variable_get_string(owl_variable_get_var(&g->vars, \"$varname\"));\n}\n";
    2626    } elsif ($vartype =~ /^INT/ or $vartype =~ /^ENUM/) {
    2727        print "void owl_global_set_$altvarname(owl_global *g, int n) {\n";
    28         print "  owl_variable_set_int(&g->vars, \"$varname\", n);\n}\n";
     28        print "  owl_variable_set_int(owl_variable_get_var(&g->vars, \"$varname\"), n);\n}\n";
    2929        print "int owl_global_get_$altvarname(const owl_global *g) {\n";
    30         print "  return owl_variable_get_int(&g->vars, \"$varname\");\n}\n";
     30        print "  return owl_variable_get_int(owl_variable_get_var(&g->vars, \"$varname\"));\n}\n";
    3131    }
    3232    }
  • tester.c

    rbc1d648 rca54fd6  
    276276int owl_variable_regtest(void) {
    277277  owl_vardict vd;
     278  owl_variable *var;
    278279  int numfailed=0;
    279280  char *value;
     
    283284  FAIL_UNLESS("setup", 0==owl_variable_dict_setup(&vd));
    284285
    285   FAIL_UNLESS("get bool", 0==owl_variable_get_bool(&vd,"rxping"));
    286   FAIL_UNLESS("get bool (no such)", -1==owl_variable_get_bool(&vd,"mumble"));
     286  FAIL_UNLESS("get bool var", NULL != (var = owl_variable_get_var(&vd, "rxping")));
     287  FAIL_UNLESS("get bool", 0 == owl_variable_get_bool(var));
     288  FAIL_UNLESS("get bool (no such)", NULL == owl_variable_get_var(&vd, "mumble"));
    287289  FAIL_UNLESS("get bool as string",
    288               !strcmp((value = owl_variable_get_tostring(&vd,"rxping")), "off"));
     290              !strcmp((value = owl_variable_get_tostring(var)), "off"));
    289291  g_free(value);
    290   FAIL_UNLESS("set bool 1", 0==owl_variable_set_bool_on(&vd,"rxping"));
    291   FAIL_UNLESS("get bool 2", 1==owl_variable_get_bool(&vd,"rxping"));
    292   FAIL_UNLESS("set bool 3", 0==owl_variable_set_fromstring(&vd,"rxping","off",0,0));
    293   FAIL_UNLESS("get bool 4", 0==owl_variable_get_bool(&vd,"rxping"));
    294   FAIL_UNLESS("set bool 5", -1==owl_variable_set_fromstring(&vd,"rxping","xxx",0,0));
    295   FAIL_UNLESS("get bool 6", 0==owl_variable_get_bool(&vd,"rxping"));
    296 
    297 
    298   FAIL_UNLESS("get string", 0==strcmp("~/zlog/people", owl_variable_get_string(&vd,"logpath")));
    299   FAIL_UNLESS("set string 7", 0==owl_variable_set_string(&vd,"logpath","whee"));
    300   FAIL_UNLESS("get string", 0==strcmp("whee", owl_variable_get_string(&vd,"logpath")));
    301 
    302   FAIL_UNLESS("get int", 8==owl_variable_get_int(&vd,"typewinsize"));
    303   FAIL_UNLESS("get int (no such)", -1==owl_variable_get_int(&vd,"mmble"));
     292  FAIL_UNLESS("set bool 1", 0 == owl_variable_set_bool_on(var));
     293  FAIL_UNLESS("get bool 2", 1 == owl_variable_get_bool(var));
     294  FAIL_UNLESS("set bool 3", 0 == owl_variable_set_fromstring(var, "off", 0));
     295  FAIL_UNLESS("get bool 4", 0 == owl_variable_get_bool(var));
     296  FAIL_UNLESS("set bool 5", -1 == owl_variable_set_fromstring(var, "xxx", 0));
     297  FAIL_UNLESS("get bool 6", 0 == owl_variable_get_bool(var));
     298
     299
     300  FAIL_UNLESS("get string var", NULL != (var = owl_variable_get_var(&vd, "logpath")));
     301  FAIL_UNLESS("get string", 0 == strcmp("~/zlog/people", owl_variable_get_string(var)));
     302  FAIL_UNLESS("set string 7", 0 == owl_variable_set_string(var, "whee"));
     303  FAIL_UNLESS("get string", !strcmp("whee", owl_variable_get_string(var)));
     304
     305  FAIL_UNLESS("get int var", NULL != (var = owl_variable_get_var(&vd, "typewinsize")));
     306  FAIL_UNLESS("get int", 8 == owl_variable_get_int(var));
     307  FAIL_UNLESS("get int (no such)", NULL == owl_variable_get_var(&vd, "mumble"));
    304308  FAIL_UNLESS("get int as string",
    305               !strcmp((value = owl_variable_get_tostring(&vd,"typewinsize")), "8"));
     309              !strcmp((value = owl_variable_get_tostring(var)), "8"));
    306310  g_free(value);
    307   FAIL_UNLESS("set int 1", 0==owl_variable_set_int(&vd,"typewinsize",12));
    308   FAIL_UNLESS("get int 2", 12==owl_variable_get_int(&vd,"typewinsize"));
    309   FAIL_UNLESS("set int 1b", -1==owl_variable_set_int(&vd,"typewinsize",-3));
    310   FAIL_UNLESS("get int 2b", 12==owl_variable_get_int(&vd,"typewinsize"));
    311   FAIL_UNLESS("set int 3", 0==owl_variable_set_fromstring(&vd,"typewinsize","9",0,0));
    312   FAIL_UNLESS("get int 4", 9==owl_variable_get_int(&vd,"typewinsize"));
    313   FAIL_UNLESS("set int 5", -1==owl_variable_set_fromstring(&vd,"typewinsize","xxx",0,0));
    314   FAIL_UNLESS("set int 6", -1==owl_variable_set_fromstring(&vd,"typewinsize","",0,0));
    315   FAIL_UNLESS("get int 7", 9==owl_variable_get_int(&vd,"typewinsize"));
     311  FAIL_UNLESS("set int 1", 0 == owl_variable_set_int(var, 12));
     312  FAIL_UNLESS("get int 2", 12 == owl_variable_get_int(var));
     313  FAIL_UNLESS("set int 1b", -1 == owl_variable_set_int(var, -3));
     314  FAIL_UNLESS("get int 2b", 12 == owl_variable_get_int(var));
     315  FAIL_UNLESS("set int 3", 0 == owl_variable_set_fromstring(var, "9", 0));
     316  FAIL_UNLESS("get int 4", 9 == owl_variable_get_int(var));
     317  FAIL_UNLESS("set int 5", -1 == owl_variable_set_fromstring(var, "xxx", 0));
     318  FAIL_UNLESS("set int 6", -1 == owl_variable_set_fromstring(var, "", 0));
     319  FAIL_UNLESS("get int 7", 9 == owl_variable_get_int(var));
    316320
    317321  owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval");
    318   FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(&vd, "stringvar")));
    319   FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(&vd, "stringvar")));
    320   owl_variable_set_string(&vd, "stringvar", "new val");
    321   FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(&vd, "stringvar")));
     322  FAIL_UNLESS("get new string var", NULL != (var = owl_variable_get_var(&vd, "stringvar")));
     323  FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(var)));
     324  FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(var)));
     325  owl_variable_set_string(var, "new val");
     326  FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(var)));
    322327
    323328  owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47);
    324   FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(&vd, "intvar")));
    325   FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(&vd, "intvar"));
    326   owl_variable_set_int(&vd, "intvar", 17);
    327   FAIL_UNLESS("update bool val", 17 == owl_variable_get_int(&vd, "intvar"));
     329  FAIL_UNLESS("get new int var", NULL != (var = owl_variable_get_var(&vd, "intvar")));
     330  FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(var)));
     331  FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(var));
     332  owl_variable_set_int(var, 17);
     333  FAIL_UNLESS("update int val", 17 == owl_variable_get_int(var));
    328334
    329335  owl_variable_dict_newvar_bool(&vd, "boolvar", "", "", 1);
    330   FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(&vd, "boolvar")));
    331   FAIL_UNLESS("get new bool val", owl_variable_get_bool(&vd, "boolvar"));
    332   owl_variable_set_bool_off(&vd, "boolvar");
    333   FAIL_UNLESS("update string val", !owl_variable_get_bool(&vd, "boolvar"));
     336  FAIL_UNLESS("get new bool var", NULL != (var = owl_variable_get_var(&vd, "boolvar")));
     337  FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(var)));
     338  FAIL_UNLESS("get new bool val", owl_variable_get_bool(var));
     339  owl_variable_set_bool_off(var);
     340  FAIL_UNLESS("update bool val", !owl_variable_get_bool(var));
    334341
    335342  owl_variable_dict_cleanup(&vd);
  • 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.