-
Notifications
You must be signed in to change notification settings - Fork 0
/
setting.go
139 lines (135 loc) · 3.48 KB
/
setting.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
package mydictionary
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
)
// setting
type settingStruct struct {
path string
Collection []struct {
Name string `json:"name"`
FileName string `json:"fileName"`
Readable bool `json:"readable"`
Writable bool `json:"writable"`
OnlineSource string `json:"onlineSource"`
} `json:"collection"`
Dictionary []struct {
Name string `json:"name"`
FileName string `json:"fileName"`
Readable bool `json:"readable"`
Writable bool `json:"writable"`
} `json:"dictionary"`
Online struct {
Mode int `json:"mode"`
modeContent struct {
userNeed bool
noFound bool
anyway bool
}
Service struct {
BingDictionary bool `json:"Bing Dictionary"`
IcibaCollins bool `json:"iCIBA Collins"`
MerriamWebster bool `json:"Merriam Webster"`
//
// NOTE:
//
// 1. Add your services as public members above, like the example below.
// 2. Add corresponding members in file "mydictionary.setting.json".
// For each member, the string of its key should be exactly the same as your service name.
// 3. Do not edit this note.
//
// Example:
//
// Service bool `json:"Service"`
//
} `json:"service"`
Cache struct {
Enable bool `json:"enable"`
ShelfLifeDay int64 `json:"shelfLifeDay"`
} `json:"cache"`
Debug bool `json:"debug"`
} `json:"online"`
}
// read setting
func (setting *settingStruct) Read() (content string, err error) {
var buf []byte
// read
setting.path = workPath + string(filepath.Separator) + "mydictionary.setting.json"
buf, err = ioutil.ReadFile(setting.path)
if err != nil {
return
}
err = json.Unmarshal(buf, setting)
if err != nil {
return
}
// set online
if setting.Online.Service.BingDictionary {
onlineList = append(onlineList, new(BingDictionaryStruct))
}
if setting.Online.Service.IcibaCollins {
onlineList = append(onlineList, new(IcibaCollinsStruct))
}
if setting.Online.Service.MerriamWebster {
onlineList = append(onlineList, new(MerriamWebsterStruct))
}
//
// NOTE:
//
// 1. Add your functions of services above, like the example below.
// 2. Do not edit this note.
//
// Example:
//
// if setting.Online.Service.Service {
// onlineList = append(onlineList, new(Service))
// }
//
if setting.Online.Mode < 0 {
setting.Online.Mode = -setting.Online.Mode
}
switch setting.Online.Mode % 8 {
case 0:
setting.Online.modeContent.userNeed = false
setting.Online.modeContent.noFound = false
setting.Online.modeContent.anyway = false
break
case 1:
setting.Online.modeContent.userNeed = true
setting.Online.modeContent.noFound = false
setting.Online.modeContent.anyway = false
break
case 2:
setting.Online.modeContent.userNeed = false
setting.Online.modeContent.noFound = true
setting.Online.modeContent.anyway = false
break
case 3:
setting.Online.modeContent.userNeed = true
setting.Online.modeContent.noFound = true
setting.Online.modeContent.anyway = false
break
default:
setting.Online.modeContent.userNeed = false
setting.Online.modeContent.noFound = false
setting.Online.modeContent.anyway = true
break
}
buf, err = json.MarshalIndent(setting, "", "\t")
content = string(buf)
return
}
// Write : write setting
func (setting *settingStruct) Write() (err error) {
var buf []byte
// write
buf, err = json.MarshalIndent(setting, "", "\t")
if err != nil {
return
}
os.Remove(setting.path)
err = ioutil.WriteFile(setting.path, buf, 0644)
return
}