1 | /*\file OptionParse.c
|
---|
2 | *\brief: functions to parse the mex options.
|
---|
3 | */
|
---|
4 | #ifdef HAVE_CONFIG_H
|
---|
5 | #include <config.h>
|
---|
6 | #else
|
---|
7 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
---|
8 | #endif
|
---|
9 |
|
---|
10 | #include <cstring>
|
---|
11 | #include "./matlabio.h"
|
---|
12 |
|
---|
13 | GenericOption<double>* OptionDoubleParse( char* name, const mxArray* prhs[]){ /*{{{*/
|
---|
14 |
|
---|
15 | GenericOption<double> *odouble = NULL;
|
---|
16 |
|
---|
17 | /*check and parse the name */
|
---|
18 | odouble=new GenericOption<double>();
|
---|
19 | odouble->name =xNew<char>(strlen(name)+1);
|
---|
20 | memcpy(odouble->name,name,(strlen(name)+1)*sizeof(char));
|
---|
21 | FetchData(&odouble->value,prhs[0]);
|
---|
22 | odouble->numel=1;
|
---|
23 | odouble->ndims=1;
|
---|
24 | odouble->size=NULL;
|
---|
25 |
|
---|
26 | return(odouble);
|
---|
27 | }/*}}}*/
|
---|
28 | GenericOption<double*>* OptionDoubleArrayParse( char* name, const mxArray* prhs[]){ /*{{{*/
|
---|
29 |
|
---|
30 | GenericOption<double*> *odouble = NULL;
|
---|
31 |
|
---|
32 | /*check and parse the name */
|
---|
33 | odouble=new GenericOption<double*>();
|
---|
34 | odouble->name =xNew<char>(strlen(name)+1);
|
---|
35 | memcpy(odouble->name,name,(strlen(name)+1)*sizeof(char));
|
---|
36 |
|
---|
37 | /*check and parse the value */
|
---|
38 | if (!mxIsClass(prhs[0],"double")){
|
---|
39 | _error_("Value of option \"" << odouble->name << "\" must be class \"double\", not class \"" << mxGetClassName(prhs[0]) <<"\".");
|
---|
40 | }
|
---|
41 | FetchData(&odouble->value,&odouble->numel,&odouble->ndims,&odouble->size,prhs[0]);
|
---|
42 |
|
---|
43 | return(odouble);
|
---|
44 | }/*}}}*/
|
---|
45 | GenericOption<bool*>* OptionLogicalParse( char* name, const mxArray* prhs[]){ /*{{{*/
|
---|
46 |
|
---|
47 | GenericOption<bool*> *ological = NULL;
|
---|
48 |
|
---|
49 | /*check and parse the name */
|
---|
50 | ological=new GenericOption<bool*>();
|
---|
51 | ological->name =xNew<char>(strlen(name)+1);
|
---|
52 | memcpy(ological->name,name,(strlen(name)+1)*sizeof(char));
|
---|
53 |
|
---|
54 | /*check and parse the value */
|
---|
55 | if (!mxIsClass(prhs[0],"logical")){
|
---|
56 | _error_("Value of option \"" << ological->name << "\" must be class \"logical\", not class \"" << mxGetClassName(prhs[0]) <<"\".");
|
---|
57 | }
|
---|
58 | FetchData(&ological->value,&ological->numel,&ological->ndims,&ological->size,prhs[0]);
|
---|
59 |
|
---|
60 | return(ological);
|
---|
61 | }/*}}}*/
|
---|
62 | GenericOption<char*>* OptionCharParse( char* name, const mxArray* prhs[]){ /*{{{*/
|
---|
63 |
|
---|
64 | GenericOption<char*> *ochar = NULL;
|
---|
65 |
|
---|
66 | /*check and parse the name */
|
---|
67 | ochar=new GenericOption<char*>();
|
---|
68 | ochar->name =xNew<char>(strlen(name)+1);
|
---|
69 | memcpy(ochar->name,name,(strlen(name)+1)*sizeof(char));
|
---|
70 |
|
---|
71 | /*check and parse the value */
|
---|
72 | if (!mxIsClass(prhs[0],"char")){
|
---|
73 | _error_("Value of option \"" << ochar->name << "\" must be class \"char\", not class \"" << mxGetClassName(prhs[0]) <<"\".");
|
---|
74 | }
|
---|
75 | FetchData(&ochar->value,&ochar->numel,&ochar->ndims,&ochar->size,prhs[0]);
|
---|
76 |
|
---|
77 | return(ochar);
|
---|
78 | }/*}}}*/
|
---|
79 | GenericOption<Options**>* OptionStructParse( char* name, const mxArray* prhs[]){ /*{{{*/
|
---|
80 |
|
---|
81 | int i;
|
---|
82 | char namei[161];
|
---|
83 | Option* option = NULL;
|
---|
84 | GenericOption<Options**> *ostruct = NULL;
|
---|
85 | const mwSize *ipt = NULL;
|
---|
86 | const mxArray *structi;
|
---|
87 | mwIndex sindex;
|
---|
88 |
|
---|
89 | /*check and parse the name */
|
---|
90 | ostruct=new GenericOption<Options**>();
|
---|
91 | ostruct->name =xNew<char>(strlen(name)+1);
|
---|
92 | memcpy(ostruct->name,name,(strlen(name)+1)*sizeof(char));
|
---|
93 |
|
---|
94 | /*check and parse the value */
|
---|
95 | if (!mxIsClass(prhs[0],"struct")){
|
---|
96 | _error_("Value of option \"" << ostruct->name << "\" must be class \"struct\", not class \"" << mxGetClassName(prhs[0]) <<"\".");
|
---|
97 | }
|
---|
98 | ostruct->numel=mxGetNumberOfElements(prhs[0]);
|
---|
99 | ostruct->ndims=mxGetNumberOfDimensions(prhs[0]);
|
---|
100 | ipt =mxGetDimensions(prhs[0]);
|
---|
101 | ostruct->size =xNew<int>(ostruct->ndims);
|
---|
102 | for (i=0; i<ostruct->ndims; i++) ostruct->size[i]=(int)ipt[i];
|
---|
103 | if (ostruct->numel) ostruct->value=xNew<Options*>(ostruct->numel);
|
---|
104 |
|
---|
105 | /*loop through and process each element of the struct array */
|
---|
106 | for (sindex=0; sindex<ostruct->numel; sindex++) {
|
---|
107 | ostruct->value[sindex]=new Options;
|
---|
108 |
|
---|
109 | /*loop through and process each field for the element */
|
---|
110 | for (i=0; i<mxGetNumberOfFields(prhs[0]); i++) {
|
---|
111 | sprintf(namei,"%s.%s",name,mxGetFieldNameByNumber(prhs[0],i));
|
---|
112 | structi=mxGetFieldByNumber(prhs[0],sindex,i);
|
---|
113 |
|
---|
114 | option=(Option*)OptionParse(namei,&structi);
|
---|
115 | ostruct->value[sindex]->AddObject((Object*)option);
|
---|
116 | option=NULL;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | return(ostruct);
|
---|
121 | }/*}}}*/
|
---|
122 | GenericOption<Options*>* OptionCellParse( char* name, const mxArray* prhs[]){ /*{{{*/
|
---|
123 |
|
---|
124 | int i;
|
---|
125 | int *dims;
|
---|
126 | char namei[161];
|
---|
127 | char cstr[81];
|
---|
128 | GenericOption<Options*> *ocell = NULL;
|
---|
129 | Option *option = NULL;
|
---|
130 | const mwSize *ipt = NULL;
|
---|
131 | const mxArray *celli;
|
---|
132 | mwIndex cindex;
|
---|
133 |
|
---|
134 | /*check and parse the name */
|
---|
135 | ocell=new GenericOption<Options*>();
|
---|
136 | ocell->name =xNew<char>(strlen(name)+1);
|
---|
137 | memcpy(ocell->name,name,(strlen(name)+1)*sizeof(char));
|
---|
138 |
|
---|
139 | /*check and parse the value */
|
---|
140 | if (!mxIsClass(prhs[0],"cell")){
|
---|
141 | _error_("Value of option \"" << ocell->name << "\" must be class \"cell\", not class \"" << mxGetClassName(prhs[0]) <<"\".");
|
---|
142 | }
|
---|
143 |
|
---|
144 | ocell->numel=mxGetNumberOfElements(prhs[0]);
|
---|
145 | ocell->ndims=mxGetNumberOfDimensions(prhs[0]);
|
---|
146 | ipt =mxGetDimensions(prhs[0]);
|
---|
147 | ocell->size =xNew<int>(ocell->ndims);
|
---|
148 | for (i=0; i<ocell->ndims; i++) ocell->size[i]=(int)ipt[i];
|
---|
149 | ocell->value=new Options;
|
---|
150 |
|
---|
151 | /*loop through and process each element of the cell array */
|
---|
152 | dims=xNew<int>(ocell->ndims);
|
---|
153 | for (cindex=0; cindex<ocell->numel; cindex++) {
|
---|
154 | ColumnWiseDimsFromIndex(dims,(int)cindex,ocell->size,ocell->ndims);
|
---|
155 | StringFromDims(cstr,dims,ocell->ndims);
|
---|
156 | #ifdef _INTEL_WIN_
|
---|
157 | _snprintf(namei,161,"%s%s",name,cstr);
|
---|
158 | #else
|
---|
159 | snprintf(namei,161,"%s%s",name,cstr);
|
---|
160 | #endif
|
---|
161 | celli=mxGetCell(prhs[0],cindex);
|
---|
162 |
|
---|
163 | option=(Option*)OptionParse(namei,&celli);
|
---|
164 | ocell->value->AddObject((Object*)option);
|
---|
165 | option=NULL;
|
---|
166 | }
|
---|
167 | xDelete<int>(dims);
|
---|
168 |
|
---|
169 | return(ocell);
|
---|
170 | }/*}}}*/
|
---|
171 | Option* OptionParse(char* name, const mxArray* prhs[]){ /*{{{*/
|
---|
172 |
|
---|
173 | Option *option = NULL;
|
---|
174 | mxArray *lhs[1];
|
---|
175 |
|
---|
176 | /*parse the value according to the matlab data type */
|
---|
177 | if (mxIsClass(prhs[0],"double") && (mxGetNumberOfElements(prhs[0])==1))
|
---|
178 | option=(Option*)OptionDoubleParse(name,prhs);
|
---|
179 | else if(mxIsClass(prhs[0],"double") && (mxGetNumberOfElements(prhs[0])!=1))
|
---|
180 | option=(Option*)OptionDoubleArrayParse(name,prhs);
|
---|
181 | else if(mxIsClass(prhs[0],"logical"))
|
---|
182 | option=(Option*)OptionLogicalParse(name,prhs);
|
---|
183 | else if(mxIsClass(prhs[0],"char"))
|
---|
184 | option=(Option*)OptionCharParse(name,prhs);
|
---|
185 | else if(mxIsClass(prhs[0],"struct"))
|
---|
186 | option=(Option*)OptionStructParse(name,prhs);
|
---|
187 | else if(mxIsClass(prhs[0],"cell"))
|
---|
188 | option=(Option*)OptionCellParse(name,prhs);
|
---|
189 | else {
|
---|
190 | _printf0_(" Converting value of option \"" << name << "\" from unrecognized class \"" << mxGetClassName(prhs[0]) << "\" to class \"" << "struct" << "\".\n");
|
---|
191 | if (!mexCallMATLAB(1,lhs,1,(mxArray**)prhs,"struct")) {
|
---|
192 | option=(Option*)OptionStructParse(name,(const mxArray**)lhs);
|
---|
193 | mxDestroyArray(lhs[0]);
|
---|
194 | }
|
---|
195 | else _error_("Second argument value of option \""<< name <<"\" is of unrecognized class \""<< mxGetClassName(prhs[0]) <<"\".");
|
---|
196 | }
|
---|
197 |
|
---|
198 | return(option);
|
---|
199 | }/*}}}*/
|
---|