Changeset 010a951 for variable.c
- Timestamp:
- Mar 5, 2011, 3:25:29 PM (14 years ago)
- Branches:
- master, release-1.10, 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)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
variable.c
rf54b07d r010a951 673 673 int owl_variable_set_fromstring(owl_vardict *d, const char *name, const char *value, int msg, int requirebool) { 674 674 owl_variable *v; 675 char buff2[1024];675 char *tostring; 676 676 if (!name) return(-1); 677 677 v = owl_dict_find_element(d, name); … … 694 694 } 695 695 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); 698 699 } 699 700 return 0; … … 726 727 } 727 728 728 int owl_variable_get_tostring(const owl_vardict *d, const char *name, char *buf, int bufsize) {729 char *owl_variable_get_tostring(const owl_vardict *d, const char *name) { 729 730 owl_variable *v; 730 if (!name) return (-1);731 if (!name) return NULL; 731 732 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 737 char *owl_variable_get_default_tostring(const owl_vardict *d, const char *name) { 737 738 owl_variable *v; 738 if (!name) return (-1);739 if (!name) return NULL; 739 740 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; 741 742 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)); 743 744 } else { 744 return v->get_tostring_fn(v, buf, bufsize,v->pval_default);745 return v->get_tostring_fn(v, v->pval_default); 745 746 } 746 747 } … … 786 787 787 788 void 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; 791 790 owl_variable *v; 792 791 … … 794 793 || (v = owl_dict_find_element(d, name)) == NULL 795 794 || !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); 798 796 return; 799 797 } 800 798 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)); 802 800 } 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); 809 807 } 810 808 811 809 void 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; 814 811 owl_variable *v; 815 812 … … 829 826 830 827 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); 833 831 owl_fmtext_append_normal(fm, "\n\n"); 834 832 835 833 836 834 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)); 838 836 } else { 839 v->get_tostring_fn(v, buff, bufflen, v->pval_default);837 tostring = v->get_tostring_fn(v, v->pval_default); 840 838 } 841 839 owl_fmtext_append_normal(fm, "Default: "); 842 owl_fmtext_append_normal(fm, buff);840 owl_fmtext_append_normal(fm, tostring); 843 841 owl_fmtext_append_normal(fm, "\n\n"); 844 842 … … 852 850 owl_fmtext_append_normal(fm, "\n\n"); 853 851 } 852 g_free(tostring); 854 853 } 855 854 … … 896 895 } 897 896 898 int owl_variable_bool_get_tostring_default(const owl_variable *v, char* buf, int bufsize, const void *val) {897 char *owl_variable_bool_get_tostring_default(const owl_variable *v, const void *val) { 899 898 if (val == NULL) { 900 snprintf(buf, bufsize, "<null>"); 901 return -1; 899 return g_strdup("<null>"); 902 900 } else if (*(const int*)val == 0) { 903 snprintf(buf, bufsize, "off"); 904 return 0; 901 return g_strdup("off"); 905 902 } else if (*(const int*)val == 1) { 906 snprintf(buf, bufsize, "on"); 907 return 0; 903 return g_strdup("on"); 908 904 } else { 909 snprintf(buf, bufsize, "<invalid>"); 910 return -1; 905 return g_strdup("<invalid>"); 911 906 } 912 907 } … … 935 930 } 936 931 937 int owl_variable_int_get_tostring_default(const owl_variable *v, char* buf, int bufsize, const void *val) {932 char *owl_variable_int_get_tostring_default(const owl_variable *v, const void *val) { 938 933 if (val == NULL) { 939 snprintf(buf, bufsize, "<null>"); 940 return -1; 934 return g_strdup("<null>"); 941 935 } else { 942 snprintf(buf, bufsize, "%d", *(const int*)val); 943 return 0; 936 return g_strdup_printf("%d", *(const int*)val); 944 937 } 945 938 } … … 976 969 } 977 970 978 int owl_variable_enum_get_tostring(const owl_variable *v, char* buf, int bufsize, const void *val) {971 char *owl_variable_enum_get_tostring(const owl_variable *v, const void *val) { 979 972 char **enums; 980 973 int nenums, i; 974 char *tostring; 981 975 982 976 if (val == NULL) { 983 snprintf(buf, bufsize, "<null>"); 984 return -1; 977 return g_strdup("<null>"); 985 978 } 986 979 enums = g_strsplit_set(v->validsettings, ",", 0); … … 988 981 i = *(const int*)val; 989 982 if (i<0 || i>=nenums) { 990 snprintf(buf, bufsize, "<invalid:%d>",i);991 983 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]); 995 987 g_strfreev(enums); 996 return 0;988 return tostring; 997 989 } 998 990 … … 1017 1009 } 1018 1010 1019 int owl_variable_string_get_tostring_default(const owl_variable *v, char* buf, int bufsize, const void *val) {1011 char *owl_variable_string_get_tostring_default(const owl_variable *v, const void *val) { 1020 1012 if (val == NULL) { 1021 snprintf(buf, bufsize, "<null>"); 1022 return -1; 1013 return g_strdup("<null>"); 1023 1014 } 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.