Commit e5bc687adb94d727763d6d79437f92a5e1611fa8
1 parent
3eb6214f
add FreeBSD conf file management.
The conf line format can be: blank line, comment only, name, value and maybe comment format. Read, Write, add_entry and replace_entry is already supported in that first version
Showing
1 changed file
with
225 additions
and
0 deletions
Show diff stats
| 1 | +/* | ||
| 2 | + * Created by PyramIDE. | ||
| 3 | + * User: フランスのトトロ | ||
| 4 | + * Date: 31/05/2015 | ||
| 5 | + * Time: 02:41 | ||
| 6 | + * © Calexium | ||
| 7 | + */ | ||
| 8 | + | ||
| 9 | + This is FreeBSD conf file interface. | ||
| 10 | + | ||
| 11 | + | ||
| 12 | +read system/logger.anubis | ||
| 13 | +read system/string.anubis | ||
| 14 | +read tools/basis.anubis | ||
| 15 | +//read tools/app_loggers.anubis | ||
| 16 | +read tools/streams.anubis | ||
| 17 | +read calexium_lib/net_services_protocols/logger_service.anubis | ||
| 18 | + | ||
| 19 | +delete_entry | ||
| 20 | +find_entry | ||
| 21 | +replace_entry = ok | ||
| 22 | +add_entry = ok | ||
| 23 | + | ||
| 24 | + | ||
| 25 | + /* Type RC_conf_entry, is a type which represent every possible entry into *.conf | ||
| 26 | + * file format | ||
| 27 | + */ | ||
| 28 | + | ||
| 29 | +public type RC_conf_entry: //One line of conf file | ||
| 30 | + blank, //blank line | ||
| 31 | + comment(String), //comment is line starting with # | ||
| 32 | + conf_nv(String name, String value), //conf line with only name with his value | ||
| 33 | + conf_nvc(String name, String value, String comment). //conf line with name, value and comment at the end | ||
| 34 | + | ||
| 35 | +public type RC_conf: //The entire conf file which represented by list of his all entries | ||
| 36 | + rc_conf(List(RC_conf_entry) list). | ||
| 37 | + | ||
| 38 | + | ||
| 39 | +define RC_conf | ||
| 40 | + _replace_entry | ||
| 41 | + ( | ||
| 42 | + List(RC_conf_entry) list, | ||
| 43 | + String entry_name, | ||
| 44 | + String value, | ||
| 45 | + List(RC_conf_entry) so_far, | ||
| 46 | + Bool found | ||
| 47 | + )= | ||
| 48 | + if list is | ||
| 49 | + { | ||
| 50 | + [] then | ||
| 51 | + (if found then | ||
| 52 | + rc_conf(reverse(so_far)) | ||
| 53 | + else | ||
| 54 | + rc_conf(reverse([conf_nv(entry_name, value) . so_far]))), | ||
| 55 | + [ h . t ] then | ||
| 56 | + with result = (Maybe(RC_conf_entry)) if h is | ||
| 57 | + { | ||
| 58 | + blank then failure, | ||
| 59 | + comment(_) then failure, | ||
| 60 | + conf_nv(name,_) then | ||
| 61 | + if name = entry_name then | ||
| 62 | + success(conf_nv(name, value)) //replace the value here | ||
| 63 | + else | ||
| 64 | + failure, | ||
| 65 | + conf_nvc(name, _, comment) then | ||
| 66 | + if name = entry_name then | ||
| 67 | + success(conf_nvc(name, value, comment)) //replace the value here | ||
| 68 | + else | ||
| 69 | + failure, | ||
| 70 | + }, | ||
| 71 | + if result is | ||
| 72 | + { | ||
| 73 | + failure then _replace_entry(t, entry_name, value, [h . so_far], found), | ||
| 74 | + success(entry) then _replace_entry(t, entry_name, value, [entry . so_far], true), | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + }. | ||
| 78 | + | ||
| 79 | +public define RC_conf | ||
| 80 | + replace_entry | ||
| 81 | + ( | ||
| 82 | + RC_conf conf, | ||
| 83 | + String entry_name, | ||
| 84 | + String value | ||
| 85 | + )= | ||
| 86 | + _replace_entry(conf.list, entry_name, value, [], false). | ||
| 87 | + | ||
| 88 | + //add_entry | ||
| 89 | + | ||
| 90 | +public define RC_conf | ||
| 91 | + add_entry | ||
| 92 | + ( | ||
| 93 | + RC_conf conf, | ||
| 94 | + String name, | ||
| 95 | + String value, | ||
| 96 | + String comment | ||
| 97 | + )= | ||
| 98 | + since conf is rc_conf(entries), | ||
| 99 | + if comment ="" then | ||
| 100 | + rc_conf([conf_nv(name, value) . entries]) | ||
| 101 | + else | ||
| 102 | + rc_conf([conf_nvc(name, value, comment) . entries]) | ||
| 103 | + . | ||
| 104 | + | ||
| 105 | +define String | ||
| 106 | + _to_String | ||
| 107 | + ( | ||
| 108 | + List(RC_conf_entry) rc_conf_entries, | ||
| 109 | + String so_far | ||
| 110 | + )= | ||
| 111 | + if rc_conf_entries is | ||
| 112 | + { | ||
| 113 | + [] then so_far, | ||
| 114 | + [ h . t] then | ||
| 115 | + if h is | ||
| 116 | + { | ||
| 117 | + blank then _to_String(t, so_far + "\n"), | ||
| 118 | + comment(s) then _to_String(t, so_far + s+"\n"), | ||
| 119 | + conf_nv(n,v) then _to_String(t, so_far + n +"=\""+v+"\"\n"), | ||
| 120 | + conf_nvc(n,v,c) then _to_String(t, so_far + n+"=\""+v+"\""+c+"\n") | ||
| 121 | + } | ||
| 122 | + }. | ||
| 123 | + | ||
| 124 | +public define String | ||
| 125 | + to_String | ||
| 126 | + ( | ||
| 127 | + RC_conf rc_conf_entries | ||
| 128 | + )= | ||
| 129 | + _to_String(rc_conf_entries.list, ""). | ||
| 130 | + | ||
| 131 | + | ||
| 132 | +define RC_conf_entry | ||
| 133 | + extract_value | ||
| 134 | + ( | ||
| 135 | + String name, | ||
| 136 | + String left_line | ||
| 137 | + )= | ||
| 138 | + with left_line = trim(left_line), | ||
| 139 | + with result = split_by_token(left_line, '\"'), | ||
| 140 | + if result is | ||
| 141 | + { | ||
| 142 | + [] then blank, | ||
| 143 | + [value . comment] then | ||
| 144 | + if comment is | ||
| 145 | + { | ||
| 146 | + [] then conf_nv(name, value), | ||
| 147 | + [ c . t ] then conf_nvc(name, value, c) | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + }. | ||
| 151 | + | ||
| 152 | +define RC_conf_entry | ||
| 153 | + parse_var_line | ||
| 154 | + ( | ||
| 155 | + String line | ||
| 156 | + ) = | ||
| 157 | + if find_char(line, '=', 0) is | ||
| 158 | + { | ||
| 159 | + failure then blank | ||
| 160 | + success(idx) then | ||
| 161 | + if sub_string(line, 0, idx) is | ||
| 162 | + { | ||
| 163 | + failure then blank | ||
| 164 | + success(val_name) then | ||
| 165 | + if sub_string(line, idx+1, (length(line) - (idx+1))) is | ||
| 166 | + { | ||
| 167 | + failure then blank | ||
| 168 | + success(value) then | ||
| 169 | + extract_value(val_name, value) | ||
| 170 | + } | ||
| 171 | + } | ||
| 172 | + } | ||
| 173 | + . | ||
| 174 | + | ||
| 175 | +define RC_conf_entry | ||
| 176 | + extract_rc_conf_line | ||
| 177 | + ( | ||
| 178 | + String line | ||
| 179 | + )= | ||
| 180 | + //check for comment | ||
| 181 | + if nth((Int)0,line) is | ||
| 182 | + { | ||
| 183 | + failure then blank, | ||
| 184 | + success(c) then | ||
| 185 | + if c = '#' then | ||
| 186 | + comment(line) | ||
| 187 | + else | ||
| 188 | + parse_var_line(line) | ||
| 189 | + }. | ||
| 190 | + | ||
| 191 | +define List(RC_conf_entry) | ||
| 192 | + read_rc_conf_lines | ||
| 193 | + ( | ||
| 194 | + Stream s, | ||
| 195 | + List(RC_conf_entry) so_far | ||
| 196 | + )= | ||
| 197 | + if read_line(s) is | ||
| 198 | + { | ||
| 199 | + failure then reverse(so_far), | ||
| 200 | + success(line) then read_rc_conf_lines(s, [extract_rc_conf_line(trim(line)) . so_far]) | ||
| 201 | + }. | ||
| 202 | + | ||
| 203 | +public define RC_conf | ||
| 204 | + read_rc_conf | ||
| 205 | + ( | ||
| 206 | + String rc_conf_file | ||
| 207 | + ) = | ||
| 208 | + if (Maybe(RStream))file(rc_conf_file, read) is | ||
| 209 | + { | ||
| 210 | + failure then rc_conf([]), | ||
| 211 | + success(f) then rc_conf(read_rc_conf_lines(make_stream(f), [])) | ||
| 212 | + }. | ||
| 213 | + | ||
| 214 | +public define Maybe(One) | ||
| 215 | + write_rc_conf | ||
| 216 | + ( | ||
| 217 | + String rc_conf_filename, | ||
| 218 | + RC_conf rc_conf | ||
| 219 | + )= | ||
| 220 | + if (Maybe(RWStream))file(rc_conf_filename, new) is | ||
| 221 | + { | ||
| 222 | + failure then failure, | ||
| 223 | + success(f) then success(forget(reliable_write(f, to_byte_array(to_String(rc_conf))))) | ||
| 224 | + }. | ||
| 225 | + |