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