/
store.jl
53 lines (47 loc) · 1.38 KB
/
store.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
"""
store(prop::Properties, filename, comment::Union{String,Nothing})
Store the property-dictionary in a text file called `filename`.
Additional comment can be added, that will be written to the beginning of the file.
"""
function store(prop::Properties, filename, comment::Union{String,Nothing})
open(abspath(filename), "w") do io
# add comment
if comment != nothing
# if multiline comment, place # before every line
comment = replace(comment, "\n" => "\n#")
print(io, "#")
println(io, comment)
end
# add date every time
println(io, "#", now())
# write every key-value pair
for v in keys(prop.properties)
println(io, v, "=" , prop.properties[v])
end
end
nothing
end
"""
store(prop::Properties, filename)
Store the property-dictionary in a text file called `filename`.
"""
function store(prop::Properties, filename)
store(prop, filename, nothing)
nothing
end
"""
load(filename)
Load `filename` propertyfile into a property-dictionary.
"""
function load(filename)
p = Dict{String,String}()
lines = readlines(abspath(filename))
for line in lines
# skip comments
startswith(line, "#") && continue
#split by "="
splitted = split(line, "=")
p[splitted[1]] = splitted[2]
end
Properties(p)
end