-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.go
62 lines (53 loc) · 1.38 KB
/
library.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
package guerillaradio
import "math/rand"
import "os"
import "path/filepath"
import "fmt"
type Library struct {
documents []SourceDocument
}
func (library *Library) Size() int {
return len(library.documents)
}
func (library *Library) AddSourceDocument(filename string) (err error) {
source := SourceDocument{FileName: filename}
err = source.ReadFile()
if err != nil {
return
}
library.documents = append(library.documents, source)
return
}
func (library *Library) AddSourceDocuments(filenames []string) (num_files int, err error) {
num_files = 0
for _, filename := range filenames {
err = library.AddSourceDocument(filename)
if err != nil {
return
}
num_files++
}
return
}
func (library *Library) RetrieveLines(number_of_lines int) (lines []string) {
index := rand.Intn(library.Size())
source := library.documents[index]
lines, lines_returned := source.RetrieveLines(number_of_lines)
if lines_returned < number_of_lines {
lines = append(lines, library.RetrieveLines(number_of_lines-lines_returned)...)
}
return
}
func (library *Library) AddDirectory(name string) (err error) {
walkTextTree := func(rootpath string, info os.FileInfo, err error) error {
if !info.IsDir() {
err := library.AddSourceDocument(rootpath)
if err != nil {
fmt.Printf("Unable to add %v due to: %v", rootpath, err)
}
}
return err
}
err = filepath.Walk(name, walkTextTree)
return
}