-
Notifications
You must be signed in to change notification settings - Fork 1
/
MenuFile.go
97 lines (81 loc) · 1.94 KB
/
MenuFile.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
package irbis
import (
"strings"
)
// MenuEntry представляет собой пару строк в MNU-файле.
type MenuEntry struct {
Code string // Условный код
Comment string // Комментарий к коду
}
func NewMenuEntry(code, comment string) *MenuEntry {
result := new(MenuEntry)
result.Code = code
result.Comment = comment
return result
}
func (entry *MenuEntry) String() string {
return entry.Code + " - " + entry.Comment
}
type MenuFile struct {
Entries []*MenuEntry
}
func (menu *MenuFile) Add(code, comment string) *MenuFile {
entry := NewMenuEntry(code, comment)
menu.Entries = append(menu.Entries, entry)
return menu
}
func (menu *MenuFile) Clear() *MenuFile {
menu.Entries = []*MenuEntry{}
return menu
}
func (menu *MenuFile) GetEntry(code string) *MenuEntry {
if menu.Entries == nil {
return nil
}
for _, entry := range menu.Entries {
if SameString(entry.Code, code) {
return entry
}
}
code = strings.TrimSpace(code)
for _, entry := range menu.Entries {
if SameString(entry.Code, code) {
return entry
}
}
code = strings.Trim(code, "-=:")
for _, entry := range menu.Entries {
if SameString(entry.Code, code) {
return entry
}
}
return nil
}
func (menu *MenuFile) GetValue(code, defaultValue string) string {
entry := menu.GetEntry(code)
if entry == nil {
return defaultValue
}
return entry.Comment
}
func (menu *MenuFile) Parse(lines []string) {
menu.Entries = make([]*MenuEntry, 0, len(lines)/2)
length := len(lines)
for i := 0; i < length; i += 2 {
code := lines[i]
if len(code) == 0 || strings.HasPrefix(code, "*****") {
break
}
comment := lines[i+1]
entry := NewMenuEntry(code, comment)
menu.Entries = append(menu.Entries, entry)
}
}
func (menu *MenuFile) String() string {
result := strings.Builder{}
for _, entry := range menu.Entries {
result.WriteString(entry.String() + "\n")
}
result.WriteString("*****\n")
return result.String()
}