Slowly going away from big config.cpp

This commit is contained in:
Guillaume Melquiond 2005-02-20 20:46:48 +00:00
parent ff4c75fc0f
commit 6eb0df334a
3 changed files with 52 additions and 7 deletions

View file

@ -33,6 +33,7 @@
#include "unit_display.hpp"
#include "util.hpp"
#include "wassert.hpp"
#include "serialization/string_utils.hpp"
#include "widgets/menu.hpp"
#include <cmath>
@ -1764,7 +1765,7 @@ size_t move_unit(display* disp, const game_data& gamedata,
msg << '\n' << _("(press $hotkey to continue)");
}
}
const std::string message = config::interpolate_variables_into_string(msg.str(),&symbols);
const std::string message = utils::interpolate_variables_into_string(msg.str(), &symbols);
font::add_floating_label(message,font::SIZE_XLARGE,font::BAD_COLOUR,
disp->map_area().w/2,disp->map_area().h/3,

View file

@ -17,8 +17,8 @@
#include <string>
#include <vector>
#include "SDL.h"
#include "config.hpp"
#include "util.hpp"
#include "serialization/string_utils.hpp"
template<typename T>
class void_value
@ -46,9 +46,6 @@ public:
animated(const std::string &cfg, const string_initializer& init=string_initializer());
// //if T can be constructed from a config&, you may use this constructor
//animated(const config& cfg, const std::string& tag);
// Adds a void frame
void add_frame(int start);
@ -142,13 +139,13 @@ animated<T,T_void_value>::animated(const std::string &cfg, const string_initiali
start_ticks_(0),
acceleration_(1)
{
std::vector<std::string> items = config::split(cfg);
std::vector<std::string> items = utils::split(cfg);
int current_time = 0;
std::vector<std::string>::const_iterator itor = items.begin();
for(; itor != items.end(); ++itor) {
const std::vector<std::string>& items = config::split(*itor, ':');
const std::vector<std::string>& items = utils::split(*itor, ':');
std::string str;
int time;

View file

@ -16,6 +16,42 @@
#include "serialization/string_utils.hpp"
namespace game_events {
std::string const &get_variable_const(std::string const &varname);
}
namespace {
bool not_id(char c)
{
return !isdigit(c) && !isalpha(c) && c != '.' && c != '_';
}
void do_interpolation(std::string &res, size_t npos, utils::string_map const *m)
{
const std::string::iterator i = std::find(res.begin() + npos, res.end(), '$');
if (i == res.end() || i + 1 == res.end())
return;
npos = i - res.begin();
const std::string::iterator end = std::find_if(i + 1, res.end(), not_id);
const std::string key(i + 1, end);
res.erase(i, end);
if (m != NULL) {
const utils::string_map::const_iterator itor = m->find(key);
if (itor != m->end())
res.insert(npos,itor->second);
} else
res.insert(npos, game_events::get_variable_const(key));
do_interpolation(res,npos,m);
}
}
namespace utils {
bool isnewline(char c)
@ -89,6 +125,17 @@ std::vector< std::string > split(std::string const &val, char c, int flags)
return res;
}
std::string interpolate_variables_into_string(std::string const &str, string_map const *symbols)
{
std::string res = str;
do_interpolation(res, 0, symbols);
//remove any pipes in the string, as they are used simply to seperate variables
res.erase(std::remove(res.begin(),res.end(),'|'),res.end());
return res;
}
}