forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inifile.jl
82 lines (70 loc) · 2.36 KB
/
inifile.jl
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
# .ini file parser
let
typealias HTSS Dict{String,String}
global IniFile
type IniFile
sections::Dict{String,HTSS}
defaults::HTSS
end
IniFile() = IniFile(Dict{String,HTSS}(), HTSS())
global read
function read(inifile::IniFile, stream::IOStream)
current_section = inifile.defaults
for line in LineIterator(stream)
s = strip(line)
# comments start with # or ;
if length(s) < 3 || s[1] == '#' || s[1] == ';'
continue
elseif s[1] == '[' && s[end] == ']'
section = s[2:end-1]
if !has(inifile.sections, section)
inifile.sections[section] = HTSS()
end
current_section = inifile.sections[section]
else
i = strchr(s, '=')
j = strchr(s, ':')
if i == 0 && j == 0
# TODO: allow multiline values
println("skipping malformed line: $s")
else
idx = min(i, j)
if idx == 0
idx = max(i, j)
end
key = rstrip(s[1:idx-1])
val = lstrip(s[idx+1:end])
current_section[key] = val
end
end
end
inifile
end
end
function read(inifile::IniFile, filename::String)
f = open(filename)
read(inifile, f)
close(f)
inifile
end
defaults(inifile::IniFile) = inifile.defaults
get(inifile::IniFile, section::String, key::String) = get(inifile, section, key, :notfound)
function get(inifile::IniFile, section::String, key::String, notfound)
if has(inifile.sections, section) && has(inifile.sections[section], key)
return inifile.sections[section][key]
elseif has(inifile.defaults, key)
return inifile.defaults[key]
end
notfound
end
function get_bool(inifile::IniFile, section::String, key::String)
sval = get(inifile, section, key)
if sval == "true"
return true
end
return false
end
has(inifile::IniFile, section::String, key::String) =
has(inifile.sections,section) && has(inifile.sections[section], key)
has_section(inifile::IniFile, section::String) = has(inifile.sections, section)
section(inifile::IniFile, section::String) = inifile.sections[section]