summaryrefslogtreecommitdiffstats
path: root/src/opt.c
blob: 4d10ea56af44ae9e9c81b7017f1de4047964c624 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
  LibRCC - abstraction for numerical and boolean configuration options

  Copyright (C) 2005-2008 Suren A. Chilingaryan <csa@dside.dyndns.org>

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License version 2.1 or later
  as published by the Free Software Foundation.

  This library is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License 
  for more details.

  You should have received a copy of the GNU Lesser General Public License 
  along with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <stdio.h>
#include <string.h>

#include "rccconfig.h"
#include "internal.h"
#include "opt.h"

rcc_option_value rccGetOption(rcc_context ctx, rcc_option option) {
    if (!ctx) {
	if (rcc_default_ctx) ctx = rcc_default_ctx;
	else return (rcc_option_value)0;
    }
    if ((option<0)||(option>=RCC_MAX_OPTIONS)) return 0;
    
    return ctx->options[option];
}

int rccOptionIsDefault(rcc_context ctx, rcc_option option) {
    if (!ctx) {
	if (rcc_default_ctx) ctx = rcc_default_ctx;
	else return -1;
    }
    if ((option<0)||(option>=RCC_MAX_OPTIONS)) return -1;

    return ctx->default_options[option];
}

int rccSetOption(rcc_context ctx, rcc_option option, rcc_option_value value) {
    rcc_option_description *desc;
    rcc_option_value min, max;
    
    
    if (!ctx) {
	if (rcc_default_ctx) ctx = rcc_default_ctx;
	else return -1;
    }
    if ((option<0)||(option>=RCC_MAX_OPTIONS)) return -1;
	
    
    desc = rccGetOptionDescription(option);
    if (desc) {
	// DS: More checks for different range types
	min = desc->range.min;
	max = desc->range.max;
	if ((min)&&(min!=max)) {
	    if ((option<min)||(option>max)) return -1;
	}
    }
    
    ctx->default_options[option] = 0;

    if (ctx->options[option] != value) {
	rccMutexLock(ctx->mutex);
	ctx->configure = 1;
	ctx->options[option]=value;
	rccMutexUnLock(ctx->mutex);
    }
    
    return 0;
}

int rccOptionSetDefault(rcc_context ctx, rcc_option option) {
    rcc_option_description *desc;
    rcc_option_value value;

    if (!ctx) {
	if (rcc_default_ctx) ctx = rcc_default_ctx;
	else return -1;
    }
    if ((option<0)||(option>=RCC_MAX_OPTIONS)) return -1;

    ctx->default_options[option] = 1;

    desc = rccGetOptionDescription(option);
    if (desc) value = desc->value;
    else value = 0;

    if (ctx->options[option] != value) {
	ctx->configure = 1;
	ctx->options[option]=value;
    }
    
    return 0;
}

rcc_option_type rccOptionGetType(rcc_context ctx, rcc_option option) {
    rcc_option_description *desc;

    desc = rccGetOptionDescription(option);
    if (desc) return desc->type;
    return 0;
}

rcc_option_range *rccOptionGetRange(rcc_context ctx, rcc_option option) {
    rcc_option_description *desc;

    desc = rccGetOptionDescription(option);
    if (desc) return &desc->range;
    return 0;
}

const char *rccOptionDescriptionGetName(rcc_option_description *desc) {
    if (desc) return desc->sn;
    return NULL;
}

rcc_option rccOptionDescriptionGetOption(rcc_option_description *desc) {
    if (desc) return desc->option;
    return (rcc_option)-1;
}

const char *rccOptionDescriptionGetValueName(rcc_option_description *desc, rcc_option_value value) {
    unsigned int i;
    
    if ((desc)&&(desc->vsn)) {
	for (i=0;desc->vsn[i];i++) {
	    if (i == value) return desc->vsn[i];
	}
    }
    return NULL;
}

rcc_option_value rccOptionDescriptionGetValueByName(rcc_option_description *desc, const char *name) {
    unsigned int i;

    if ((desc)&&(desc->vsn)&&(name)) {
	for (i=0;desc->vsn[i];i++) {
	    if (!strcasecmp(desc->vsn[i], name)) return (rcc_option_value)i;
	}
    }

    return (rcc_option_value)-1;
}


const char *rccGetOptionName(rcc_option option) {
    rcc_option_description *desc;
    
    desc = rccGetOptionDescription(option);
    return rccOptionDescriptionGetName(desc);
}

const char *rccGetOptionValueName(rcc_option option, rcc_option_value value) {
    rcc_option_description *desc;
    
    desc = rccGetOptionDescription(option);
    return rccOptionDescriptionGetValueName(desc, value);
}

rcc_option rccGetOptionByName(const char *name) {
    rcc_option_description *desc;
    
    desc = rccGetOptionDescriptionByName(name);
    return rccOptionDescriptionGetOption(desc);
}

rcc_option_value rccGetOptionValueByName(rcc_option option, const char *name) {
    rcc_option_description *desc;
    
    desc = rccGetOptionDescription(option);
    return rccOptionDescriptionGetValueByName(desc, name);
}