forked from frizinak/gphoto2go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gp_camera_file.go
82 lines (60 loc) · 1.69 KB
/
gp_camera_file.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
package gphoto2
// #cgo pkg-config: libgphoto2
// #include <gphoto2.h>
// #include <stdlib.h>
import "C"
type CameraFile struct {
cam *Camera
c_ref *C.CameraFile
}
func newCameraFileAllocate(cam *Camera) *CameraFile {
cf := &CameraFile{
cam: cam,
}
if err := toError(C.gp_file_new(&cf.c_ref)); err != nil {
panic(err)
}
return cf
}
func newCameraFileOpen(cam *Camera, path string) (*CameraFile, error) {
cf := newCameraFileAllocate(cam)
c_path, c_path_free := cString(path)
defer c_path_free()
if err := toError(C.gp_file_open(cf.c_ref, c_path)); err != nil {
cf.Close()
return nil, err
}
return cf, nil
}
func newCameraFileGet(cam *Camera, cfp CameraFilePath) (*CameraFile, error) {
cf := newCameraFileAllocate(cam)
c_folder, c_file, c_ff_free := cfp.cFolderFile()
defer c_ff_free()
if err := toError(C.gp_camera_file_get(cam.c_ref, c_folder, c_file, C.GP_FILE_TYPE_NORMAL, cf.c_ref, cam.ctx.c_ref)); err != nil {
cf.Close()
return nil, err
}
return cf, nil
}
func (cf *CameraFile) Close() {
if err := toError(C.gp_file_free(cf.c_ref)); err != nil {
panic(err)
} else {
cf.c_ref = nil
}
}
func (cf *CameraFile) GetPath() (string, error) {
var c_name *C.char
if err := toError(C.gp_file_get_name(cf.c_ref, &c_name)); err != nil {
return "", err
}
return goString(c_name), nil
}
func (cf *CameraFile) GetDataAndSize() ([]byte, error) {
var c_size C.ulong
var c_buf *C.char
if err := toError(C.gp_file_get_data_and_size(cf.c_ref, &c_buf, &c_size)); err != nil {
return nil, err
}
return goBytes(c_buf, int(c_size)), nil
}