forked from frizinak/gphoto2go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gp_camera_filepath.go
160 lines (131 loc) · 2.79 KB
/
gp_camera_filepath.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
package gphoto2
// #cgo pkg-config: libgphoto2
// #include <gphoto2.h>
// #include <stdlib.h>
import "C"
import (
"io"
"path"
"unsafe"
)
type CameraFilePath struct {
cam *Camera
folder string
file string
}
func newCameraFilePath(cam *Camera, folder, file string) CameraFilePath {
cf := CameraFilePath{
cam: cam,
folder: folder,
file: file,
}
return cf
}
func (cf CameraFilePath) cFolderFile() (c_folder, c_file *C.char, free func()) {
c_folder, c_folder_free := cString(cf.folder)
c_file, c_file_free := cString(cf.file)
return c_folder, c_file, func() {
c_folder_free()
c_file_free()
}
}
func (cf CameraFilePath) Folder() string {
return cf.folder
}
func (cf CameraFilePath) Name() string {
return cf.file
}
func (cf CameraFilePath) Path() string {
return path.Join(cf.Folder(), cf.Name())
}
func (cf CameraFilePath) Delete() error {
c_folder, c_file, c_ff_free := cf.cFolderFile()
defer c_ff_free()
return toError(C.gp_camera_file_delete(cf.cam.c_ref, c_folder, c_file, cf.cam.ctx.c_ref))
}
type CameraFileInfo struct {
File CameraFileInfoFile
}
type CameraFileInfoFile struct {
Size int64
MTime int64
Width, Height int
}
func (cf CameraFilePath) GetInfo() (*CameraFileInfo, error) {
c_info := new(C.CameraFileInfo)
c_folder, c_file, c_ff_free := cf.cFolderFile()
defer c_ff_free()
if err := toError(C.gp_camera_file_get_info(
cf.cam.c_ref,
c_folder,
c_file,
c_info,
cf.cam.ctx.c_ref,
)); err != nil {
return nil, err
}
return &CameraFileInfo{
File: CameraFileInfoFile{
Size: int64(c_info.file.size),
MTime: int64(c_info.file.mtime),
Width: int(c_info.file.width),
Height: int(c_info.file.height),
},
}, nil
}
func (cf CameraFilePath) ReadOffsetSize(offset, size int) ([]byte, error) {
var (
c_size = cSize(size)
c_offset = cSize(offset)
)
c_buf, c_buf_free := cMalloc(size)
defer c_buf_free()
c_folder, c_file, c_ff_free := cf.cFolderFile()
defer c_ff_free()
if err := toError(C.gp_camera_file_read(
cf.cam.c_ref,
c_folder,
c_file,
C.GP_FILE_TYPE_NORMAL,
c_offset,
c_buf,
&c_size,
cf.cam.ctx.c_ref,
)); err != nil {
return nil, err
}
var (
buf = goBytes(c_buf, int(c_size))
err error
)
if len(buf) < size {
err = io.EOF
}
return buf, err
}
func (cf CameraFilePath) ReadOffset(offset int, p []byte) (int, error) {
var (
c_size = cSize(len(p))
c_offset = cSize(offset)
)
c_buf := (*C.char)(unsafe.Pointer(&p[0]))
c_folder, c_file, c_ff_free := cf.cFolderFile()
defer c_ff_free()
if err := toError(C.gp_camera_file_read(
cf.cam.c_ref,
c_folder,
c_file,
C.GP_FILE_TYPE_NORMAL,
c_offset,
c_buf,
&c_size,
cf.cam.ctx.c_ref,
)); err != nil {
return 0, err
}
if int(c_size) < len(p) {
return int(c_size), io.EOF
} else {
return int(c_size), nil
}
}