made strin2rgb unthrowing (fix for bug #19052)

Updated all callers.

Some callers were calling it without catching the bad_lexical_cast it
could throw.
This commit is contained in:
Anonymissimus 2011-11-25 22:54:24 +00:00
parent 1eb7ec9851
commit 5ffabe7d14
5 changed files with 60 additions and 40 deletions

View file

@ -87,7 +87,8 @@ std::map<Uint32, Uint32> recolor_range(const color_range& new_range, const std::
return map_rgb;
}
std::vector<Uint32> string2rgb(std::string s){
bool string2rgb(const std::string& s, std::vector<Uint32>& result) {
result = std::vector<Uint32>();
std::vector<Uint32> out;
std::vector<std::string> rgb_vec = utils::split(s);
std::vector<std::string>::iterator c=rgb_vec.begin();
@ -96,28 +97,33 @@ std::vector<Uint32> string2rgb(std::string s){
Uint32 rgb_hex;
if(c->length() != 6)
{
// integer triplets, e.g. white="255,255,255"
rgb_hex = (0x00FF0000 & ((lexical_cast<int>(*c++))<<16)); //red
if(c!=rgb_vec.end())
{
rgb_hex += (0x0000FF00 & ((lexical_cast<int>(*c++))<<8)); //green
try {
// integer triplets, e.g. white="255,255,255"
rgb_hex = (0x00FF0000 & ((lexical_cast<int>(*c++))<<16)); //red
if(c!=rgb_vec.end())
{
rgb_hex += (0x000000FF & ((lexical_cast<int>(*c++))<<0)); //blue
rgb_hex += (0x0000FF00 & ((lexical_cast<int>(*c++))<<8)); //green
if(c!=rgb_vec.end())
{
rgb_hex += (0x000000FF & ((lexical_cast<int>(*c++))<<0)); //blue
}
}
} catch (bad_lexical_cast&) {
return false;
}
} else {
// hexadecimal format, e.g. white="FFFFFF"
char* endptr;
rgb_hex = (0x00FFFFFF & strtol(c->c_str(), &endptr, 16));
if (*endptr != '\0') {
throw bad_lexical_cast();
return false;
}
++c;
}
out.push_back(rgb_hex);
}
return(out);
result = out;
return true;
}
std::vector<Uint32> palette(color_range cr){

View file

@ -31,8 +31,10 @@
#include "SDL_types.h"
/** Convert comma separated string into rgb values. */
std::vector<Uint32> string2rgb(std::string s);
/* Convert comma separated string into rgb values.
* Return false and empty result on error.
*/
bool string2rgb(const std::string& s, std::vector<Uint32>& result);
/**
* A color range definition is made of four reference RGB colors, used

View file

@ -228,20 +228,34 @@ namespace game_config
if (const config::attribute_value *a = v.get("flag_rgb"))
flag_rgb = a->str();
red_green_scale = string2rgb(v["red_green_scale"]);
std::string color_string = v["red_green_scale"].str();
if(!string2rgb(color_string, red_green_scale)) {
ERR_NG << "can't parse color string red_green_scale, ignoring: " << color_string << "\n";
}
if (red_green_scale.empty()) {
red_green_scale.push_back(0x00FFFF00);
}
red_green_scale_text = string2rgb(v["red_green_scale_text"]);
color_string = v["red_green_scale_text"].str();
if(!string2rgb(color_string, red_green_scale_text)) {
ERR_NG << "can't parse color string red_green_scale_text, ignoring: " << color_string << "\n";
}
if (red_green_scale_text.empty()) {
red_green_scale_text.push_back(0x00FFFF00);
}
blue_white_scale = string2rgb(v["blue_white_scale"]);
color_string = v["blue_white_scale"].str();
if(!string2rgb(color_string, blue_white_scale)) {
ERR_NG << "can't parse color string blue_white_scale, ignoring: " << color_string << "\n";
}
if (blue_white_scale.empty()) {
blue_white_scale.push_back(0x00FFFFFF);
}
blue_white_scale_text = string2rgb(v["blue_white_scale_text"]);
color_string = v["blue_white_scale_text"].str();
if(!string2rgb(color_string, blue_white_scale_text)) {
ERR_NG << "can't parse color string blue_white_scale_text, ignoring: " << color_string << "\n";
}
if (blue_white_scale_text.empty()) {
blue_white_scale_text.push_back(0x00FFFFFF);
}
@ -264,7 +278,12 @@ namespace game_config
*a2 = teamC.get("rgb");
if (!a1 || !a2) continue;
std::string id = *a1;
std::vector<Uint32> temp = string2rgb(*a2);
std::vector<Uint32> temp;
if(!string2rgb(*a2, temp)) {
std::stringstream ss;
ss << "can't parse color string:\n" << teamC.debug() << "\n";
throw config::error(ss.str());
}
team_rgb_range.insert(std::make_pair(id,color_range(temp)));
team_rgb_name[id] = teamC["name"];
//generate palette of same name;
@ -291,12 +310,11 @@ namespace game_config
{
foreach (const config::attribute &rgb, cp.attribute_range())
{
try {
team_rgb_colors.insert(std::make_pair(rgb.first, string2rgb(rgb.second)));
} catch(bad_lexical_cast&) {
//throw config::error(_("Invalid team color: ") + rgb_it->second);
std::vector<Uint32> temp;
if(!string2rgb(rgb.second, temp)) {
ERR_NG << "Invalid team color: " << rgb.second << "\n";
}
team_rgb_colors.insert(std::make_pair(rgb.first, temp));
}
}
}
@ -305,14 +323,11 @@ namespace game_config
{
std::map<std::string, color_range>::const_iterator i = team_rgb_range.find(name);
if(i == team_rgb_range.end()) {
try {
team_rgb_range.insert(std::make_pair(name,color_range(string2rgb(name))));
return color_info(name);
} catch(bad_lexical_cast&) {
//ERR_NG << "Invalid color range: " << name;
//return color_info();
std::vector<Uint32> temp;
if(!string2rgb(name, temp))
throw config::error(_("Invalid color range: ") + name);
}
team_rgb_range.insert(std::make_pair(name,color_range(temp)));
return color_info(name);
}
return i->second;
}
@ -321,15 +336,14 @@ namespace game_config
{
std::map<std::string, std::vector<Uint32> >::const_iterator i = team_rgb_colors.find(name);
if(i == team_rgb_colors.end()) {
try {
team_rgb_colors.insert(std::make_pair(name,string2rgb(name)));
return tc_info(name);
} catch(bad_lexical_cast&) {
std::vector<Uint32> temp;
if(!string2rgb(name, temp)) {
static std::vector<Uint32> stv;
//throw config::error(_("Invalid team color: ") + name);
ERR_NG << "Invalid team color: " << name << "\n";
return stv;
}
team_rgb_colors.insert(std::make_pair(name,temp));
return tc_info(name);
}
return i->second;
}

View file

@ -322,12 +322,7 @@ void terrain_label::read(const config &cfg)
if(!tmp_color.empty()) {
std::vector<Uint32> temp_rgb;
try {
temp_rgb = string2rgb(tmp_color);
} catch(bad_lexical_cast&) {
//throw config::error(_("Invalid color range: ") + name);
}
if(!temp_rgb.empty()) {
if(string2rgb(tmp_color, temp_rgb) && !temp_rgb.empty()) {
color = int_to_color(temp_rgb[0]);
}
}

View file

@ -155,8 +155,11 @@ void team::team_info::read(const config &cfg)
std::map<std::string, color_range>::iterator global_rgb = game_config::team_rgb_range.find(cfg["side"]);
if(!temp_rgb_str.empty()){
std::vector<Uint32> temp_rgb = string2rgb(temp_rgb_str);
team_color_range_[side] = color_range(temp_rgb);
std::vector<Uint32> temp_rgb;
if(string2rgb(temp_rgb_str, temp_rgb))
team_color_range_[side] = color_range(temp_rgb);
else
WRN_NG << "can't parse color string, ignoring: " << temp_rgb_str << "\n";
}else if(global_rgb != game_config::team_rgb_range.end()){
team_color_range_[side] = global_rgb->second;
}