forked from elliotchance/c2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
64 lines (53 loc) · 1.5 KB
/
util.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
package noarch
import (
"reflect"
)
// CStringToString returns a string that contains all the bytes in the
// provided C string up until the first NULL character.
func CStringToString(s []byte) string {
if s == nil {
return ""
}
end := -1
for i, b := range s {
if b == 0 {
end = i
break
}
}
if end == -1 {
end = len(s)
}
newSlice := make([]byte, end)
copy(newSlice, s)
return string(newSlice)
}
// CStringIsNull will test if a C string is NULL. This is equivalent to:
//
// s == NULL
func CStringIsNull(s []byte) bool {
if s == nil || len(s) < 1 {
return true
}
return s[0] == 0
}
// CPointerToGoPointer converts a C-style pointer into a Go-style pointer.
//
// C pointers are represented as slices that have one element pointing to where
// the original C pointer would be referencing. This isn't useful if the pointed
// value needs to be passed to another Go function in these libraries.
//
// See also GoPointerToCPointer.
func CPointerToGoPointer(a interface{}) interface{} {
t := reflect.TypeOf(a).Elem()
return reflect.New(t).Elem().Addr().Interface()
}
// GoPointerToCPointer does the opposite of CPointerToGoPointer.
//
// A Go pointer (simply a pointer) is converted back into the original slice
// structure (of the original slice reference) so that the calling functions
// will be able to see the new data of that pointer.
func GoPointerToCPointer(destination interface{}, value interface{}) {
v := reflect.ValueOf(destination).Elem()
reflect.ValueOf(value).Index(0).Set(v)
}