1 /*
2   LibRCC - abstraction for numerical and boolean configuration options
3 
4   Copyright (C) 2005-2008 Suren A. Chilingaryan <csa@dside.dyndns.org>
5 
6   This library is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License version 2.1 or later
8   as published by the Free Software Foundation.
9 
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
13   for more details.
14 
15   You should have received a copy of the GNU Lesser General Public License
16   along with this program; if not, write to the Free Software Foundation, Inc.,
17   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #include <stdio.h>
21 #include <string.h>
22 
23 #include "rccconfig.h"
24 #include "internal.h"
25 #include "opt.h"
26 
rccGetOption(rcc_context ctx,rcc_option option)27 rcc_option_value rccGetOption(rcc_context ctx, rcc_option option) {
28     if (!ctx) {
29 	if (rcc_default_ctx) ctx = rcc_default_ctx;
30 	else return (rcc_option_value)0;
31     }
32     if ((option<0)||(option>=RCC_MAX_OPTIONS)) return 0;
33 
34     return ctx->options[option];
35 }
36 
rccOptionIsDefault(rcc_context ctx,rcc_option option)37 int rccOptionIsDefault(rcc_context ctx, rcc_option option) {
38     if (!ctx) {
39 	if (rcc_default_ctx) ctx = rcc_default_ctx;
40 	else return -1;
41     }
42     if ((option<0)||(option>=RCC_MAX_OPTIONS)) return -1;
43 
44     return ctx->default_options[option];
45 }
46 
rccSetOption(rcc_context ctx,rcc_option option,rcc_option_value value)47 int rccSetOption(rcc_context ctx, rcc_option option, rcc_option_value value) {
48     rcc_option_description *desc;
49     rcc_option_value min, max;
50 
51 
52     if (!ctx) {
53 	if (rcc_default_ctx) ctx = rcc_default_ctx;
54 	else return -1;
55     }
56     if ((option<0)||(option>=RCC_MAX_OPTIONS)) return -1;
57 
58 
59     desc = rccGetOptionDescription(option);
60     if (desc) {
61 	// DS: More checks for different range types
62 	min = desc->range.min;
63 	max = desc->range.max;
64 	if ((min)&&(min!=max)) {
65 	    if ((option<min)||(option>max)) return -1;
66 	}
67     }
68 
69     ctx->default_options[option] = 0;
70 
71     if (ctx->options[option] != value) {
72 	rccMutexLock(ctx->mutex);
73 	ctx->configure = 1;
74 	ctx->options[option]=value;
75 	rccMutexUnLock(ctx->mutex);
76     }
77 
78     return 0;
79 }
80 
rccOptionSetDefault(rcc_context ctx,rcc_option option)81 int rccOptionSetDefault(rcc_context ctx, rcc_option option) {
82     rcc_option_description *desc;
83     rcc_option_value value;
84 
85     if (!ctx) {
86 	if (rcc_default_ctx) ctx = rcc_default_ctx;
87 	else return -1;
88     }
89     if ((option<0)||(option>=RCC_MAX_OPTIONS)) return -1;
90 
91     ctx->default_options[option] = 1;
92 
93     desc = rccGetOptionDescription(option);
94     if (desc) value = desc->value;
95     else value = 0;
96 
97     if (ctx->options[option] != value) {
98 	ctx->configure = 1;
99 	ctx->options[option]=value;
100     }
101 
102     return 0;
103 }
104 
rccOptionGetType(rcc_context ctx,rcc_option option)105 rcc_option_type rccOptionGetType(rcc_context ctx, rcc_option option) {
106     rcc_option_description *desc;
107 
108     desc = rccGetOptionDescription(option);
109     if (desc) return desc->type;
110     return 0;
111 }
112 
rccOptionGetRange(rcc_context ctx,rcc_option option)113 rcc_option_range *rccOptionGetRange(rcc_context ctx, rcc_option option) {
114     rcc_option_description *desc;
115 
116     desc = rccGetOptionDescription(option);
117     if (desc) return &desc->range;
118     return 0;
119 }
120 
rccOptionDescriptionGetName(rcc_option_description * desc)121 const char *rccOptionDescriptionGetName(rcc_option_description *desc) {
122     if (desc) return desc->sn;
123     return NULL;
124 }
125 
rccOptionDescriptionGetOption(rcc_option_description * desc)126 rcc_option rccOptionDescriptionGetOption(rcc_option_description *desc) {
127     if (desc) return desc->option;
128     return (rcc_option)-1;
129 }
130 
rccOptionDescriptionGetValueName(rcc_option_description * desc,rcc_option_value value)131 const char *rccOptionDescriptionGetValueName(rcc_option_description *desc, rcc_option_value value) {
132     unsigned int i;
133 
134     if ((desc)&&(desc->vsn)) {
135 	for (i=0;desc->vsn[i];i++) {
136 	    if (i == value) return desc->vsn[i];
137 	}
138     }
139     return NULL;
140 }
141 
rccOptionDescriptionGetValueByName(rcc_option_description * desc,const char * name)142 rcc_option_value rccOptionDescriptionGetValueByName(rcc_option_description *desc, const char *name) {
143     unsigned int i;
144 
145     if ((desc)&&(desc->vsn)&&(name)) {
146 	for (i=0;desc->vsn[i];i++) {
147 	    if (!strcasecmp(desc->vsn[i], name)) return (rcc_option_value)i;
148 	}
149     }
150 
151     return (rcc_option_value)-1;
152 }
153 
154 
rccGetOptionName(rcc_option option)155 const char *rccGetOptionName(rcc_option option) {
156     rcc_option_description *desc;
157 
158     desc = rccGetOptionDescription(option);
159     return rccOptionDescriptionGetName(desc);
160 }
161 
rccGetOptionValueName(rcc_option option,rcc_option_value value)162 const char *rccGetOptionValueName(rcc_option option, rcc_option_value value) {
163     rcc_option_description *desc;
164 
165     desc = rccGetOptionDescription(option);
166     return rccOptionDescriptionGetValueName(desc, value);
167 }
168 
rccGetOptionByName(const char * name)169 rcc_option rccGetOptionByName(const char *name) {
170     rcc_option_description *desc;
171 
172     desc = rccGetOptionDescriptionByName(name);
173     return rccOptionDescriptionGetOption(desc);
174 }
175 
rccGetOptionValueByName(rcc_option option,const char * name)176 rcc_option_value rccGetOptionValueByName(rcc_option option, const char *name) {
177     rcc_option_description *desc;
178 
179     desc = rccGetOptionDescription(option);
180     return rccOptionDescriptionGetValueByName(desc, name);
181 }
182