/
put.go
221 lines (185 loc) · 4.96 KB
/
put.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package mat
// #include <string.h>
// #include <mat.h>
import "C"
import (
"errors"
"reflect"
"unsafe"
)
// Put writes an object into the file.
func (f *File) Put(name string, object interface{}) error {
array, err := f.writeObject(reflect.ValueOf(object))
if err != nil {
return err
}
defer C.mxDestroyArray(array)
return f.putVariable(name, array)
}
// PutArray writes a multidimensional array into the file.
func (f *File) PutArray(name string, object interface{}, dimensions ...uint) error {
value := reflect.ValueOf(object)
length := uint(value.Len())
switch len(dimensions) {
case 0:
dimensions = append(dimensions, length, 1)
case 1:
dimensions = append(dimensions, 1)
}
size := dimensions[0]
for i := 1; i < len(dimensions); i++ {
size *= dimensions[i]
}
if length != size {
return errors.New("the dimensions do not match")
}
array, err := f.writeArray(value, dimensions...)
if err != nil {
return err
}
defer C.mxDestroyArray(array)
return f.putVariable(name, array)
}
// PutMatrix writes a matrix into the file.
func (f *File) PutMatrix(name string, object interface{}, rows, cols uint) error {
return f.PutArray(name, object, rows, cols)
}
func (f *File) writeObject(value reflect.Value) (*C.mxArray, error) {
switch value.Kind() {
case reflect.Slice:
return f.writeArray(value, uint(value.Len()), 1)
case reflect.Struct:
return f.writeStruct(value)
default:
return f.writeArray(value, 1, 1)
}
}
func (f *File) writeArray(value reflect.Value, dimensions ...uint) (*C.mxArray, error) {
var kind reflect.Kind
if value.Kind() == reflect.Slice {
kind = value.Type().Elem().Kind()
} else {
kind = value.Kind()
}
classid, ok := kindClassMapping[kind]
if !ok {
return nil, errors.New("encountered an unsupported data type")
}
// NOTE: Do we need a proper conversion from uint to C.size_t?
array := C.mxCreateNumericArray(C.size_t(len(dimensions)),
(*C.size_t)(unsafe.Pointer(&dimensions[0])), classid, C.mxREAL)
if array == nil {
return nil, errors.New("cannot create an array")
}
parray := unsafe.Pointer(C.mxGetPr(array))
if parray == nil {
C.mxDestroyArray(array)
return nil, errors.New("cannot create an array")
}
size, ok := classSizeMapping[classid]
if !ok {
return nil, errors.New("encountered an unsupported data type")
}
if value.Kind() == reflect.Slice {
if err := writeSlice(value, parray, size); err != nil {
C.mxDestroyArray(array)
return nil, err
}
} else {
if err := writeScalar(value, parray); err != nil {
C.mxDestroyArray(array)
return nil, err
}
}
return array, nil
}
func (f *File) writeStruct(value reflect.Value) (*C.mxArray, error) {
typo := value.Type()
count := typo.NumField()
names := make([]*C.char, 0, count)
arrays := make([]*C.mxArray, 0, count)
// NOTE: Should be called only when the function fails. If it succeeds, all
// arrays will be disposed when the struct gets destroyed.
cleanup := func() {
for _, array := range arrays {
C.mxDestroyArray(array)
}
}
for i := 0; i < count; i++ {
field := typo.Field(i)
name := C.CString(field.Name)
defer C.free(unsafe.Pointer(name))
names = append(names, name)
array, err := f.writeObject(value.Field(i))
if err != nil {
cleanup()
return nil, err
}
arrays = append(arrays, array)
}
count = len(names)
var pnames **C.char
if count > 0 {
pnames = (**C.char)(&names[0])
}
array := C.mxCreateStructMatrix(1, 1, C.int(count), pnames)
if array == nil {
cleanup()
return nil, errors.New("cannot create a structure")
}
for i := 0; i < count; i++ {
C.mxSetFieldByNumber(array, 0, C.int(i), arrays[i])
}
return array, nil
}
func writeScalar(v reflect.Value, p unsafe.Pointer) error {
switch v.Kind() {
case reflect.Int:
if is64bit {
*((*int64)(p)) = int64(v.Int())
} else {
*((*int32)(p)) = int32(v.Int())
}
case reflect.Uint:
if is64bit {
*((*uint64)(p)) = uint64(v.Uint())
} else {
*((*uint32)(p)) = uint32(v.Uint())
}
case reflect.Int8:
*((*int8)(p)) = int8(v.Int())
case reflect.Uint8:
*((*uint8)(p)) = uint8(v.Uint())
case reflect.Int16:
*((*int16)(p)) = int16(v.Int())
case reflect.Uint16:
*((*uint16)(p)) = uint16(v.Uint())
case reflect.Int32:
*((*int32)(p)) = int32(v.Int())
case reflect.Uint32:
*((*uint32)(p)) = uint32(v.Uint())
case reflect.Int64:
*((*int64)(p)) = int64(v.Int())
case reflect.Uint64:
*((*uint64)(p)) = uint64(v.Uint())
case reflect.Float32:
*((*float32)(p)) = float32(v.Float())
case reflect.Float64:
*((*float64)(p)) = float64(v.Float())
default:
return errors.New("encountered an unsupported data type")
}
return nil
}
func writeSlice(v reflect.Value, p unsafe.Pointer, s C.size_t) error {
C.memcpy(p, unsafe.Pointer(v.Pointer()), C.size_t(v.Len())*s)
return nil
}
func (f *File) putVariable(name string, array *C.mxArray) error {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
if C.matPutVariable(f.mat, cname, array) != 0 {
return errors.New("cannot write a variable into the file")
}
return nil
}