| 139 | |
| 140 | public: |
| 141 | //! Convert existing Setting to Matlab arrays |
| 142 | static mxArray * create_mxArray( const Setting &setting ) |
| 143 | { |
| 144 | return group2mxstruct ( setting ); |
| 145 | } |
| 146 | |
| 147 | //! Convert existing Setting to Matlab arrays |
| 148 | mxArray * create_mxArray( ) |
| 149 | { |
| 150 | return group2mxstruct ( *this ); |
| 151 | } |
| 152 | |
| 153 | private: |
| 154 | //! Convert libconfig's array to Matlab vector |
| 155 | static mxArray* array2mxvector ( const Setting &setting ) { |
| 156 | if ( !setting.isArray() ) mexErrMsgTxt ( "Given setting is not an array" ); |
| 157 | mxArray *result = mxCreateDoubleMatrix ( 1, setting.getLength(), mxREAL ); |
| 158 | double *elements = mxGetPr ( result ); |
| 159 | for ( int i = 0; i < setting.getLength(); i++ ) { |
| 160 | if ( setting.getType() == Setting::TypeInt ) { //TODO: tady je chyba -- zaporna cisla nejsou TypeInt |
| 161 | elements[i] = ( int ) setting[i]; |
| 162 | } else { |
| 163 | elements[i] = setting[i]; |
| 164 | } |
| 165 | } |
| 166 | return result; |
| 167 | } |
| 168 | |
| 169 | //! Convert libconfig's array to Matlab matrix |
| 170 | static mxArray* list2mxmatrix ( const Setting &setting ) { |
| 171 | if ( !setting.isList() || ( strcmp ( "matrix", setting[0] ) != 0 ) ) |
| 172 | mexErrMsgTxt ( "Given setting is not a matrix" ); |
| 173 | int rows = setting[1]; |
| 174 | int cols = setting[2]; |
| 175 | if ( setting[3].getLength() != rows*cols ) |
| 176 | mexErrMsgTxt ( "Matrix elements do not fit to rows*cols" ); |
| 177 | double *elements = new double[rows*cols]; |
| 178 | for ( int i = 0; i < rows*cols; i++ ) { |
| 179 | elements[i] = setting[3][i]; |
| 180 | } |
| 181 | mat &m = * ( new mat ( elements, rows, cols ) ); |
| 182 | mxArray *result = mxCreateDoubleMatrix ( rows, cols, mxREAL ); |
| 183 | mat2mxArray ( m, result ); |
| 184 | delete &m; |
| 185 | delete [] elements; |
| 186 | return result; |
| 187 | } |
| 188 | |
| 189 | //! Convert libconfig's gourp to Matlab structure |
| 190 | static mxArray* group2mxstruct ( const Setting &setting ) { |
| 191 | if ( !setting.isGroup() ) mexErrMsgTxt ( "Given setting is not a group." ); |
| 192 | const char ** keys = new const char*[setting.getLength() ]; |
| 193 | for ( int i = 0; i < setting.getLength(); i++ ) { |
| 194 | keys[i] = setting[i].getName(); |
| 195 | } |
| 196 | mxArray *result = mxCreateStructMatrix ( 1, 1, setting.getLength(), keys ); |
| 197 | delete keys; |
| 198 | for ( int i = 0; i < setting.getLength(); i++ ) { |
| 199 | Setting &value = setting[i]; |
| 200 | mxArray *old = mxGetFieldByNumber ( result, 0, i ); |
| 201 | if ( old ) mxDestroyArray ( old ); |
| 202 | switch ( value.getType() ) { |
| 203 | case Setting::TypeString: |
| 204 | mxSetFieldByNumber ( result, 0, i, mxCreateString ( value ) ); |
| 205 | break; |
| 206 | case Setting::TypeBoolean: |
| 207 | mxSetFieldByNumber ( result, 0, i, mxCreateLogicalScalar ( value ) ); |
| 208 | break; |
| 209 | case Setting::TypeGroup: |
| 210 | mxSetFieldByNumber ( result, 0, i, group2mxstruct ( value ) ); |
| 211 | break; |
| 212 | case Setting::TypeList: |
| 213 | mxSetFieldByNumber ( result, 0, i, list2mxcell ( value ) ); |
| 214 | break; |
| 215 | case Setting::TypeArray: |
| 216 | mxSetFieldByNumber ( result, 0, i, array2mxvector ( value ) ); |
| 217 | break; |
| 218 | case Setting::TypeInt: |
| 219 | case Setting::TypeInt64: |
| 220 | mxSetFieldByNumber ( result, 0, i, mxCreateDoubleScalar ( ( int ) value ) ); |
| 221 | break; |
| 222 | case Setting::TypeFloat: |
| 223 | mxSetFieldByNumber ( result, 0, i, mxCreateDoubleScalar ( value ) ); |
| 224 | break; |
| 225 | default: |
| 226 | //this should never happen |
| 227 | mexErrMsgTxt ( "Unknown type of a setting." ); |
| 228 | } |
| 229 | } |
| 230 | return result; |
| 231 | |
| 232 | } |
| 233 | //! Convert libconfig's list to Matlab cell |
| 234 | static mxArray* list2mxcell ( const Setting &setting ) { |
| 235 | if ( !setting.isList() ) mexErrMsgTxt ( "Given setting is not a list." ); |
| 236 | if ( setting.getLength() == 0 ) { |
| 237 | mxArray *result = mxCreateCellMatrix ( 1, 0 ); |
| 238 | return result; |
| 239 | } |
| 240 | |
| 241 | if ( ( setting[0].getType() == Setting::TypeString ) ) { |
| 242 | string s = ( setting[0] ); |
| 243 | if ( s == "matrix" ) { |
| 244 | return list2mxmatrix ( setting ); |
| 245 | } |
| 246 | } |
| 247 | mxArray *result = mxCreateCellMatrix ( 1, setting.getLength() ); |
| 248 | for ( int i = 0; i < setting.getLength(); i++ ) { |
| 249 | Setting &value = setting[i]; |
| 250 | mxArray *old = mxGetCell ( result, i ); |
| 251 | if ( old ) mxDestroyArray ( old ); |
| 252 | switch ( value.getType() ) { |
| 253 | case Setting::TypeString: |
| 254 | mxSetCell ( result, i, mxCreateString ( value ) ); |
| 255 | break; |
| 256 | case Setting::TypeBoolean: |
| 257 | mxSetCell ( result, i, mxCreateLogicalScalar ( value ) ); |
| 258 | break; |
| 259 | case Setting::TypeGroup: |
| 260 | mxSetCell ( result, i, group2mxstruct ( value ) ); |
| 261 | break; |
| 262 | case Setting::TypeList: |
| 263 | mxSetCell ( result, i, list2mxcell ( value ) ); |
| 264 | break; |
| 265 | case Setting::TypeArray: |
| 266 | mxSetCell ( result, i, array2mxvector ( value ) ); |
| 267 | break; |
| 268 | case Setting::TypeInt: |
| 269 | case Setting::TypeInt64: |
| 270 | mxSetCell ( result, i, mxCreateDoubleScalar ( ( int ) value ) ); |
| 271 | break; |
| 272 | case Setting::TypeFloat: |
| 273 | mxSetCell ( result, i, mxCreateDoubleScalar ( value ) ); |
| 274 | break; |
| 275 | default: |
| 276 | //this should never happen |
| 277 | mexErrMsgTxt ( "Unknown type of a setting." ); |
| 278 | } |
| 279 | } |
| 280 | return result; |
| 281 | } |