Changeset ca54fd6 for variable.c
- Timestamp:
- Jul 3, 2011, 8:42:55 PM (13 years ago)
- 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)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
variable.c
rbc1d648 rca54fd6 665 665 { 666 666 owl_variable *old = owl_variable_get_var(vd, name); 667 if (old && o ld->type== OWL_VARIABLE_STRING) {667 if (old && owl_variable_get_type(old) == OWL_VARIABLE_STRING) { 668 668 owl_variable_update(old, summ, desc); 669 669 g_free(old->pval_default); … … 686 686 { 687 687 owl_variable *old = owl_variable_get_var(vd, name); 688 if (old && o ld->type== OWL_VARIABLE_INT) {688 if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT) { 689 689 owl_variable_update(old, summ, desc); 690 690 old->ival_default = initval; … … 708 708 { 709 709 owl_variable *old = owl_variable_get_var(vd, name); 710 if (old && o ld->type== OWL_VARIABLE_BOOL) {710 if (old && owl_variable_get_type(old) == OWL_VARIABLE_BOOL) { 711 711 owl_variable_update(old, summ, desc); 712 712 old->ival_default = initval; … … 753 753 754 754 755 const char *owl_variable_get_name(const owl_variable *v) 756 { 757 return v->name; 758 } 759 755 760 const char *owl_variable_get_description(const owl_variable *v) { 756 761 return v->description; … … 769 774 } 770 775 776 int owl_variable_get_type(const owl_variable *v) 777 { 778 return v->type; 779 } 780 771 781 /* functions for getting and setting variable values */ 772 782 773 783 /* 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; 784 int owl_variable_set_fromstring(owl_variable *v, const char *value, int msg) { 776 785 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 }783 786 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)); 785 788 return -1; 786 789 } 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 }791 790 if (0 != v->set_fromstring_fn(v, value)) { 792 if (msg) owl_function_error("Unable to set %s (must be %s)", name,793 791 if (msg) owl_function_error("Unable to set %s (must be %s)", owl_variable_get_name(v), 792 owl_variable_get_validsettings(v)); 794 793 return -1; 795 794 } 796 795 if (msg && v->get_tostring_fn) { 797 796 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); 799 798 g_free(tostring); 800 799 } … … 802 801 } 803 802 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); 803 int owl_variable_set_string(owl_variable *v, const char *newval) 804 { 805 if (v->type != OWL_VARIABLE_STRING) return -1; 810 806 return v->set_fn(v, newval); 811 807 } 812 808 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); 809 int owl_variable_set_int(owl_variable *v, int newval) 810 { 811 if (v->type != OWL_VARIABLE_INT && v->type != OWL_VARIABLE_BOOL) return -1; 819 812 return v->set_fn(v, &newval); 820 813 } 821 814 822 int owl_variable_set_bool_on(owl_var dict *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; 815 int 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 821 int 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 827 CALLER_OWN char *owl_variable_get_tostring(const owl_variable *v) 828 { 836 829 return v->get_tostring_fn(v, v->get_fn(v)); 837 830 } 838 831 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; 832 CALLER_OWN char *owl_variable_get_default_tostring(const owl_variable *v) 833 { 845 834 if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) { 846 835 return v->get_tostring_fn(v, &(v->ival_default)); … … 860 849 861 850 /* 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; 851 const void *owl_variable_get(const owl_variable *v) 852 { 866 853 return v->get_fn(v); 867 854 } 868 855 856 const 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 869 865 /* 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); 866 const 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 875 int 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); 884 882 if (!pi) return -1; 885 883 return *pi; 886 884 } 887 885 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); 886 int 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); 891 893 if (!pi) return -1; 892 894 return *pi; 893 895 } 894 896 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 } 897 void owl_variable_describe(const owl_variable *v, owl_fmtext *fm) 898 { 899 char *default_buf = owl_variable_get_default_tostring(v); 900 910 901 owl_fmtext_appendf_normal(fm, OWL_TABSTR "%-20s - %s (default: '%s')\n", 911 v->name,902 owl_variable_get_name(v), 912 903 owl_variable_get_summary(v), default_buf); 913 904 g_free(default_buf); 914 905 } 915 906 916 void owl_variable_get_help(const owl_var dict *d, const char *name, owl_fmtext *fm) {907 void owl_variable_get_help(const owl_variable *v, owl_fmtext *fm) { 917 908 char *tostring; 918 owl_variable *v;919 920 if (!name921 || (v = owl_dict_find_element(d, name)) == NULL922 || !v->get_fn) {923 owl_fmtext_append_normal(fm, "No such variable...\n");924 return;925 }926 909 927 910 owl_fmtext_append_bold(fm, "OWL VARIABLE\n\n"); 928 911 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)); 930 913 owl_fmtext_append_normal(fm, " - "); 931 owl_fmtext_append_normal(fm, v->summary);914 owl_fmtext_append_normal(fm, owl_variable_get_summary(v)); 932 915 owl_fmtext_append_normal(fm, "\n\n"); 933 916 934 917 owl_fmtext_append_normal(fm, "Current: "); 935 tostring = owl_variable_get_tostring( d, name);918 tostring = owl_variable_get_tostring(v); 936 919 owl_fmtext_append_normal(fm, tostring); 937 920 g_free(tostring); … … 939 922 940 923 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); 946 925 owl_fmtext_append_normal(fm, "Default: "); 947 926 owl_fmtext_append_normal(fm, tostring);
Note: See TracChangeset
for help on using the changeset viewer.