Changeset 0d935a1 for variable.c
- Timestamp:
- Jun 27, 2011, 4:28:51 AM (14 years ago)
- Children:
- b75a8ac
- Parents:
- f4e857f
- git-author:
- Jason Gross <jgross@mit.edu> (06/27/11 01:14:13)
- git-committer:
- Jason Gross <jgross@mit.edu> (06/27/11 04:28:51)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
variable.c
rf4e857f r0d935a1 717 717 718 718 719 const char *owl_variable_get_name(const owl_variable *v) 720 { 721 return v->name; 722 } 723 719 724 const char *owl_variable_get_description(const owl_variable *v) { 720 725 return v->description; … … 732 737 733 738 /* returns 0 on success, prints a status msg if msg is true */ 734 int owl_variable_set_fromstring(owl_vardict *d, const char *name, const char *value, int msg, int requirebool) { 735 owl_variable *v; 739 int owl_variable_set_fromstring(owl_variable *v, const char *value, int msg) { 736 740 char *tostring; 737 if (!name) return(-1);738 v = owl_dict_find_element(d, name);739 if (v == NULL) {740 if (msg) owl_function_error("Unknown variable %s", name);741 return -1;742 }743 741 if (!v->set_fromstring_fn) { 744 if (msg) owl_function_error("Variable %s is read-only", name);742 if (msg) owl_function_error("Variable %s is read-only", owl_variable_get_name(v)); 745 743 return -1; 746 744 } 747 if (requirebool && v->type!=OWL_VARIABLE_BOOL) {748 if (msg) owl_function_error("Variable %s is not a boolean", name);749 return -1;750 }751 745 if (0 != v->set_fromstring_fn(v, value)) { 752 if (msg) owl_function_error("Unable to set %s (must be %s)", name,753 746 if (msg) owl_function_error("Unable to set %s (must be %s)", owl_variable_get_name(v), 747 owl_variable_get_validsettings(v)); 754 748 return -1; 755 749 } 756 750 if (msg) { 757 751 tostring = v->get_tostring_fn(v, v->get_fn(v)); 758 owl_function_makemsg("%s = '%s'", name, tostring); 752 if (tostring) 753 owl_function_makemsg("%s = '%s'", owl_variable_get_name(v), tostring); 754 else 755 owl_function_makemsg("%s = <null>", owl_variable_get_name(v)); 759 756 g_free(tostring); 760 757 } … … 762 759 } 763 760 764 int owl_variable_set_string(owl_vardict *d, const char *name, const char *newval) { 765 owl_variable *v; 766 if (!name) return(-1); 767 v = owl_dict_find_element(d, name); 768 if (v == NULL || !v->set_fn) return(-1); 769 if (v->type!=OWL_VARIABLE_STRING) return(-1); 761 int owl_variable_set_string(owl_variable *v, const char *newval) 762 { 763 if (v->type != OWL_VARIABLE_STRING) return -1; 770 764 return v->set_fn(v, newval); 771 765 } 772 766 773 int owl_variable_set_int(owl_vardict *d, const char *name, int newval) { 774 owl_variable *v; 775 if (!name) return(-1); 776 v = owl_dict_find_element(d, name); 777 if (v == NULL || !v->set_fn) return(-1); 778 if (v->type!=OWL_VARIABLE_INT && v->type!=OWL_VARIABLE_BOOL) return(-1); 767 int owl_variable_set_int(owl_variable *v, int newval) 768 { 769 if (v->type != OWL_VARIABLE_INT && v->type != OWL_VARIABLE_BOOL) return -1; 779 770 return v->set_fn(v, &newval); 780 771 } 781 772 782 int owl_variable_set_bool_on(owl_var dict *d, const char *name) {783 return owl_variable_set_int(d,name,1); 784 } 785 786 int owl_variable_set_bool_off(owl_vardict *d, const char *name) { 787 return owl_variable_set_int(d,name,0); 788 } 789 790 CALLER_OWN char *owl_variable_get_tostring(const owl_vardict *d, const char *name) 791 { 792 owl_variable *v; 793 if (!name) return NULL; 794 v = owl_dict_find_element(d, name); 795 if (v == NULL) return NULL; 773 int owl_variable_set_bool_on(owl_variable *v) 774 { 775 if (v->type != OWL_VARIABLE_BOOL) return -1; 776 return owl_variable_set_int(v, true); 777 } 778 779 int owl_variable_set_bool_off(owl_variable *v) 780 { 781 if (v->type != OWL_VARIABLE_BOOL) return -1; 782 return owl_variable_set_int(v, false); 783 } 784 785 CALLER_OWN char *owl_variable_get_tostring(const owl_variable *v) 786 { 796 787 return v->get_tostring_fn(v, v->get_fn(v)); 797 788 } 798 789 799 CALLER_OWN char *owl_variable_get_default_tostring(const owl_vardict *d, const char *name) 800 { 801 owl_variable *v; 802 if (!name) return NULL; 803 v = owl_dict_find_element(d, name); 804 if (v == NULL) return NULL; 790 CALLER_OWN char *owl_variable_get_default_tostring(const owl_variable *v) 791 { 805 792 if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) { 806 793 return v->get_tostring_fn(v, &(v->ival_default)); … … 812 799 owl_variable *owl_variable_get_var(const owl_vardict *d, const char *name) 813 800 { 814 owl_variable *v; 815 if (!name) return(NULL); 816 v = owl_dict_find_element(d, name); 817 if (v == NULL) return NULL; 818 return v; 801 if (name == NULL) return NULL; 802 return owl_dict_find_element(d, name); 819 803 } 820 804 821 805 /* returns a reference */ 822 const void *owl_variable_get(const owl_vardict *d, const char *name) 823 { 824 const owl_variable *v = owl_variable_get_var(d, name); 825 if(v == NULL) return NULL; 806 const void *owl_variable_get(const owl_variable *v) 807 { 826 808 return v->get_fn(v); 827 809 } 828 810 829 811 /* returns a reference */ 830 const char *owl_variable_get_string(const owl_vardict *d, const char *name) { 812 const char *owl_variable_get_string(const owl_variable *v) 813 { 831 814 /* XXX TODO: Put type checking back in */ 832 return owl_variable_get( d, name);815 return owl_variable_get(v); 833 816 } 834 817 835 818 /* returns a reference */ 836 const void *owl_variable_get_other(const owl_vardict *d, const char *name) { 819 const void *owl_variable_get_other(const owl_variable *v) 820 { 837 821 /* XXX TODO: Put type checking back in */ 838 return owl_variable_get(d, name); 839 } 840 841 int owl_variable_get_int(const owl_vardict *d, const char *name) { 822 return owl_variable_get(v); 823 } 824 825 int owl_variable_get_int(const owl_variable *v) 826 { 842 827 /* XXX TODO: Put type checking back in */ 843 const int *pi = owl_variable_get( d, name);828 const int *pi = owl_variable_get(v); 844 829 if (!pi) return(-1); 845 830 return(*pi); 846 831 } 847 832 848 int owl_variable_get_bool(const owl_vardict *d, const char *name) { 833 int owl_variable_get_bool(const owl_variable *v) 834 { 849 835 /* XXX TODO: Put type checking back in */ 850 const int *pi = owl_variable_get( d, name);836 const int *pi = owl_variable_get(v); 851 837 if (!pi) return(-1); 852 838 return(*pi); 853 839 } 854 840 855 void owl_variable_describe(const owl_vardict *d, const char *name, owl_fmtext *fm) { 841 void owl_variable_describe(const owl_variable *v, owl_fmtext *fm) 842 { 843 char *tostring = owl_variable_get_default_tostring(v); 856 844 char *default_buf; 857 owl_variable *v; 858 859 if (!name 860 || (v = owl_dict_find_element(d, name)) == NULL) { 861 owl_fmtext_appendf_normal(fm, " No such variable '%s'\n", name); 862 return; 863 } 864 if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) { 865 default_buf = v->get_tostring_fn(v, &(v->ival_default)); 866 } else { 867 default_buf = v->get_tostring_fn(v, v->pval_default); 868 } 869 owl_fmtext_appendf_normal(fm, OWL_TABSTR "%-20s - %s (default: '%s')\n", 870 v->name, 871 owl_variable_get_summary(v), default_buf); 845 846 if (tostring) 847 default_buf = g_strdup_printf("'%s'", tostring); 848 else 849 default_buf = g_strdup("<null>"); 850 owl_fmtext_appendf_normal(fm, OWL_TABSTR "%-20s - %s (default: %s)\n", 851 owl_variable_get_name(v), 852 owl_variable_get_summary(v), default_buf); 872 853 g_free(default_buf); 873 } 874 875 void owl_variable_get_help(const owl_vardict *d, const char *name, owl_fmtext *fm) { 854 g_free(tostring); 855 } 856 857 void owl_variable_get_help(const owl_variable *v, owl_fmtext *fm) { 876 858 char *tostring; 877 owl_variable *v;878 879 if (!name880 || (v = owl_dict_find_element(d, name)) == NULL) {881 owl_fmtext_append_normal(fm, "No such variable...\n");882 return;883 }884 859 885 860 owl_fmtext_append_bold(fm, "OWL VARIABLE\n\n"); 886 861 owl_fmtext_append_normal(fm, OWL_TABSTR); 887 owl_fmtext_append_normal(fm, name);862 owl_fmtext_append_normal(fm, v->name); 888 863 owl_fmtext_append_normal(fm, " - "); 889 864 owl_fmtext_append_normal(fm, v->summary); … … 891 866 892 867 owl_fmtext_append_normal(fm, "Current: "); 893 tostring = owl_variable_get_tostring(d, name); 894 owl_fmtext_append_normal(fm, tostring); 868 tostring = owl_variable_get_tostring(v); 869 if (tostring) 870 owl_fmtext_append_normal(fm, tostring); 871 else 872 owl_fmtext_append_normal(fm, "<null>"); 895 873 g_free(tostring); 896 874 owl_fmtext_append_normal(fm, "\n\n"); 897 875 898 876 899 if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) { 900 tostring = v->get_tostring_fn(v, &(v->ival_default)); 901 } else { 902 tostring = v->get_tostring_fn(v, v->pval_default); 903 } 877 tostring = owl_variable_get_default_tostring(v); 904 878 owl_fmtext_append_normal(fm, "Default: "); 905 owl_fmtext_append_normal(fm, tostring); 879 if (tostring) 880 owl_fmtext_append_normal(fm, tostring); 881 else 882 owl_fmtext_append_normal(fm, "<null>"); 906 883 owl_fmtext_append_normal(fm, "\n\n"); 907 884
Note: See TracChangeset
for help on using the changeset viewer.