[12011] | 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 |
|
---|
[12704] | 10 | #include <cstring>
|
---|
[12011] | 11 | #include "./matlabio.h"
|
---|
| 12 |
|
---|
[13267] | 13 | GenericOption<double>* OptionDoubleParse( char* name, const mxArray* prhs[]){ /*{{{*/
|
---|
[12011] | 14 |
|
---|
[13267] | 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 |
|
---|
[13216] | 30 | GenericOption<double*> *odouble = NULL;
|
---|
[12011] | 31 |
|
---|
| 32 | /*check and parse the name */
|
---|
[13216] | 33 | odouble=new GenericOption<double*>();
|
---|
[12434] | 34 | odouble->name =xNew<char>(strlen(name)+1);
|
---|
[12011] | 35 | memcpy(odouble->name,name,(strlen(name)+1)*sizeof(char));
|
---|
| 36 |
|
---|
| 37 | /*check and parse the value */
|
---|
| 38 | if (!mxIsClass(prhs[0],"double")){
|
---|
[13056] | 39 | _error_("Value of option \"" << odouble->name << "\" must be class \"double\", not class \"" << mxGetClassName(prhs[0]) <<"\".");
|
---|
[12011] | 40 | }
|
---|
[13216] | 41 | FetchData(&odouble->value,&odouble->numel,&odouble->ndims,&odouble->size,prhs[0]);
|
---|
[12011] | 42 |
|
---|
| 43 | return(odouble);
|
---|
| 44 | }/*}}}*/
|
---|
[13216] | 45 | GenericOption<bool*>* OptionLogicalParse( char* name, const mxArray* prhs[]){ /*{{{*/
|
---|
[12011] | 46 |
|
---|
[13216] | 47 | GenericOption<bool*> *ological = NULL;
|
---|
[12011] | 48 |
|
---|
| 49 | /*check and parse the name */
|
---|
[13216] | 50 | ological=new GenericOption<bool*>();
|
---|
[12434] | 51 | ological->name =xNew<char>(strlen(name)+1);
|
---|
[12011] | 52 | memcpy(ological->name,name,(strlen(name)+1)*sizeof(char));
|
---|
| 53 |
|
---|
| 54 | /*check and parse the value */
|
---|
| 55 | if (!mxIsClass(prhs[0],"logical")){
|
---|
[13056] | 56 | _error_("Value of option \"" << ological->name << "\" must be class \"logical\", not class \"" << mxGetClassName(prhs[0]) <<"\".");
|
---|
[12011] | 57 | }
|
---|
[13216] | 58 | FetchData(&ological->value,&ological->numel,&ological->ndims,&ological->size,prhs[0]);
|
---|
[12011] | 59 |
|
---|
| 60 | return(ological);
|
---|
| 61 | }/*}}}*/
|
---|
[13216] | 62 | GenericOption<char*>* OptionCharParse( char* name, const mxArray* prhs[]){ /*{{{*/
|
---|
[12011] | 63 |
|
---|
[13216] | 64 | GenericOption<char*> *ochar = NULL;
|
---|
[12011] | 65 |
|
---|
| 66 | /*check and parse the name */
|
---|
[13216] | 67 | ochar=new GenericOption<char*>();
|
---|
[12434] | 68 | ochar->name =xNew<char>(strlen(name)+1);
|
---|
[12011] | 69 | memcpy(ochar->name,name,(strlen(name)+1)*sizeof(char));
|
---|
| 70 |
|
---|
| 71 | /*check and parse the value */
|
---|
| 72 | if (!mxIsClass(prhs[0],"char")){
|
---|
[13056] | 73 | _error_("Value of option \"" << ochar->name << "\" must be class \"char\", not class \"" << mxGetClassName(prhs[0]) <<"\".");
|
---|
[12011] | 74 | }
|
---|
[13216] | 75 | FetchData(&ochar->value,&ochar->numel,&ochar->ndims,&ochar->size,prhs[0]);
|
---|
[12011] | 76 |
|
---|
| 77 | return(ochar);
|
---|
| 78 | }/*}}}*/
|
---|
[13216] | 79 | GenericOption<Options**>* OptionStructParse( char* name, const mxArray* prhs[]){ /*{{{*/
|
---|
[12011] | 80 |
|
---|
| 81 | int i;
|
---|
| 82 | char namei[161];
|
---|
[13216] | 83 | Option* option = NULL;
|
---|
| 84 | GenericOption<Options**> *ostruct = NULL;
|
---|
[12011] | 85 | const mwSize *ipt = NULL;
|
---|
| 86 | const mxArray *structi;
|
---|
| 87 | mwIndex sindex;
|
---|
| 88 |
|
---|
| 89 | /*check and parse the name */
|
---|
[13216] | 90 | ostruct=new GenericOption<Options**>();
|
---|
[12434] | 91 | ostruct->name =xNew<char>(strlen(name)+1);
|
---|
[12011] | 92 | memcpy(ostruct->name,name,(strlen(name)+1)*sizeof(char));
|
---|
| 93 |
|
---|
| 94 | /*check and parse the value */
|
---|
| 95 | if (!mxIsClass(prhs[0],"struct")){
|
---|
[13056] | 96 | _error_("Value of option \"" << ostruct->name << "\" must be class \"struct\", not class \"" << mxGetClassName(prhs[0]) <<"\".");
|
---|
[12011] | 97 | }
|
---|
| 98 | ostruct->numel=mxGetNumberOfElements(prhs[0]);
|
---|
| 99 | ostruct->ndims=mxGetNumberOfDimensions(prhs[0]);
|
---|
| 100 | ipt =mxGetDimensions(prhs[0]);
|
---|
[12434] | 101 | ostruct->size =xNew<int>(ostruct->ndims);
|
---|
[12011] | 102 | for (i=0; i<ostruct->ndims; i++) ostruct->size[i]=(int)ipt[i];
|
---|
[13216] | 103 | if (ostruct->numel) ostruct->value=xNew<Options*>(ostruct->numel);
|
---|
[12011] | 104 |
|
---|
| 105 | /*loop through and process each element of the struct array */
|
---|
| 106 | for (sindex=0; sindex<ostruct->numel; sindex++) {
|
---|
[13216] | 107 | ostruct->value[sindex]=new Options;
|
---|
[12011] | 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);
|
---|
[13216] | 115 | ostruct->value[sindex]->AddObject((Object*)option);
|
---|
[12011] | 116 | option=NULL;
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | return(ostruct);
|
---|
| 121 | }/*}}}*/
|
---|
[13216] | 122 | GenericOption<Options*>* OptionCellParse( char* name, const mxArray* prhs[]){ /*{{{*/
|
---|
[12011] | 123 |
|
---|
| 124 | int i;
|
---|
| 125 | int *dims;
|
---|
| 126 | char namei[161];
|
---|
| 127 | char cstr[81];
|
---|
[13216] | 128 | GenericOption<Options*> *ocell = NULL;
|
---|
[12011] | 129 | Option *option = NULL;
|
---|
| 130 | const mwSize *ipt = NULL;
|
---|
| 131 | const mxArray *celli;
|
---|
| 132 | mwIndex cindex;
|
---|
| 133 |
|
---|
| 134 | /*check and parse the name */
|
---|
[13216] | 135 | ocell=new GenericOption<Options*>();
|
---|
[12434] | 136 | ocell->name =xNew<char>(strlen(name)+1);
|
---|
[12011] | 137 | memcpy(ocell->name,name,(strlen(name)+1)*sizeof(char));
|
---|
| 138 |
|
---|
| 139 | /*check and parse the value */
|
---|
| 140 | if (!mxIsClass(prhs[0],"cell")){
|
---|
[13056] | 141 | _error_("Value of option \"" << ocell->name << "\" must be class \"cell\", not class \"" << mxGetClassName(prhs[0]) <<"\".");
|
---|
[12011] | 142 | }
|
---|
| 143 |
|
---|
| 144 | ocell->numel=mxGetNumberOfElements(prhs[0]);
|
---|
| 145 | ocell->ndims=mxGetNumberOfDimensions(prhs[0]);
|
---|
| 146 | ipt =mxGetDimensions(prhs[0]);
|
---|
[12434] | 147 | ocell->size =xNew<int>(ocell->ndims);
|
---|
[12011] | 148 | for (i=0; i<ocell->ndims; i++) ocell->size[i]=(int)ipt[i];
|
---|
[13216] | 149 | ocell->value=new Options;
|
---|
[12011] | 150 |
|
---|
| 151 | /*loop through and process each element of the cell array */
|
---|
[12434] | 152 | dims=xNew<int>(ocell->ndims);
|
---|
[12011] | 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);
|
---|
[13216] | 164 | ocell->value->AddObject((Object*)option);
|
---|
[12011] | 165 | option=NULL;
|
---|
| 166 | }
|
---|
[12434] | 167 | xDelete<int>(dims);
|
---|
[12011] | 168 |
|
---|
| 169 | return(ocell);
|
---|
| 170 | }/*}}}*/
|
---|
[13216] | 171 | Option* OptionParse(char* name, const mxArray* prhs[]){ /*{{{*/
|
---|
[12011] | 172 |
|
---|
[13267] | 173 | Option *option = NULL;
|
---|
| 174 | mxArray *lhs[1];
|
---|
[12011] | 175 |
|
---|
| 176 | /*parse the value according to the matlab data type */
|
---|
[13267] | 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);
|
---|
[12011] | 189 | else {
|
---|
[15396] | 190 | _printf0_(" Converting value of option \"" << name << "\" from unrecognized class \"" << mxGetClassName(prhs[0]) << "\" to class \"" << "struct" << "\".\n");
|
---|
[12011] | 191 | if (!mexCallMATLAB(1,lhs,1,(mxArray**)prhs,"struct")) {
|
---|
| 192 | option=(Option*)OptionStructParse(name,(const mxArray**)lhs);
|
---|
| 193 | mxDestroyArray(lhs[0]);
|
---|
| 194 | }
|
---|
[13056] | 195 | else _error_("Second argument value of option \""<< name <<"\" is of unrecognized class \""<< mxGetClassName(prhs[0]) <<"\".");
|
---|
[12011] | 196 | }
|
---|
| 197 |
|
---|
| 198 | return(option);
|
---|
| 199 | }/*}}}*/
|
---|