/*\file OptionParse.c *\brief: functions to parse the mex options. */ #ifdef HAVE_CONFIG_H #include #else #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!" #endif #include #include "./matlabio.h" GenericOption* OptionDoubleParse( char* name, const mxArray* prhs[]){ /*{{{*/ GenericOption *odouble = NULL; /*check and parse the name */ odouble=new GenericOption(); odouble->name =xNew(strlen(name)+1); memcpy(odouble->name,name,(strlen(name)+1)*sizeof(char)); FetchData(&odouble->value,prhs[0]); odouble->numel=1; odouble->ndims=1; odouble->size=NULL; return(odouble); }/*}}}*/ GenericOption* OptionDoubleArrayParse( char* name, const mxArray* prhs[]){ /*{{{*/ GenericOption *odouble = NULL; /*check and parse the name */ odouble=new GenericOption(); odouble->name =xNew(strlen(name)+1); memcpy(odouble->name,name,(strlen(name)+1)*sizeof(char)); /*check and parse the value */ if (!mxIsClass(prhs[0],"double")){ _error_("Value of option \"" << odouble->name << "\" must be class \"double\", not class \"" << mxGetClassName(prhs[0]) <<"\"."); } FetchData(&odouble->value,&odouble->numel,&odouble->ndims,&odouble->size,prhs[0]); return(odouble); }/*}}}*/ GenericOption* OptionLogicalParse( char* name, const mxArray* prhs[]){ /*{{{*/ GenericOption *ological = NULL; /*check and parse the name */ ological=new GenericOption(); ological->name =xNew(strlen(name)+1); memcpy(ological->name,name,(strlen(name)+1)*sizeof(char)); /*check and parse the value */ if (!mxIsClass(prhs[0],"logical")){ _error_("Value of option \"" << ological->name << "\" must be class \"logical\", not class \"" << mxGetClassName(prhs[0]) <<"\"."); } FetchData(&ological->value,&ological->numel,&ological->ndims,&ological->size,prhs[0]); return(ological); }/*}}}*/ GenericOption* OptionCharParse( char* name, const mxArray* prhs[]){ /*{{{*/ GenericOption *ochar = NULL; /*check and parse the name */ ochar=new GenericOption(); ochar->name =xNew(strlen(name)+1); memcpy(ochar->name,name,(strlen(name)+1)*sizeof(char)); /*check and parse the value */ if (!mxIsClass(prhs[0],"char")){ _error_("Value of option \"" << ochar->name << "\" must be class \"char\", not class \"" << mxGetClassName(prhs[0]) <<"\"."); } FetchData(&ochar->value,&ochar->numel,&ochar->ndims,&ochar->size,prhs[0]); return(ochar); }/*}}}*/ GenericOption* OptionStructParse( char* name, const mxArray* prhs[]){ /*{{{*/ int i; char namei[161]; Option* option = NULL; GenericOption *ostruct = NULL; const mwSize *ipt = NULL; const mxArray *structi; mwIndex sindex; /*check and parse the name */ ostruct=new GenericOption(); ostruct->name =xNew(strlen(name)+1); memcpy(ostruct->name,name,(strlen(name)+1)*sizeof(char)); /*check and parse the value */ if (!mxIsClass(prhs[0],"struct")){ _error_("Value of option \"" << ostruct->name << "\" must be class \"struct\", not class \"" << mxGetClassName(prhs[0]) <<"\"."); } ostruct->numel=mxGetNumberOfElements(prhs[0]); ostruct->ndims=mxGetNumberOfDimensions(prhs[0]); ipt =mxGetDimensions(prhs[0]); ostruct->size =xNew(ostruct->ndims); for (i=0; indims; i++) ostruct->size[i]=(int)ipt[i]; if (ostruct->numel) ostruct->value=xNew(ostruct->numel); /*loop through and process each element of the struct array */ for (sindex=0; sindexnumel; sindex++) { ostruct->value[sindex]=new Options; /*loop through and process each field for the element */ for (i=0; ivalue[sindex]->AddObject((Object*)option); option=NULL; } } return(ostruct); }/*}}}*/ GenericOption* OptionCellParse( char* name, const mxArray* prhs[]){ /*{{{*/ int i; int *dims; char namei[161]; char cstr[81]; GenericOption *ocell = NULL; Option *option = NULL; const mwSize *ipt = NULL; const mxArray *celli; mwIndex cindex; /*check and parse the name */ ocell=new GenericOption(); ocell->name =xNew(strlen(name)+1); memcpy(ocell->name,name,(strlen(name)+1)*sizeof(char)); /*check and parse the value */ if (!mxIsClass(prhs[0],"cell")){ _error_("Value of option \"" << ocell->name << "\" must be class \"cell\", not class \"" << mxGetClassName(prhs[0]) <<"\"."); } ocell->numel=mxGetNumberOfElements(prhs[0]); ocell->ndims=mxGetNumberOfDimensions(prhs[0]); ipt =mxGetDimensions(prhs[0]); ocell->size =xNew(ocell->ndims); for (i=0; indims; i++) ocell->size[i]=(int)ipt[i]; ocell->value=new Options; /*loop through and process each element of the cell array */ dims=xNew(ocell->ndims); for (cindex=0; cindexnumel; cindex++) { ColumnWiseDimsFromIndex(dims,(int)cindex,ocell->size,ocell->ndims); StringFromDims(cstr,dims,ocell->ndims); #ifdef _INTEL_WIN_ _snprintf(namei,161,"%s%s",name,cstr); #else snprintf(namei,161,"%s%s",name,cstr); #endif celli=mxGetCell(prhs[0],cindex); option=(Option*)OptionParse(namei,&celli); ocell->value->AddObject((Object*)option); option=NULL; } xDelete(dims); return(ocell); }/*}}}*/ Option* OptionParse(char* name, const mxArray* prhs[]){ /*{{{*/ Option *option = NULL; mxArray *lhs[1]; /*parse the value according to the matlab data type */ if (mxIsClass(prhs[0],"double") && (mxGetNumberOfElements(prhs[0])==1)) option=(Option*)OptionDoubleParse(name,prhs); else if(mxIsClass(prhs[0],"double") && (mxGetNumberOfElements(prhs[0])!=1)) option=(Option*)OptionDoubleArrayParse(name,prhs); else if(mxIsClass(prhs[0],"logical")) option=(Option*)OptionLogicalParse(name,prhs); else if(mxIsClass(prhs[0],"char")) option=(Option*)OptionCharParse(name,prhs); else if(mxIsClass(prhs[0],"struct")) option=(Option*)OptionStructParse(name,prhs); else if(mxIsClass(prhs[0],"cell")) option=(Option*)OptionCellParse(name,prhs); else { _printf0_(" Converting value of option \"" << name << "\" from unrecognized class \"" << mxGetClassName(prhs[0]) << "\" to class \"" << "struct" << "\".\n"); if (!mexCallMATLAB(1,lhs,1,(mxArray**)prhs,"struct")) { option=(Option*)OptionStructParse(name,(const mxArray**)lhs); mxDestroyArray(lhs[0]); } else _error_("Second argument value of option \""<< name <<"\" is of unrecognized class \""<< mxGetClassName(prhs[0]) <<"\"."); } return(option); }/*}}}*/