forked from frizinak/gphoto2go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gp_camera_list.go
77 lines (58 loc) · 1.29 KB
/
gp_camera_list.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
package gphoto2
// #cgo pkg-config: libgphoto2
// #include <gphoto2.h>
// #include <stdlib.h>
import "C"
import "unsafe"
type CameraList struct {
c_ref *C.CameraList
}
func newCameraList() CameraList {
cl := CameraList{}
if err := toError(C.gp_list_new(&cl.c_ref)); err != nil {
panic(err)
}
return cl
}
func (cl CameraList) Close() {
C.free(unsafe.Pointer(cl.c_ref))
// FIXME: Why this fails? "pointer being freed was not allocated"
//if err := toError(C.gp_list_free(cl.c_ref)); err != nil {
// panic(err)
//}
}
func (cl CameraList) each(fn func(key, value string)) {
var (
size = int(C.gp_list_count(cl.c_ref))
)
if size < 0 {
return
}
for i := 0; i < size; i++ {
(func() {
var c_key *C.char
var c_val *C.char
C.gp_list_get_name(cl.c_ref, C.int(i), &c_key)
C.gp_list_get_value(cl.c_ref, C.int(i), &c_val)
defer C.free(unsafe.Pointer(c_key))
defer C.free(unsafe.Pointer(c_val))
key := C.GoString(c_key)
val := C.GoString(c_val)
fn(key, val)
})()
}
}
func (cl CameraList) ToMap() map[string]string {
vals := map[string]string{}
cl.each(func(key, value string) {
vals[key] = value
})
return vals
}
func (cl CameraList) Keys() []string {
var keys []string
cl.each(func(key, value string) {
keys = append(keys, key)
})
return keys
}