factor out addSuppressionLine from file reading function

This commit is contained in:
Greg Hewgill 2011-02-16 22:26:16 +13:00
parent 8a85b18283
commit 331788246b
2 changed files with 56 additions and 40 deletions

View File

@ -70,46 +70,7 @@ std::string Settings::Suppressions::parseFile(std::istream &istr)
if (line.length() >= 2 && line[0] == '/' && line[1] == '/') if (line.length() >= 2 && line[0] == '/' && line[1] == '/')
continue; continue;
std::istringstream lineStream(line); const std::string errmsg(addSuppressionLine(line));
std::string id;
std::string file;
unsigned int lineNumber = 0;
if (std::getline(lineStream, id, ':'))
{
if (std::getline(lineStream, file))
{
// If there is not a dot after the last colon in "file" then
// the colon is a separator and the contents after the colon
// is a line number..
// Get position of last colon
const std::string::size_type pos = file.rfind(":");
// if a colon is found and there is no dot after it..
if (pos != std::string::npos &&
file.find(".", pos) == std::string::npos)
{
// Try to parse out the line number
try
{
std::istringstream istr1(file.substr(pos+1));
istr1 >> lineNumber;
}
catch (...)
{
lineNumber = 0;
}
if (lineNumber > 0)
{
file.erase(pos);
}
}
}
}
// We could perhaps check if the id is valid and return error if it is not
const std::string errmsg(addSuppression(id, file, lineNumber));
if (!errmsg.empty()) if (!errmsg.empty())
return errmsg; return errmsg;
} }
@ -117,6 +78,54 @@ std::string Settings::Suppressions::parseFile(std::istream &istr)
return ""; return "";
} }
std::string Settings::Suppressions::addSuppressionLine(const std::string &line)
{
std::istringstream lineStream(line);
std::string id;
std::string file;
unsigned int lineNumber = 0;
if (std::getline(lineStream, id, ':'))
{
if (std::getline(lineStream, file))
{
// If there is not a dot after the last colon in "file" then
// the colon is a separator and the contents after the colon
// is a line number..
// Get position of last colon
const std::string::size_type pos = file.rfind(":");
// if a colon is found and there is no dot after it..
if (pos != std::string::npos &&
file.find(".", pos) == std::string::npos)
{
// Try to parse out the line number
try
{
std::istringstream istr1(file.substr(pos+1));
istr1 >> lineNumber;
}
catch (...)
{
lineNumber = 0;
}
if (lineNumber > 0)
{
file.erase(pos);
}
}
}
}
// We could perhaps check if the id is valid and return error if it is not
const std::string errmsg(addSuppression(id, file, lineNumber));
if (!errmsg.empty())
return errmsg;
return "";
}
bool Settings::Suppressions::FileMatcher::match(const std::string &pattern, const std::string &name) bool Settings::Suppressions::FileMatcher::match(const std::string &pattern, const std::string &name)
{ {
const char *p = pattern.c_str(); const char *p = pattern.c_str();

View File

@ -180,6 +180,13 @@ public:
*/ */
std::string parseFile(std::istream &istr); std::string parseFile(std::istream &istr);
/**
* @brief Don't show the given error.
* @param str Description of error to suppress (in id:file:line format).
* @return error message. empty upon success
*/
std::string addSuppressionLine(const std::string &line);
/** /**
* @brief Don't show this error. If file and/or line are optional. In which case * @brief Don't show this error. If file and/or line are optional. In which case
* the errorId alone is used for filtering. * the errorId alone is used for filtering.