-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheapi.go
131 lines (109 loc) · 3.16 KB
/
eapi.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
package pkgcraft
// #cgo pkg-config: pkgcraft
// #include <pkgcraft.h>
import "C"
import (
"unsafe"
)
var EAPIS_OFFICIAL = getOfficialEapis()
var EAPIS = getEapis()
var EAPI_LATEST_OFFICIAL *Eapi
var EAPI_LATEST *Eapi
// Convert an array of Eapi pointers to a slice of Eapi objects.
func eapisToSlice(c_eapis []*C.Eapi, start int) []*Eapi {
var eapis []*Eapi
for i, ptr := range c_eapis {
if i >= start {
s := C.pkgcraft_eapi_as_str(ptr)
id := C.GoString(s)
defer C.pkgcraft_str_free(s)
eapis = append(eapis, &Eapi{ptr, id})
}
}
return eapis
}
// Return the mapping of all official EAPIs.
func getOfficialEapis() map[string]*Eapi {
var length C.size_t
c_eapis := C.pkgcraft_eapis_official(&length)
eapis := eapisToSlice(unsafe.Slice(c_eapis, length), 0)
C.pkgcraft_array_free((*unsafe.Pointer)(unsafe.Pointer(c_eapis)), length)
// set global alias for the most recent, official EAPI
EAPI_LATEST_OFFICIAL = eapis[len(eapis)-1]
m := make(map[string]*Eapi)
for _, eapi := range eapis {
m[eapi.id] = eapi
}
return m
}
// Return the mapping of all known EAPIs.
func getEapis() map[string]*Eapi {
var eapis []*Eapi
var length C.size_t
c_eapis := C.pkgcraft_eapis(&length)
// copy official Eapi objects
for _, eapi := range EAPIS_OFFICIAL {
eapis = append(eapis, eapi)
}
// append unofficial Eapi objects
unofficial_eapis := eapisToSlice(unsafe.Slice(c_eapis, length), len(eapis))
eapis = append(eapis, unofficial_eapis...)
C.pkgcraft_array_free((*unsafe.Pointer)(unsafe.Pointer(c_eapis)), length)
// set global alias for the most recent EAPI
EAPI_LATEST = unofficial_eapis[len(unofficial_eapis)-1]
m := make(map[string]*Eapi)
for _, eapi := range eapis {
m[eapi.id] = eapi
}
return m
}
// Convert an EAPI range into an array of Eapi objects.
func EapiRange(s string) ([]*Eapi, error) {
var length C.size_t
cstr := C.CString(s)
defer C.free(unsafe.Pointer(cstr))
c_eapis := C.pkgcraft_eapis_range(cstr, &length)
if c_eapis == nil {
return nil, newPkgcraftError()
}
var eapis []*Eapi
for _, ptr := range unsafe.Slice(c_eapis, length) {
s := C.pkgcraft_eapi_as_str(ptr)
id := C.GoString(s)
defer C.pkgcraft_str_free(s)
eapis = append(eapis, EAPIS[id])
}
return eapis, nil
}
type Eapi struct {
ptr *C.Eapi
// cached fields
id string
}
// Return the string for an EAPI.
func (self *Eapi) String() string {
return self.id
}
// Check if an EAPI has a given feature.
func (self *Eapi) Has(s string) bool {
cstr := C.CString(s)
defer C.free(unsafe.Pointer(cstr))
return bool(C.pkgcraft_eapi_has(self.ptr, cstr))
}
// Get an EAPI's dependency keys.
func (self *Eapi) DepKeys() []string {
var length C.size_t
ptr := C.pkgcraft_eapi_dep_keys(self.ptr, &length)
return charArrayToSlice(ptr, length)
}
// Get an EAPI's metadata keys.
func (self *Eapi) MetadataKeys() []string {
var length C.size_t
ptr := C.pkgcraft_eapi_metadata_keys(self.ptr, &length)
return charArrayToSlice(ptr, length)
}
// Compare an Eapi with another Eapi chronologically returning -1, 0, or 1 if
// the first is less than, equal to, or greater than the second, respectively.
func (self *Eapi) Cmp(other *Eapi) int {
return int(C.pkgcraft_eapi_cmp(self.ptr, other.ptr))
}