forked from mrosset/go-alpm
-
Notifications
You must be signed in to change notification settings - Fork 4
/
types.go
92 lines (77 loc) · 1.64 KB
/
types.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
package alpm
// #cgo CFLAGS: -D_FILE_OFFSET_BITS=64
// #include <alpm.h>
import "C"
import (
"reflect"
"unsafe"
)
// Description of a dependency.
type Depend struct {
Name string
Version string
Mod DepMod
}
func convertDepend(dep *C.alpm_depend_t) Depend {
return Depend{
Name: C.GoString(dep.name),
Version: C.GoString(dep.version),
Mod: DepMod(dep.mod)}
}
func (dep Depend) String() string {
return dep.Name + dep.Mod.String() + dep.Version
}
// Description of package files.
type File struct {
Name string
Size int64
Mode uint32
}
func convertFilelist(files *C.alpm_filelist_t) []File {
size := int(files.count)
items := make([]File, size)
raw_items := reflect.SliceHeader{
Len: size,
Cap: size,
Data: uintptr(unsafe.Pointer(files.files))}
c_files := *(*[]C.alpm_file_t)(unsafe.Pointer(&raw_items))
for i := 0; i < size; i++ {
items[i] = File{
Name: C.GoString(c_files[i].name),
Size: int64(c_files[i].size),
Mode: uint32(c_files[i].mode)}
}
return items
}
// Internal alpm list structure.
type list struct {
Data unsafe.Pointer
Prev *list
Next *list
}
// Iterates a function on a list and stop on error.
func (l *list) forEach(f func(unsafe.Pointer) error) error {
for ; l != nil; l = l.Next {
err := f(l.Data)
if err != nil {
return err
}
}
return nil
}
type StringList struct {
*list
}
func (l StringList) ForEach(f func(string) error) error {
return l.forEach(func(p unsafe.Pointer) error {
return f(C.GoString((*C.char)(p)))
})
}
func (l StringList) Slice() []string {
slice := []string{}
l.ForEach(func(s string) error {
slice = append(slice, s)
return nil
})
return slice
}