diff --git a/gui/gui.pro b/gui/gui.pro index 44f850a96..78d3feeab 100644 --- a/gui/gui.pro +++ b/gui/gui.pro @@ -145,3 +145,9 @@ win32 { HEADERS += ../lib/version.h LIBS += -lshlwapi } + + +# CFGDIR=xyz +contains(CFGDIR, .+) { + DEFINES += CFGDIR=\\\"$${CFGDIR}\\\" +} diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 3959385f8..234d0aebe 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -512,6 +512,31 @@ void MainWindow::AddIncludeDirs(const QStringList &includeDirs, Settings &result } } +bool MainWindow::LoadLibrary(Library *library, QString filename) +{ + // Try to load the library from the project folder.. + if (mProject) { + QString path = QFileInfo(mProject->GetProjectFile()->GetFilename()).canonicalPath(); + if (library->load(NULL, (path+"/"+filename).toLatin1())) + return true; + } + + if (Library::cfgdir() && library->load(NULL, (Library::cfgdir()+('/'+filename)).toLatin1())) + return true; + + // Try to load the library from the application folder.. + QString path = QFileInfo(QCoreApplication::applicationFilePath()).canonicalPath(); + if (library->load(NULL, (path+"/"+filename).toLatin1())) + return true; + + // Try to load the library from the cfg subfolder.. + path = path + "/cfg"; + if (library->load(NULL, (path+"/"+filename).toLatin1())) + return true; + + return false; +} + Settings MainWindow::GetCppcheckSettings() { Settings result; @@ -533,23 +558,8 @@ Settings MainWindow::GetCppcheckSettings() QStringList libraries = pfile->GetLibraries(); foreach(QString library, libraries) { const QString filename = library + ".cfg"; - - // Try to load the library from the project folder.. - QString path = QFileInfo(pfile->GetFilename()).canonicalPath(); - if (result.library.load("", (path+"/"+filename).toLatin1())) - continue; - - // Try to load the library from the application folder.. - path = QFileInfo(QCoreApplication::applicationFilePath()).canonicalPath(); - if (result.library.load("", (path+"/"+filename).toLatin1())) - continue; - - // Try to load the library from the cfg subfolder.. - path = path + "/cfg"; - if (result.library.load("", (path+"/"+filename).toLatin1())) - continue; - - QMessageBox::information(this, tr("Information"), tr("Failed to load the selected library %1").arg(filename)); + if (!LoadLibrary(&result.library, filename)) + QMessageBox::information(this, tr("Information"), tr("Failed to load the selected library %1").arg(filename)); } // Only check the given -D configuration @@ -586,11 +596,10 @@ Settings MainWindow::GetCppcheckSettings() result.standards.c = mSettings->value(SETTINGS_STD_C99, true).toBool() ? Standards::C99 : (mSettings->value(SETTINGS_STD_C11, false).toBool() ? Standards::C11 : Standards::C89); result.standards.posix = mSettings->value(SETTINGS_STD_POSIX, false).toBool(); - const QString applicationFilePath = QCoreApplication::applicationFilePath(); - bool std = result.library.load(applicationFilePath.toLatin1(), "std.cfg"); + bool std = LoadLibrary(&result.library, "std.cfg"); bool posix = true; if (result.standards.posix) - posix = result.library.load(applicationFilePath.toLatin1(), "posix.cfg"); + posix = LoadLibrary(&result.library, "posix.cfg"); if (!std || !posix) QMessageBox::warning(this, tr("Error"), tr("Failed to load %1. Your Cppcheck installation is broken.").arg(!std ? "std.cfg" : "posix.cfg")); diff --git a/gui/mainwindow.h b/gui/mainwindow.h index d6bf7ac69..3ecfca49a 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -424,6 +424,14 @@ private: */ void LoadProjectFile(const QString &filePath); + /** + * @brief Load library file + * @param Library library to use + * @param filename filename (no path) + * @return True if successful + */ + bool LoadLibrary(Library *library, QString filename); + /** * @brief Update project MRU items in File-menu. */ diff --git a/gui/projectfiledialog.cpp b/gui/projectfiledialog.cpp index bde3aee19..f138c0023 100644 --- a/gui/projectfiledialog.cpp +++ b/gui/projectfiledialog.cpp @@ -26,6 +26,7 @@ #include #include "common.h" #include "projectfiledialog.h" +#include "library.h" ProjectFileDialog::ProjectFileDialog(const QString &path, QWidget *parent) : QDialog(parent) @@ -42,23 +43,33 @@ ProjectFileDialog::ProjectFileDialog(const QString &path, QWidget *parent) // Checkboxes for the libraries.. const QString applicationFilePath = QCoreApplication::applicationFilePath(); const QString appPath = QFileInfo(applicationFilePath).canonicalPath(); - const QString searchPaths[] = { appPath, appPath + "/cfg", inf.canonicalPath() }; - for (int i = 0; i < 3; i++) { - QDir dir(searchPaths[i]); + QStringList searchPaths; + if (Library::cfgdir()) + searchPaths << Library::cfgdir(); + searchPaths << inf.canonicalPath(); + searchPaths << appPath; + searchPaths << (appPath + "/cfg"); + QStringList libraries; + foreach(const QString path, searchPaths) { + QDir dir(path); dir.setSorting(QDir::Name); dir.setNameFilters(QStringList("*.cfg")); dir.setFilter(QDir::Files | QDir::NoDotAndDotDot); foreach(QFileInfo item, dir.entryInfoList()) { - QString library = item.fileName(); - library.chop(4); - if (library.compare("std", Qt::CaseInsensitive) == 0) - continue; - QCheckBox *checkbox = new QCheckBox(this); - checkbox->setText(library); - mUI.librariesLayout->addWidget(checkbox); - mLibraryCheckboxes << checkbox; + libraries << item.fileName(); } } + libraries.removeDuplicates(); + libraries.sort(); + foreach(QString library, libraries) { + library.chop(4); + if (library.compare("std", Qt::CaseInsensitive) == 0) + continue; + QCheckBox *checkbox = new QCheckBox(this); + checkbox->setText(library); + mUI.librariesLayout->addWidget(checkbox); + mLibraryCheckboxes << checkbox; + } connect(mUI.mButtons, SIGNAL(accepted()), this, SLOT(accept())); connect(mUI.mBtnAddInclude, SIGNAL(clicked()), this, SLOT(AddIncludeDir())); diff --git a/lib/library.h b/lib/library.h index 36ce11978..88454b60c 100644 --- a/lib/library.h +++ b/lib/library.h @@ -44,6 +44,15 @@ class CPPCHECKLIB Library { public: Library(); + /** return cfgdir or NULL */ + static const char *cfgdir() { +#ifdef CFGDIR + return CFGDIR; +#else + return NULL; +#endif + } + bool load(const char exename [], const char path []); bool load(const tinyxml2::XMLDocument &doc);