Changeset 010a951


Ignore:
Timestamp:
Mar 5, 2011, 3:25:29 PM (11 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.8, release-1.9
Children:
e3a75ed
Parents:
9620c8d
git-author:
David Benjamin <davidben@mit.edu> (02/28/11 10:53:18)
git-committer:
David Benjamin <davidben@mit.edu> (03/05/11 15:25:29)
Message:
Make owl_variable's get_tostring_fn return a newly allocated string

The mess with buffer lengths and whatnot is obnoxious. Note that this
does change semantics slightly: instead of (optionally) filling a buffer
and giving a return code for good vs no-variable/invalid, we now return
NULL for missing variables and always return a string if the variable
exists. This also appears to be more accurate from the perspective of
the calls anyway.
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • commands.c

    r259e0475 r010a951  
    16141614{
    16151615  const char *var;
    1616   char valbuff[1024];
     1616  char *value;
    16171617
    16181618  if (argc==1) {
     
    16261626  var=argv[1];
    16271627   
    1628   if (0 == owl_variable_get_tostring(owl_global_get_vardict(&g),
    1629                                      var, valbuff, 1024)) {
    1630     owl_function_makemsg("%s = '%s'", var, valbuff);
     1628  value = owl_variable_get_tostring(owl_global_get_vardict(&g), var);
     1629  if (value) {
     1630    owl_function_makemsg("%s = '%s'", var, value);
     1631    g_free(value);
    16311632  } else {
    16321633    owl_function_makemsg("Unknown variable '%s'.", var);
     
    25072508char *owl_command_getvar(int argc, const char *const *argv, const char *buff)
    25082509{
    2509   char tmpbuff[1024];
    25102510  if (argc != 2) {
    25112511    owl_function_makemsg("Wrong number of arguments for %s", argv[0]);
    25122512    return NULL;
    25132513  }
    2514   if (owl_variable_get_tostring(owl_global_get_vardict(&g),
    2515                                 argv[1], tmpbuff, 1024)) {
    2516     return NULL;
    2517   }
    2518   return g_strdup(tmpbuff);
     2514  return owl_variable_get_tostring(owl_global_get_vardict(&g), argv[1]);
    25192515}
    25202516
  • functions.c

    r3472845 r010a951  
    16161616{
    16171617  const char *name;
    1618   char var[LINE];
     1618  char *var;
    16191619  owl_list varnames;
    16201620  int i, numvarnames;
     
    16291629    if (name && name[0]!='_') {
    16301630      g_string_append_printf(str, "\n%-20s = ", name);
    1631       owl_variable_get_tostring(owl_global_get_vardict(&g), name, var, LINE);
    1632       g_string_append(str, var);
     1631      var = owl_variable_get_tostring(owl_global_get_vardict(&g), name);
     1632      if (var) {
     1633        g_string_append(str, var);
     1634        g_free(var);
     1635      }
    16331636    }
    16341637  }
  • owl.h

    rf54b07d r010a951  
    261261                                 * WARNING:  this approach is hard to make
    262262                                 * thread-safe... */
    263   int  (*get_tostring_fn)(const struct _owl_variable *v,
    264                           char *buf, int bufsize, const void *val);
    265                                 /* converts val to a string
    266                                  * and puts into buf */
     263  char *(*get_tostring_fn)(const struct _owl_variable *v, const void *val);
     264                                /* converts val to a string;
     265                                 * caller must free the result */
    267266  void (*delete_fn)(struct _owl_variable *v);
    268267                                /* frees val as needed */
  • tester.c

    rddbbcffa r010a951  
    269269  owl_vardict vd;
    270270  int numfailed=0;
    271   char buf[1024];
     271  char *value;
    272272  const void *v;
    273273
     
    277277  FAIL_UNLESS("get bool", 0==owl_variable_get_bool(&vd,"rxping"));
    278278  FAIL_UNLESS("get bool (no such)", -1==owl_variable_get_bool(&vd,"mumble"));
    279   FAIL_UNLESS("get bool as string 1", 0==owl_variable_get_tostring(&vd,"rxping", buf, 1024));
    280   FAIL_UNLESS("get bool as string 2", 0==strcmp(buf,"off"));
     279  FAIL_UNLESS("get bool as string",
     280              !strcmp((value = owl_variable_get_tostring(&vd,"rxping")), "off"));
     281  g_free(value);
    281282  FAIL_UNLESS("set bool 1", 0==owl_variable_set_bool_on(&vd,"rxping"));
    282283  FAIL_UNLESS("get bool 2", 1==owl_variable_get_bool(&vd,"rxping"));
     
    293294  FAIL_UNLESS("get int", 8==owl_variable_get_int(&vd,"typewinsize"));
    294295  FAIL_UNLESS("get int (no such)", -1==owl_variable_get_int(&vd,"mmble"));
    295   FAIL_UNLESS("get int as string 1", 0==owl_variable_get_tostring(&vd,"typewinsize", buf, 1024));
    296   FAIL_UNLESS("get int as string 2", 0==strcmp(buf,"8"));
     296  FAIL_UNLESS("get int as string",
     297              !strcmp((value = owl_variable_get_tostring(&vd,"typewinsize")), "8"));
     298  g_free(value);
    297299  FAIL_UNLESS("set int 1", 0==owl_variable_set_int(&vd,"typewinsize",12));
    298300  FAIL_UNLESS("get int 2", 12==owl_variable_get_int(&vd,"typewinsize"));
  • variable.c

    rf54b07d r010a951  
    673673int owl_variable_set_fromstring(owl_vardict *d, const char *name, const char *value, int msg, int requirebool) {
    674674  owl_variable *v;
    675   char buff2[1024];
     675  char *tostring;
    676676  if (!name) return(-1);
    677677  v = owl_dict_find_element(d, name);
     
    694694  }
    695695  if (msg && v->get_tostring_fn) {
    696     v->get_tostring_fn(v, buff2, 1024, v->val);
    697     owl_function_makemsg("%s = '%s'", name, buff2);
     696    tostring = v->get_tostring_fn(v, v->val);
     697    owl_function_makemsg("%s = '%s'", name, tostring);
     698    g_free(tostring);
    698699  }   
    699700  return 0;
     
    726727}
    727728
    728 int owl_variable_get_tostring(const owl_vardict *d, const char *name, char *buf, int bufsize) {
     729char *owl_variable_get_tostring(const owl_vardict *d, const char *name) {
    729730  owl_variable *v;
    730   if (!name) return(-1);
     731  if (!name) return NULL;
    731732  v = owl_dict_find_element(d, name);
    732   if (v == NULL || !v->get_tostring_fn) return(-1);
    733   return v->get_tostring_fn(v, buf, bufsize, v->val);
    734 }
    735 
    736 int owl_variable_get_default_tostring(const owl_vardict *d, const char *name, char *buf, int bufsize) {
     733  if (v == NULL || !v->get_tostring_fn) return NULL;
     734  return v->get_tostring_fn(v, v->val);
     735}
     736
     737char *owl_variable_get_default_tostring(const owl_vardict *d, const char *name) {
    737738  owl_variable *v;
    738   if (!name) return(-1);
     739  if (!name) return NULL;
    739740  v = owl_dict_find_element(d, name);
    740   if (v == NULL || !v->get_tostring_fn) return(-1);
     741  if (v == NULL || !v->get_tostring_fn) return NULL;
    741742  if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
    742     return v->get_tostring_fn(v, buf, bufsize, &(v->ival_default));
     743    return v->get_tostring_fn(v, &(v->ival_default));
    743744  } else {
    744     return v->get_tostring_fn(v, buf, bufsize, v->pval_default);
     745    return v->get_tostring_fn(v, v->pval_default);
    745746  }
    746747}
     
    786787
    787788void owl_variable_describe(const owl_vardict *d, const char *name, owl_fmtext *fm) {
    788   char defaultbuf[50];
    789   char buf[1024];
    790   int buflen = 1023;
     789  char *default_buf;
    791790  owl_variable *v;
    792791
     
    794793      || (v = owl_dict_find_element(d, name)) == NULL
    795794      || !v->get_fn) {
    796     snprintf(buf, buflen, "     No such variable '%s'\n", name);     
    797     owl_fmtext_append_normal(fm, buf);
     795    owl_fmtext_appendf_normal(fm, "     No such variable '%s'\n", name);
    798796    return;
    799797  }
    800798  if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
    801     v->get_tostring_fn(v, defaultbuf, 50, &(v->ival_default));
     799    default_buf = v->get_tostring_fn(v, &(v->ival_default));
    802800  } else {
    803     v->get_tostring_fn(v, defaultbuf, 50, v->pval_default);
    804   }
    805   snprintf(buf, buflen, OWL_TABSTR "%-20s - %s (default: '%s')\n",
    806                   v->name,
    807                   owl_variable_get_summary(v), defaultbuf);
    808   owl_fmtext_append_normal(fm, buf);
     801    default_buf = v->get_tostring_fn(v, v->pval_default);
     802  }
     803  owl_fmtext_appendf_normal(fm, OWL_TABSTR "%-20s - %s (default: '%s')\n",
     804                            v->name,
     805                            owl_variable_get_summary(v), default_buf);
     806  g_free(default_buf);
    809807}
    810808
    811809void owl_variable_get_help(const owl_vardict *d, const char *name, owl_fmtext *fm) {
    812   char buff[1024];
    813   int bufflen = 1023;
     810  char *tostring;
    814811  owl_variable *v;
    815812
     
    829826
    830827  owl_fmtext_append_normal(fm, "Current:        ");
    831   owl_variable_get_tostring(d, name, buff, bufflen);
    832   owl_fmtext_append_normal(fm, buff);
     828  tostring = owl_variable_get_tostring(d, name);
     829  owl_fmtext_append_normal(fm, tostring);
     830  g_free(tostring);
    833831  owl_fmtext_append_normal(fm, "\n\n");
    834832
    835833
    836834  if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
    837     v->get_tostring_fn(v, buff, bufflen, &(v->ival_default));
     835    tostring = v->get_tostring_fn(v, &(v->ival_default));
    838836  } else {
    839     v->get_tostring_fn(v, buff, bufflen, v->pval_default);
     837    tostring = v->get_tostring_fn(v, v->pval_default);
    840838  }
    841839  owl_fmtext_append_normal(fm, "Default:        ");
    842   owl_fmtext_append_normal(fm, buff);
     840  owl_fmtext_append_normal(fm, tostring);
    843841  owl_fmtext_append_normal(fm, "\n\n");
    844842
     
    852850    owl_fmtext_append_normal(fm, "\n\n");
    853851  }
     852  g_free(tostring);
    854853}
    855854
     
    896895}
    897896
    898 int owl_variable_bool_get_tostring_default(const owl_variable *v, char* buf, int bufsize, const void *val) {
     897char *owl_variable_bool_get_tostring_default(const owl_variable *v, const void *val) {
    899898  if (val == NULL) {
    900     snprintf(buf, bufsize, "<null>");
    901     return -1;
     899    return g_strdup("<null>");
    902900  } else if (*(const int*)val == 0) {
    903     snprintf(buf, bufsize, "off");
    904     return 0;
     901    return g_strdup("off");
    905902  } else if (*(const int*)val == 1) {
    906     snprintf(buf, bufsize, "on");
    907     return 0;
     903    return g_strdup("on");
    908904  } else {
    909     snprintf(buf, bufsize, "<invalid>");
    910     return -1;
     905    return g_strdup("<invalid>");
    911906  }
    912907}
     
    935930}
    936931
    937 int owl_variable_int_get_tostring_default(const owl_variable *v, char* buf, int bufsize, const void *val) {
     932char *owl_variable_int_get_tostring_default(const owl_variable *v, const void *val) {
    938933  if (val == NULL) {
    939     snprintf(buf, bufsize, "<null>");
    940     return -1;
     934    return g_strdup("<null>");
    941935  } else {
    942     snprintf(buf, bufsize, "%d", *(const int*)val);
    943     return 0;
     936    return g_strdup_printf("%d", *(const int*)val);
    944937  }
    945938}
     
    976969}
    977970
    978 int owl_variable_enum_get_tostring(const owl_variable *v, char* buf, int bufsize, const void *val) {
     971char *owl_variable_enum_get_tostring(const owl_variable *v, const void *val) {
    979972  char **enums;
    980973  int nenums, i;
     974  char *tostring;
    981975
    982976  if (val == NULL) {
    983     snprintf(buf, bufsize, "<null>");
    984     return -1;
     977    return g_strdup("<null>");
    985978  }
    986979  enums = g_strsplit_set(v->validsettings, ",", 0);
     
    988981  i = *(const int*)val;
    989982  if (i<0 || i>=nenums) {
    990     snprintf(buf, bufsize, "<invalid:%d>",i);
    991983    g_strfreev(enums);
    992     return(-1);
    993   }
    994   snprintf(buf, bufsize, "%s", enums[i]);
     984    return g_strdup_printf("<invalid:%d>", i);
     985  }
     986  tostring = g_strdup(enums[i]);
    995987  g_strfreev(enums);
    996   return 0;
     988  return tostring;
    997989}
    998990
     
    10171009}
    10181010
    1019 int owl_variable_string_get_tostring_default(const owl_variable *v, char* buf, int bufsize, const void *val) {
     1011char *owl_variable_string_get_tostring_default(const owl_variable *v, const void *val) {
    10201012  if (val == NULL) {
    1021     snprintf(buf, bufsize, "<null>");
    1022     return -1;
     1013    return g_strdup("<null>");
    10231014  } else {
    1024     snprintf(buf, bufsize, "%s", (const char*)val);
    1025     return 0;
    1026   }
    1027 }
    1028 
     1015    return g_strdup((const char*)val);
     1016  }
     1017}
     1018
Note: See TracChangeset for help on using the changeset viewer.