-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIniStore.ahk
140 lines (127 loc) · 3.1 KB
/
IniStore.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
;ini store
class IniStore
{
;statics
static DIR := A_Temp
static SECTION := "main"
;new instance
__New(ini := "", section := "")
{
this._ini := this.getPath(ini)
this._section := this.getSection(section)
}
;get name
getName(name := "")
{
;param value
if (name := Trim(name, " `r`n`t"))
{
name := Trim(RegExReplace(Trim(Format("{:L}", RegExReplace(name, "([A-Z])", "-$1")), " `r`n`t"), "i)[^a-z0-9]", "-"), "-")
if RegExMatch(name, "[^-]")
return name
}
;default value
SplitPath, A_ScriptFullPath,,,, name
name := Trim(RegExReplace(Trim(Format("{:L}", RegExReplace(name, "([A-Z])", "-$1")), " `r`n`t"), "i)[^a-z0-9]", "-"), "-")
if RegExMatch(name, "[^-]")
return name "-ahk"
return "store-ahk"
}
;get path
getPath(ini := "")
{
;param value
if (ini := Trim(ini, " `r`n`t"))
{
SplitPath, ini, fname, dir, ext, name
if (fdir && name)
return fdir "\" name ".ini"
}
;instance value
if (ini := Trim(this._ini, " `r`n`t"))
{
SplitPath, ini, fname, dir, ext, name
if (fdir && name)
return fdir "\" name ".ini"
}
;default value
name := this.getName()
dir := this.base.DIR
if !InStr(FileExist(dir), "D")
dir := A_Temp
return dir "\" name ".ini"
}
;get section
getSection(section := "")
{
;param value
if ((val := Trim(section, " `r`n`t")) && RegExMatch(val, "i)^[-_a-z0-9]+$"))
return val
;instance value
if ((val := Trim(this._section, " `r`n`t")) && RegExMatch(val, "i)^[-_a-z0-9]+$"))
return val
;default value
if ((val := Trim(this.base.SECTION, " `r`n`t")) && RegExMatch(val, "i)^[-_a-z0-9]+$"))
return val
return "main"
}
;key
key(key, throwable := true)
{
if ((val := Trim(key, " `r`n`t")) && RegExMatch(val, "i)^[-_a-z0-9]+$"))
return val
if throwable
throw Exception("Invalid key!", "IniStore -> key: " key, "IniStore key value is invalid.")
}
;value
value(val, parse := false)
{
if (val := Trim(val, " `r`n`t"))
{
if parse
{
StringReplace, val, val, ``r, `r, All
StringReplace, val, val, ``n, `n, All
StringReplace, val, val, ``t, `t, All
}
else {
StringReplace, val, val, `r, ``r, All
StringReplace, val, val, `n, ``n, All
StringReplace, val, val, `t, ``t, All
}
}
return val
}
;read
get(key, default := "", section := "", ini := "", ByRef failure := 0)
{
failure := 0
key := this.key(key)
ini := this.getPath(ini)
section := this.getSection(section)
IniRead, val, % ini, % section, % key
if (ErrorLevel || Format("{:L}", val) == "error")
{
failure := 1
val := default != "" ? this.set(key, default) : default
}
return this.value(val, 1)
}
;write
set(key, value, section := "", ini := "", ByRef failure := 0, throwable := true)
{
failure := 0
key := this.key(key)
val := this.value(value)
ini := this.getPath(ini)
section := this.getSection(section)
IniWrite, % val, % ini, % section, % key
if ErrorLevel
{
failure := 1
if throwable
throw Exception("IniWrite failure!", "IniStore -> set: key=" key ", val=" val ", ini=" ini ", section=" section, "IniStore set key value failed.")
}
else return value
}
}