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 &label = child.add(Setting::TypeString);
00033 label = "matrix";
00034 Setting &cols = child.add( Setting::TypeInt );
00035 Setting &rows = child.add( Setting::TypeInt );
00036 Setting &elements = child.add( Setting::TypeArray );
00037 cols = val.cols();
00038 rows = val.rows();
00039 for (int i=0; i<val.rows(); i++) {
00040 for (int j=0; j<val.cols(); j++) {
00041 Setting &el = elements.add(Setting::TypeFloat);
00042 el = val(i,j);
00043 }
00044 }
00045 }
00046 }
00047
00048 void fillGroup(Setting &setting, const mxArray *mxarray) {
00049 if (!mxIsStruct(mxarray)) { mexErrMsgTxt("Given mxArray is not a struct."); };
00050 for(int i=0; i < mxGetNumberOfFields(mxarray); i++) {
00051 const char *key = mxGetFieldNameByNumber(mxarray, i);
00052 mxArray *value = mxGetField(mxarray, 0, key);
00053 if (mxIsChar(value)) {
00054 Setting &child = setting.add(key, Setting::TypeString);
00055 child = mxArray2string(value);
00056 }
00057 if (mxIsLogical(value)) {
00058 Setting &child = setting.add(key, Setting::TypeBoolean);
00059 child = (bool) mxArray2bin(value);
00060 }
00061 if (mxIsStruct(value)) {
00062 Setting &child = setting.add(key, Setting::TypeGroup);
00063 fillGroup(child, value);
00064 }
00065 if (mxIsCell(value)) {
00066 Setting &child = setting.add(key, Setting::TypeList);
00067 fillList(child, value);
00068 }
00069 if (mxIsNumeric(value)) {
00070 storeNumeric(setting, value, (string)key);
00071 }
00072 }
00073 }
00074
00075 void fillList(Setting &setting, const mxArray *mxarray) {
00076 if (!mxIsCell(mxarray)) { mexErrMsgTxt("Given mxArray is not a cell."); };
00077 for(int i=0; i < mxGetNumberOfElements(mxarray); i++) {
00078 mxArray *value = mxGetCell(mxarray, i);
00079 if (mxIsChar(value)) {
00080 Setting &child = setting.add(Setting::TypeString);
00081 child = mxArray2string(value);
00082 }
00083 if (mxIsLogical(value)) {
00084 Setting &child = setting.add(Setting::TypeBoolean);
00085 child = (bool) mxArray2bin(value);
00086 }
00087 if (mxIsStruct(value)) {
00088 Setting &child = setting.add(Setting::TypeGroup);
00089 fillGroup(child, value);
00090 }
00091 if (mxIsCell(value)) {
00092 Setting &child = setting.add(Setting::TypeList);
00093 fillList(child, value);
00094 }
00095 if (mxIsNumeric(value)) {
00096 storeNumeric(setting, value);
00097 }
00098 }
00099 }
00100 };