1 | /*
|
---|
2 | * \file Options.c
|
---|
3 | * \brief: implementation of the Options class, derived from DataSet class
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*Headers: {{{1*/
|
---|
7 | #ifdef HAVE_CONFIG_H
|
---|
8 | #include <config.h>
|
---|
9 | #else
|
---|
10 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
---|
11 | #endif
|
---|
12 |
|
---|
13 | #include <vector>
|
---|
14 | #include <algorithm>
|
---|
15 | #include <cstring>
|
---|
16 |
|
---|
17 | #include "./DataSet.h"
|
---|
18 | #include "../shared/shared.h"
|
---|
19 | #include "../io/io.h"
|
---|
20 | #include "../include/include.h"
|
---|
21 | #include "../shared/shared.h"
|
---|
22 | #include "../EnumDefinitions/EnumDefinitions.h"
|
---|
23 | #include "../io/io.h"
|
---|
24 | /*}}}*/
|
---|
25 |
|
---|
26 | /*Object constructors and destructor*/
|
---|
27 | /*FUNCTION Options::Options(){{{1*/
|
---|
28 | Options::Options(){
|
---|
29 | return;
|
---|
30 | }
|
---|
31 | /*}}}*/
|
---|
32 | /*FUNCTION Options::~Options(){{{1*/
|
---|
33 | Options::~Options(){
|
---|
34 | return;
|
---|
35 | }
|
---|
36 | /*}}}*/
|
---|
37 |
|
---|
38 | /*Object management*/
|
---|
39 | /*FUNCTION Options::AddOption{{{1*/
|
---|
40 | int Options::AddOption(Option* in_option){
|
---|
41 |
|
---|
42 | vector<Object*>::iterator object;
|
---|
43 | Option* option=NULL;
|
---|
44 |
|
---|
45 | /*In debugging mode, check that the option is not a NULL pointer*/
|
---|
46 | _assert_(in_option);
|
---|
47 |
|
---|
48 | /*Also, check the option name*/
|
---|
49 | if(!in_option->name) _error_("input option has an empty name");
|
---|
50 | if(strchr(in_option->name,'.')) _error_("Option \"%s\" has a protected character \".\"",in_option->name);
|
---|
51 | if(strchr(in_option->name,'[')) _error_("Option \"%s\" has a protected character \"[\"",in_option->name);
|
---|
52 | if(strchr(in_option->name,']')) _error_("Option \"%s\" has a protected character \"]\"",in_option->name);
|
---|
53 |
|
---|
54 | /*Finally, check that no option of the same name already exists in the dataset*/
|
---|
55 | for(object=objects.begin();object<objects.end();object++){
|
---|
56 |
|
---|
57 | option=(Option*)(*object);
|
---|
58 | if (!strcmp(option->name,in_option->name)){
|
---|
59 | _error_("Options \"%s\" found multiple times",in_option->name);
|
---|
60 | break;
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | /*OK, all checks went well, add option to dataset*/
|
---|
65 | this->AddObject(in_option);
|
---|
66 |
|
---|
67 | return 1;
|
---|
68 | }
|
---|
69 | /*}}}*/
|
---|
70 | /*FUNCTION Options::Get(int* pvalue, char* name){{{1*/
|
---|
71 | void Options::Get(int* pvalue,const char* name){
|
---|
72 |
|
---|
73 | vector<Object*>::iterator object;
|
---|
74 | Option* option=NULL;
|
---|
75 |
|
---|
76 | /*Get option*/
|
---|
77 | option=GetOption(name);
|
---|
78 |
|
---|
79 | /*If the pointer is not NULL, the option has been found*/
|
---|
80 | if(option){
|
---|
81 | option->Get(pvalue);
|
---|
82 | }
|
---|
83 | /*Else, the Option does not exist, no default provided*/
|
---|
84 | else{
|
---|
85 | _error_("option of name \"%s\" not found, and no default value has been provided",name);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | /*}}}*/
|
---|
89 | /*FUNCTION Options::Get(int* pvalue, char* name,int default_value){{{1*/
|
---|
90 | void Options::Get(int* pvalue,const char* name,int default_value){
|
---|
91 |
|
---|
92 | vector<Object*>::iterator object;
|
---|
93 | Option* option=NULL;
|
---|
94 |
|
---|
95 | /*Get option*/
|
---|
96 | option=GetOption(name);
|
---|
97 |
|
---|
98 | /*If the pointer is not NULL, the option has been found*/
|
---|
99 | if(option){
|
---|
100 | option->Get(pvalue);
|
---|
101 | }
|
---|
102 | /*Else, the Option does not exist, a default is provided here*/
|
---|
103 | else{
|
---|
104 | *pvalue=default_value;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | /*}}}*/
|
---|
108 | /*FUNCTION Options::Get(double* pvalue, char* name){{{1*/
|
---|
109 | void Options::Get(double* pvalue,const char* name){
|
---|
110 |
|
---|
111 | vector<Object*>::iterator object;
|
---|
112 | Option* option=NULL;
|
---|
113 |
|
---|
114 | /*Get option*/
|
---|
115 | option=GetOption(name);
|
---|
116 |
|
---|
117 | /*If the pointer is not NULL, the option has been found*/
|
---|
118 | if(option){
|
---|
119 | option->Get(pvalue);
|
---|
120 | }
|
---|
121 | /*Else, the Option does not exist, no default provided*/
|
---|
122 | else{
|
---|
123 | _error_("option of name \"%s\" not found, and no default value has been provided",name);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | /*}}}*/
|
---|
127 | /*FUNCTION Options::Get(double* pvalue, char* name,double default_value){{{1*/
|
---|
128 | void Options::Get(double* pvalue,const char* name,double default_value){
|
---|
129 |
|
---|
130 | vector<Object*>::iterator object;
|
---|
131 | Option* option=NULL;
|
---|
132 |
|
---|
133 | /*Get option*/
|
---|
134 | option=GetOption(name);
|
---|
135 |
|
---|
136 | /*If the pointer is not NULL, the option has been found*/
|
---|
137 | if(option){
|
---|
138 | option->Get(pvalue);
|
---|
139 | }
|
---|
140 | /*Else, the Option does not exist, a default is provided here*/
|
---|
141 | else{
|
---|
142 | *pvalue=default_value;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | /*}}}*/
|
---|
146 | /*FUNCTION Options::Get(bool* pvalue, char* name){{{1*/
|
---|
147 | void Options::Get(bool* pvalue,const char* name){
|
---|
148 |
|
---|
149 | vector<Object*>::iterator object;
|
---|
150 | Option* option=NULL;
|
---|
151 |
|
---|
152 | /*Get option*/
|
---|
153 | option=GetOption(name);
|
---|
154 |
|
---|
155 | /*If the pointer is not NULL, the option has been found*/
|
---|
156 | if(option){
|
---|
157 | option->Get(pvalue);
|
---|
158 | }
|
---|
159 | /*Else, the Option does not exist, no default provided*/
|
---|
160 | else{
|
---|
161 | _error_("option of name \"%s\" not found, and no default value has been provided",name);
|
---|
162 | }
|
---|
163 | }
|
---|
164 | /*}}}*/
|
---|
165 | /*FUNCTION Options::Get(bool* pvalue, char* name,bool default_value){{{1*/
|
---|
166 | void Options::Get(bool* pvalue,const char* name,bool default_value){
|
---|
167 |
|
---|
168 | vector<Object*>::iterator object;
|
---|
169 | Option* option=NULL;
|
---|
170 |
|
---|
171 | /*Get option*/
|
---|
172 | option=GetOption(name);
|
---|
173 |
|
---|
174 | /*If the pointer is not NULL, the option has been found*/
|
---|
175 | if(option){
|
---|
176 | option->Get(pvalue);
|
---|
177 | }
|
---|
178 | /*Else, the Option does not exist, a default is provided here*/
|
---|
179 | else{
|
---|
180 | *pvalue=default_value;
|
---|
181 | }
|
---|
182 | }
|
---|
183 | /*}}}*/
|
---|
184 | /*FUNCTION Options::Get(char** pvalue, char* name){{{1*/
|
---|
185 | void Options::Get(char** pvalue,const char* name){
|
---|
186 |
|
---|
187 | vector<Object*>::iterator object;
|
---|
188 | Option* option=NULL;
|
---|
189 | char* outstring=NULL;
|
---|
190 | int stringsize;
|
---|
191 |
|
---|
192 | /*Get option*/
|
---|
193 | option=GetOption(name);
|
---|
194 |
|
---|
195 | /*If the pointer is not NULL, the option has been found*/
|
---|
196 | if(option){
|
---|
197 | option->Get(pvalue);
|
---|
198 | }
|
---|
199 | /*Else, the Option does not exist, no default provided*/
|
---|
200 | else{
|
---|
201 | _error_("option of name \"%s\" not found, and no default value has been provided",name);
|
---|
202 | }
|
---|
203 |
|
---|
204 | }
|
---|
205 | /*}}}*/
|
---|
206 | /*FUNCTION Options::Get(char** pvalue, char* name,char* default_value){{{1*/
|
---|
207 | void Options::Get(char** pvalue,const char* name,const char* default_value){
|
---|
208 |
|
---|
209 | vector<Object*>::iterator object;
|
---|
210 | Option* option=NULL;
|
---|
211 | char* outstring=NULL;
|
---|
212 | int stringsize;
|
---|
213 |
|
---|
214 | /*Get option*/
|
---|
215 | option=GetOption(name);
|
---|
216 |
|
---|
217 | /*If the pointer is not NULL, the option has been found*/
|
---|
218 | if(option){
|
---|
219 | option->Get(pvalue);
|
---|
220 | }
|
---|
221 | /*Else, the Option does not exist, a default is provided here*/
|
---|
222 | else{
|
---|
223 | stringsize=strlen(default_value)+1;
|
---|
224 | outstring=(char*)xmalloc(stringsize*sizeof(char));
|
---|
225 | memcpy(outstring,default_value,stringsize*sizeof(char));
|
---|
226 | *pvalue=outstring;
|
---|
227 | }
|
---|
228 |
|
---|
229 | }
|
---|
230 | /*}}}*/
|
---|
231 | /*FUNCTION Options::Get(char*** ppvalue,int* numel,char* name){{{1*/
|
---|
232 | void Options::Get(char*** ppvalue,int* numel,const char* name){
|
---|
233 |
|
---|
234 | vector<Object*>::iterator object;
|
---|
235 | Option* option=NULL;
|
---|
236 | Option* option2=NULL;
|
---|
237 | Options* options=NULL;
|
---|
238 | int i;
|
---|
239 |
|
---|
240 | /*Get option*/
|
---|
241 | option=GetOption(name);
|
---|
242 |
|
---|
243 | /*If the pointer is not NULL, the option has been found*/
|
---|
244 | if(option){
|
---|
245 | /*If the object is a Cell, copy the strings from its options dataset*/
|
---|
246 | if(option->ObjectEnum()==OptionCellEnum){
|
---|
247 | if (option->NumEl()) {
|
---|
248 | *ppvalue=(char **) xmalloc(option->NumEl()*sizeof(char *));
|
---|
249 | if (numel) *numel=option->NumEl();
|
---|
250 | option->Get(&options);
|
---|
251 | for (i=0; i<option->NumEl(); i++) {
|
---|
252 | option2=((Option *)options->GetObjectByOffset(i));
|
---|
253 | if(option2->ObjectEnum()==OptionCharEnum)
|
---|
254 | option2->Get(&((*ppvalue)[i]));
|
---|
255 | else
|
---|
256 | ((*ppvalue)[i])=NULL;
|
---|
257 | }
|
---|
258 | }
|
---|
259 | }
|
---|
260 | /*If the object is a Char, copy the strings from its concatenation*/
|
---|
261 | else if(option->ObjectEnum()==OptionCharEnum){
|
---|
262 | option->Get(ppvalue,numel);
|
---|
263 | }
|
---|
264 | /*Else: not supported*/
|
---|
265 | else{
|
---|
266 | _error_("Cannot recover field \"%s\" for an option of type %s",name,EnumToStringx(option->ObjectEnum()));
|
---|
267 | }
|
---|
268 | }
|
---|
269 | /*Else, the Option does not exist, no default provided*/
|
---|
270 | else{
|
---|
271 | *ppvalue=NULL;
|
---|
272 | if (numel) *numel=0;
|
---|
273 | }
|
---|
274 |
|
---|
275 | }
|
---|
276 | /*}}}*/
|
---|
277 | /*FUNCTION Options::Get(double** pvalue,int* numel,const char* name){{{1*/
|
---|
278 | void Options::Get(double** pvalue,int* numel,const char* name){
|
---|
279 |
|
---|
280 | vector<Object*>::iterator object;
|
---|
281 | Option* option=NULL;
|
---|
282 |
|
---|
283 | /*Get option*/
|
---|
284 | option=GetOption(name);
|
---|
285 |
|
---|
286 | /*If the pointer is not NULL, the option has been found*/
|
---|
287 | if(option){
|
---|
288 | option->Get(pvalue,numel);
|
---|
289 | }
|
---|
290 | /*Else, the Option does not exist, no default provided*/
|
---|
291 | else{
|
---|
292 | _error_("option of name \"%s\" not found, and no default value has been provided",name);
|
---|
293 | }
|
---|
294 | }
|
---|
295 | /*}}}*/
|
---|
296 | /*FUNCTION Options::GetOption{{{1*/
|
---|
297 | Option* Options::GetOption(const char* name){
|
---|
298 |
|
---|
299 | vector<Object*>::iterator object;
|
---|
300 | Option* option=NULL;
|
---|
301 |
|
---|
302 | /*Go through options and find option: */
|
---|
303 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
---|
304 |
|
---|
305 | option=(Option*)(*object);
|
---|
306 | if (!strncmp(name,option->name,strlen(option->name))){
|
---|
307 |
|
---|
308 | /*OK, now do we have a complete name? If not, it is a cell or a structure, we need to go further*/
|
---|
309 | if(!strcmp(name,option->name)){
|
---|
310 | return option;
|
---|
311 | }
|
---|
312 | else{
|
---|
313 | /*If the object is a Cell, recursive call to its options*/
|
---|
314 | if(option->ObjectEnum()==OptionCellEnum){
|
---|
315 | return ((OptionCell*)option)->values->GetOption(name);
|
---|
316 | }
|
---|
317 | /*If the object is a Struct loop over its size and recursive call*/
|
---|
318 | else if(option->ObjectEnum()==OptionStructEnum){
|
---|
319 | for(int i=0;i<option->numel;i++){
|
---|
320 | _assert_(((OptionStruct*)option)->values[i]);
|
---|
321 | return ((OptionStruct*)option)->values[i]->GetOption(name);
|
---|
322 | }
|
---|
323 | }
|
---|
324 | /*Else: not supported*/
|
---|
325 | else{
|
---|
326 | _error_("Cannot recover field \"%s\" for an option of type %s",name,EnumToStringx(option->ObjectEnum()));
|
---|
327 | }
|
---|
328 | }
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | /*Option not found return NULL pointer*/
|
---|
333 | return NULL;
|
---|
334 | }
|
---|
335 | /*}}}*/
|
---|