1 | #include "../../bdm/libconfig/libconfig.h++" |
---|
2 | #include <itpp/itbase.h> |
---|
3 | #include <itpp/itmex.h> |
---|
4 | #include <stdlib.h> |
---|
5 | |
---|
6 | using namespace itpp; |
---|
7 | using namespace std; |
---|
8 | using namespace libconfig; |
---|
9 | |
---|
10 | class UImxConfig : public Config { |
---|
11 | public: |
---|
12 | mxArray *mxconfig; |
---|
13 | UImxConfig(const char * filename) { |
---|
14 | Config config; |
---|
15 | config.readFile(filename); |
---|
16 | mxconfig = group2mxstruct(config.getRoot()); |
---|
17 | } |
---|
18 | UImxConfig(const Setting &setting) { |
---|
19 | mxconfig = group2mxstruct(setting); |
---|
20 | } |
---|
21 | |
---|
22 | private: |
---|
23 | |
---|
24 | mxArray* array2mxvector(const Setting &setting) { |
---|
25 | if (!setting.isArray()) mexErrMsgTxt("Given setting is not an array"); |
---|
26 | double *elements = new double[setting.getLength()]; |
---|
27 | for (int i=0; i<setting.getLength(); i++) { |
---|
28 | elements[i] = setting[i]; |
---|
29 | } |
---|
30 | vec &v = *(new vec(elements, setting.getLength())); |
---|
31 | mxArray *result; |
---|
32 | vec2mxArray(v, result); |
---|
33 | delete &v; |
---|
34 | delete [] elements; |
---|
35 | return result; |
---|
36 | } |
---|
37 | |
---|
38 | mxArray* list2mxmatrix(const Setting &setting) { |
---|
39 | if (!setting.isList() || ("matrix" != setting[0])) |
---|
40 | mexErrMsgTxt("Given setting is not a matrix"); |
---|
41 | int rows = setting[1]; |
---|
42 | int cols = setting[2]; |
---|
43 | if (setting[3].getLength() != rows*cols) |
---|
44 | mexErrMsgTxt("Matrix elements do not fit to rows*cols"); |
---|
45 | double *elements = new double[rows*cols]; |
---|
46 | for (int i=0; i<rows*cols; i++) { |
---|
47 | elements[i] = setting[3][i]; |
---|
48 | } |
---|
49 | mat &m = *(new mat(elements, rows, cols)); |
---|
50 | mxArray *result; |
---|
51 | mat2mxArray(m, result); |
---|
52 | delete &m; |
---|
53 | delete [] elements; |
---|
54 | return result; |
---|
55 | } |
---|
56 | |
---|
57 | mxArray* group2mxstruct(const Setting &setting) { |
---|
58 | if (!setting.isGroup()) mexErrMsgTxt("Given setting is not a group."); |
---|
59 | const char ** keys = new const char*[setting.getLength()]; |
---|
60 | for (int i=0; i < setting.getLength(); i++) { |
---|
61 | keys[i] = setting[i].getName(); |
---|
62 | } |
---|
63 | mxArray *result = mxCreateStructMatrix(1, 1, setting.getLength(), keys); |
---|
64 | delete keys; |
---|
65 | for (int i=0; i < setting.getLength(); i++) { |
---|
66 | Setting &value = setting[i]; |
---|
67 | mxArray *old = mxGetFieldByNumber(result, 0, i); |
---|
68 | if (old) mxDestroyArray(old); |
---|
69 | switch (value.getType()) { |
---|
70 | case Setting::TypeString: |
---|
71 | mxSetFieldByNumber(result, 0, i, mxCreateString(value)); |
---|
72 | break; |
---|
73 | case Setting::TypeBoolean: |
---|
74 | mxSetFieldByNumber(result, 0, i, mxCreateLogicalScalar(value)); |
---|
75 | break; |
---|
76 | case Setting::TypeGroup: |
---|
77 | mxSetFieldByNumber(result, 0, i, group2mxstruct(value)); |
---|
78 | break; |
---|
79 | case Setting::TypeList: |
---|
80 | mxSetFieldByNumber(result, 0, i, list2mxcell(value)); |
---|
81 | break; |
---|
82 | case Setting::TypeArray: |
---|
83 | mxSetFieldByNumber(result, 0, i, array2mxvector(value)); |
---|
84 | break; |
---|
85 | case Setting::TypeInt: |
---|
86 | case Setting::TypeInt64: |
---|
87 | mxSetFieldByNumber(result, 0, i, mxCreateDoubleScalar((int) value)); |
---|
88 | break; |
---|
89 | case Setting::TypeFloat: |
---|
90 | mxSetFieldByNumber(result, 0, i, mxCreateDoubleScalar(value)); |
---|
91 | break; |
---|
92 | default: |
---|
93 | //this should never happen |
---|
94 | mexErrMsgTxt("Unknown type of a setting."); |
---|
95 | } |
---|
96 | } |
---|
97 | return result; |
---|
98 | |
---|
99 | } |
---|
100 | |
---|
101 | mxArray* list2mxcell(const Setting &setting) { |
---|
102 | if (!setting.isList()) mexErrMsgTxt("Given setting is not a list."); |
---|
103 | if ((setting[0].getType() == Setting::TypeString) && ("matrix" == setting[0])) { |
---|
104 | return list2mxmatrix(setting); |
---|
105 | } |
---|
106 | mxArray *result = mxCreateCellMatrix(1, setting.getLength()); |
---|
107 | for (int i=0; i < setting.getLength(); i++) { |
---|
108 | Setting &value = setting[i]; |
---|
109 | mxArray *old = mxGetCell(result, i); |
---|
110 | if (old) mxDestroyArray(old); |
---|
111 | switch (value.getType()) { |
---|
112 | case Setting::TypeString: |
---|
113 | mxSetCell(result, i, mxCreateString(value)); |
---|
114 | break; |
---|
115 | case Setting::TypeBoolean: |
---|
116 | mxSetCell(result, i, mxCreateLogicalScalar(value)); |
---|
117 | break; |
---|
118 | case Setting::TypeGroup: |
---|
119 | mxSetCell(result, i, group2mxstruct(value)); |
---|
120 | break; |
---|
121 | case Setting::TypeList: |
---|
122 | mxSetCell(result, i, list2mxcell(value)); |
---|
123 | break; |
---|
124 | case Setting::TypeArray: |
---|
125 | mxSetCell(result, i, array2mxvector(value)); |
---|
126 | break; |
---|
127 | case Setting::TypeInt: |
---|
128 | case Setting::TypeInt64: |
---|
129 | mxSetCell(result, i, mxCreateDoubleScalar((int) value)); |
---|
130 | break; |
---|
131 | case Setting::TypeFloat: |
---|
132 | mxSetCell(result, i, mxCreateDoubleScalar(value)); |
---|
133 | break; |
---|
134 | default: |
---|
135 | //this should never happen |
---|
136 | mexErrMsgTxt("Unknown type of a setting."); |
---|
137 | } |
---|
138 | } |
---|
139 | return result; |
---|
140 | } |
---|
141 | }; |
---|