Library: If load from current path fails, try to load 'default' configuration from cppcheck-executable path. Allow that '.cfg' extension is not used. Allow that multiple configurations are provided (comma separated).

This commit is contained in:
Daniel Marjamäki 2013-07-20 10:21:05 +02:00
parent 1dc8b1706b
commit 1a2aaa6780
3 changed files with 33 additions and 6 deletions

View File

@ -482,8 +482,8 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// --library
else if (std::strncmp(argv[i], "--library=", 10) == 0) {
if (!_settings->library.load(argv[i]+10)) {
PrintMessage("cppcheck: Failed to load library file '" + std::string(argv[i]+10) + "'");
if (!_settings->library.load(argv[0], argv[i]+10)) {
PrintMessage("cppcheck: Failed to load library configuration file '" + std::string(argv[i]+10) + "'");
return false;
}
}

View File

@ -48,12 +48,39 @@ Library::Library(const Library &lib) :
Library::~Library() { }
bool Library::load(const char path[])
bool Library::load(const char exename[], const char path[])
{
tinyxml2::XMLDocument doc;
const tinyxml2::XMLError error = doc.LoadFile(path);
if (error != tinyxml2::XML_NO_ERROR)
if (std::strchr(path,',') != NULL) {
bool ret = true;
std::string p(path);
while (p.find(",") != std::string::npos) {
const std::string::size_type pos = p.find(",");
ret &= load(exename, p.substr(0,pos).c_str());
p = p.substr(pos+1);
}
if (!p.empty())
ret &= load(exename, p.c_str());
return ret;
}
FILE *fp = fopen(path, "rt");
if (fp == NULL) {
std::string fullpath(exename);
if (fullpath.find_first_of("/\\") != std::string::npos)
fullpath = fullpath.substr(0, 1U + fullpath.find_last_of("/\\"));
fullpath += path;
fp = fopen(fullpath.c_str(), "rt");
if (fp == NULL) {
fullpath += ".cfg";
fp = fopen(fullpath.c_str(), "rt");
if (fp == NULL)
return false;
}
}
if (doc.LoadFile(fp) != tinyxml2::XML_NO_ERROR)
return false;
const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();

View File

@ -36,7 +36,7 @@ public:
Library(const Library &);
~Library();
bool load(const char path[]);
bool load(const char exename[], const char path[]);
/** get allocation id for function (by name) */
int alloc(const std::string &name) const {