/* * 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))))) }.