diff --git a/qpp/prefetch/Source/Core/const_core.h b/qpp/prefetch/Source/Core/const_core.h index a383342..6258079 100644 --- a/qpp/prefetch/Source/Core/const_core.h +++ b/qpp/prefetch/Source/Core/const_core.h @@ -31,8 +31,11 @@ namespace Const_Core // Argi: index namespace Arg { + // Assume index 0 is application.exe path + const int ArgBeginIndex = 1; const int IniArgc = 2; const int IniArgi = 1; + const QString BoolFlagBeginWith = "-"; const QString SkipStartup = "skipStartup"; const QString ShowMainWindow = "showMainWindow"; } diff --git a/qpp/prefetch/Source/Core/scan_cache.cpp b/qpp/prefetch/Source/Core/scan_cache.cpp index d38eccf..ce46821 100644 --- a/qpp/prefetch/Source/Core/scan_cache.cpp +++ b/qpp/prefetch/Source/Core/scan_cache.cpp @@ -6,9 +6,10 @@ #include "..\Setting\setting.h" #include "const_core.h" #include "ReadFile\read_file.h" +#include "..\Global\global.h" QSettings *ScanCache::cache = NULL; -QString ScanCache::cacheFilePath; +QString ScanCache::cacheFilePath = NULL; bool ScanCache::cacheFileExist = false; bool ScanCache::init_cacheFileExist() @@ -17,38 +18,13 @@ bool ScanCache::init_cacheFileExist() return cacheFile.exists(); } -Setting::GetGenericResult init_getCacheFilePath(int argc, QStringList argv) -{ - using namespace Const_Core; - - Setting::GetGenericResult getCacheFilePath; - - if (argc < Arg::IniArgc) - { - getCacheFilePath.success = false; - return getCacheFilePath; - } - - auto settingFile = QFileInfo(argv[1]); - QString cacheFileName = settingFile.baseName() + Const_Cache::DefaultCacheFilePathSuffix; - auto cacheFile = QFileInfo(cacheFileName); - ScanCache::cacheFileExist = cacheFile.exists(); - - getCacheFilePath.result = cacheFileName; - return getCacheFilePath; -} - -void ScanCache::init(int argc, QStringList argv) +void ScanCache::init() { // Cache file path default value cacheFilePath = Const_Cache::DefaultCacheFilePath; // Get cache file path - auto getCacheFilePath = init_getCacheFilePath(argc, argv); - if (getCacheFilePath.success) - { - cacheFilePath = getCacheFilePath.result; - } + cacheFilePath = Global::commandLineArgumentAddress->getScanCacheFilePath(); cacheFilePath = QApplication::applicationDirPath() + Const_Cache::PathSplitter + cacheFilePath; cacheFileExist = init_cacheFileExist(); diff --git a/qpp/prefetch/Source/Core/scan_cache.h b/qpp/prefetch/Source/Core/scan_cache.h index 28927ed..d752432 100644 --- a/qpp/prefetch/Source/Core/scan_cache.h +++ b/qpp/prefetch/Source/Core/scan_cache.h @@ -10,7 +10,7 @@ class ScanCache // Init code: check cache file exist static bool init_cacheFileExist(); // Any init code - static void init(int argc, QStringList argv); + static void init(); // Snapshot current thread pool to cache file static void saveScanCache(QList *readThreadQueueAddress, bool override = false); diff --git a/qpp/prefetch/Source/Core/startup.cpp b/qpp/prefetch/Source/Core/startup.cpp index 33dd2b8..5bbbc49 100644 --- a/qpp/prefetch/Source/Core/startup.cpp +++ b/qpp/prefetch/Source/Core/startup.cpp @@ -12,11 +12,13 @@ using namespace Const_Setting::ConfigGroupName; void (*Startup::startOnce)() = &_startOnce; -void Startup::init(int argc, QStringList argv) +void Startup::init() { using namespace Const_Core::Arg; - if (argv.contains(SkipStartup)) + bool skipStartup = Global::commandLineArgumentAddress->getSkipStartup(); + + if (skipStartup) { startOnce_remove(); return; diff --git a/qpp/prefetch/Source/Core/startup.h b/qpp/prefetch/Source/Core/startup.h index 869d916..c48646d 100644 --- a/qpp/prefetch/Source/Core/startup.h +++ b/qpp/prefetch/Source/Core/startup.h @@ -1,7 +1,7 @@ class Startup { public: - static void init(int argc, QStringList argv); + static void init(); // This function will only run once static void (*startOnce)(); diff --git a/qpp/prefetch/Source/Global/global.cpp b/qpp/prefetch/Source/Global/global.cpp index b460d14..1fbcff8 100644 --- a/qpp/prefetch/Source/Global/global.cpp +++ b/qpp/prefetch/Source/Global/global.cpp @@ -17,7 +17,8 @@ QApplication *Global::qGuiApplication = NULL; MainWindow *Global::qMainWindow = NULL; InputLoopThread *Global::inputLoopThreadAddress = NULL; ReadFile *Global::readFileLoopThreadAddress = NULL; -TrayIcon *Global::trayIconInstanceAddress = new TrayIcon(); +TrayIcon *Global::trayIconInstanceAddress = NULL; +CommandLineArgument *Global::commandLineArgumentAddress = NULL; void Global::init(int argc, char *argv[]) { @@ -27,20 +28,24 @@ void Global::init(int argc, char *argv[]) // QApplication qGuiApplication = new QApplication(argc, argv); - auto commandLineArguments = QApplication::arguments(); + + // Command line argument + auto argvQStringList = QApplication::arguments(); #if LOG_ENABLED - Log::init(argc, commandLineArguments); + Log::init(argc, argvQStringList); #endif LAST_KNOWN_POSITION(0) + commandLineArgumentAddress = new CommandLineArgument(argc, argvQStringList); + StdIn::init(); LAST_KNOWN_POSITION(2) StdOut::init(); LAST_KNOWN_POSITION(2) - Setting::init(argc, commandLineArguments); + Setting::init(); LAST_KNOWN_POSITION(2) #if TRANSLATE_ENABLED @@ -50,10 +55,10 @@ void Global::init(int argc, char *argv[]) LAST_KNOWN_POSITION(2) #endif - Startup::init(argc, commandLineArguments); + Startup::init(); LAST_KNOWN_POSITION(2) - ScanCache::init(argc, commandLineArguments); + ScanCache::init(); LAST_KNOWN_POSITION(2) Dpi::init(); @@ -84,6 +89,7 @@ void Global::init(int argc, char *argv[]) LAST_KNOWN_POSITION(2) // Tray icon + trayIconInstanceAddress = new TrayIcon(); trayIconInstanceAddress->init(); LAST_KNOWN_POSITION(2) diff --git a/qpp/prefetch/Source/Global/global.h b/qpp/prefetch/Source/Global/global.h index c32535e..9dbdec7 100644 --- a/qpp/prefetch/Source/Global/global.h +++ b/qpp/prefetch/Source/Global/global.h @@ -7,6 +7,7 @@ #include "..\Core\ReadFile\read_file.h" #include "..\Interface\TrayIcon\tray_icon.h" #include "..\Interface\MainWindow\mainwindow.h" +#include "..\Input\command_line_argument.h" class Global { @@ -21,6 +22,8 @@ class Global static TrayIcon *trayIconInstanceAddress; + static CommandLineArgument *commandLineArgumentAddress; + // Any init code static void init(int argc, char *argv[]); diff --git a/qpp/prefetch/Source/Input/Thread/InputLoop/Level1/expiresc_itill1.cpp b/qpp/prefetch/Source/Input/Thread/InputLoop/Level1/expiresc_itill1.cpp index 6fbd074..24ada33 100644 --- a/qpp/prefetch/Source/Input/Thread/InputLoop/Level1/expiresc_itill1.cpp +++ b/qpp/prefetch/Source/Input/Thread/InputLoop/Level1/expiresc_itill1.cpp @@ -1,12 +1,9 @@ #include "..\..\input_loop_thread.h" #include "..\..\..\..\Input\const_input.h" -#include "..\..\..\..\Global\const_global.h" -#include "..\..\..\..\Core\const_core.h" #include "..\..\..\..\Output\stdout.h" #include "..\..\..\..\Core\scan_cache.h" #include "..\..\..\..\Core\ReadFile\read_file.h" #include "..\..\..\..\Global\global.h" -#include "..\..\..\..\Setting\setting.h" #define Parent InputLoopThread::ConsoleCommandFunction_Level1 @@ -14,8 +11,6 @@ void Parent::expiresc() { using namespace Const_Input; using namespace Const_Input::Message; - using namespace Const_Global::CommonString; - using namespace Const_Core::Arg; StdOut::printLine(TryingToExpireScanCache); @@ -37,83 +32,15 @@ void Parent::expiresc() // Open self again - // Get exist argument - // <.exe> <.ini> - auto selfArgumentsStringList = Global::qGuiApplication->arguments(); - - // Get exe - // <.exe> - auto selfExePath = QFileInfo(selfArgumentsStringList[0]).absoluteFilePath(); - - // Add exe quote if not already - // "<.exe>" - bool quotedSelfExePath = selfExePath.startsWith(Quote); - if (!quotedSelfExePath) - { - selfExePath = Quote + selfExePath + Quote; - } - - // Get ini - // <.ini> - // Remove quote if exist - auto settingFileName = QFileInfo(Setting::settingFilePath).fileName(); - settingFileName.replace(Quote, EmptyString); - - // Collect search result - bool settingFileNotInArgv = true; - bool skipStartupNotInArgv = true; - bool showMainWindowNotInArgv = true; - // Search start from 2nd element - QListIterator iterator(selfArgumentsStringList); - iterator.next(); - while (iterator.hasNext()) - { - auto value = iterator.next(); - - // Confirm <.ini> - if (value.contains(settingFileName)) - { - - settingFileNotInArgv = false; - } - - // Confirm - if (value == SkipStartup) - { - skipStartupNotInArgv = false; - } - if (value == ShowMainWindow) - { - showMainWindowNotInArgv = false; - } - } - - // Add necessary arguments if not exist - if (settingFileNotInArgv) - { - // "<.ini>" - auto settingFilePath_arguments = Quote + settingFileName + Quote; - selfArgumentsStringList.insert(IniArgi, settingFilePath_arguments); - } - if (skipStartupNotInArgv) - { - // - selfArgumentsStringList.append(SkipStartup); - } - if (showMainWindowNotInArgv) - { - selfArgumentsStringList.append(ShowMainWindow); - } - - // Remove <.exe> - // Join rest element - selfArgumentsStringList.removeFirst(); + // Add necessary arguments + Global::commandLineArgumentAddress->setSkipStartup(true); + Global::commandLineArgumentAddress->setShowMainWindow(true); // Send run quiet command // `run ` // + `"<.exe>"` // + `"<.ini>" ` - auto self_command = Command_Level2::run_withSplitter + Quote + selfExePath + Quote + Space + selfArgumentsStringList.join(Space); + auto self_command = Global::commandLineArgumentAddress->toString(); Global::inputLoopThreadAddress->receiveText(self_command); // Exit diff --git a/qpp/prefetch/Source/Input/cli_constructor.cpp b/qpp/prefetch/Source/Input/cli_constructor.cpp new file mode 100644 index 0000000..9c5de93 --- /dev/null +++ b/qpp/prefetch/Source/Input/cli_constructor.cpp @@ -0,0 +1,97 @@ +#include + +#include "command_line_argument.h" +#include "..\Core\const_core.h" +#include "..\Setting\const_setting.h" + +void CommandLineArgument::CommandLineArgument_parseSettingFileName(int *argIndex, QStringList *argv) +{ + using namespace Const_Core; + + { + // Assume this argument is the first argument, and not start with "-" + bool firstArgumentStartWithMinus = (*argv)[Arg::IniArgi].startsWith(Arg::BoolFlagBeginWith); + + // If first argument start with "-", then setting file name is not provided + if (firstArgumentStartWithMinus) + { + // In this case, use default value + settingFilePath = Const_Setting::DefaultSettingFilePath; + scanCacheFilePath = Const_Cache::DefaultCacheFilePath; + return; + } + } + + { + // Setting file name founded, other start from 2nd + *argIndex = *argIndex + 1; + + // Save + settingFilePath = (*argv)[Arg::IniArgi]; + } + + { + // Generate scan cache file name together + auto settingFile = QFileInfo(settingFilePath); + scanCacheFilePath = settingFile.baseName() + Const_Cache::DefaultCacheFilePathSuffix; + } +} + +bool CommandLineArgument_parseBoolFlag(QString match, int argIndex, QStringList *argv) +{ + auto arg = (*argv)[argIndex]; + return arg.contains(match); +} + +void CommandLineArgument::CommandLineArgument_parseStartup(int argIndex, QStringList *argv) +{ + using namespace Const_Core::Arg; + + bool get = CommandLineArgument_parseBoolFlag(SkipStartup, argIndex, argv); + if (get) + { + skipStartup = true; + } +} + +void CommandLineArgument::CommandLineArgument_parseShowMainWindow(int argIndex, QStringList *argv) +{ + using namespace Const_Core::Arg; + + bool get = CommandLineArgument_parseBoolFlag(ShowMainWindow, argIndex, argv); + if (get) + { + showMainWindow = true; + } +} + +CommandLineArgument::CommandLineArgument(int argc, QStringList argv) +{ + using namespace Const_Core; + + // Create loop index + // This variable may modify by function before loop + int argIndex = Arg::ArgBeginIndex; + + // Not found any possible argument + if (argc < Arg::IniArgc) + { + // Put default value + settingFilePath = Const_Setting::DefaultSettingFilePath; + scanCacheFilePath = Const_Cache::DefaultCacheFilePath; + skipStartup = false; + showMainWindow = false; + + // Terminate + return; + } + + CommandLineArgument_parseSettingFileName(&argIndex, &argv); + + // Parse other argument + for (; argIndex < argc; argIndex++) + { + CommandLineArgument_parseStartup(argIndex, &argv); + CommandLineArgument_parseShowMainWindow(argIndex, &argv); + } +} diff --git a/qpp/prefetch/Source/Input/cli_to_string.cpp b/qpp/prefetch/Source/Input/cli_to_string.cpp new file mode 100644 index 0000000..589516c --- /dev/null +++ b/qpp/prefetch/Source/Input/cli_to_string.cpp @@ -0,0 +1,47 @@ +#include "command_line_argument.h" +#include "..\Core\const_core.h" +#include "..\Global\const_global.h" +#include "const_input.h" +#include "..\Setting\const_setting.h" +#include "..\Global\global.h" + +void toString_appendBoolFlag(QString flag, QString *command) +{ + using namespace Const_Global::CommonString; + using namespace Const_Core; + + *command = *command + Space + Arg::BoolFlagBeginWith + flag; +} +QString CommandLineArgument::toString() +{ + using namespace Const_Input; + using namespace Const_Global::CommonString; + using namespace Const_Core; + + // Get self exe full path + auto argv = Global::qGuiApplication->arguments(); + auto selfExePath = QFileInfo(argv[0]).absoluteFilePath(); + + // Self command template + // `run "prefetch.exe"` + auto self_command = Command_Level2::run_withSplitter + Quote + selfExePath + Quote; + + // Setting file + // `... chromium.ini` + if (settingFilePath != Const_Setting::DefaultSettingFilePath) + { + self_command = self_command + Space + settingFilePath; + } + + // Bool flags + if (skipStartup) + { + toString_appendBoolFlag(Arg::SkipStartup, &self_command); + } + if (showMainWindow) + { + toString_appendBoolFlag(Arg::ShowMainWindow, &self_command); + } + + return self_command; +} diff --git a/qpp/prefetch/Source/Input/command_line_argument.cpp b/qpp/prefetch/Source/Input/command_line_argument.cpp new file mode 100644 index 0000000..1a20912 --- /dev/null +++ b/qpp/prefetch/Source/Input/command_line_argument.cpp @@ -0,0 +1,26 @@ +#include "command_line_argument.h" + +QString CommandLineArgument::getSettingFilePath() +{ + return settingFilePath; +} +QString CommandLineArgument::getScanCacheFilePath() +{ + return scanCacheFilePath; +} +bool CommandLineArgument::getSkipStartup() +{ + return skipStartup; +} +void CommandLineArgument::setSkipStartup(bool value) +{ + skipStartup = value; +} +bool CommandLineArgument::getShowMainWindow() +{ + return showMainWindow; +} +void CommandLineArgument::setShowMainWindow(bool value) +{ + showMainWindow = value; +} diff --git a/qpp/prefetch/Source/Input/command_line_argument.h b/qpp/prefetch/Source/Input/command_line_argument.h new file mode 100644 index 0000000..9fcfdea --- /dev/null +++ b/qpp/prefetch/Source/Input/command_line_argument.h @@ -0,0 +1,44 @@ +#ifndef Command_Line_Argument_h +#define Command_Line_Argument_h + +#include +#include + +class CommandLineArgument +{ +public: + CommandLineArgument(int argc, QStringList argv); + + QString getSettingFilePath(); + QString getScanCacheFilePath(); + bool getSkipStartup(); + // Only use if reasonable, because command line argument is generally read only + void setSkipStartup(bool value); + bool getShowMainWindow(); + // Only use if reasonable, because command line argument is generally read only + void setShowMainWindow(bool value); + + // Generate restart command + QString toString(); + +private: + // Read only internal variable + QString settingFilePath; + QString scanCacheFilePath; + bool skipStartup; + bool showMainWindow; + + // Parse first argument (setting file name, but also use as instance name like scan cache) + void CommandLineArgument_parseSettingFileName(int *argIndex, QStringList *argv); + + // Parse skip startup + void CommandLineArgument_parseStartup(int argIndex, QStringList *argv); + + // Parse show main window + void CommandLineArgument_parseShowMainWindow(int argIndex, QStringList *argv); + + // Disallow create without argument + CommandLineArgument() {} +}; + +#endif diff --git a/qpp/prefetch/Source/Interface/MainWindow/mainwindow.cpp b/qpp/prefetch/Source/Interface/MainWindow/mainwindow.cpp index 31cf376..4411f11 100644 --- a/qpp/prefetch/Source/Interface/MainWindow/mainwindow.cpp +++ b/qpp/prefetch/Source/Interface/MainWindow/mainwindow.cpp @@ -29,10 +29,10 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui->setupUi(this); // Initialize - auto argv = Global::qGuiApplication->arguments(); startToTray = Setting::getBool(Instance, StartToTray, Setting::setting); - if (argv.contains(ShowMainWindow)) + // Command line override + if (Global::commandLineArgumentAddress->getShowMainWindow()) { startToTray = false; } diff --git a/qpp/prefetch/Source/Output/log.cpp b/qpp/prefetch/Source/Output/log.cpp index ba921f3..e41ea93 100644 --- a/qpp/prefetch/Source/Output/log.cpp +++ b/qpp/prefetch/Source/Output/log.cpp @@ -17,7 +17,17 @@ Setting::GetGenericResult init_getLogFilePath(int argc, QStringList arg Setting::GetGenericResult getLogFilePath; - if (argc < Arg::IniArgc) + bool argcNotEnough = argc < Arg::IniArgc; + if (argcNotEnough) + { + getLogFilePath.success = false; + return getLogFilePath; + } + + // Watch out! This one can crash if argc check not pass + // First argument is not alternative setting ini, ignore + bool firstArgIsBoolFlag = argv[1].startsWith(Arg::BoolFlagBeginWith); + if (firstArgIsBoolFlag) { getLogFilePath.success = false; return getLogFilePath; diff --git a/qpp/prefetch/Source/Setting/setting.cpp b/qpp/prefetch/Source/Setting/setting.cpp index 10335fe..b3b2359 100644 --- a/qpp/prefetch/Source/Setting/setting.cpp +++ b/qpp/prefetch/Source/Setting/setting.cpp @@ -4,36 +4,19 @@ #include "setting.h" #include "const_setting.h" #include "..\Core\const_core.h" +#include "..\Global\global.h" -QSettings *Setting::setting; -QString Setting::settingFilePath = Const_Setting::DefaultSettingFilePath; +QSettings *Setting::setting = NULL; +QString Setting::settingFilePath = NULL; -Setting::GetGenericResult init_getSettingFilePath(int argc, QStringList argv) -{ - using namespace Const_Core; - - Setting::GetGenericResult getSettingFilePath; - - if (argc < Arg::IniArgc) - { - getSettingFilePath.success = false; - return getSettingFilePath; - } - - getSettingFilePath.result = argv[1]; - return getSettingFilePath; -} - -void Setting::init(int argc, QStringList argv) +void Setting::init() { using namespace Const_Setting; // Get setting file path - auto getSettingFilePath = init_getSettingFilePath(argc, argv); - if (getSettingFilePath.success) - { - settingFilePath = getSettingFilePath.result; - } + settingFilePath = Global::commandLineArgumentAddress->getSettingFilePath(); + + // Fill with applicationDirPath settingFilePath = QApplication::applicationDirPath() + PathSplitter + settingFilePath; // Read ini from exe stored folder diff --git a/qpp/prefetch/Source/Setting/setting.h b/qpp/prefetch/Source/Setting/setting.h index 8078dc2..016b999 100644 --- a/qpp/prefetch/Source/Setting/setting.h +++ b/qpp/prefetch/Source/Setting/setting.h @@ -8,7 +8,7 @@ class Setting static QString settingFilePath; // Any init code - static void init(int argc, QStringList argv); + static void init(); template struct GetGenericResult diff --git a/qpp/prefetch/prefetch.pro b/qpp/prefetch/prefetch.pro index 7a52319..b118870 100644 --- a/qpp/prefetch/prefetch.pro +++ b/qpp/prefetch/prefetch.pro @@ -20,6 +20,7 @@ HEADERS += \ Source/Define/define.h \ Source/Global/global.h \ Source/Input/Thread/input_loop_thread.h \ + Source/Input/command_line_argument.h \ Source/Input/stdin.h \ Source/Interface/Dpi/dpi.h \ Source/Interface/MainWindow/mainwindow.h \ @@ -62,6 +63,9 @@ SOURCES += \ Source/Input/Thread/InputLoop/Level2/lang_itill1.cpp \ Source/Input/Thread/InputLoop/Level2/run_itill1.cpp \ Source/Input/Thread/input_loop_thread.cpp \ + Source/Input/cli_constructor.cpp \ + Source/Input/cli_to_string.cpp \ + Source/Input/command_line_argument.cpp \ Source/Input/const_input.cpp \ Source/Input/const_input_tdummy.cpp \ Source/Input/stdin.cpp \