source: issm/trunk/src/c/Container/Options.cpp@ 9761

Last change on this file since 9761 was 9761, checked in by Eric.Larour, 14 years ago

Added --with-control macro to configure script.
Can now strip out all control related routines from the parallel issm compilation.

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