Cppcheck
main.cpp
Go to the documentation of this file.
00001 /*
00002  * Cppcheck - A tool for static C/C++ code analysis
00003  * Copyright (C) 2007-2013 Daniel Marjamäki and Cppcheck team.
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation, either version 3 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include <QApplication>
00020 #include <QCoreApplication>
00021 #include <QTextCodec>
00022 #include <QMetaType>
00023 #include <QStringList>
00024 #include <QSettings>
00025 #ifdef _WIN32
00026 #include <QMessageBox>
00027 #else
00028 #include <iostream>
00029 #endif
00030 #include "cppcheck.h"
00031 #include "common.h"
00032 #include "mainwindow.h"
00033 #include "erroritem.h"
00034 #include "aboutdialog.h"
00035 #include "translationhandler.h"
00036 
00037 
00038 void ShowUsage();
00039 void ShowVersion();
00040 bool CheckArgs(const QStringList &args);
00041 
00042 int main(int argc, char *argv[])
00043 {
00044     QApplication app(argc, argv);
00045 
00046     // Set codecs so that UTF-8 strings in sources are handled correctly.
00047     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
00048     QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
00049 
00050     QCoreApplication::setOrganizationName("Cppcheck");
00051     QCoreApplication::setApplicationName("Cppcheck-GUI");
00052 
00053     TranslationHandler* th = new TranslationHandler(&app);
00054     QSettings* settings = new QSettings("Cppcheck", "Cppcheck-GUI", &app);
00055     th->SetLanguage(settings->value(SETTINGS_LANGUAGE, th->SuggestLanguage()).toString());
00056 
00057     if (!CheckArgs(app.arguments()))
00058         return 0;
00059 
00060     app.setWindowIcon(QIcon(":icon.png"));
00061 
00062     // Register this metatype that is used to transfer error info
00063     qRegisterMetaType<ErrorItem>("ErrorItem");
00064 
00065     MainWindow window(th, settings);
00066     window.show();
00067     return app.exec();
00068 }
00069 
00070 // Check only arguments needing action before GUI is shown.
00071 // Rest of the arguments are handled in MainWindow::HandleCLIParams()
00072 bool CheckArgs(const QStringList &args)
00073 {
00074     if (args.contains("-h") || args.contains("--help")) {
00075         ShowUsage();
00076         return false;
00077     }
00078     if (args.contains("-v") || args.contains("--version")) {
00079         ShowVersion();
00080         return false;
00081     }
00082     return true;
00083 }
00084 
00085 void ShowUsage()
00086 {
00087     QString helpMessage = MainWindow::tr(
00088                               "Cppcheck GUI.\n\n"
00089                               "Syntax:\n"
00090                               "    cppcheck-gui [OPTIONS] [files or paths]\n\n"
00091                               "Options:\n"
00092                               "    -h, --help     Print this help\n"
00093                               "    -p <file>      Open given project file and start checking it\n"
00094                               "    -l <file>      Open given results xml file\n"
00095                               "    -d <directory> Specify the directory that was checked to generate the results xml specified with -l\n"
00096                               "    -v, --version  Show program version");
00097 #if defined(_WIN32)
00098     QMessageBox msgBox(QMessageBox::Information,
00099                        MainWindow::tr("Cppcheck GUI - Command line parameters"),
00100                        helpMessage,
00101                        QMessageBox::Ok
00102                       );
00103     (void)msgBox.exec();
00104 #else
00105     std::cout << helpMessage.toStdString() << std::endl;
00106 #endif
00107 }
00108 
00109 void ShowVersion()
00110 {
00111 #if defined(_WIN32)
00112     AboutDialog *dlg = new AboutDialog(CppCheck::version(), CppCheck::extraVersion(), 0);
00113     dlg->exec();
00114     delete dlg;
00115 #else
00116     std::string versionMessage("Cppcheck ");
00117     versionMessage += CppCheck::version();
00118     const char * extraVersion = CppCheck::extraVersion();
00119     if (*extraVersion != 0)
00120         versionMessage += std::string(" (") + extraVersion + ")";
00121 
00122     std::cout << versionMessage << std::endl;
00123 #endif
00124 }