Fix a few string bugs add StartsWith for strings

This commit is contained in:
Tobias Loose
2013-11-29 10:56:33 +01:00
parent 9d5a83d588
commit 672b0be7bd
3 changed files with 44 additions and 18 deletions
+16
View File
@@ -140,4 +140,20 @@ template <class T> istream& operator>>(istream& in, T& p)
/* replace_all ... replacement to avoid depending on boost for that */ /* replace_all ... replacement to avoid depending on boost for that */
void ReplaceString(std::string& subject, const std::string& search, const std::string& replace); void ReplaceString(std::string& subject, const std::string& search, const std::string& replace);
/*! \brief Returns true if base starts with start, otherwise false
*
* Compares the first strlen(start) characters of base with start and
* returns true if both match.
*/
bool StartsWith(const std::string& base, const char *start);
/*! \brief Returns true if base starts with start, otherwise false
*
* This version is slightly more efficient as strlen does not need to
* get called. Otherwise, it behaves exactly like
* StartsWith(const std::string& base, const char *start)
*
* \see StartsWith(const std::string& base, const char *start)
*/
bool StartsWith(const std::string& base, const std::string& start);
#endif #endif
+18 -18
View File
@@ -47,15 +47,15 @@ static inline int getGrade(int v)
int MTGAllCards::processConfLine(string &s, MTGCard *card, CardPrimitive * primitive) int MTGAllCards::processConfLine(string &s, MTGCard *card, CardPrimitive * primitive)
{ {
if ('#' == s[0]) return 1; // a comment shouldn't be treated as an error condition if ('#' == s[0]) return 1; // a comment shouldn't be treated as an error condition
size_t i = s.find_first_of('='); size_t del_pos = s.find_first_of('=');
if (i == string::npos || 0 == i) if (del_pos == string::npos || 0 == del_pos)
return 0; return 0;
s[i] = '\0'; s[del_pos] = '\0';
const string key = s.substr(0, i); const string key = s.substr(0, del_pos);
const char* val = s.c_str()+i+1; const string val = s.substr(del_pos + 1);
switch (s[0]) switch (key[0])
{ {
case 'a': case 'a':
if (key == "auto") if (key == "auto")
@@ -63,7 +63,7 @@ int MTGAllCards::processConfLine(string &s, MTGCard *card, CardPrimitive * primi
if (!primitive) primitive = NEW CardPrimitive(); if (!primitive) primitive = NEW CardPrimitive();
primitive->addMagicText(val); primitive->addMagicText(val);
} }
else if (key.compare(0, strlen("auto")-1, "auto") == 0) else if (StartsWith(key, "auto"))
{ {
if (!primitive) primitive = NEW CardPrimitive(); if (!primitive) primitive = NEW CardPrimitive();
primitive->addMagicText(val, key.substr(4)); primitive->addMagicText(val, key.substr(4));
@@ -71,7 +71,7 @@ int MTGAllCards::processConfLine(string &s, MTGCard *card, CardPrimitive * primi
else if (key == "alias") else if (key == "alias")
{ {
if (!primitive) primitive = NEW CardPrimitive(); if (!primitive) primitive = NEW CardPrimitive();
primitive->alias = atoi(val); primitive->alias = atoi(val.c_str());
} }
else if (key == "abilities") else if (key == "abilities")
{ {
@@ -152,12 +152,12 @@ int MTGAllCards::processConfLine(string &s, MTGCard *card, CardPrimitive * primi
} }
case 'g': //grade case 'g': //grade
if (s.size() - i - 1 > 2) currentGrade = getGrade(val[2]); if (s.size() - del_pos - 1 > 2) currentGrade = getGrade(val[2]);
break; break;
case 'i': //id case 'i': //id
if (!card) card = NEW MTGCard(); if (!card) card = NEW MTGCard();
card->setMTGId(atoi(val)); card->setMTGId(atoi(val.c_str()));
break; break;
case 'k': //kicker case 'k': //kicker
@@ -222,7 +222,7 @@ int MTGAllCards::processConfLine(string &s, MTGCard *card, CardPrimitive * primi
break; break;
case 'p': case 'p':
if ('r' == key[1]) if (key[1] == 'r')
{ // primitive { // primitive
if (!card) card = NEW MTGCard(); if (!card) card = NEW MTGCard();
map<string, CardPrimitive*>::iterator it = primitives.find(val); map<string, CardPrimitive*>::iterator it = primitives.find(val);
@@ -231,18 +231,18 @@ int MTGAllCards::processConfLine(string &s, MTGCard *card, CardPrimitive * primi
else else
{ //power { //power
if (!primitive) primitive = NEW CardPrimitive(); if (!primitive) primitive = NEW CardPrimitive();
primitive->setPower(atoi(val)); primitive->setPower(atoi(val.c_str()));
} }
break; break;
case 'r': //retrace/rarity//restrictions case 'r': //retrace/rarity//restrictions
if('s' == key[2] && 't' == key[3])//restrictions if(key[2] == 's' && key[3] == 't')//restrictions
{ {
if (!primitive) primitive = NEW CardPrimitive(); if (!primitive) primitive = NEW CardPrimitive();
string value = val; string value = val;
primitive->setRestrictions(value); primitive->setRestrictions(value);
} }
else if ('e' == key[1] && 't' == key[2]) else if (key[1] == 'e' && key[2] == 't')
{ //retrace { //retrace
if (!primitive) primitive = NEW CardPrimitive(); if (!primitive) primitive = NEW CardPrimitive();
if (ManaCost * cost = primitive->getManaCost()) if (ManaCost * cost = primitive->getManaCost())
@@ -279,7 +279,7 @@ int MTGAllCards::processConfLine(string &s, MTGCard *card, CardPrimitive * primi
else else
{ {
if (!primitive) primitive = NEW CardPrimitive(); if (!primitive) primitive = NEW CardPrimitive();
vector<string> values = split(val, ' '); vector<string> values = split(val.c_str(), ' ');
for (size_t values_i = 0; values_i < values.size(); ++values_i) for (size_t values_i = 0; values_i < values.size(); ++values_i)
primitive->setSubtype(values[values_i]); primitive->setSubtype(values[values_i]);
} }
@@ -302,7 +302,7 @@ int MTGAllCards::processConfLine(string &s, MTGCard *card, CardPrimitive * primi
for (size_t values_i = 0; values_i < values.size(); ++values_i) for (size_t values_i = 0; values_i < values.size(); ++values_i)
primitive->setType(values[values_i]); primitive->setType(values[values_i]);
} }
else if (key == "toughness") primitive->setToughness(atoi(val)); else if (key == "toughness") primitive->setToughness(atoi(val.c_str()));
break; break;
default: default:
@@ -317,7 +317,7 @@ int MTGAllCards::processConfLine(string &s, MTGCard *card, CardPrimitive * primi
tempPrimitive = primitive; tempPrimitive = primitive;
tempCard = card; tempCard = card;
return i; return del_pos;
} }
+10
View File
@@ -399,3 +399,13 @@ void ReplaceString(std::string& subject, const std::string& search, const std::s
} }
} }
bool StartsWith(const std::string& base, const char *start)
{
return base.compare(0, strlen(start), start) == 0;
}
bool StartsWith(const std::string& base, const std::string& start)
{
return base.compare(0, start.length(), start) == 0;
}