source: issm/branches/trunk-jpl-damage/src/c/matlab/io/OptionParse.cpp@ 13101

Last change on this file since 13101 was 13101, checked in by cborstad, 13 years ago

merged trunk-jpl through revision 13099 into branch

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