note description: "[ Parses Aranea config files consisting of simple name/value pairs (currently /etc/aranea/aranea.conf). IMPORTANT: This is NOT a full-fledged INI parser but a custom tailored parser for the aranea config file. ]" author: "Dennis Rietmann" date: "$Date$" revision: "$Revision$" class A_CONF_PARSER inherit A_SHARED_LOGGERS create make feature -- creation make (a_conf_file: STRING) -- Initializes the parser with the given config file. -- `conf_file'denotes the complete path of the config file. require a_conf_file_is_ok: not a_conf_file.is_empty do set_config_file(a_conf_file) end feature {NONE} -- Implementation conf_file: KL_TEXT_INPUT_FILE properties: DS_HASH_TABLE[STRING, STRING] -- The table where the properties are stored parse -- Parses the config file and makes its properties available in `get' require file_exists: conf_file.exists local line: STRING do -- read file and parse line by line conf_file.open_read from until conf_file.end_of_file loop conf_file.read_line line := conf_file.last_string parse_line (line) end conf_file.close end parse_line (line: STRING) -- Parses a single line of the config file -- comments and lines with only whitespaces are ignored local key: STRING value: STRING index: INTEGER do prune_whitespaces (line) if (not line.is_empty and not line.starts_with (";") and not line.starts_with ("[")) then -- Split line into key and value index := line.index_of ('=', 1) key := line.substring (1, index - 1) value := line.substring (index + 1, line.count) prune_whitespaces (key) prune_whitespaces (value) if (value.starts_with ("%"") and value.ends_with ("%"")) or (value.starts_with ("'") and value.ends_with ("'")) then -- Remove surrounding quotes in the value part -- This is only required because the php parser cannot handle '=' correctly in the value part (see bug 411) value.remove_head (1) value.remove_tail (1) end properties.force (value, key) -- io.put_string ("Parsed entry: key=" + key + " value= " + value) end end prune_whitespaces (text: STRING) -- removes leading and trailing whitespaces do text.prune_all_leading (' ') text.prune_all_trailing (' ') end feature -- Access get (key: STRING) : STRING -- Returns the value of the property with the given key or Void if no such key exists do if contains (key) then Result := properties.item (key) if Result = Void then node_logger.error("ERROR: Failed to load property: " + key) end else Result := Void end end get_default (key: STRING; default_value: STRING): STRING -- Returns the value of a property or the provided default value if it does not exist do if contains (key) then Result := properties.item (key) if Result = Void then -- property not set, use default Result := default_value end else Result := default_value end Result := Result.twin end contains (key: STRING) : BOOLEAN -- Returns true if the key exists do Result := properties.has (key) end feature -- Change set_config_file (a_conf_file : STRING) -- Sets a new config file `a_conf_file' require conf_file_valid: a_conf_file /= Void conf_file_not_empty: not a_conf_file.is_empty do create conf_file.make (a_conf_file) create properties.make_default parse ensure config_file_set: conf_file /= Void config_file_parsed: properties /= Void end feature -- Misc print_hash_table (table: DS_HASH_TABLE[STRING, STRING]) -- pretty print for hash tables (only used for debugging) local key: STRING value: STRING do from properties.start until properties.after loop value := properties.item_for_iteration key := properties.key_for_iteration io.put_string ("key='" + key + "' - value='" + value + "'%N") properties.forth end end end