[391] | 1 | #include "../base/libconfig/libconfig.h++" |
---|
[331] | 2 | #include <itpp/itbase.h> |
---|
[327] | 3 | #include <itpp/itmex.h> |
---|
| 4 | #include <stdlib.h> |
---|
| 5 | |
---|
[331] | 6 | using namespace itpp; |
---|
[327] | 7 | using namespace std; |
---|
| 8 | using namespace libconfig; |
---|
| 9 | |
---|
[536] | 10 | //! Class for writing Settings into Matlab's mxArray |
---|
[327] | 11 | class UImxArray : public Config { |
---|
| 12 | public: |
---|
[536] | 13 | //! Build an instance of Config with fields filled from the given \a mxarray |
---|
[477] | 14 | UImxArray ( const mxArray *mxarray ) : Config() { |
---|
[327] | 15 | Setting & setting = this->getRoot(); //setting is a group |
---|
[477] | 16 | if ( !mxIsStruct ( mxarray ) ) { |
---|
| 17 | mexErrMsgTxt ( "Given mxArray is not a struct." ); |
---|
| 18 | }; |
---|
[327] | 19 | //mexCallMATLAB(0, NULL, 1, (mxArray **) &mxarray, "dump"); |
---|
[477] | 20 | fillGroup ( setting, mxarray ); |
---|
| 21 | setAutoConvert ( true ); |
---|
[327] | 22 | } |
---|
[477] | 23 | UImxArray() : Config() { |
---|
| 24 | setAutoConvert ( true ); |
---|
| 25 | } |
---|
[536] | 26 | //! Add libconfig's \c list to the structure |
---|
[477] | 27 | void addList ( const mxArray *mxarray, const char* name ) { |
---|
[388] | 28 | Setting & setting = this->getRoot(); //setting is a group |
---|
[477] | 29 | Setting & child = setting.add ( name, Setting::TypeList ); |
---|
| 30 | fillList ( child, mxarray ); |
---|
[388] | 31 | } |
---|
[536] | 32 | //! Add libconfig's \c group to the structure |
---|
[477] | 33 | void addGroup ( const mxArray *mxarray, const char* name ) { |
---|
[392] | 34 | Setting & setting = this->getRoot(); //setting is a group |
---|
[477] | 35 | Setting & child = setting.add ( name, Setting::TypeGroup ); |
---|
| 36 | fillGroup ( child, mxarray ); |
---|
[392] | 37 | } |
---|
[536] | 38 | //! Operator for more convenient access to this Confic |
---|
[477] | 39 | operator Setting&() { |
---|
| 40 | return getRoot(); |
---|
[392] | 41 | } |
---|
[327] | 42 | private: |
---|
[477] | 43 | void storeNumeric ( Setting &setting, const mxArray *value, string key = "" ) { |
---|
[327] | 44 | //TODO: integer matrices |
---|
[477] | 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 ); |
---|
[327] | 53 | } else { |
---|
[477] | 54 | Setting &child = ( key == "" ) ? setting.add ( Setting::TypeList ) |
---|
| 55 | : setting.add ( key, Setting::TypeList ); |
---|
| 56 | Setting &label = child.add ( Setting::TypeString ); |
---|
[371] | 57 | label = "matrix"; |
---|
[477] | 58 | Setting &rows = child.add ( Setting::TypeInt ); |
---|
| 59 | Setting &cols = child.add ( Setting::TypeInt ); |
---|
| 60 | Setting &elements = child.add ( Setting::TypeArray ); |
---|
[327] | 61 | cols = val.cols(); |
---|
[345] | 62 | rows = val.rows(); |
---|
[477] | 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 ); |
---|
[327] | 67 | } |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
[477] | 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 ); |
---|
[327] | 82 | } |
---|
[477] | 83 | if ( mxIsLogical ( value ) ) { |
---|
| 84 | Setting &child = setting.add ( key, Setting::TypeBoolean ); |
---|
| 85 | child = ( bool ) mxArray2bin ( value ); |
---|
[327] | 86 | } |
---|
[477] | 87 | if ( mxIsStruct ( value ) ) { |
---|
| 88 | Setting &child = setting.add ( key, Setting::TypeGroup ); |
---|
| 89 | fillGroup ( child, value ); |
---|
[327] | 90 | } |
---|
[477] | 91 | if ( mxIsCell ( value ) ) { |
---|
| 92 | Setting &child = setting.add ( key, Setting::TypeList ); |
---|
| 93 | fillList ( child, value ); |
---|
[327] | 94 | } |
---|
[477] | 95 | if ( mxIsNumeric ( value ) ) { |
---|
| 96 | storeNumeric ( setting, value, ( string ) key ); |
---|
[327] | 97 | } |
---|
| 98 | } |
---|
| 99 | } |
---|
[477] | 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 ); |
---|
[327] | 110 | } |
---|
[477] | 111 | if ( mxIsLogical ( value ) ) { |
---|
| 112 | Setting &child = setting.add ( Setting::TypeBoolean ); |
---|
| 113 | child = ( bool ) mxArray2bin ( value ); |
---|
[327] | 114 | } |
---|
[477] | 115 | if ( mxIsStruct ( value ) ) { |
---|
| 116 | Setting &child = setting.add ( Setting::TypeGroup ); |
---|
| 117 | fillGroup ( child, value ); |
---|
[327] | 118 | } |
---|
[477] | 119 | if ( mxIsCell ( value ) ) { |
---|
| 120 | Setting &child = setting.add ( Setting::TypeList ); |
---|
| 121 | fillList ( child, value ); |
---|
[327] | 122 | } |
---|
[477] | 123 | if ( mxIsNumeric ( value ) ) { |
---|
| 124 | storeNumeric ( setting, value ); |
---|
[327] | 125 | } |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | }; |
---|