-
Notifications
You must be signed in to change notification settings - Fork 2
/
io.go
52 lines (47 loc) · 1.15 KB
/
io.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
package gogd
/*
#include <gd.h>
#include <gd_color_map.h>
#cgo LDFLAGS: -lgd
extern int gogd_get_c(struct gdIOCtx *);
extern int gogd_get_buf(struct gdIOCtx *, const void*, int);
extern int gogd_put_c(struct gdIOCtx *, int);
extern int gogd_put_buf(struct gdIOCtx *, const void*, int);
extern void gogd_error(int, const char*, va_list);
void gogd_error_setup() {
gdSetErrorMethod(gogd_error);
}
*/
import "C"
import "unsafe"
func init() {
C.gogd_error_setup()
}
// GetContext makes a gdio context
func getContext(g gdio) *IOCtx {
return (*IOCtx)(unsafe.Pointer(&C.gdIOCtx{
getC: (*[0]byte)(C.gogd_get_c),
getBuf: (*[0]byte)(C.gogd_get_buf),
putC: (*[0]byte)(C.gogd_put_c),
putBuf: (*[0]byte)(C.gogd_put_buf),
data: unsafe.Pointer(&g),
}))
}
func toGdioCtx(i *IOCtx) *C.gdIOCtx {
return (*C.gdIOCtx)(unsafe.Pointer(i))
}
func fromGdioCtx(i *C.gdIOCtx) *IOCtx {
return (*IOCtx)(unsafe.Pointer(i))
}
// IOCbPtr is an unsafe pointer to a callback function
type IOCbPtr unsafe.Pointer
type IOCtx struct {
getC IOCbPtr
getBuf IOCbPtr
putC IOCbPtr
putBuf IOCbPtr
seek IOCbPtr
tell IOCbPtr
gd_free IOCbPtr
data unsafe.Pointer
}