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