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

Last change on this file since 7746 was 7746, checked in by Mathieu Morlighem, 14 years ago

Partially finished Options

File size: 6.7 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 FetchData(&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::GetOption{{{1*/
220Option* Options::GetOption(const char* name){
221
222 vector<Object*>::iterator object;
223 Option* option=NULL;
224
225 /*Go through options and find option: */
226 for ( object=objects.begin() ; object < objects.end(); object++ ){
227
228 option=(Option*)(*object);
229 if (!strncmp(name,option->name,strlen(option->name))){
230
231 /*OK, now do we have a complete name? If not, it is a cell or a structure, we need to go further*/
232 if(!strcmp(name,option->name)){
233 return option;
234 }
235 else{
236 /*If the object is a Cell, recursive call to its options*/
237 if(option->Enum()==OptionCellEnum){
238 return ((OptionCell*)option)->values->GetOption(name);
239 }
240 /*If the object is a Struct loop over its size and recursive call*/
241 else if(option->Enum()==OptionStructEnum){
242 for(int i=0;i<option->numel;i++){
243 _assert_(((OptionStruct*)option)->values[i]);
244 return ((OptionStruct*)option)->values[i]->GetOption(name);
245 }
246 }
247 /*Else: not supported*/
248 else{
249 _error_("Cannot recover field \"%s\" for an option of type %s",name,EnumToString(option->Enum()));
250 }
251 }
252 }
253 }
254
255 /*Option not found return NULL pointer*/
256 return NULL;
257}
258/*}}}*/
Note: See TracBrowser for help on using the repository browser.