Changeset 010a951 for variable.c


Ignore:
Timestamp:
Mar 5, 2011, 3:25:29 PM (10 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.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.