-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathini.ahk
249 lines (215 loc) · 7.27 KB
/
ini.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
;Sunday, June 16, 2013
;ini.ahk
;by Brother Gabriel-Marie
;a common library for the usual ini functions
;make sure core.ahk is loaded since it is required - it will only be included again if it hasn't already
#include core.ahk
#include file.ahk
#include text.ahk
;write(whatini, whatsection, whatkey, whatvalue)
;read(whatini, whatsection, whatkey, whatdefault)
;validate(whatini)
;getsectiontext(whatini, whatsection)
;delete(whatini, whatsection, whatkey)
;getsectioncount(whatini)
;getsectionarray(whatini)
;open(whatini)
;readparameter(whatini, whatsection, whatkey, whatparameter=1, whatdelimiter="|")
;writeparameter(whatvalue, whatini, whatsection, whatkey, whatparameter=1, whatdelimiter="|")
;getsectioncount(whatini)
;getsectionarray(whatini)
;--------------------------------------------------------------------------------------------------------------------------------------
class ini
{
write(whatini, whatsection, whatkey, whatvalue, default0=0){
if(!whatkey) ;keep from overwriting the section if the key is empty!!!
return
;send a string for 0 so you can write "0" to the ini file.
if(default0){
if(!whatvalue)
whatvalue := "0"
}
iniwrite, %whatvalue%, %whatini%, %whatsection%, %whatkey%
if ErrorLevel ; i.e. it's not blank or zero.
return false ;failure
else
return true ;success
}
writesection(whatini, whatsection, whatvalue){
iniwrite,%whatvalue%, %whatini%, %whatsection%
if ErrorLevel ; i.e. it's not blank or zero.
return false ;failure
else
return true ;success
}
;returns the text of a specific section without the comments
readsection(whatini, whatsection){
iniread, thissection, %whatini%, %whatsection%
return thissection
}
;returns a list of section names separated by a linefeed `n character
sections(whatini){
iniread, thesenames, %whatini%
return thesenames
}
read(whatini, whatsection, whatkey, whatdefault="", suppress=0){
;if there is an error, the returnvalue will be whatdefault
if(suppress){
if(whatdefault = "")
whatdefault := a_space
if(whatdefault = 0)
whatdefault := "0"
}
iniread, returnvalue, %whatini%, %whatsection%, %whatkey%, %whatdefault%
return returnvalue
}
delete(whatini, whatsection, whatkey){
if(!whatkey) ;keep from overwriting the section if the key is empty!!!
return
inidelete, %whatini%, %whatsection%, %whatkey%
;if no key is specified, the entire section is deleted
if ErrorLevel ; i.e. it's not blank or zero.
return false ;failure
else
return true ;success
}
validate(whatini){
if(FileExist(whatini) ){
return true ;success
}else{
return false ;failure
}
}
open(whatini){
runit("notepad", whatini)
}
;-------------------------------------------------------------------
;INI KEY PARAMETERS
;in an ini key,you can specify parameters like this:
; thiskey=first|second|third
;fetch a particular parameter
readparameter(whatini, whatsection, whatkey, whatparameter=1, whatdefault="", whatdelimiter="|"){
thisstring := this.read(whatini, whatsection, whatkey, whatdefault)
stringsplit, parameter, thisstring, %whatdelimiter%, %a_space%
parameter%whatparameter% := trimmatch(parameter%whatparameter%, "|")
return parameter%whatparameter%
}
;write to a particular parameter
writeparameter(whatvalue, whatini, whatsection, whatkey, whatparameter=1, whatdelimiter="|"){
thisstring := this.read(whatini, whatsection, whatkey, "")
stringsplit, parameter, thisstring, %whatdelimiter%, %a_space%
parameter%whatparameter% := whatvalue
loop %parameter0%{
theseparameters .= parameter%a_index% . whatdelimiter
}
theseparameters := trimmatch(theseparameters, "|")
this.write(whatini, whatsection, whatkey, theseparameters)
}
;return a count of the parameters in the key
countparameters(whatini, whatsection, whatkey, whatdefault=0, whatdelimiter="|"){
parametercount := whatdefault
theseparameters := this.read(whatini, whatsection, whatkey, "ERROR")
stringsplit, parametercount, thisstring, %whatdelimiter%, %a_space%
return parametercount0
}
;return a key name from a known parameter
;In the case of menus, we may know only a certain parameter and have to grab the item's key.
;this will work as long as the parameters for each corresponding keys are unique.
;for example:
;100=first|param1|param2
;101=second|param1|param2
;in these keys, the first parameter is unique, so we can fetch that parameter.
;you can use this in a section of keys that don't have parameters as long as the other keys don't contain the delimiter.
keyfromparameter(whatini, whatsection, whatparameter, whatvalue, whatdelimiter="|"){
sectiontext := this.readsection(whatini, whatsection)
loop, parse, sectiontext, `r, %a_space%
{
stringsplit, thisline, a_loopfield, =, %a_space%`r`n
if(trimtrim(thisline1)){ ;make sure there is a keyname
IfNotInString, thisline2, %whatdelimiter%
continue
stringsplit, parameter, thisline2, %whatdelimiter%, %a_space%
thisvalue := parameter%whatparameter%
if(thisvalue = whatvalue){
return thisline1
}
}
}
return ""
}
countkeys(whatini, whatsection){
sectiontext := this.readsection(whatini, whatsection)
keycount := 0
loop, parse, sectiontext, `r, %a_space%
{
stringsplit, thisline, a_loopfield, =, %a_space%
if(trim(thisline1," `t`r`n")){
keycount++
}
}
return keycount
}
;-------------------------------------------------------------------
;http://stackoverflow.com/questions/15468085/regex-return-ini-section-as-string#15468861
getsectiontextx(whatini, whatsection){
sectiontext := ""
;thisfileencoding := getFileEncoding(whatini) ;cp1252 is my default ANSI encoding
;msgbox, %thisfileencoding%
;curencoding := a_fileencoding
;FileEncoding, UTF-8
fileread, INIall, %whatini%
;FileEncoding := curencoding
inisection := "[" . whatsection . "]"
;iniregex := "ms)(?<=^\[" . whatsection . "\])(?:(?!^\[).)*"
;iniregex := "ms)(?<=^\[" . whatsection . "\])(?:(?!^\[.+\]$).)*"
;iniregex := "(?ms)(?<=^\[" . whatsection . "\])(?:(?!^\[[^]\r\n]+]).)*"
iniregex := "ms)^(?<=\[" . whatsection . "\]\r\n)(?:(?!^\[).)*(?=\r\n)"
;find from the end of inisection to the next section definition or the eof
ismatch := regexmatch(INIall, iniregex, sectiontext) ;get the key and the value whilst excluding the comments
if(!ismatch)
return ""
else
return trimmatch(sectiontext, "`r`n") ;remove a tailing carriage return
}
;sinkfaze's attempt
;Fetches an entire section as a block of text
;however, this will fetch also a trailing carriage return, so we will have to trim it
getsectiontextfull(whatini, whatsection) {
FileRead, INIall, %whatini%
ismatch := RegExMatch(INIall,"`ami)\[" whatsection "\]\v+\K[^\[]+", sectionText)
if(!ismatch)
return ""
else
return trimtrim(sectiontext) ;remove a tailing carriage return
}
;get the count of sections in the file
;a section is a line that that begins with "["
getsectioncount(whatini){
iniall := text.get(whatini)
sectioncount := 0
loop, parse, iniall
{
if(left(trim(a_loopfield), 1) = "["){
sectioncount++
}
}
return sectioncount
}
;gets the list of sections into an array
getsectionarray(whatini){
iniall := text.get(whatini)
sectionarray := object()
sectionindex := 0
loop, parse, iniall, `n, `r
{
if(left(a_loopfield, 1) = "["){
thissection := trim(replacechars(a_loopfield, "[]"))
sectionindex++
sectionarray[sectionindex] := thissection
}else{
continue
}
}
return sectionarray
}
} ;end ini class