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