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