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