diff options
author | zelgadis <zelgadis@c06c8d41-db1a-0410-9941-cceddc491573> | 2008-06-04 08:28:28 +0000 |
---|---|---|
committer | zelgadis <zelgadis@c06c8d41-db1a-0410-9941-cceddc491573> | 2008-06-04 08:28:28 +0000 |
commit | c11f0970447ed0c44f3aae338544802e1e65d21c (patch) | |
tree | 9e2ea64060100fc6fa0af3fe73279f129226025e /crawl-ref/source/initfile.cc | |
parent | 4fb433c499f4dd091ca5db56268a729579a420a0 (diff) | |
download | crawl-ref-c11f0970447ed0c44f3aae338544802e1e65d21c.tar.gz crawl-ref-c11f0970447ed0c44f3aae338544802e1e65d21c.zip |
FR 1977121: alias for option values (I called them "variables"). You
can have an option file line "$FOO := BAR", and then whenever you have
a line "option = $FOO" it will get turned into "option = BAR". Doesn't
undo variable settings from include files, so that we can theoretically
have a settings/standard_colours.txt to define the default colours used
in settings/food_colouring and settings/menu_colours, and then the
user can override them.
git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@5473 c06c8d41-db1a-0410-9941-cceddc491573
Diffstat (limited to 'crawl-ref/source/initfile.cc')
-rw-r--r-- | crawl-ref/source/initfile.cc | 64 |
1 files changed, 61 insertions, 3 deletions
diff --git a/crawl-ref/source/initfile.cc b/crawl-ref/source/initfile.cc index 5b414e5158..45c0c45df3 100644 --- a/crawl-ref/source/initfile.cc +++ b/crawl-ref/source/initfile.cc @@ -1523,15 +1523,72 @@ int game_options::read_explore_stop_conditions(const std::string &field) const void game_options::add_alias(const std::string &key, const std::string &val) { - aliases[key] = val; + if (key[0] == '$') + variables[key.substr(1)] = val; + else + aliases[key] = val; } std::string game_options::unalias(const std::string &key) const { - std::map<std::string, std::string>::const_iterator i = aliases.find(key); + string_map::const_iterator i = aliases.find(key); return (i == aliases.end()? key : i->second); } +#define IS_VAR_CHAR(c) (isalpha(c) || c == '_') + +std::string game_options::expand_vars(const std::string &field) const +{ + std::string field_out = field; + + std::string::size_type curr_pos = 0; + + // Only try 100 times, so as to not get stuck in infinite recursion. + for (int i = 0; i < 100; i++) + { + std::string::size_type dollar_pos = field_out.find("$", curr_pos); + + if (dollar_pos == std::string::npos + || field_out.size() == (dollar_pos + 1)) + { + break; + } + + std::string::size_type start_pos = dollar_pos + 1; + + if (!IS_VAR_CHAR(field_out[start_pos])) + continue; + + std::string::size_type end_pos; + for (end_pos = start_pos; end_pos < field_out.size(); end_pos++) + { + if (!IS_VAR_CHAR(field_out[end_pos + 1])) + break; + } + + std::string var_name = field_out.substr(start_pos, + end_pos - start_pos + 1); + + string_map::const_iterator x = variables.find(var_name); + + if (x == aliases.end()) + { + curr_pos = end_pos + 1; + continue; + } + + std::string dollar_plus_name = "$"; + dollar_plus_name += var_name; + + field_out = replace_all(field_out, dollar_plus_name, x->second); + + // Start over at begining + curr_pos = 0; + } + + return field_out; +} + void game_options::add_message_colour_mappings(const std::string &field) { std::vector<std::string> fragments = split_string(",", field); @@ -1688,7 +1745,8 @@ void game_options::read_option_line(const std::string &str, bool runscript) if (first_equals < 0) return; - field = str.substr( first_equals + 1 ); + field = str.substr( first_equals + 1 ); + field = expand_vars(field); std::string prequal = trimmed_string( str.substr(0, first_equals) ); |