From ba0a75881ab4c0539fbb83d3be5c9f677dc7e40d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 16 Jun 2019 15:46:01 +0200 Subject: [PATCH] Refactor AddonInfo::getAddonInfo --- lib/cppcheck.cpp | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 9cce5c4de..42641e8c3 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -63,12 +63,47 @@ namespace { std::string scriptFile; std::string args; + std::string getFullPath(const std::string &fileName, const std::string &exename) const { + if (Path::fileExists(fileName)) + return fileName; + + const std::string exepath = Path::getPathFromFilename(exename); + if (Path::fileExists(exepath + fileName)) + return exepath + fileName; + if (Path::fileExists(exepath + "addons/" + fileName)) + return exepath + "addons/" + fileName; + +#ifdef CFGDIR + if (Path::fileExists(CFGDIR + fileName)) + return CFGDIR + fileName; + if (Path::fileExists(CFGDIR + ("../addons/" + fileName))) + return CFGDIR + ("../addons/" + fileName); +#endif + return ""; + } + std::string getAddonInfo(const std::string &fileName, const std::string &exename) { - if (!endsWith(fileName, ".json", 5)) { - name = fileName; - scriptFile = Path::getPathFromFilename(exename) + "addons/" + fileName + ".py"; + if (endsWith(fileName, ".py", 3)) { + scriptFile = getFullPath(fileName, exename); + if (scriptFile.empty()) + return "Did not find addon " + fileName; + + std::string::size_type pos1 = scriptFile.rfind("/"); + if (pos1 == std::string::npos) + pos1 = 0; + else + pos1++; + std::string::size_type pos2 = scriptFile.rfind("."); + if (pos2 < pos1) + pos2 = std::string::npos; + name = scriptFile.substr(pos1, pos2 - pos1); + return ""; } + + if (!endsWith(fileName, ".json", 5)) + return "Failed to open addon " + fileName; + std::ifstream fin(fileName); if (!fin.is_open()) return "Failed to open " + fileName; @@ -83,9 +118,8 @@ namespace { for (const picojson::value &v : obj["args"].get()) args += " " + v.get(); } - name = obj["script"].get(); - scriptFile = Path::getPathFromFilename(exename) + "addons/" + name + ".py"; - return ""; + + return getAddonInfo(obj["script"].get(), exename); } }; }