-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproperties.go
117 lines (89 loc) · 2.34 KB
/
properties.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
package xar
// #include <stdlib.h>
// #include <xar/xar.h>
import "C"
import "unsafe"
func (f *File) SetProperty(key, value string) error {
cKey := C.CString(key)
defer C.free(unsafe.Pointer(cKey))
cValue := C.CString(value)
defer C.free(unsafe.Pointer(cValue))
if C.xar_prop_set(f.file, cKey, cValue) != 0 {
return ErrNonZero
}
return nil
}
func (f *File) CreateProperty(key, value string) error {
cKey := C.CString(key)
defer C.free(unsafe.Pointer(cKey))
cValue := C.CString(value)
defer C.free(unsafe.Pointer(cValue))
if C.xar_prop_create(f.file, cKey, cValue) != 0 {
return ErrNonZero
}
return nil
}
func (f *File) GetProperty(key string) (string, error) {
cKey := C.CString(key)
defer C.free(unsafe.Pointer(cKey))
var buffer *C.char
if C.xar_prop_get(f.file, cKey, &buffer) != 0 {
return "", ErrNonZero
}
return C.GoString(buffer), nil
}
func (f *File) ListProperties() []string {
iter := C.xar_iter_new()
if iter == nil {
panic(ErrNil) // memory allocation failed
}
defer C.xar_iter_free(iter)
list := []string{}
next := C.xar_prop_first(f.file, iter)
for next != nil {
list = append(list, C.GoString(next))
next = C.xar_prop_next(iter)
}
return list
}
func (f *File) RemoveProperty(key string) {
cKey := C.CString(key)
defer C.free(unsafe.Pointer(cKey))
C.xar_prop_unset(f.file, cKey)
}
func (f *File) GetPropertyAttr(prop, attr string) string {
cProp := C.CString(prop)
defer C.free(unsafe.Pointer(cProp))
cAttr := C.CString(attr)
defer C.free(unsafe.Pointer(cAttr))
result := C.xar_attr_get(f.file, cProp, cAttr)
return C.GoString(result)
}
func (f *File) SetPropertyAttr(prop, attr, value string) error {
cProp := C.CString(prop)
defer C.free(unsafe.Pointer(cProp))
cAttr := C.CString(attr)
defer C.free(unsafe.Pointer(cAttr))
cValue := C.CString(value)
defer C.free(unsafe.Pointer(cValue))
if C.xar_attr_set(f.file, cProp, cAttr, cValue) != 0 {
return ErrNonZero
}
return nil
}
func (f *File) ListPropertyAttr(prop string) []string {
cProp := C.CString(prop)
defer C.free(unsafe.Pointer(cProp))
iter := C.xar_iter_new()
if iter == nil {
panic(ErrNil) // memory allocation failed
}
defer C.xar_iter_free(iter)
list := []string{}
next := C.xar_attr_first(f.file, cProp, iter)
for next != nil {
list = append(list, C.GoString(next))
next = C.xar_attr_next(iter)
}
return list
}