rc_conf.anubis
5.29 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Created by PyramIDE.
* User: フランスのトトロ
* Date: 31/05/2015
* Time: 02:41
* © David RENÉ
*/
This is FreeBSD conf file interface.
read system/logger.anubis
read system/string.anubis
read tools/basis.anubis
//read tools/app_loggers.anubis
read tools/streams.anubis
//read calexium_lib/net_services_protocols/logger_service.anubis
delete_entry
find_entry
replace_entry = ok
add_entry = ok
/* Type RC_conf_entry, is a type which represent every possible entry into *.conf
* file format
*/
public type RC_conf_entry: //One line of conf file
blank, //blank line
comment(String), //comment is line starting with #
conf_nv(String name, String value), //conf line with only name with his value
conf_nvc(String name, String value, String comment). //conf line with name, value and comment at the end
public type RC_conf: //The entire conf file which represented by list of his all entries
rc_conf(List(RC_conf_entry) list).
define RC_conf
_replace_entry
(
List(RC_conf_entry) list,
String entry_name,
String value,
List(RC_conf_entry) so_far,
Bool found
)=
if list is
{
[] then
(if found then
rc_conf(reverse(so_far))
else
rc_conf(reverse([conf_nv(entry_name, value) . so_far]))),
[ h . t ] then
with result = (Maybe(RC_conf_entry)) if h is
{
blank then failure,
comment(_) then failure,
conf_nv(name,_) then
if name = entry_name then
success(conf_nv(name, value)) //replace the value here
else
failure,
conf_nvc(name, _, comment) then
if name = entry_name then
success(conf_nvc(name, value, comment)) //replace the value here
else
failure,
},
if result is
{
failure then _replace_entry(t, entry_name, value, [h . so_far], found),
success(entry) then _replace_entry(t, entry_name, value, [entry . so_far], true),
}
}.
public define RC_conf
replace_entry
(
RC_conf conf,
String entry_name,
String value
)=
_replace_entry(conf.list, entry_name, value, [], false).
//add_entry
public define RC_conf
add_entry
(
RC_conf conf,
String name,
String value,
String comment
)=
since conf is rc_conf(entries),
if comment ="" then
rc_conf([conf_nv(name, value) . entries])
else
rc_conf([conf_nvc(name, value, comment) . entries])
.
define String
_to_String
(
List(RC_conf_entry) rc_conf_entries,
String so_far
)=
if rc_conf_entries is
{
[] then so_far,
[ h . t] then
if h is
{
blank then _to_String(t, so_far + "\n"),
comment(s) then _to_String(t, so_far + s+"\n"),
conf_nv(n,v) then _to_String(t, so_far + n +"=\""+v+"\"\n"),
conf_nvc(n,v,c) then _to_String(t, so_far + n+"=\""+v+"\""+c+"\n")
}
}.
public define String
to_String
(
RC_conf rc_conf_entries
)=
_to_String(rc_conf_entries.list, "").
define RC_conf_entry
extract_value
(
String name,
String line
)=
with left_line = trim(line),
with result = split_by_token(left_line, '\"'),
if result is
{
[] then blank,
[value . comment] then
if comment is
{
[] then conf_nv(name, value),
[ c . t ] then conf_nvc(name, value, c)
}
}.
define RC_conf_entry
parse_var_line
(
String line
) =
if find_char(line, '=', 0) is
{
failure then blank
success(idx) then
if sub_string(line, 0, idx) is
{
failure then blank
success(val_name) then
if sub_string(line, idx+1, (length(line) - (idx+1))) is
{
failure then blank
success(value) then
extract_value(val_name, value)
}
}
}
.
define RC_conf_entry
extract_rc_conf_line
(
String line
)=
//check for comment
if nth((Int)0,line) is
{
failure then blank,
success(c) then
if c = '#' then
comment(line)
else
parse_var_line(line)
}.
define List(RC_conf_entry)
read_rc_conf_lines
(
Stream s,
List(RC_conf_entry) so_far
)=
if read_line(s) is
{
failure then reverse(so_far),
success(line) then read_rc_conf_lines(s, [extract_rc_conf_line(trim(line)) . so_far])
}.
public define RC_conf
read_rc_conf
(
String rc_conf_file
) =
if (Maybe(RStream))file(rc_conf_file, read) is
{
failure then rc_conf([]),
success(f) then rc_conf(read_rc_conf_lines(make_stream(f), []))
}.
public define Maybe(One)
write_rc_conf
(
String rc_conf_filename,
RC_conf rc_conf
)=
if (Maybe(RWStream))file(rc_conf_filename, new) is
{
failure then failure,
success(f) then success(forget(reliable_write(f, to_byte_array(to_String(rc_conf)))))
}.