-
Notifications
You must be signed in to change notification settings - Fork 72
/
SettingRW.ahk
381 lines (300 loc) · 9.96 KB
/
SettingRW.ahk
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
SettingLoad(SettingFilePath) {
FileOutput := FileRead(SettingFilePath)
return FileOutput
}
SettingAddSection(SettingData,SectionName,SectionData,IncludeBrackets:=True) {
CurSec := SettingGetSection(SettingData,SectionName,IncludeBrackets)
If (IncludeBrackets) {
SectionName := "[" SectionName "]"
}
; MsgBox % SettingData
TempBlank := Trim(StrReplace(SettingData,SectionName "`r`n" CurSec,""),OmitChars:=" `t`r`n")
NewSetData := Trim(TempBlank,OmitChars:=" `t`r`n") "`r`n`r`n" SectionName "`r`n" SectionData
return NewSetData
}
SettingRemoveSection(SettingData,SectionName,IncludeBrackets:=True) {
CurSec := SettingGetSection(SettingData,SectionName,IncludeBrackets)
If (IncludeBrackets) {
SectionName := "[" SectionName "]"
}
TempBlank := Trim(StrReplace(SettingData,SectionName "`r`n" CurSec,""),OmitChars:=" `t`r`n")
return TempBlank
}
SettingGetSectionList(SettingData,sFilter:="",IncludeBrackets:=True) {
ReadSection := false
SectionList := ""
Loop Parse SettingData, "`r", "`n"
{
If (A_Index = 1) {
CurSection := A_LoopField
CurSection := StrReplace(StrReplace(CurSection,"[",""),"]","")
} Else {
CurSection := ""
If (IncludeBrackets) {
If (InStr(A_LoopField,"[") = 1 And InStr(A_LoopField,"]") = StrLen(A_LoopField)) {
CurSection := A_LoopField
CurSection := StrReplace(StrReplace(CurSection,"[",""),"]","")
}
} Else {
If (ReadSection) {
CurSection := A_LoopField
ReadSection := false
}
If (A_LoopField = "") {
ReadSection := true
}
}
}
If (SectionList = "" And CurSection != "") {
SectionList := CurSection
} Else If (CurSection != "") {
SectionList := SectionList "|" CurSection
}
}
If (sFilter = "") {
FinalList := SectionList
} Else {
Loop Parse SectionList, "|"
{
If (InStr(A_LoopField,sFilter) > 0) {
FinalList .= A_LoopField "|"
}
}
}
FinalList := Sort(FinalList,"U D|")
return FinalList
}
SettingGetSection(SettingData,SectionName,IncludeBrackets:=True) {
ReadData := False
If (IncludeBrackets) {
SectionConv := "[" SectionName "]"
} Else {
SectionConv := SectionName
}
Loop Parse SettingData, "`r", "`n"
{
If (ReadData) {
If (IncludeBrackets) {
If (SubStr(A_LoopField,1,1) = "[") {
Break
}
} Else {
If (A_LoopField = "") {
Break
}
}
SectionFilter .= A_LoopField "`r`n"
}
If (SectionConv = A_LoopField) {
ReadData := True
}
}
; SectionFilter := SectionConv "`r`n" SectionFilter
return Trim(SectionFilter,OmitChars:=" `r`n`t")
}
SettingReadSectionItem(SettingData,SectionName,ItemName,IncludeBrackets:=True) {
CurSec := SettingGetSection(SettingData,SectionName,IncludeBrackets)
; MsgBox % CurSec
ItemData := SettingRead(CurSec,ItemName,"=")
return ItemData
}
SettingAddSectionItem(SettingData,SectionName,ItemName,ItemValue,IncludeBrackets:=True) {
CurSec := SettingGetSection(SettingData,SectionName,IncludeBrackets)
; MsgBox % "CurSec: " "`r`n`r`n" CurSec
NewCurSec := SettingUpdate(CurSec,ItemName,ItemValue,"=")
; MsgBox % "NewCurSec: " "`r`n`r`n" NewCurSec
If (IncludeBrackets) {
SectionName := "[" SectionName "]"
}
CurSec := SectionName "`r`n" Trim(CurSec,OmitChars:=" `t`r`n")
; MsgBox % "CurSec2:" "`r`n`r`n" CurSec
TempBlank := Trim(StrReplace(SettingData,CurSec,""),OmitChars:=" `t`r`n")
; MsgBox % "TempBlank: " "`r`n`r`n" TempBlank
NewSetData := Trim(TempBlank,OmitChars:=" `t`r`n") "`r`n`r`n" SectionName "`r`n" NewCurSec
; MsgBox % "NewSetData: " "`r`n`r`n" NewSetData
return NewSetData
}
SettingTotalLines(SettingData) {
i := 0
Loop Parse SettingData, "`r", "`n"
i++
return i
}
SettingStripComments(SettingData) {
Loop Parse SettingData, "`r", "`n"
{
If (SubStr(A_LoopField,1,1) != "#") {
NewSettingData .= A_LoopField . "`r`n"
}
}
return NewSettingData
}
SettingRead(SettingData,SettingName,Sep,TrimOutput:=False) {
SettingValue := ""
Loop Parse SettingData, "`r", "`n"
{
If (SubStr(A_LoopField,1,1) != "#") { ; ignore comments starting with #
If (Trim(GetTok(A_LoopField,Sep,1)) = SettingName) {
TotTok := NumTok(A_LoopField,Sep)
sRange := "2-" TotTok
If (TrimOutput = True) {
SettingValue := Trim(GetTok(A_LoopField,Sep,sRange))
} Else {
SettingValue := GetTok(A_LoopField,Sep,sRange)
}
Break
}
}
}
return SettingValue
}
SettingUpdate(SettingData,SettingName,SettingValue,Sep) {
NewSettingData := ""
i := 0
DataAdded := 0
SettingData := StrReplace(StrReplace(SettingData,"`r`n`r`n","`r`n"),"`r`n`r`n","`r`n")
SettingData := Trim(SettingData,OmitChars := " `t`r`n")
; don't forget to do this:
; SettingVar := SettingUpdate(...)
Loop Parse SettingData, "`r", "`n"
{
If (SubStr(A_LoopField,1,1) != "#") {
TotTok := NumTok(A_LoopField,Sep)
CurName := GetTok(A_Loopfield,Sep,"1")
CurValue := GetTok(A_LoopField,Sep,"2-" . TotTok)
If (CurName = SettingName) {
DataAdded := 1
SettingLine := CurName Sep SettingValue
} Else {
SettingLine := CurName Sep CurValue
}
If (NewSettingData = "") {
NewSettingData := SettingLine
} Else {
NewSettingData := NewSettingData "`r`n" SettingLine
}
}
i++
}
If (DataAdded = 0) {
If (NewSettingData = "") {
NewSettingData := SettingName Sep SettingValue
} Else {
NewSettingData .= "`r`n" SettingName Sep SettingValue
}
}
; no more remove blank lines
; NewSettingData := Trim(StrReplace(NewSettingData,"`r`n`r`n","`r`n"),OmitChars := " `t`r`n")
NewSettingData := Trim(NewSettingData,OmitChars := " `t`r`n")
; don't forget to do this:
; SettingVar := SettingUpdate(...)
return NewSettingData
}
SettingWrite(SettingFilePath,SettingData,RemoveBlankLines:=False) {
BackupFolder := A_WorkingDir . "\Backup"
SettingFileName := FileFromPath(SettingFilePath)
SettingFolder := PathToFile(SettingFilePath)
SettingFileTitle := FileTitle(SettingFileName)
SettingFileTemp := SettingFileTitle "Temp"
; ErrResult := MakeFolderCheck(BackupFolder)
SettingBackup := SettingData
; remove blank lines
If (RemoveBlankLines) {
SettingData := StrReplace(StrReplace(SettingData,"`r`n`r`n","`r`n"),"`r`n`r`n","`r`n")
}
SettingData := Trim(SettingData,OmitChars := " `r`n`t")
FileMove SettingFilePath, SettingFolder SettingFileTemp ".txt"
If (ErrorLevel > 0) {
MsgBox("Backing up setting file failed!`r`n`r`n" SettingFolder SettingFileTemp ".txt")
return
}
FileAppend SettingData, SettingFilePath
If (ErrorLevel > 0) {
MsgBox("Writing setting file failed!`r`n`r`n" SettingFilePath)
return
}
FileDelete SettingFolder SettingFileTemp ".txt"
If (ErrorLevel > 0) {
MsgBox("Removing backup file failed!`r`n`r`n" SettingFolder SettingFileTemp ".txt")
return
}
}
SettingDelete(SettingData,SettingName,Sep) {
NewData := ""
Loop Parse SettingData, "`r", "`n"
{
If (A_LoopField != "") {
CurName := GetTok(A_LoopField,Sep,1)
CurValue := GetTok(A_LoopField,Sep,"2-")
;MsgBox % CurName . " / " . SettingName
If (CurName != SettingName) {
NewData .= CurName Sep CurValue "`r`n"
}
}
}
return NewData
}
SettingLine(SettingData,LineNum) {
LineValue := ""
Loop Parse SettingData, "`r", "`n"
{
LineValue := A_LoopField
If (LineNum = A_Index)
Break
}
return LineValue
}
LineDelete(SettingData,IndexVal,NoBlanks:=False) {
NewData := ""
If (NoBlanks = True) {
Loop Parse SettingData, "`r", "`n"
{
If (A_Index != "" And A_LoopField != IndexVal) {
NewData .= A_LoopField "`r`n"
}
}
} Else {
Loop Parse SettingData, "`r", "`n"
{
If (A_Index != IndexVal)
NewData .= A_LoopField "`r`n"
}
}
return NewData
}
MakeFolderCheck(FolderName) {
If (FileExist(FolderName)="") {
DirCreate FolderName
}
If (ErrorLevel > 0) {
MsgBox("There was an error: " A_LastError " / " ErrorLevel "`r`n`r`n" "Can't make folder:" "`r`n`r`n" FolderName)
}
return ErrorLevel
}
; FileEncoding, CP65001 ; UTF-8
; FileEncoding, CP1200 ; UTF-16
WriteMultiByte(sFilePath,sFileData) {
FileEncoding "CP65001" ; UTF-8
fo := FileOpen(sFilePath,"w")
If !IsObject(fo) {
MsgBox("Can't open " sFilePath " for writing.")
return
}
fo.Write(sFileData)
fo.Close()
return ErrorLevel
}
ReadMultiByte(sFilePath,lNumChars:=0) {
FileEncoding "CP65001" ; UTF-8
fo := FileOpen(sFilePath,"rw")
If !IsObject(fo) {
MsgBox("Can't open " sFilePath " for writing.")
return
}
If (lNumChars=0) {
sString := fo.Read()
} Else {
sString := fo.Read(lNumChars)
}
return ErrorLevel
}