-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.go
148 lines (122 loc) · 3.81 KB
/
get.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
package mat
// #include <string.h>
// #include <mat.h>
import "C"
import (
"errors"
"reflect"
"unsafe"
)
// Get reads an object from the file.
func (f *File) Get(name string, object interface{}) error {
value := reflect.ValueOf(object)
if value.Kind() != reflect.Ptr {
return errors.New("expected a pointer")
}
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
array := C.matGetVariable(f.mat, cname)
if array == nil {
return errors.New("cannot find the variable")
}
defer C.mxDestroyArray(array)
return f.readObject(array, value)
}
func (f *File) readObject(array *C.mxArray, value reflect.Value) error {
ivalue := reflect.Indirect(value)
switch ivalue.Kind() {
case reflect.Struct:
return f.readStruct(array, ivalue)
default:
return f.readArray(array, ivalue)
}
}
func (f *File) readArray(array *C.mxArray, ivalue reflect.Value) error {
parray := unsafe.Pointer(C.mxGetPr(array))
if parray == nil {
return errors.New("cannot read the variable")
}
count := C.mxGetM(array) * C.mxGetN(array)
size, ok := classSizeMapping[C.mxGetClassID(array)]
if !ok {
return errors.New("encountered an unsupported data type")
}
if ivalue.Kind() == reflect.Slice {
return readSlice(ivalue, parray, count, size)
} else {
if count != 1 {
return errors.New("expected to find a scalar")
}
return readScalar(ivalue, parray)
}
}
func (f *File) readStruct(array *C.mxArray, ivalue reflect.Value) error {
if C.mxSTRUCT_CLASS != C.mxGetClassID(array) {
return errors.New("expected to find a structure")
}
if C.mxGetM(array)*C.mxGetN(array) != 1 {
return errors.New("expected to find one structure")
}
typo := ivalue.Type()
count := typo.NumField()
if count != int(C.mxGetNumberOfFields(array)) {
return errors.New("expected to find all fields of the structure")
}
for i := 0; i < count; i++ {
field := typo.Field(i)
name := C.CString(field.Name)
defer C.free(unsafe.Pointer(name))
farray := C.mxGetField(array, 0, name)
if farray == nil {
return errors.New("cannot read a field of the structure")
}
if err := f.readObject(farray, ivalue.Field(i)); err != nil {
return err
}
}
return nil
}
func readScalar(iv reflect.Value, p unsafe.Pointer) error {
switch iv.Kind() {
case reflect.Int:
*(*int)(unsafe.Pointer(iv.UnsafeAddr())) = *(*int)(p)
case reflect.Uint:
*(*uint)(unsafe.Pointer(iv.UnsafeAddr())) = *(*uint)(p)
case reflect.Int8:
*(*int8)(unsafe.Pointer(iv.UnsafeAddr())) = *(*int8)(p)
case reflect.Uint8:
*(*uint8)(unsafe.Pointer(iv.UnsafeAddr())) = *(*uint8)(p)
case reflect.Int16:
*(*int16)(unsafe.Pointer(iv.UnsafeAddr())) = *(*int16)(p)
case reflect.Uint16:
*(*uint16)(unsafe.Pointer(iv.UnsafeAddr())) = *(*uint16)(p)
case reflect.Int32:
*(*int32)(unsafe.Pointer(iv.UnsafeAddr())) = *(*int32)(p)
case reflect.Uint32:
*(*uint32)(unsafe.Pointer(iv.UnsafeAddr())) = *(*uint32)(p)
case reflect.Int64:
*(*int64)(unsafe.Pointer(iv.UnsafeAddr())) = *(*int64)(p)
case reflect.Uint64:
*(*uint64)(unsafe.Pointer(iv.UnsafeAddr())) = *(*uint64)(p)
case reflect.Float32:
*(*float32)(unsafe.Pointer(iv.UnsafeAddr())) = *(*float32)(p)
case reflect.Float64:
*(*float64)(unsafe.Pointer(iv.UnsafeAddr())) = *(*float64)(p)
default:
return errors.New("encountered an unsupported data type")
}
return nil
}
func readSlice(iv reflect.Value, p unsafe.Pointer, c C.size_t, s C.size_t) error {
w := reflect.MakeSlice(iv.Type(), int(c), int(c))
C.memcpy(unsafe.Pointer(w.Pointer()), p, c*s)
iw := reflect.Indirect(reflect.New(iv.Type()))
iw.Set(w)
// FIXME: Bad, bad, bad! But how else to fill in unexported fields?
src := (*reflect.SliceHeader)(unsafe.Pointer(iw.UnsafeAddr()))
dst := (*reflect.SliceHeader)(unsafe.Pointer(iv.UnsafeAddr()))
dst.Data, src.Data = src.Data, dst.Data
dst.Len, src.Len = src.Len, dst.Len
dst.Cap, src.Cap = src.Cap, dst.Cap
return nil
}