indexing description: "Parses origo config files consisting of simple name/value pairs" author: "Dennis Rietmann" date: "$Date$" revision: "$Revision$" class ORIGO_CONF_PARSER inherit O_SHARED_LOGGERS create make, make_default feature -- creation make_default is -- creates the parser and initializes it with the default config file (/etc/origo/origo.conf) do make (default_conf_file) end make (a_conf_file: STRING) is -- 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 default_conf_file: STRING is "/etc/origo/origo.conf" conf_file: KL_TEXT_INPUT_FILE properties: DS_HASH_TABLE[STRING, STRING] -- The table where the properties are stored parser: GOA_STRING_TOKENIZER -- internal parser parse is -- 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 -- print some debug output -- print_hash_table (properties) end parse_line (line: STRING) is -- Parses a single line of the config file -- comments and lines with only whitespaces are ignored local key: STRING value: STRING do prune_rubbish (line) if ((not line.is_empty) and (line.index_of (';', 1) /= 1) and (line.index_of ('[', 1) /= 1)) then create parser.make (line, "=") key := parser.item parser.forth value := parser.item prune_rubbish (key) prune_rubbish (value) properties.force (value, key) end end prune_rubbish (text: STRING) is -- removes leading and trailing whitespaces do text.prune_all_leading (' ') text.prune_all_trailing (' ') end feature -- Access get (key: STRING) : STRING is -- Returns the value of the property with the given key or Void if no such key exists do Result := properties.item (key) if Result = Void then node_logger.error("ERROR: Failed to load property: " + key) end end get_default (key: STRING; default_value: STRING): STRING is -- Returns the value of a property or the provided default value if it does not exist do Result := properties.item (key) if Result = Void then -- property not set, use default Result := default_value end end contains (key: STRING) : BOOLEAN is -- Returns true if the key exists do Result := properties.has (key) end feature -- Change set_config_file (a_conf_file : STRING) is -- 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]) is -- 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