cmdlineparser: terminate with error message if --append=<filename> fails

Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
Stefan Weil 2011-11-12 10:43:26 +01:00
parent c317c8979c
commit d11f18b5df
3 changed files with 13 additions and 4 deletions

View File

@ -267,8 +267,13 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
_settings->_errorsOnly = true; _settings->_errorsOnly = true;
// Append userdefined code to checked source code // Append userdefined code to checked source code
else if (strncmp(argv[i], "--append=", 9) == 0) else if (strncmp(argv[i], "--append=", 9) == 0) {
_settings->append(9 + argv[i]); const std::string filename = 9 + argv[i];
if (!_settings->append(filename)) {
PrintMessage("cppcheck: Couldn't open the file: \"" + filename + "\".");
return false;
}
}
else if (strncmp(argv[i], "--enable=", 9) == 0) { else if (strncmp(argv[i], "--enable=", 9) == 0) {
const std::string errmsg = _settings->addEnabled(argv[i] + 9); const std::string errmsg = _settings->addEnabled(argv[i] + 9);

View File

@ -118,14 +118,18 @@ bool Settings::isEnabled(const std::string &str) const
} }
void Settings::append(const std::string &filename) bool Settings::append(const std::string &filename)
{ {
_append = "\n"; _append = "\n";
std::ifstream fin(filename.c_str()); std::ifstream fin(filename.c_str());
if (!fin.is_open()) {
return false;
}
std::string line; std::string line;
while (std::getline(fin, line)) { while (std::getline(fin, line)) {
_append += line + "\n"; _append += line + "\n";
} }
return true;
} }
std::string Settings::append() const std::string Settings::append() const

View File

@ -116,7 +116,7 @@ public:
std::list<std::string> _includePaths; std::list<std::string> _includePaths;
/** @brief assign append code (--append) */ /** @brief assign append code (--append) */
void append(const std::string &filename); bool append(const std::string &filename);
/** @brief get append code (--append) */ /** @brief get append code (--append) */
std::string append() const; std::string append() const;