prefer lexical_cast over stringstream in record/replay

This commit is contained in:
2024-09-02 12:41:45 -05:00
committed by Celtic Minstrel
parent b1be43eb7e
commit 4e66edc7e3
7 changed files with 53 additions and 171 deletions

View File

@@ -7,7 +7,6 @@
*/
#include "control.hpp"
#include <sstream>
#include "dialogxml/dialogs/dialog.hpp"
#include "sounds.hpp"
#include "button.hpp"
@@ -19,6 +18,7 @@
#include "tools/prefs.hpp"
#include "tools/cursors.hpp"
#include "replay.hpp"
#include <boost/lexical_cast.hpp>
void cControl::setText(std::string l){
lbl = l;
@@ -351,9 +351,7 @@ bool cControl::haveHandler(eDlogEvt t) const {
bool cControl::triggerClickHandler(cDialog& dlg, std::string id, eKeyMod mods){
if(recording){
std::stringstream sstr;
sstr << mods;
std::map<std::string, std::string> action_info = {{"id", id}, {"mods", sstr.str()}};
std::map<std::string, std::string> action_info = {{"id", id}, {"mods", boost::lexical_cast<std::string>(mods)}};
record_action("control_click", action_info);
}
triggerEvent<EVT_CLICK>(dlg, id, mods);

View File

@@ -4,7 +4,6 @@
#include "boe.global.hpp"
#include "tools/replay.hpp"
#include <sstream>
#include "universe/universe.hpp"
#include "boe.actions.hpp"
@@ -253,15 +252,8 @@ bool prime_time() {
void handle_spellcast(eSkill which_type, bool& did_something, bool& need_redraw, bool& need_reprint, bool record) {
if(record && recording){
std::map<std::string,std::string> info;
std::ostringstream sstr;
sstr << which_type;
info["which_type"] = sstr.str();
sstr.str("");
sstr << std::boolalpha << spell_forced;
info["spell_forced"] = sstr.str();
info["which_type"] = boost::lexical_cast<std::string>(which_type);
info["spell_forced"] = boost::lexical_cast<std::string>(spell_forced);
record_action("handle_spellcast", info);
}
short store_sp[6];
@@ -321,9 +313,7 @@ void handle_spellcast(eSkill which_type, bool& did_something, bool& need_redraw,
void handle_begin_look(bool right_button, bool& need_redraw) {
if(recording){
std::ostringstream sstr;
sstr << std::boolalpha << right_button;
record_action("handle_begin_look", sstr.str());
record_action("handle_begin_look", boost::lexical_cast<std::string>(right_button));
}
if(overall_mode == MODE_OUTDOORS) overall_mode = MODE_LOOK_OUTDOORS;
if(overall_mode == MODE_TOWN) overall_mode = MODE_LOOK_TOWN;
@@ -495,18 +485,9 @@ void handle_pause(bool& did_something, bool& need_redraw) {
void handle_look(location destination, bool right_button, eKeyMod mods, bool& need_redraw, bool& need_reprint) {
if(recording){
std::map<std::string,std::string> info;
std::ostringstream sstr;
sstr << destination;
info["destination"] = sstr.str();
sstr.str("");
sstr << std::boolalpha << right_button;
info["right_button"] = sstr.str();
sstr.str("");
sstr << mods;
info["mods"] = sstr.str();
info["destination"] = boost::lexical_cast<std::string>(destination);
info["right_button"] = boost::lexical_cast<std::string>(right_button);
info["mods"] = boost::lexical_cast<std::string>(mods);
record_action("handle_look", info);
}
need_reprint = true;
@@ -699,15 +680,8 @@ void handle_talk(location destination, bool& did_something, bool& need_redraw, b
void handle_target_space(location destination, bool& did_something, bool& need_redraw, bool& need_reprint) {
if(recording){
std::map<std::string,std::string> info;
std::ostringstream sstr;
sstr << destination;
info["destination"] = sstr.str();
sstr.str("");
sstr << num_targets_left;
info["num_targets_left"] = sstr.str();
info["destination"] = boost::lexical_cast<std::string>(destination);
info["num_targets_left"] = boost::lexical_cast<std::string>(num_targets_left);
record_action("handle_target_space", info);
}
if(overall_mode == MODE_SPELL_TARGET)
@@ -737,9 +711,7 @@ void handle_target_space(location destination, bool& did_something, bool& need_r
void handle_drop_item(location destination, bool& need_redraw) {
if(recording){
std::ostringstream sstr;
sstr << destination;
record_action("handle_drop_item_location", sstr.str());
record_action("handle_drop_item_location", boost::lexical_cast<std::string>(destination));
}
if(overall_mode == MODE_DROP_COMBAT) {
@@ -782,9 +754,7 @@ void handle_use_space_select(bool& need_reprint) {
void handle_use_space(location destination, bool& did_something, bool& need_redraw) {
if(recording){
std::ostringstream sstr;
sstr << destination;
record_action("handle_use_space", sstr.str());
record_action("handle_use_space", boost::lexical_cast<std::string>(destination));
}
if(!adjacent(destination,univ.party.town_loc))
add_string_to_buf(" Must be adjacent.");
@@ -815,13 +785,12 @@ void handle_bash_pick_select(bool& need_reprint, bool isBash) {
void handle_bash_pick(location destination, bool& did_something, bool& need_redraw, bool isBash) {
if(recording){
std::map<std::string,std::string> info;
std::ostringstream sstr;
sstr << destination;
std::string destination_str = boost::lexical_cast<std::string>(destination);
if(isBash)
record_action("handle_bash", sstr.str());
record_action("handle_bash", destination_str);
else
record_action("handle_pick", sstr.str());
record_action("handle_pick", destination_str);
}
if(!adjacent(destination,univ.party.town_loc))
add_string_to_buf(" Must be adjacent.");
@@ -1223,14 +1192,8 @@ static void handle_party_death() {
void screen_shift(int dx, int dy, bool& need_redraw) {
if(recording){
std::map<std::string,std::string> info;
std::ostringstream sstr;
sstr << dx;
info["dx"] = sstr.str();
sstr.str("");
sstr << dy;
info["dy"] = sstr.str();
info["dx"] = boost::lexical_cast<std::string>(dx);
info["dy"] = boost::lexical_cast<std::string>(dy);
record_action("screen_shift", info);
}
@@ -1707,9 +1670,7 @@ bool someone_awake() {
void handle_menu_spell(eSpell spell_picked) {
if(recording){
std::ostringstream sstr;
sstr << (int)spell_picked;
record_action("handle_menu_spell", sstr.str());
record_action("handle_menu_spell", boost::lexical_cast<std::string>((int) spell_picked));
}
eSkill spell_type = (*spell_picked).type;
@@ -1787,13 +1748,8 @@ void show_inventory() {
void give_help_and_record(short help1, short help2) {
if(recording){
std::map<std::string,std::string> info;
std::ostringstream sstr;
sstr << help1;
info["help1"] = sstr.str();
sstr.str("");
sstr << help2;
info["help2"] = sstr.str();
info["help1"] = boost::lexical_cast<std::string>(help1);
info["help2"] = boost::lexical_cast<std::string>(help2);
record_action("give_help", info);
}
give_help(help1, help2);

View File

@@ -4,7 +4,6 @@
#include <list>
#include <unordered_map>
#include <string>
#include <sstream>
#include <memory>
#include "boe.global.hpp"
@@ -40,6 +39,8 @@
#include "tools/winutil.hpp"
#include "tools/prefs.hpp"
#include "replay.hpp"
#include <boost/lexical_cast.hpp>
#ifndef MSBUILD_GITREV
#include "tools/gitrev.hpp"
@@ -444,9 +445,7 @@ void arrow_button_click(rectangle button_rect) {
if(recording){
// This action is purely cosmetic, for playing the animation and sound accompanying a click on a button whose real action
// is recorded afterward
std::ostringstream sstr;
sstr << button_rect;
record_action("arrow_button_click", sstr.str());
record_action("arrow_button_click", boost::lexical_cast<std::string>(button_rect));
}
mainPtr.setActive();
clip_rect(mainPtr, button_rect);

View File

@@ -115,15 +115,8 @@ static bool display_spells_event_filter(cDialog& me, std::string item_hit, eSkil
void display_spells(eSkill mode,short force_spell,cDialog* parent) {
if(recording){
std::map<std::string,std::string> info;
std::ostringstream sstr;
sstr << mode;
info["mode"] = sstr.str();
sstr.str("");
sstr << force_spell;
info["force_spell"] = sstr.str();
info["mode"] = boost::lexical_cast<std::string>(mode);
info["force_spell"] = boost::lexical_cast<std::string>(force_spell);
record_action("display_spells", info);
}
using namespace std::placeholders;
@@ -183,10 +176,7 @@ static bool display_skills_event_filter(cDialog& me, std::string item_hit, eKeyM
void display_skills(eSkill force_skill,cDialog* parent) {
if(recording){
std::ostringstream sstr;
sstr << force_skill;
record_action("display_skills", sstr.str());
record_action("display_skills", boost::lexical_cast<std::string>(force_skill));
}
if(force_skill != eSkill::INVALID)
skill_pos = int(force_skill);

View File

@@ -12,7 +12,6 @@
#include <memory>
#include <iostream>
#include <ctime>
#include <sstream>
#include <deque>
#include "boe.graphics.hpp"
#include "boe.newgraph.hpp"
@@ -339,9 +338,7 @@ static void replay_next_action() {
}else if(t == "close_window"){
handle_quit_event();
}else if(t == "arrow_button_click"){
rectangle button_rect;
std::istringstream sstr(next_action.GetText());
sstr >> button_rect;
rectangle button_rect = boost::lexical_cast<rectangle>(next_action.GetText());
arrow_button_click(button_rect);
}else if(t == "show_dialog_action"){
show_dialog_action(next_action.GetText());
@@ -371,21 +368,15 @@ static void replay_next_action() {
display_alchemy();
}else if(t == "display_spells"){
auto info = info_from_action(next_action);
std::istringstream sstr(info["mode"]);
sstr >> enum_v;
enum_v = boost::lexical_cast<int>(info["mode"]);
eSkill mode = static_cast<eSkill>(enum_v);
short force_spell;
sstr.str(info["force_spell"]);
sstr >> force_spell;
short force_spell = boost::lexical_cast<short>(info["force_spell"]);
display_spells(mode, force_spell, nullptr);
}else if(t == "display_skills"){
std::istringstream sstr(next_action.GetText());
sstr >> enum_v;
enum_v = boost::lexical_cast<int>(next_action.GetText());
eSkill force_skill = static_cast<eSkill>(enum_v);
display_skills(force_skill, nullptr);
}else if(t == "tip_of_day"){
tip_of_day();
@@ -396,37 +387,22 @@ static void replay_next_action() {
}else if(t == "handle_help_toc"){
handle_help_toc();
}else if(t == "menu_give_help"){
std::istringstream sstr(next_action.GetText());
short help1;
sstr >> help1;
short help1 = short_from_action(next_action);
menu_give_help(help1);
}else if(t == "handle_begin_look"){
std::istringstream sstr(next_action.GetText());
bool right_button;
sstr >> std::boolalpha >> right_button;
bool right_button = boost::lexical_cast<bool>(next_action.GetText());
handle_begin_look(right_button, need_redraw);
}else if(t == "handle_look"){
auto info = info_from_action(next_action);
std::istringstream sstr(info["destination"]);
location destination;
sstr >> destination;
sstr.str(info["right_button"]);
bool right_button;
sstr >> std::boolalpha >> right_button;
location destination = boost::lexical_cast<location>(info["destination"]);
bool right_button = boost::lexical_cast<bool>(info["right_button"]);
eKeyMod mods = static_cast<eKeyMod>(std::stoi(info["mods"]));
handle_look(destination, right_button, mods, need_redraw, need_reprint);
}else if(t == "screen_shift"){
auto info = info_from_action(next_action);
std::istringstream sstr(info["dx"]);
int dx = 0;
sstr >> dx;
sstr.str(info["dy"]);
int dy = 0;
sstr >> dy;
int dx = std::stoi(info["dx"]);
int dy = std::stoi(info["dy"]);
screen_shift(dx, dy, need_redraw);
}else if(t == "handle_rest"){
@@ -436,25 +412,14 @@ static void replay_next_action() {
handle_menu_spell(spell_picked);
}else if(t == "handle_spellcast"){
auto info = info_from_action(next_action);
std::istringstream sstr(info["which_type"]);
eSkill which_type;
sstr >> which_type;
sstr.str(info["spell_forced"]);
sstr.seekg(0);
sstr >> std::boolalpha >> spell_forced;
eSkill which_type = boost::lexical_cast<eSkill>(info["which_type"]);
spell_forced = boost::lexical_cast<bool>(info["spell_forced"]);
handle_spellcast(which_type, did_something, need_redraw, need_reprint);
}else if(t == "handle_target_space"){
auto info = info_from_action(next_action);
std::istringstream sstr(info["destination"]);
location destination;
sstr >> destination;
sstr.str(info["num_targets_left"]);
sstr.seekg(0);
sstr >> num_targets_left;
location destination = boost::lexical_cast<location>(info["destination"]);
num_targets_left = boost::lexical_cast<short>(info["num_targets_left"]);
handle_target_space(destination, did_something, need_redraw, need_reprint);
}else if(t == "spell_cast_hit_return"){
@@ -977,9 +942,7 @@ void handle_help_toc() {
void menu_give_help(short help1){
if(recording){
std::ostringstream sstr;
sstr << help1;
record_action("menu_give_help", sstr.str());
record_action("menu_give_help", boost::lexical_cast<std::string>(help1));
}
give_help(help1, 0);
}

View File

@@ -25,12 +25,11 @@
#include "gfx/render_image.hpp"
#include "tools/enum_map.hpp"
#include "replay.hpp"
#include <boost/lexical_cast.hpp>
#include <vector>
using std::vector;
#include <sstream>
extern bool party_in_memory;
extern long register_flag;
extern sf::RenderWindow mainPtr;
@@ -43,15 +42,8 @@ enum_map(eStartButton, rectangle) startup_button;
void handle_startup_button_click(eStartButton btn, eKeyMod mods) {
if(recording){
std::map<std::string, std::string> info;
std::ostringstream sstr;
sstr << btn;
info["btn"] = sstr.str();
sstr.str("");
sstr << mods;
info["mods"] = sstr.str();
info["btn"] = boost::lexical_cast<std::string>(btn);
info["mods"] = boost::lexical_cast<std::string>(mods);
record_action("startup_button_click", info);
}

View File

@@ -97,15 +97,10 @@ void record_action(std::string action_type, std::map<std::string,std::string> in
void record_field_input(cKey key) {
std::map<std::string,std::string> info;
std::ostringstream sstr;
info["spec"] = boost::lexical_cast<std::string>(key.spec);
sstr << std::boolalpha << key.spec;
info["spec"] = sstr.str();
sstr.str("");
if(key.spec){
sstr << key.k;
info["k"] = sstr.str();
info["k"] = boost::lexical_cast<std::string>(key.k);
}else{
std::wostringstream wstr;
wstr << key.c;
@@ -114,9 +109,7 @@ void record_field_input(cKey key) {
info["c"] = c;
}
sstr.str("");
sstr << key.mod;
info["mod"] = sstr.str();
info["mod"] = boost::lexical_cast<std::string>(key.mod);
record_action("field_input", info);
}
@@ -185,17 +178,11 @@ void decode_file(std::string data, fs::path file) {
}
location location_from_action(Element& action) {
location l;
std::istringstream sstr(action.GetText());
sstr >> l;
return l;
return boost::lexical_cast<location>(action.GetText());
}
short short_from_action(Element& action) {
short s;
std::istringstream sstr(action.GetText());
sstr >> s;
return s;
return boost::lexical_cast<short>(action.GetText());
}
cKey key_from_action(Element& action) {
@@ -203,12 +190,10 @@ cKey key_from_action(Element& action) {
cKey key;
int enum_v;
std::istringstream sstr(info["spec"]);
sstr >> std::boolalpha >> key.spec;
key.spec = boost::lexical_cast<bool>(info["spec"]);
if(key.spec){
sstr.str(info["k"]);
sstr >> enum_v;
enum_v = boost::lexical_cast<int>(info["k"]);
key.k = static_cast<eSpecKey>(enum_v);
}else{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
@@ -217,8 +202,7 @@ cKey key_from_action(Element& action) {
wsstr >> key.c;
}
sstr.str(info["mod"]);
sstr >> enum_v;
enum_v = boost::lexical_cast<int>(info["mod"]);
key.mod = static_cast<eKeyMod>(enum_v);
return key;