root/library/bdm/mex/mex_parser.h @ 536

Revision 536, 4.2 kB (checked in by smidl, 15 years ago)

removal of unused functions _e() and samplecond(,) and added documentation lines

Line 
1#include "../base/libconfig/libconfig.h++"
2#include <itpp/itbase.h>
3#include <itpp/itmex.h>
4#include <stdlib.h>
5
6using namespace itpp;
7using namespace std;
8using namespace libconfig;
9
10//! Class for writing Settings into Matlab's mxArray
11class UImxArray : public Config {
12public:
13        //! Build an instance of Config with fields filled from the given \a mxarray
14        UImxArray ( const mxArray *mxarray ) : Config() {
15                Setting & setting = this->getRoot(); //setting is a group
16                if ( !mxIsStruct ( mxarray ) ) {
17                        mexErrMsgTxt ( "Given mxArray is not a struct." );
18                };
19                //mexCallMATLAB(0, NULL, 1, (mxArray **) &mxarray, "dump");
20                fillGroup ( setting, mxarray );
21                setAutoConvert ( true );
22        }
23        UImxArray() : Config() {
24                setAutoConvert ( true );
25        }
26        //! Add libconfig's \c list to the structure
27        void addList ( const mxArray *mxarray, const char* name ) {
28                Setting & setting = this->getRoot(); //setting is a group
29                Setting & child = setting.add ( name, Setting::TypeList );
30                fillList ( child, mxarray );
31        }
32        //! Add libconfig's \c group to the structure
33        void addGroup ( const mxArray *mxarray, const char* name ) {
34                Setting & setting = this->getRoot(); //setting is a group
35                Setting & child = setting.add ( name, Setting::TypeGroup );
36                fillGroup ( child, mxarray );
37        }
38        //! Operator for more convenient access to this Confic
39        operator Setting&() {
40                return getRoot();
41        }
42private:
43        void storeNumeric ( Setting &setting, const mxArray *value, string key = "" ) {
44                //TODO: integer matrices
45                if ( !mxIsNumeric ( value ) ) {
46                        mexErrMsgTxt ( "Given mxArray is not numeric." );
47                };
48                mat val = mxArray2mat ( value );
49                if ( ( val.rows() == 1 ) && ( val.cols() == 1 ) ) {
50                        Setting &child = ( key == "" ) ? setting.add ( Setting::TypeFloat )
51                                         : setting.add ( key, Setting::TypeFloat );
52                        child = val ( 0, 0 );
53                } else {
54                        Setting &child = ( key == "" ) ? setting.add ( Setting::TypeList )
55                                         : setting.add ( key, Setting::TypeList );
56                        Setting &label = child.add ( Setting::TypeString );
57                        label = "matrix";
58                        Setting &rows = child.add ( Setting::TypeInt );
59                        Setting &cols = child.add ( Setting::TypeInt );
60                        Setting &elements = child.add ( Setting::TypeArray );
61                        cols = val.cols();
62                        rows = val.rows();
63                        for ( int i = 0; i < val.rows(); i++ ) {
64                                for ( int j = 0; j < val.cols(); j++ ) {
65                                        Setting &el = elements.add ( Setting::TypeFloat );
66                                        el = val ( i, j );
67                                }
68                        }
69                }
70        }
71
72        void fillGroup ( Setting &setting, const mxArray *mxarray ) {
73                if ( !mxIsStruct ( mxarray ) ) {
74                        mexErrMsgTxt ( "Given mxArray is not a struct." );
75                };
76                for ( int i = 0; i < mxGetNumberOfFields ( mxarray ); i++ ) {
77                        const char *key = mxGetFieldNameByNumber ( mxarray, i );
78                        mxArray *value = mxGetField ( mxarray, 0, key );
79                        if ( mxIsChar ( value ) ) {
80                                Setting &child = setting.add ( key, Setting::TypeString );
81                                child = mxArray2string ( value );
82                        }
83                        if ( mxIsLogical ( value ) ) {
84                                Setting &child = setting.add ( key, Setting::TypeBoolean );
85                                child = ( bool ) mxArray2bin ( value );
86                        }
87                        if ( mxIsStruct ( value ) ) {
88                                Setting &child = setting.add ( key, Setting::TypeGroup );
89                                fillGroup ( child, value );
90                        }
91                        if ( mxIsCell ( value ) ) {
92                                Setting &child = setting.add ( key, Setting::TypeList );
93                                fillList ( child, value );
94                        }
95                        if ( mxIsNumeric ( value ) ) {
96                                storeNumeric ( setting, value, ( string ) key );
97                        }
98                }
99        }
100
101        void fillList ( Setting &setting, const mxArray *mxarray ) {
102                if ( !mxIsCell ( mxarray ) ) {
103                        mexErrMsgTxt ( "Given mxArray is not a cell." );
104                };
105                for ( uint i = 0; i < mxGetNumberOfElements ( mxarray ); i++ ) {
106                        mxArray *value = mxGetCell ( mxarray, i );
107                        if ( mxIsChar ( value ) ) {
108                                Setting &child = setting.add ( Setting::TypeString );
109                                child = mxArray2string ( value );
110                        }
111                        if ( mxIsLogical ( value ) ) {
112                                Setting &child = setting.add ( Setting::TypeBoolean );
113                                child = ( bool ) mxArray2bin ( value );
114                        }
115                        if ( mxIsStruct ( value ) ) {
116                                Setting &child = setting.add ( Setting::TypeGroup );
117                                fillGroup ( child, value );
118                        }
119                        if ( mxIsCell ( value ) ) {
120                                Setting &child = setting.add ( Setting::TypeList );
121                                fillList ( child, value );
122                        }
123                        if ( mxIsNumeric ( value ) ) {
124                                storeNumeric ( setting, value );
125                        }
126                }
127        }
128};
Note: See TracBrowser for help on using the browser.