Source file configurationOld.ml
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
let s =
try
String.sub s 0 (String.index s '#')
with Not_found ->
s
let remove_optional_quotes s =
let len = String.length s in
if len > 1 then
let first = s.[0] in
let last = s.[pred len] in
match first, last with
| '\"', '\"'
| '\'', '\'' -> true, String.sub s 1 (len - 2)
| _ -> false, s
else
false, s
type error =
| Empty_property_name
| Invalid_line
let string_of_error = function
| Empty_property_name -> "empty property name"
| Invalid_line -> "invalid line"
let load filename =
let first = ref true in
let sections = ref [] in
let curr_name = ref "" in
let curr_elems = ref [] in
let add_section () =
if not (!first && (!curr_elems = [])) then
sections := { Configuration.name = Name.of_string !curr_name;
Configuration.elements = List.rev !curr_elems } :: !sections in
let ch = open_in filename in
let line_no = ref 0 in
let fail s =
raise (Configuration.Exception (!line_no, string_of_error s)) in
try
while true do
let line = input_line ch in
incr line_no;
let contents = Utils.trim (remove_comment line) in
let len = String.length contents in
if len > 0 then begin
if contents.[0] = '[' && contents.[pred len] = ']' then begin
add_section ();
curr_name := Utils.trim (String.sub contents 1 (len - 2));
first := false;
curr_elems := [];
end else
try
let idx = String.index contents '=' in
let name = String.sub contents 0 idx in
let value = String.sub contents (succ idx) (len - idx - 1) in
if name = "" then fail Empty_property_name;
let name = Utils.trim name in
let quoted, value = remove_optional_quotes (Utils.trim value) in
let value =
if quoted then
Configuration.String value
else
try
Configuration.Integer (int_of_string value)
with _ ->
try
Configuration.Float (float_of_string value)
with _ ->
Configuration.Identifier value in
let elem = name, value in
curr_elems := elem :: !curr_elems
with Not_found -> fail Invalid_line
end
done;
assert false
with
| End_of_file ->
add_section ();
close_in_noerr ch;
List.rev !sections
| e ->
close_in_noerr ch;
raise e