Replace usage of CParser for CColor.
Switches format to use integers only. Adds logging if the string is invalid. Refs #2589. This was SVN commit r15278.
This commit is contained in:
parent
db39d742f0
commit
2c14d94559
@ -1687,7 +1687,7 @@ void CGUI::Xeromyces_ReadEffects(XMBElement Element, CXeromyces* pFile, SGUIImag
|
||||
if (attr_name == "add_color")
|
||||
{
|
||||
CColor color;
|
||||
if (!GUI<int>::ParseColor(attr_value, color, 0.f))
|
||||
if (!GUI<int>::ParseColor(attr_value, color, 0))
|
||||
LOGERROR(L"GUI: Error parsing '%hs' (\"%ls\")", attr_name.c_str(), attr_value.c_str());
|
||||
else effects.m_AddColor = color;
|
||||
}
|
||||
@ -1933,7 +1933,7 @@ void CGUI::Xeromyces_ReadColor(XMBElement Element, CXeromyces* pFile)
|
||||
if (! value.empty())
|
||||
{
|
||||
// Try setting color to value
|
||||
if (!color.ParseString(value, 255.f))
|
||||
if (!color.ParseString(value))
|
||||
{
|
||||
LOGERROR(L"GUI: Unable to create custom color '%hs'. Invalid color syntax.", name.c_str());
|
||||
}
|
||||
|
@ -99,7 +99,7 @@ bool __ParseString<CClientArea>(const CStrW& Value, CClientArea &Output)
|
||||
}
|
||||
|
||||
template <>
|
||||
bool GUI<int>::ParseColor(const CStrW& Value, CColor &Output, float DefaultAlpha)
|
||||
bool GUI<int>::ParseColor(const CStrW& Value, CColor &Output, int DefaultAlpha)
|
||||
{
|
||||
// First, check our database in g_GUI for pre-defined colors
|
||||
// If we find anything, we'll ignore DefaultAlpha
|
||||
@ -119,7 +119,7 @@ bool __ParseString<CColor>(const CStrW& Value, CColor &Output)
|
||||
if (g_GUI->GetPreDefinedColor(Value.ToUTF8(), Output))
|
||||
return true;
|
||||
|
||||
return Output.ParseString(Value.ToUTF8(), 255.f);
|
||||
return Output.ParseString(Value.ToUTF8());
|
||||
}
|
||||
|
||||
template <>
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* Copyright (C) 2009 Wildfire Games.
|
||||
/* Copyright (C) 2014 Wildfire Games.
|
||||
* This file is part of 0 A.D.
|
||||
*
|
||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
||||
@ -235,7 +235,7 @@ public:
|
||||
return __ParseString<T>(Value, tOutput);
|
||||
}
|
||||
|
||||
static bool ParseColor(const CStrW& Value, CColor &tOutput, float DefaultAlpha);
|
||||
static bool ParseColor(const CStrW& Value, CColor &tOutput, int DefaultAlpha);
|
||||
|
||||
private:
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* Copyright (C) 2013 Wildfire Games.
|
||||
/* Copyright (C) 2014 Wildfire Games.
|
||||
* This file is part of 0 A.D.
|
||||
*
|
||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
||||
@ -24,31 +24,49 @@ Overlay.cpp
|
||||
#include <string>
|
||||
|
||||
#include "Overlay.h"
|
||||
#include "Parser.h"
|
||||
#include "CLogger.h"
|
||||
#include "CStr.h"
|
||||
|
||||
|
||||
bool CColor::ParseString(const CStr8& Value, float DefaultAlpha)
|
||||
/**
|
||||
* Try to parse @p Value as a color. Returns true on success, false otherwise.
|
||||
* @param Value Should be "r g b" or "r g b a" where each value is an integer in [0,255].
|
||||
* @param DefaultAlpha The alpha value that is used if the format of @p Value is "r g b".
|
||||
*/
|
||||
bool CColor::ParseString(const CStr8& Value, int DefaultAlpha)
|
||||
{
|
||||
// Use the parser to parse the values
|
||||
CParser& parser (CParserCache::Get("_[-$arg(_minus)]$value_[-$arg(_minus)]$value_[-$arg(_minus)]$value_[[-$arg(_minus)]$value_]"));
|
||||
|
||||
std::string str = Value;
|
||||
|
||||
CParserLine line;
|
||||
line.ParseString(parser, str);
|
||||
if (!line.m_ParseOK)
|
||||
unsigned int NUM_VALS= 4;
|
||||
int values[NUM_VALS] = { 0, 0, 0, DefaultAlpha };
|
||||
std::stringstream stream;
|
||||
stream.str(Value);
|
||||
// Parse each value
|
||||
size_t i;
|
||||
for (i = 0; i < NUM_VALS; ++i)
|
||||
{
|
||||
// TODO Gee: Parsing failed
|
||||
return false;
|
||||
}
|
||||
float values[4] = { 0, 0, 0, DefaultAlpha };
|
||||
for (int i=0; i<(int)line.GetArgCount(); ++i)
|
||||
{
|
||||
if (!line.GetArgFloat(i, values[i]))
|
||||
if (stream.eof())
|
||||
break;
|
||||
|
||||
stream >> values[i];
|
||||
if ((stream.rdstate() & std::stringstream::failbit) != 0)
|
||||
{
|
||||
// Parsing failed
|
||||
LOGWARNING(L"Unable to parse CColor parameters. Your input: '%s'", Value.c_str());
|
||||
return false;
|
||||
}
|
||||
if (values[i] < 0 || values[i] > 255)
|
||||
{
|
||||
LOGWARNING(L"Invalid value (<0 or >255) when parsing CColor parameters. Your input: '%s'", Value.c_str());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (i < 3)
|
||||
{
|
||||
LOGWARNING(L"Not enough parameters when parsing as CColor. Your input: '%s'", Value.c_str());
|
||||
return false;
|
||||
}
|
||||
if (!stream.eof())
|
||||
{
|
||||
LOGWARNING(L"Too many parameters when parsing as CColor. Your input: '%s'", Value.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
r = values[0]/255.f;
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* Copyright (C) 2013 Wildfire Games.
|
||||
/* Copyright (C) 2014 Wildfire Games.
|
||||
* This file is part of 0 A.D.
|
||||
*
|
||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
||||
@ -36,7 +36,7 @@ struct CColor
|
||||
CColor() : r(-1.f), g(-1.f), b(-1.f), a(1.f) {}
|
||||
CColor(float cr,float cg,float cb,float ca) : r(cr), g(cg), b(cb), a(ca) {}
|
||||
|
||||
bool ParseString(const CStr8& Value, float DefaultAlpha);
|
||||
bool ParseString(const CStr8& Value, int DefaultAlpha = 255);
|
||||
|
||||
bool operator == (const CColor &color) const;
|
||||
|
||||
|
80
source/ps/tests/test_CColor.h
Normal file
80
source/ps/tests/test_CColor.h
Normal file
@ -0,0 +1,80 @@
|
||||
/* Copyright (C) 2014 Wildfire Games.
|
||||
* This file is part of 0 A.D.
|
||||
*
|
||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 0 A.D. is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "lib/self_test.h"
|
||||
|
||||
#include "ps/Overlay.h"
|
||||
#include "ps/CLogger.h"
|
||||
|
||||
class TestCColor : public CxxTest::TestSuite
|
||||
{
|
||||
public:
|
||||
void test_parse()
|
||||
{
|
||||
TestLogger nolog;
|
||||
#define CHECK_CCOLOR_EQUAL(v,r,g,b,a) \
|
||||
{ \
|
||||
CStr str = v; \
|
||||
CColor c; \
|
||||
TS_ASSERT(c.ParseString(str, 255)); \
|
||||
CColor c_(r/255.f,g/255.f,b/255.f,a/255.f); \
|
||||
TS_ASSERT_EQUALS(c, c_); \
|
||||
}
|
||||
|
||||
CHECK_CCOLOR_EQUAL("0 0 0 0", 0, 0, 0, 0);
|
||||
CHECK_CCOLOR_EQUAL("255 0 255 0", 255, 0, 255, 0);
|
||||
CHECK_CCOLOR_EQUAL("0 123 0 0", 0, 123, 0, 0);
|
||||
CHECK_CCOLOR_EQUAL("0 0 0 55", 0, 0, 0, 55);
|
||||
CHECK_CCOLOR_EQUAL("76 24 0", 76, 24, 0, 255);
|
||||
CHECK_CCOLOR_EQUAL("159 98 24", 159, 98, 24, 255);
|
||||
#undef CHECK_CCOLOR_EQUAL
|
||||
}
|
||||
|
||||
void test_parse_failure()
|
||||
{
|
||||
TestLogger nolog;
|
||||
|
||||
#define CHECK_CCOLOR_FAIL(v) \
|
||||
{ \
|
||||
CStr str = v; \
|
||||
CColor a; \
|
||||
TS_ASSERT(!a.ParseString(str, 255)); \
|
||||
}
|
||||
|
||||
CHECK_CCOLOR_FAIL("abc");
|
||||
CHECK_CCOLOR_FAIL("0.123 1 2 3");
|
||||
CHECK_CCOLOR_FAIL("0 0 0 0 adfasd");
|
||||
CHECK_CCOLOR_FAIL("0 a0 b0 ax");
|
||||
CHECK_CCOLOR_FAIL("-124dsaf");
|
||||
CHECK_CCOLOR_FAIL("\0");
|
||||
CHECK_CCOLOR_FAIL("1 2 xxxx");
|
||||
|
||||
// Not enough parameters
|
||||
CHECK_CCOLOR_FAIL("");
|
||||
CHECK_CCOLOR_FAIL("124");
|
||||
CHECK_CCOLOR_FAIL("0 55");
|
||||
|
||||
// More parameters than allowed
|
||||
CHECK_CCOLOR_FAIL("0 5 1 5 6");
|
||||
|
||||
// Out of bounds
|
||||
CHECK_CCOLOR_FAIL("-1 0 0");
|
||||
CHECK_CCOLOR_FAIL("256 0 0 0");
|
||||
CHECK_CCOLOR_FAIL("255 256 -1 0");
|
||||
#undef CHECK_CCLOLO_FAIL
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user