-
Notifications
You must be signed in to change notification settings - Fork 1
/
songbook.go
177 lines (150 loc) · 3.35 KB
/
songbook.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
170
171
172
173
174
175
176
177
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
)
//Structs used when parsing a song file
type Songbook struct {
FixedOrder bool
UseSection bool
IndexChorus bool
IndexPosition int
Filename string
Title string
Songs map[int]Song
}
func (sb Songbook) IndexAtStart() bool {
return sb.IndexPosition == IndexStart
}
func (sb Songbook) IndexAtEnd() bool {
return sb.IndexPosition == IndexEnd
}
func (sb Songbook) NoIndex() bool {
return sb.IndexPosition == IndexNone
}
const (
IndexNone = 0
IndexStart = 1
IndexEnd = 2
)
func ParseSongbookFile(filename string, songs_root string) (*Songbook, error) {
file, err := os.Open(filename)
filename = filepath.Base(filename)
title := filename[0 : len(filename)-len(".songlist")]
if err != nil {
return nil, err
}
defer file.Close()
var (
scanner = bufio.NewScanner(file)
fixed_order = false
use_section = false
use_chorus = false
index_pos = IndexNone
songs = make(map[int]Song)
)
//bad_command_regex := regexp.MustCompile("\\{|\\}")
for scanner.Scan() {
line := scanner.Text()
//is this a command
if strings.HasPrefix(line, "{") {
command := strings.ToLower(line)
if strings.HasPrefix(command, "{title:") {
title = parseCommand(line)
continue
} else if strings.HasPrefix(command, "{fixed_order}") {
fixed_order = true
continue
} else if strings.HasPrefix(command, "{index_use_sections}") {
use_section = true
continue
} else if strings.HasPrefix(command, "{index_use_chorus}") {
use_chorus = true
continue
} else if strings.HasPrefix(command, "{index_position:") {
p := parseCommand(line)
switch p {
case "start":
index_pos = IndexStart
break
case "end":
index_pos = IndexEnd
break
case "none":
default:
index_pos = IndexNone
}
continue
} else {
fmt.Printf("Unknown tag: %s\n", line)
continue
}
}
line = strings.TrimSpace(line)
//ignore blank lines
if len(line) > 0 {
num := -1
//check for fixed numbering
if strings.Index(line, ",") > 0 {
num_str := line[0:strings.Index(line, ",")]
num, err = strconv.Atoi(num_str)
if err == nil {
line = line[len(num_str)+1 : len(line)]
} else {
num = -1
}
}
//including '.song' extension is optional
if !strings.HasSuffix(line, ".song") {
line += ".song"
}
song, err := ParseSongFile(songs_root+"/"+line, 0)
if err != nil {
fmt.Println(num, ":", err)
} else {
if num < 0 {
num = len(songs) + 1
}
song.SongNumber = num
songs[num] = *song
}
}
}
return &Songbook{
Title: title,
FixedOrder: fixed_order,
Filename: filename,
UseSection: use_section,
IndexChorus: use_chorus,
IndexPosition: index_pos,
Songs: songs},
nil
}
func GetSongOrder(sbook *Songbook) (keys []int) {
keys = make([]int, len(sbook.Songs))
i := 0
for k := range sbook.Songs {
keys[i] = k
i++
}
sort.Sort(sort.IntSlice(keys))
return keys
}
func GetSongSlice(sbook *Songbook) (songs []Song) {
keys := GetSongOrder(sbook)
songs = make([]Song, len(sbook.Songs))
ind := 0
for _, k := range keys {
songs[ind] = sbook.Songs[k]
ind++
}
return songs
}
func (sbook Songbook) Link() string {
return sbook.Filename[0 : len(sbook.Filename)-9]
}