improvement to macros to handle arguments with spaces

This commit is contained in:
Dave White 2004-04-21 22:32:44 +00:00
parent dc7eab7dc9
commit d5ee7c5f7a
3 changed files with 87 additions and 8 deletions

View file

@ -3,7 +3,8 @@
id=dawn
name=Dawn
image=Dawn.png
mask=terrain/dawn.png
red=-20
green=-20
[/time]
[illuminated_time]
id=morning
@ -48,7 +49,8 @@ lawful_bonus=25
id=dusk
name=Dusk
image=Dusk.png
mask=terrain/dusk.png
green=-20
blue=-20
[/time]
[illuminated_time]
id=afternoon
@ -64,13 +66,16 @@ id=first_watch
name=First Watch
image=FirstWatch.png
lawful_bonus=-25
mask=terrain/night.png
red=-40
green=-40
blue=-10
[/time]
[illuminated_time]
id=dusk
name=Dusk
image=Dusk.png
mask=terrain/dusk.png
green=-20
blue=-20
[/illuminated_time]
#enddef
@ -80,13 +85,16 @@ id=second_watch
name=Second Watch
image=SecondWatch.png
lawful_bonus=-25
mask=terrain/night.png
red=-40
green=-40
blue=-10
[/time]
[illuminated_time]
id=dusk
name=Dusk
image=Dusk.png
mask=terrain/dusk.png
green=-20
blue=-20
[/illuminated_time]
#enddef

View file

@ -145,6 +145,47 @@ void write_file(const std::string& fname, const std::string& data)
namespace {
//this function takes a macro and parses it into the macro followed by its
//arguments. Arguments are seperated by spaces, but an argument appearing inside
//braces is treated as a single argument.
std::vector<std::string> parse_macro_arguments(const std::string& macro)
{
const std::vector<std::string> args = config::split(macro,' ');
std::vector<std::string> res;
if(args.empty()) {
res.push_back("");
return res;
}
res.push_back(args.front());
bool in_braces = false;
for(std::vector<std::string>::const_iterator i = args.begin()+1; i != args.end(); ++i) {
size_t begin = 0, end = i->size();
if((*i)[0] == '{') {
++begin;
}
if((*i)[i->size()-1] == '}') {
in_braces = false;
--end;
}
if(!in_braces) {
res.push_back("");
}
res.back() += " " + i->substr(begin,end-begin);
config::strip(res.back());
if(begin == 1) {
in_braces = true;
}
}
return res;
}
void internal_preprocess_file(const std::string& fname,
preproc_map& defines_map,
int depth, std::vector<char>& res,
@ -165,8 +206,18 @@ void internal_preprocess_data(const std::string& data,
}
if(c == '{') {
int bracket_depth = 1;
std::stringstream newfile;
for(++i; i != data.end() && *i != '}'; ++i) {
for(++i; i != data.end(); ++i) {
if(*i == '{') {
bracket_depth++;
} else if(*i == '}') {
bracket_depth--;
if(bracket_depth == 0) {
break;
}
}
newfile << *i;
}
@ -174,7 +225,7 @@ void internal_preprocess_data(const std::string& data,
break;
const std::string newfilename = newfile.str();
std::vector<std::string> items = config::split(newfilename,' ');
std::vector<std::string> items = parse_macro_arguments(newfilename);
const std::string symbol = items.front();
//if this is a known pre-processing symbol, then we insert

View file

@ -721,6 +721,26 @@ bool event_handler::handle_event_command(const queued_event& event_info, const s
}
//displaying a message on-screen
else if(cmd == "print") {
const std::string& text = cfg["text"];
const std::string& id = cfg["id"];
const int size = lexical_cast_default<int>(cfg["size"],12);
const int lifetime = lexical_cast_default<int>(cfg["duration"],20);
const int red = lexical_cast_default<int>(cfg["red"],0);
const int green = lexical_cast_default<int>(cfg["red"],0);
const int blue = lexical_cast_default<int>(cfg["red"],0);
SDL_Color colour = {red,green,blue,255};
const std::string& msg = translate_string_default(id,text);
if(msg != "") {
const SDL_Rect rect = screen->map_area();
font::add_floating_label(msg,size,colour,rect.w/2,rect.h/2,
0.0,0.0,lifetime,rect,font::CENTER_ALIGN);
}
}
//displaying a message dialog
else if(cmd == "message") {
std::map<gamemap::location,unit>::iterator speaker = units->end();