00001 #include "../../bdm/libconfig/libconfig.h++"
00002 #include <itpp/itbase.h>
00003 #include <itpp/itmex.h>
00004 #include <stdlib.h>
00005
00006 using namespace itpp;
00007 using namespace std;
00008 using namespace libconfig;
00009
00010 class UImxArray : public Config {
00011 public:
00012 UImxArray(const mxArray *mxarray) : Config() {
00013 Setting & setting = this->getRoot();
00014 if (!mxIsStruct(mxarray)) { mexErrMsgTxt("Given mxArray is not a struct."); };
00015
00016 fillGroup(setting, mxarray);
00017 setAutoConvert(true);
00018 }
00019
00020 private:
00021 void storeNumeric(Setting &setting, const mxArray *value, string key = "") {
00022
00023 if (!mxIsNumeric(value)) { mexErrMsgTxt("Given mxArray is not numeric."); };
00024 mat val = mxArray2mat(value);
00025 if ((val.rows() == 1) && (val.cols() == 1)) {
00026 Setting &child = (key=="") ? setting.add(Setting::TypeFloat)
00027 : setting.add(key, Setting::TypeFloat);
00028 child = val(0,0);
00029 } else {
00030 Setting &child = (key=="") ? setting.add(Setting::TypeList)
00031 : setting.add(key, Setting::TypeList);
00032 Setting &cols = child.add( Setting::TypeInt );
00033 Setting &rows = child.add( Setting::TypeInt );
00034 Setting &elements = child.add( Setting::TypeArray );
00035 cols = val.cols();
00036 rows = val.rows();
00037 for (int i=0; i<val.rows(); i++) {
00038 for (int j=0; j<val.cols(); j++) {
00039 Setting &el = elements.add(Setting::TypeFloat);
00040 el = val(i,j);
00041 }
00042 }
00043 }
00044 }
00045
00046 void fillGroup(Setting &setting, const mxArray *mxarray) {
00047 if (!mxIsStruct(mxarray)) { mexErrMsgTxt("Given mxArray is not a struct."); };
00048 for(int i=0; i < mxGetNumberOfFields(mxarray); i++) {
00049 const char *key = mxGetFieldNameByNumber(mxarray, i);
00050 mxArray *value = mxGetField(mxarray, 0, key);
00051 if (mxIsChar(value)) {
00052 Setting &child = setting.add(key, Setting::TypeString);
00053 child = mxArray2string(value);
00054 }
00055 if (mxIsLogical(value)) {
00056 Setting &child = setting.add(key, Setting::TypeBoolean);
00057 child = (bool) mxArray2bin(value);
00058 }
00059 if (mxIsStruct(value)) {
00060 Setting &child = setting.add(key, Setting::TypeGroup);
00061 fillGroup(child, value);
00062 }
00063 if (mxIsCell(value)) {
00064 Setting &child = setting.add(key, Setting::TypeList);
00065 fillList(child, value);
00066 }
00067 if (mxIsNumeric(value)) {
00068 storeNumeric(setting, value, (string)key);
00069 }
00070 }
00071 }
00072
00073 void fillList(Setting &setting, const mxArray *mxarray) {
00074 if (!mxIsCell(mxarray)) { mexErrMsgTxt("Given mxArray is not a cell."); };
00075 for(int i=0; i < mxGetNumberOfElements(mxarray); i++) {
00076 mxArray *value = mxGetCell(mxarray, i);
00077 if (mxIsChar(value)) {
00078 Setting &child = setting.add(Setting::TypeString);
00079 child = mxArray2string(value);
00080 }
00081 if (mxIsLogical(value)) {
00082 Setting &child = setting.add(Setting::TypeBoolean);
00083 child = (bool) mxArray2bin(value);
00084 }
00085 if (mxIsStruct(value)) {
00086 Setting &child = setting.add(Setting::TypeGroup);
00087 fillGroup(child, value);
00088 }
00089 if (mxIsCell(value)) {
00090 Setting &child = setting.add(Setting::TypeList);
00091 fillList(child, value);
00092 }
00093 if (mxIsNumeric(value)) {
00094 storeNumeric(setting, value);
00095 }
00096 }
00097 }
00098 };