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

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

Quality control changes:

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