From ab21de1ef1d559c242ed0a00ceb1952afd6eb657 Mon Sep 17 00:00:00 2001 From: Guillaume Melquiond Date: Mon, 5 Apr 2010 06:02:01 +0000 Subject: [PATCH] Added conversions from config::proxy_string to int. --- src/campaign_server/campaign_server.cpp | 6 +++--- src/cavegen.cpp | 12 ++++++------ src/config.cpp | 5 +++++ src/config.hpp | 4 +++- src/game_events.cpp | 22 +++++++++++----------- src/multiplayer.cpp | 2 +- src/multiplayer_connect.cpp | 6 +++--- src/multiplayer_ui.cpp | 4 ++-- src/multiplayer_wait.cpp | 2 +- src/playcampaign.cpp | 6 +++--- src/playsingle_controller.cpp | 2 +- src/savegame.cpp | 8 ++++---- src/server/server.cpp | 10 +++++----- src/statistics.cpp | 2 +- src/theme.cpp | 2 +- src/tod_manager.cpp | 2 +- src/unit.cpp | 4 ++-- src/unit_types.cpp | 14 +++++++------- 18 files changed, 60 insertions(+), 53 deletions(-) diff --git a/src/campaign_server/campaign_server.cpp b/src/campaign_server/campaign_server.cpp index 7d8a3e2cba9..cb71e9efeb9 100644 --- a/src/campaign_server/campaign_server.cpp +++ b/src/campaign_server/campaign_server.cpp @@ -525,13 +525,13 @@ namespace { LOG_CS << "Upload aborted - invalid file names in add-on data.\n"; network::send_data(construct_error("Add-on rejected: The add-on contains an illegal file or directory name." " File or directory names may not contain any of the following characters: '/ \\ : ~'"), sock, gzipped); - } else if (campaign && (*campaign)["passphrase"] != upload["passphrase"]) { + } else if (campaign && (*campaign)["passphrase"].str() != upload["passphrase"]) { // the user password failed, now test for the master password, in master password // mode the upload behaves different since it's only intended to update translations. // In a later version the translations will be separated from the addon. LOG_CS << "Upload is admin upload.\n"; if (!campaigns()["master_password"].empty() - && campaigns()["master_password"] == upload["passphrase"]) + && campaigns()["master_password"].str() == upload["passphrase"]) { std::string message = "Add-on accepted."; @@ -585,7 +585,7 @@ namespace { (*campaign)["title"] = upload["title"]; (*campaign)["name"] = upload["name"]; - (*campaign)["filename"] = "data/" + upload["name"]; + (*campaign)["filename"] = "data/" + upload["name"].str(); (*campaign)["passphrase"] = upload["passphrase"]; (*campaign)["author"] = upload["author"]; (*campaign)["description"] = upload["description"]; diff --git a/src/cavegen.cpp b/src/cavegen.cpp index 870c4c5d13d..4d6c530f2f4 100644 --- a/src/cavegen.cpp +++ b/src/cavegen.cpp @@ -48,18 +48,18 @@ cave_map_generator::cave_map_generator(const config &cfg) : flipx_(false), flipy_(false) { - width_ = atoi(cfg_["map_width"].c_str()); - height_ = atoi(cfg_["map_height"].c_str()); + width_ = cfg_["map_width"]; + height_ = cfg_["map_height"]; - village_density_ = atoi(cfg_["village_density"].c_str()); + village_density_ = cfg_["village_density"]; - const int r = rand()%100; - const int chance = atoi(cfg_["flipx_chance"].c_str()); + int r = rand() % 100; + int chance = cfg_["flipx_chance"]; flipx_ = r < chance; LOG_NG << "flipx: " << r << " < " << chance << " = " << (flipx_ ? "true" : "false") << "\n"; - flipy_ = (rand()%100) < atoi(cfg_["flipy_chance"].c_str()); + flipy_ = rand() % 100 < cfg_["flipy_chance"]; } std::string cave_map_generator::config_name() const diff --git a/src/config.cpp b/src/config.cpp index 1ffc1149f7c..b825cdc2740 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -44,6 +44,11 @@ config::proxy_string &config::proxy_string::operator=(int v) return *this; } +int config::proxy_string::to_int(int def) const +{ + return lexical_cast_default(real_str_.c_str(), def); +} + config config::invalid; const char* config::diff_track_attribute = "__diff_track"; diff --git a/src/config.hpp b/src/config.hpp index 8aa4d89fd93..9ac6f6d64c6 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -190,11 +190,13 @@ public: proxy_string& operator=(const t_string &str) { real_str_ = str; return *this; } + int to_int(int def = 0) const; + bool empty() const { return real_str_.empty(); } - const char *c_str() const { return real_str_.c_str(); } const std::string &str() const { return real_str_.str(); } const t_string &t_str() const { return real_str_; } + operator int() const { return to_int(); } operator std::string() const { return real_str_.str(); } operator t_string() const { return real_str_; } diff --git a/src/game_events.cpp b/src/game_events.cpp index 901f96f1fab..c74cbf5bbfb 100644 --- a/src/game_events.cpp +++ b/src/game_events.cpp @@ -1112,27 +1112,27 @@ WML_HANDLER_FUNCTION(set_variable, /*event_info*/, cfg) const std::string add = cfg["add"]; if(add.empty() == false) { if(isint(var.str()) && isint(add)) { - var = str_cast( std::atoi(var.c_str()) + std::atoi(add.c_str()) ); + var = var.to_int() + atoi(add.c_str()); } else { - var = str_cast( std::atof(var.c_str()) + std::atof(add.c_str()) ); + var = str_cast(atof(var.str().c_str()) + atof(add.c_str())); } } const std::string sub = cfg["sub"]; if(sub.empty() == false) { if(isint(var.str()) && isint(sub)) { - var = str_cast( std::atoi(var.c_str()) - std::atoi(sub.c_str()) ); + var = var.to_int() - atoi(sub.c_str()); } else { - var = str_cast( std::atof(var.c_str()) - std::atof(sub.c_str()) ); + var = str_cast(atof(var.str().c_str()) - atof(sub.c_str())); } } const std::string multiply = cfg["multiply"]; if(multiply.empty() == false) { if(isint(var.str()) && isint(multiply)) { - var = str_cast( std::atoi(var.c_str()) * std::atoi(multiply.c_str()) ); + var = var.to_int() * atoi(multiply.c_str()); } else { - var = str_cast( std::atof(var.c_str()) * std::atof(multiply.c_str()) ); + var = str_cast(atof(var.str().c_str()) * atof(multiply.c_str())); } } @@ -1143,9 +1143,9 @@ WML_HANDLER_FUNCTION(set_variable, /*event_info*/, cfg) return; } if(isint(var.str()) && isint(divide)) { - var = str_cast( std::atoi(var.c_str()) / std::atoi(divide.c_str()) ); + var = var.to_int() / atoi(divide.c_str()); } else { - var = str_cast( std::atof(var.c_str()) / std::atof(divide.c_str()) ); + var = str_cast(atof(var.str().c_str()) / atof(divide.c_str())); } } @@ -1156,16 +1156,16 @@ WML_HANDLER_FUNCTION(set_variable, /*event_info*/, cfg) return; } if(isint(var.str()) && isint(modulo)) { - var = str_cast( std::atoi(var.c_str()) % std::atoi(modulo.c_str()) ); + var = var.to_int() % atoi(modulo.c_str()); } else { - double value = std::fmod( std::atof(var.c_str()), std::atof(modulo.c_str()) ); + double value = std::fmod(atof(var.str().c_str()), atof(modulo.c_str())); var = str_cast(value); } } const std::string round_val = cfg["round"]; if(round_val.empty() == false) { - double value = std::atof(var.c_str()); + double value = atof(var.str().c_str()); if (round_val == "ceil") { value = std::ceil(value); } else if (round_val == "floor") { diff --git a/src/multiplayer.cpp b/src/multiplayer.cpp index 3437ea058f8..75c7b28e294 100644 --- a/src/multiplayer.cpp +++ b/src/multiplayer.cpp @@ -321,7 +321,7 @@ static server_type open_connection(game_display& disp, const std::string& origin } else if((*error)["error_code"] == MP_INCORRECT_PASSWORD_ERROR) { error_message = _("The password you provided was incorrect."); } else { - error_message = (*error)["message"]; + error_message = (*error)["message"].str(); } gui2::tmp_login dlg(error_message, !((*error)["password_request"].empty())); diff --git a/src/multiplayer_connect.cpp b/src/multiplayer_connect.cpp index 6827ad8d364..f8f3ddec90a 100644 --- a/src/multiplayer_connect.cpp +++ b/src/multiplayer_connect.cpp @@ -1559,7 +1559,7 @@ void connect::load_game() } else { level_.clear(); params_.saved_game = false; - params_.mp_scenario = params_.scenario_data["id"]; + params_.mp_scenario = params_.scenario_data["id"].str(); level_.merge_with(params_.scenario_data); level_["turns"] = num_turns_; level_.add_child("multiplayer", params_.to_config()); @@ -1585,7 +1585,7 @@ void connect::load_game() } // Add the map name to the title. - append_to_title(" - " + level_["name"]); + append_to_title(" - " + level_["name"].t_str()); std::string era = params_.mp_era; @@ -1668,7 +1668,7 @@ void connect::update_and_send_diff(bool update_time_of_day) if (update_time_of_day) { // Set random start ToD - tod_manager tod_mng(level_,atoi(level_["turns"].c_str()),&state_); + tod_manager tod_mng(level_, level_["turns"], &state_); } config diff = level_.get_diff(old_level); diff --git a/src/multiplayer_ui.cpp b/src/multiplayer_ui.cpp index 14979080c26..7029d434bc3 100644 --- a/src/multiplayer_ui.cpp +++ b/src/multiplayer_ui.cpp @@ -124,8 +124,8 @@ void level_to_gamestate(config& level, game_state& state) level["campaign_type"] = "multiplayer"; state.classification().campaign_type = "multiplayer"; - state.classification().completion = level["completion"]; - state.classification().version = level["version"]; + state.classification().completion = level["completion"].str(); + state.classification().version = level["version"].str(); if (const config &vars = level.child("variables")) { state.set_variables(vars); diff --git a/src/multiplayer_wait.cpp b/src/multiplayer_wait.cpp index f27834ba1bb..c5390d04120 100644 --- a/src/multiplayer_wait.cpp +++ b/src/multiplayer_wait.cpp @@ -209,7 +209,7 @@ void wait::join_game(bool observe) } // Add the map name to the title. - append_to_title(": " + level_["name"]); + append_to_title(": " + level_["name"].t_str()); if (!observe) { //search for an appropriate vacant slot. If a description is set diff --git a/src/playcampaign.cpp b/src/playcampaign.cpp index 67630f46bb5..73470a7ad27 100644 --- a/src/playcampaign.cpp +++ b/src/playcampaign.cpp @@ -91,7 +91,7 @@ void play_replay(display& disp, game_state& gamestate, const config& game_config try { // Preserve old label eg. replay if (gamestate.classification().label.empty()) - gamestate.classification().label = starting_pos["name"]; + gamestate.classification().label = starting_pos["name"].str(); //if (gamestate.abbrev.empty()) // gamestate.abbrev = (*scenario)["abbrev"]; @@ -261,7 +261,7 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_ gamestate.set_menu_items(gamestate.snapshot.child_range("menu_item")); // Replace game label with that from snapshot if (!gamestate.snapshot["label"].empty()){ - gamestate.classification().label = gamestate.snapshot["label"]; + gamestate.classification().label = gamestate.snapshot["label"].str(); } // Helper for transitioning middle-of-scenario savefiles from 1.6 to 1.8. // To be removed for 1.10. @@ -497,7 +497,7 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_ { std::string id = side["save_id"]; if(id.empty()) { - id = side["id"]; + id = side["id"].str(); } if(!id.empty()) { /* Update side info to match current_player info diff --git a/src/playsingle_controller.cpp b/src/playsingle_controller.cpp index bae86e15ed5..026085c2655 100644 --- a/src/playsingle_controller.cpp +++ b/src/playsingle_controller.cpp @@ -320,7 +320,7 @@ LEVEL_RESULT playsingle_controller::play_scenario( // Log before prestart events: they do weird things. if (first_human_team_ != -1) { //sp logs log.start(gamestate_, teams_[first_human_team_], - loading_game_ ? gamestate_.get_variable("turn_number").c_str() : "", + loading_game_ ? gamestate_.get_variable("turn_number").str().c_str() : "", number_of_turns(), resources::game_map->write()); } else { //ai vs. ai upload logs log.start(gamestate_, resources::game_map->write()); diff --git a/src/savegame.cpp b/src/savegame.cpp index 1450f0287eb..12bde281b14 100644 --- a/src/savegame.cpp +++ b/src/savegame.cpp @@ -450,11 +450,11 @@ void loadgame::load_game(std::string& filename, bool show_replay, bool cancel_or } } - gamestate_.classification().difficulty = load_config_["difficulty"]; - gamestate_.classification().campaign_define = load_config_["campaign_define"]; - gamestate_.classification().campaign_type = load_config_["campaign_type"]; + gamestate_.classification().difficulty = load_config_["difficulty"].str(); + gamestate_.classification().campaign_define = load_config_["campaign_define"].str(); + gamestate_.classification().campaign_type = load_config_["campaign_type"].str(); gamestate_.classification().campaign_xtra_defines = utils::split(load_config_["campaign_extra_defines"]); - gamestate_.classification().version = load_config_["version"]; + gamestate_.classification().version = load_config_["version"].str(); check_version_compatibility(); diff --git a/src/server/server.cpp b/src/server/server.cpp index f22d0841c37..1a350f056ad 100644 --- a/src/server/server.cpp +++ b/src/server/server.cpp @@ -439,14 +439,14 @@ void server::load_config() { input_.reset(new input_stream(fifo_path)); save_replays_ = utils::string_bool(cfg_["save_replays"], false); - replay_save_path_ = cfg_["replay_save_path"]; + replay_save_path_ = cfg_["replay_save_path"].str(); tor_ip_list_ = utils::split(cfg_["tor_ip_list_path"].empty() ? "" : read_file(cfg_["tor_ip_list_path"]), '\n'); - admin_passwd_ = cfg_["passwd"]; - motd_ = cfg_["motd"]; + admin_passwd_ = cfg_["passwd"].str(); + motd_ = cfg_["motd"].str(); lan_server_ = lexical_cast_default(cfg_["lan_server"], 0); - uh_name_ = cfg_["user_handler"]; + uh_name_ = cfg_["user_handler"].str(); deny_unregistered_login_ = utils::string_bool(cfg_["deny_unregistered_login"], false); @@ -478,7 +478,7 @@ void server::load_config() { // Example config line: // restart_command="./wesnothd-debug -d -c ~/.wesnoth1.5/server.cfg" // remember to make new one as a daemon or it will block old one - restart_command = cfg_["restart_command"]; + restart_command = cfg_["restart_command"].str(); fps_limit_.set_ms_per_frame(lexical_cast_default(cfg_["ms_per_frame"], 20)); diff --git a/src/statistics.cpp b/src/statistics.cpp index 7b2bdc51b4f..72cd848e851 100644 --- a/src/statistics.cpp +++ b/src/statistics.cpp @@ -161,7 +161,7 @@ static stats::battle_result_map read_battle_result_map(const config& cfg) foreach (const config &i, cfg.child_range("sequence")) { config item = i; - const int key = atoi(item["_num"].c_str()); + int key = item["_num"]; item.remove_attribute("_num"); m[key] = read_str_int_map(item); } diff --git a/src/theme.cpp b/src/theme.cpp index d360af4fed6..59a45659fb9 100644 --- a/src/theme.cpp +++ b/src/theme.cpp @@ -799,7 +799,7 @@ theme::menu* theme::refresh_title(const std::string& id, const std::string& new_ theme::menu* theme::refresh_title2(const std::string& id, const std::string& title_tag){ std::string new_title; - config& cfg = find_ref(id, cfg_, false); + const config &cfg = find_ref(id, cfg_, false); if (! cfg[title_tag].empty()) new_title = cfg[title_tag]; diff --git a/src/tod_manager.cpp b/src/tod_manager.cpp index ae62022eacf..1745e721aef 100644 --- a/src/tod_manager.cpp +++ b/src/tod_manager.cpp @@ -201,7 +201,7 @@ void tod_manager::set_start_ToD(config &level, int current_turn) { if (!level["current_tod"].empty()) { - set_time_of_day(atoi(level["current_tod"].c_str())); + set_time_of_day(level["current_tod"]); return; } std::string random_start_time = level["random_start_time"]; diff --git a/src/unit.cpp b/src/unit.cpp index 4c8dfd3f460..cb422e3f35f 100644 --- a/src/unit.cpp +++ b/src/unit.cpp @@ -2517,12 +2517,12 @@ void unit::add_modification(const std::string& type, const config& mod, bool no_ // Apply variations -- only apply if we are adding this for the first time. if (!last_effect.empty() && no_add == false) { if ((last_effect)["apply_to"] == "variation") { - variation_ = (last_effect)["name"]; + variation_ = last_effect["name"].str(); advance_to(this->type()); } else if ((last_effect)["apply_to"] == "type") { if (!new_child->has_attribute("prev_type")) (*new_child)["prev_type"] = type_id(); - type_ = (last_effect)["name"]; + type_ = last_effect["name"].str(); int hit_points = hit_points_; int experience = experience_; int movement = movement_; diff --git a/src/unit_types.cpp b/src/unit_types.cpp index 15fa6720c6f..3419904e0c2 100644 --- a/src/unit_types.cpp +++ b/src/unit_types.cpp @@ -692,8 +692,8 @@ unit_type::~unit_type() void unit_type::set_config(const config& cfg) { - cfg_ = cfg; - id_ = cfg_["id"]; + cfg_ = cfg; + id_ = cfg["id"]; } void unit_type::build_full(const config& cfg, const movement_type_map& mv_types, @@ -800,10 +800,10 @@ void unit_type::build_help_index(const config& cfg, const movement_type_map& mv_ movement_ = lexical_cast_default(cfg["movement"], 1); max_attacks_ = lexical_cast_default(cfg["attacks"], 1); cost_ = lexical_cast_default(cfg["cost"], 1); - usage_ = cfg_["usage"]; - undead_variation_ = cfg_["undead_variation"]; - image_ = cfg_["image"]; - image_profile_ = cfg_["profile"]; + usage_ = cfg_["usage"].str(); + undead_variation_ = cfg_["undead_variation"].str(); + image_ = cfg_["image"].str(); + image_profile_ = cfg_["profile"].str(); const race_map::const_iterator race_it = races.find(cfg["race"]); if(race_it != races.end()) { @@ -1168,7 +1168,7 @@ void unit_type_data::set_config(config &cfg) merge_cfg.clear_children("base_unit"); std::string id = merge_cfg["id"]; if(id.empty()) { - id = from_cfg["name"]; + id = from_cfg["name"].str(); } ut = merge_cfg;