-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_test.go
142 lines (120 loc) · 3.14 KB
/
example_test.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
package wpk_test
import (
"fmt"
"log"
"os"
"strings"
"github.com/schwarzlichtbezirk/wpk"
)
func ExampleFTT_GetInfo() {
var err error
// Open package files tags table
var pkg = wpk.NewPackage()
if err = pkg.OpenFile("example.wpk"); err != nil {
log.Fatal(err)
}
// Format package information
var items = []string{
fmt.Sprintf("records: %d", pkg.TagsetNum()),
fmt.Sprintf("datasize: %d", pkg.DataSize()),
}
if str, ok := pkg.GetInfo().TagStr(wpk.TIDlabel); ok {
items = append(items, fmt.Sprintf("label: %s", str))
}
if str, ok := pkg.GetInfo().TagStr(wpk.TIDlink); ok {
items = append(items, fmt.Sprintf("link: %s", str))
}
log.Println(strings.Join(items, ", "))
}
func ExampleFTT_SetInfo() {
var err error
var f *os.File
var pkg = wpk.NewPackage()
const (
label = "empty-package"
link = "github.com/schwarzlichtbezirk/wpk"
author = "schwarzlichtbezirk"
)
// open temporary file for read/write
if f, err = os.OpenFile("example.wpk", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
log.Fatal(err)
}
defer f.Close()
// starts new package
if err = pkg.Begin(f, nil); err != nil {
log.Fatal(err)
}
// put package info somewhere at the code before finalize
pkg.SetInfo(wpk.TagsetRaw{}.
Put(wpk.TIDlabel, wpk.StrTag(label)).
Put(wpk.TIDlink, wpk.StrTag(link)).
Put(wpk.TIDauthor, wpk.StrTag(author)))
// finalize at the end
if err = pkg.Sync(f, nil); err != nil {
log.Fatal(err)
}
}
func ExamplePackage_Enum() {
var err error
// Open package files tags table
var pkg = wpk.NewPackage()
if err = pkg.OpenFile("example.wpk"); err != nil {
log.Fatal(err)
}
// How many unique records in package
var m = map[uint]wpk.Void{}
pkg.Enum(func(fkey string, ts wpk.TagsetRaw) bool {
if offset, ok := ts.TagUint(wpk.TIDoffset); ok {
m[offset] = wpk.Void{} // count unique offsets
}
return true
})
log.Printf("total %d unique files and %d aliases in package", len(m), pkg.TagsetNum()-len(m))
}
func ExamplePackage_Glob() {
var err error
// Open package files tags table
var pkg = wpk.NewPackage()
if err = pkg.OpenFile("example.wpk"); err != nil {
log.Fatal(err)
}
// Get all JPEG-files in subdirectories
var res []string
if res, err = pkg.Glob("*/*.jpg"); err != nil {
log.Fatal(err)
}
// and print them
for _, fname := range res {
log.Println(fname)
}
}
func ExampleGetPackageInfo() {
// list of packages to get info
var list = []string{
"example1.wpk", "example2.wpk", "example3.wpk",
}
for _, fname := range list {
func() {
var err error
// open package file
var f wpk.RFile
if f, err = os.Open(fname); err != nil {
log.Printf("can not open package %s, %s", fname, err.Error())
return
}
defer f.Close()
// get quick package info
var hdr wpk.Header
var info wpk.TagsetRaw
if hdr, info, err = wpk.GetPackageInfo(f); err != nil {
log.Printf("can not get package info %s, %s", fname, err.Error())
return
}
// print package info
var label, _ = info.TagStr(wpk.TIDlabel)
log.Printf("package %s info: %d records, %d data size, %d info notes, label '%s'",
fname, hdr.Count(), hdr.DataSize(), info.Num(), label)
}()
}
}
// The End.