-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpv.go
169 lines (148 loc) · 3.46 KB
/
cpv.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
161
162
163
164
165
166
167
168
169
package pkgcraft
// #cgo pkg-config: pkgcraft
// #include <pkgcraft.h>
import "C"
import (
"runtime"
"unsafe"
)
type Cpv struct {
ptr *C.Cpv
// cached fields
_category string
_package string
_version *Version
_hash uint64
}
type cpvPtr interface {
p() *C.Cpv
}
func cpvFromPtr(ptr *C.Cpv) (*Cpv, error) {
if ptr != nil {
cpv := &Cpv{ptr: ptr}
runtime.SetFinalizer(cpv, func(self *Cpv) { C.pkgcraft_cpv_free(self.ptr) })
return cpv, nil
} else {
return nil, newPkgcraftError()
}
}
// Parse a string into a Cpv object.
func NewCpv(s string) (*Cpv, error) {
c_str := C.CString(s)
defer C.free(unsafe.Pointer(c_str))
ptr := C.pkgcraft_cpv_new(c_str)
return cpvFromPtr(ptr)
}
func (self *Cpv) p() *C.Cpv {
return self.ptr
}
// Return an Cpv's category.
func (self *Cpv) Category() string {
if self._category == "" {
s := C.pkgcraft_cpv_category(self.ptr)
defer C.pkgcraft_str_free(s)
self._category = C.GoString(s)
}
return self._category
}
// Return a Cpv's package name.
func (self *Cpv) Package() string {
if self._package == "" {
s := C.pkgcraft_cpv_package(self.ptr)
defer C.pkgcraft_str_free(s)
self._package = C.GoString(s)
}
return self._package
}
// Return a Cpv's version.
func (self *Cpv) Version() *Version {
if self._version == nil {
ptr := C.pkgcraft_cpv_version(self.ptr)
if ptr != nil {
self._version, _ = versionFromPtr(ptr)
} else {
self._version = &Version{}
}
}
return self._version
}
// Return a Cpv's revision.
func (self *Cpv) Revision() *Revision {
version := self.Version()
if *version != (Version{}) {
return version.Revision()
}
return &Revision{}
}
// Return a Cpv's package and version.
func (self *Cpv) P() string {
c_str := C.pkgcraft_cpv_p(self.ptr)
defer C.pkgcraft_str_free(c_str)
return C.GoString(c_str)
}
// Return a Cpv's package, version, and revision.
func (self *Cpv) Pf() string {
c_str := C.pkgcraft_cpv_pf(self.ptr)
defer C.pkgcraft_str_free(c_str)
return C.GoString(c_str)
}
// Return a Cpv's revision.
func (self *Cpv) Pr() string {
c_str := C.pkgcraft_cpv_pr(self.ptr)
if c_str != nil {
defer C.pkgcraft_str_free(c_str)
return C.GoString(c_str)
}
return ""
}
// Return a Cpv's version.
func (self *Cpv) Pv() string {
c_str := C.pkgcraft_cpv_pv(self.ptr)
if c_str != nil {
defer C.pkgcraft_str_free(c_str)
return C.GoString(c_str)
}
return ""
}
// Return a Cpv's version and revision.
func (self *Cpv) Pvr() string {
c_str := C.pkgcraft_cpv_pvr(self.ptr)
if c_str != nil {
defer C.pkgcraft_str_free(c_str)
return C.GoString(c_str)
}
return ""
}
// Return a Cpv object's Cpn.
func (self *Cpv) Cpn() *Cpn {
ptr := C.pkgcraft_cpv_cpn(self.ptr)
cpn, _ := cpnFromPtr(ptr)
return cpn
}
func (self *Cpv) String() string {
s := C.pkgcraft_cpv_str(self.ptr)
defer C.pkgcraft_str_free(s)
return C.GoString(s)
}
func (self *Cpv) Hash() uint64 {
if self._hash == 0 {
self._hash = uint64(C.pkgcraft_cpv_hash(self.ptr))
}
return self._hash
}
// Compare two deps returning -1, 0, or 1 if the first is less than, equal to,
// or greater than the second, respectively.
func (self *Cpv) Cmp(other *Cpv) int {
return int(C.pkgcraft_cpv_cmp(self.ptr, other.ptr))
}
// Determine if two Cpv or Dep objects intersect.
func (self *Cpv) Intersects(other interface{}) bool {
switch other := other.(type) {
case *Cpv:
return bool(C.pkgcraft_cpv_intersects(self.ptr, other.ptr))
case *Dep:
return bool(C.pkgcraft_cpv_intersects_dep(self.ptr, other.ptr))
default:
return false
}
}