-
Notifications
You must be signed in to change notification settings - Fork 71
/
ReadIni.ahk
140 lines (106 loc) · 4.43 KB
/
ReadIni.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
ReadIni(fnSectionName := "",fnVarName := "",fnSkipEmptyValues := 1,fnIniFilePath := "", fnVariablePrefix := "")
{
; reads values from an entire file, a section or a single line in a .ini file, setting a variable equal to the value in the line
; an empty value for fnVarName will read all values in that section
; MsgBox fnSectionName: %fnSectionName%`nfnVarName: %fnVarName%`nfnSkipEmptyValues: %fnSkipEmptyValues%`nfnIniFilePath: %fnIniFilePath%
; declare local, global, static variables
Global
Local ReturnValue, SectionName, VarName, IniFilePath, SectionFirstChar, SectionLastChar, IniFileContents, FoundVar, FoundSection, ThisLine, LineLength, LinePart0, LinePart1, LinePart2, ThisLineFirstChar, ThisLineLastChar, ThisLineIsSectionHeader
Try
{
; set default return value
ReturnValue := 0 ; success
; validate parameters
; initialise variables
SectionName := fnSectionName
VarName := fnVarName
IniFilePath := fnIniFilePath ? fnIniFilePath : StrReplace(StrReplace(A_ScriptFullPath,".ahk",".ini"),".exe",".ini")
If SectionName
If !(SubStr(SectionName,1,1) = "[" && SubStr(StrReverse(SectionName),1,1) = "]")
SectionName := "[" fnSectionName "]"
; read the ini file
FileRead, IniFileContents, %IniFilePath%
If ErrorLevel
Throw, Exception("Unable to read contents of " IniFilePath)
; concatenate lines
IniFileContents := RegExReplace(IniFileContents,"iS)\s*(;.*?)?\r\n\s*,",",")
; replace system variables
ReplaceSystemVariables(IniFileContents)
; parse the required values
FoundSection := 0
Loop, Parse, IniFileContents, `n, `r
{
ThisLine := A_LoopField
; remove trailing comments
LineLength := InStr(ThisLine,A_Space ";") ? InStr(ThisLine,A_Space ";")-1 : StrLen(ThisLine)
ThisLine := SubStr(ThisLine,1,LineLength)
; trim leading and trailing white space
ThisLine := Trim(ThisLine," `t")
; whole line comments
ThisLine := SubStr(ThisLine,1,1) = ";" ? "" : ThisLine
; skip empty lines
If !ThisLine
Continue
; identify section headers
StringLeft, ThisLineFirstChar, ThisLine, 1
StringRight, ThisLineLastChar , ThisLine, 1
ThisLineIsSectionHeader := (ThisLineFirstChar = "[" && ThisLineLastChar = "]") ? 1 : 0
; find the right place in the file
If (ThisLineIsSectionHeader && SectionName && SectionName = ThisLine) ; turn on assignment for required section
FoundSection := 1
If (ThisLineIsSectionHeader && SectionName && SectionName != ThisLine) ; turn off assignment for required section
FoundSection := 0
If !SectionName ; if no section specified, find all
FoundSection := 1
; assign values to variables
LinePart1 := ""
LinePart2 := ""
FoundVar := 0
If (!ThisLineIsSectionHeader && FoundSection)
{
StringReplace, ThisLine, ThisLine, ``=, ¢, UseErrorLevel ; escaped equals sign
StringSplit, LinePart, ThisLine, =, %A_Space%%A_Tab%
StringReplace, LinePart2, LinePart2, ¢, =, All
; skip empty values
If (fnSkipEmptyValues && !LinePart2)
Continue
StringReplace, LinePart2, LinePart2, xCompanyNamex, %CompanyName%, All ; only works where CompanyName is defined in .ini file before xCompanyNamex is used
StringReplace, LinePart2, LinePart2, `%A_Space`% , %A_Space% , All
StringReplace, LinePart2, LinePart2, `%A_Tab`% , %A_Tab% , All
StringReplace, LinePart2, LinePart2, ``r , `r , All
StringReplace, LinePart2, LinePart2, ``n , `n , All
StringReplace, LinePart2, LinePart2, ``t , `t , All
If (VarName && VarName = LinePart1) ; turn on assignment for required variables
FoundVar := 1
If (VarName && VarName != LinePart1) ; turn off assignment for required variable
FoundVar := 0
If !VarName ; if no variable specified, find all
FoundVar := 1
; assign the value to the variable
If FoundVar
%fnVariablePrefix%%LinePart1% := LinePart2
}
}
}
Catch, ThrownValue
{
ReturnValue := !ReturnValue
CatchHandler(A_ThisFunc,ThrownValue.Message,ThrownValue.What,ThrownValue.Extra,ThrownValue.File,ThrownValue.Line,0,0,0)
}
Finally
{
}
; return
Return ReturnValue
}
/* ; testing
SomeSectionName := ""
; SectionName := "[last section]"
; SectionName := "next section"
SomeVarName := ""
; VarName := "abc"
SomeVarNameReturnValue := ReadIni(SomeSectionName,SomeVarName)
MsgBox, ReadIni`n`nSomeVarNameReturnValue: %SomeVarNameReturnValue%
ListVars
Pause
; */