More flexible loading of platform files, when using --platform it should not be necessary to provide the full path

This commit is contained in:
Daniel Marjamäki 2018-02-09 16:22:22 +01:00
parent 8e6ac60797
commit 780bd7e63e
3 changed files with 31 additions and 7 deletions

View File

@ -714,7 +714,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
_settings->platform(Settings::Native); _settings->platform(Settings::Native);
else if (platform == "unspecified") else if (platform == "unspecified")
_settings->platform(Settings::Unspecified); _settings->platform(Settings::Unspecified);
else if (!_settings->platformFile(platform)) { else if (!_settings->platformFile(argv[0], platform)) {
std::string message("cppcheck: error: unrecognized platform: \""); std::string message("cppcheck: error: unrecognized platform: \"");
message += platform; message += platform;
message += "\"."; message += "\".";

View File

@ -17,12 +17,13 @@
*/ */
#include "platform.h" #include "platform.h"
#include "path.h"
#include "tinyxml2.h" #include "tinyxml2.h"
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <limits> #include <limits>
#include <vector>
#include <iostream>
cppcheck::Platform::Platform() cppcheck::Platform::Platform()
{ {
@ -154,12 +155,35 @@ bool cppcheck::Platform::platform(cppcheck::Platform::PlatformType type)
return false; return false;
} }
bool cppcheck::Platform::platformFile(const std::string &filename) bool cppcheck::Platform::platformFile(const char exename[], const std::string &filename)
{ {
// open file.. // open file..
tinyxml2::XMLDocument doc; tinyxml2::XMLDocument doc;
if (doc.LoadFile(filename.c_str()) != tinyxml2::XML_SUCCESS) if (doc.LoadFile(filename.c_str()) != tinyxml2::XML_SUCCESS) {
std::vector<std::string> filenames;
filenames.push_back(filename + ".xml");
if (exename && strchr(exename, '/')) {
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + "platforms/" + filename);
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + "platforms/" + filename + ".xml");
}
#ifdef CFGDIR
std::string cfgdir = CFGDIR;
if (cfgdir[cfgdir.size()-1] != '/')
cfgdir += '/';
filenames.push_back(CFG_DIR + ("../platforms/" + filename));
filenames.push_back(CFG_DIR + ("../platforms/" + filename + ".xml"));
#endif
bool success = false;
for (int i = 0; i < filenames.size(); ++i) {
std::cout << "platform:" << filenames[i] << std::endl;
if (doc.LoadFile(filenames[i].c_str()) == tinyxml2::XML_SUCCESS) {
success = true;
break;
}
}
if (!success)
return false; return false;
}
const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement(); const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();

View File

@ -97,7 +97,7 @@ namespace cppcheck {
bool platform(PlatformType type); bool platform(PlatformType type);
/** set the platform type for user specified platforms */ /** set the platform type for user specified platforms */
bool platformFile(const std::string &filename); bool platformFile(const char exename[], const std::string &filename);
/** /**
* @brief Returns true if platform type is Windows * @brief Returns true if platform type is Windows