Improved handling of command-line arguments: Arguments are now provided in the from of a QMap. Also some improvements to directory clean-up code.

This commit is contained in:
LoRd_MuldeR 2015-01-04 17:09:12 +01:00
parent 4572f69d33
commit c3b213dcbc
7 changed files with 73 additions and 37 deletions

View File

@ -55,6 +55,6 @@ namespace MUtils
} }
cpu_info_t; cpu_info_t;
MUTILS_API cpu_info_t detect(const QStringList &argv); MUTILS_API cpu_info_t detect(void);
} }
} }

View File

@ -26,6 +26,7 @@
//Qt //Qt
#include <QString> #include <QString>
#include <QMap>
#include <QDate> #include <QDate>
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -100,7 +101,8 @@ namespace MUtils
MUTILS_API void system_message_err(const wchar_t *const title, const wchar_t *const text); MUTILS_API void system_message_err(const wchar_t *const title, const wchar_t *const text);
//CLI Arguments //CLI Arguments
MUTILS_API const QStringList &arguments(void); typedef QMap<QString,QString> ArgumentMap;
MUTILS_API const ArgumentMap &arguments(void);
//Copy file //Copy file
MUTILS_API bool copy_file(const QString &sourcePath, const QString &outputPath, const bool &overwrite = true); MUTILS_API bool copy_file(const QString &sourcePath, const QString &outputPath, const bool &overwrite = true);

View File

@ -25,12 +25,14 @@
//MUtils //MUtils
#include <MUtils/CPUFeatures.h> #include <MUtils/CPUFeatures.h>
#include <MUtils/OSSupport.h>
//Qt //Qt
#include <QLibrary> #include <QLibrary>
MUtils::CPUFetaures::cpu_info_t MUtils::CPUFetaures::detect(const QStringList &argv) MUtils::CPUFetaures::cpu_info_t MUtils::CPUFetaures::detect(void)
{ {
const OS::ArgumentMap &args = OS::arguments();
typedef BOOL (WINAPI *IsWow64ProcessFun)(__in HANDLE hProcess, __out PBOOL Wow64Process); typedef BOOL (WINAPI *IsWow64ProcessFun)(__in HANDLE hProcess, __out PBOOL Wow64Process);
cpu_info_t features; cpu_info_t features;
@ -119,16 +121,14 @@ MUtils::CPUFetaures::cpu_info_t MUtils::CPUFetaures::detect(const QStringList &a
features.count = qBound(1UL, systemInfo.dwNumberOfProcessors, 64UL); features.count = qBound(1UL, systemInfo.dwNumberOfProcessors, 64UL);
} }
if(argv.count() > 0)
{
bool flag = false; bool flag = false;
for(int i = 0; i < argv.count(); i++) if(args.contains("force-cpu-no-64bit")) { flag = true; features.x64 = false; }
if(args.contains("force-cpu-no-sse" )) { flag = true; features.features &= (~(FLAG_SSE | FLAG_SSE2 | FLAG_SSE3 | FLAG_SSSE3 | FLAG_SSE4 | FLAG_SSE42)); }
if(args.contains("force-cpu-no-intel")) { flag = true; features.intel = false; }
if(flag)
{ {
if(!argv[i].compare("--force-cpu-no-64bit", Qt::CaseInsensitive)) { flag = true; features.x64 = false; } qWarning("CPU flags overwritten by user-defined parameters. Take care!\n");
if(!argv[i].compare("--force-cpu-no-sse", Qt::CaseInsensitive)) { flag = true; features.features &= (~(FLAG_SSE | FLAG_SSE2 | FLAG_SSE3 | FLAG_SSSE3 | FLAG_SSE4 | FLAG_SSE42)); }
if(!argv[i].compare("--force-cpu-no-intel", Qt::CaseInsensitive)) { flag = true; features.intel = false; }
}
if(flag) qWarning("CPU flags overwritten by user-defined parameters. Take care!\n");
} }
return features; return features;

View File

@ -68,10 +68,13 @@ namespace MUtils
if(m_lockFile->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Unbuffered)) if(m_lockFile->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Unbuffered))
{ {
if(m_lockFile->write(testData) >= testData.size()) if(m_lockFile->write(testData) >= testData.size())
{
if(m_lockFile->error() == QFile::NoError)
{ {
okay = true; okay = true;
break; break;
} }
}
m_lockFile->remove(); m_lockFile->remove();
} }
} }
@ -88,26 +91,17 @@ namespace MUtils
{ {
for(int i = 0; i < 16; i++) for(int i = 0; i < 16; i++)
{ {
if(m_lockFile->remove()) if(m_lockFile->remove() || (!m_lockFile->exists()))
{
break;
}
OS::sleep_ms(125);
}
m_lockFile.reset(NULL);
}
for(int i = 0; i < 16; i++)
{
if(MUtils::remove_directory(m_dirPath, true))
{ {
okay = true; okay = true;
break; break;
} }
OS::sleep_ms(125); OS::sleep_ms(125);
} }
}
if(!okay) if(!okay)
{ {
OS::system_message_wrn(L"Directory Lock", L"Warning: Not all temporary files could be removed!"); qWarning("DirLock: The lock file could not be removed!");
} }
} }

View File

@ -152,6 +152,24 @@ static MUtils::Internal::DirLock *try_init_temp_folder(const QString &baseDir)
return NULL; return NULL;
} }
static void temp_folder_cleanup_helper(const QString &tempPath)
{
bool okay = false;
for(int i = 0; i < 32; i++)
{
if(MUtils::remove_directory(tempPath, true))
{
okay = true;
break;
}
MUtils::OS::sleep_ms(125);
}
if(!okay)
{
MUtils::OS::system_message_wrn(L"Temp Cleaner", L"Warning: Not all temporary files could be removed!");
}
}
static void temp_folder_cleaup(void) static void temp_folder_cleaup(void)
{ {
QWriteLocker writeLock(&g_temp_folder_lock); QWriteLocker writeLock(&g_temp_folder_lock);
@ -159,7 +177,9 @@ static void temp_folder_cleaup(void)
//Clean the directory //Clean the directory
while(!g_temp_folder_file.isNull()) while(!g_temp_folder_file.isNull())
{ {
const QString tempPath = g_temp_folder_file->getPath();
g_temp_folder_file.reset(NULL); g_temp_folder_file.reset(NULL);
temp_folder_cleanup_helper(tempPath);
} }
} }

View File

@ -72,9 +72,9 @@ void MUtils::OS::system_message_err(const wchar_t *const title, const wchar_t *c
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
static QReadWriteLock g_arguments_lock; static QReadWriteLock g_arguments_lock;
static QScopedPointer<QStringList> g_arguments_list; static QScopedPointer<MUtils::OS::ArgumentMap> g_arguments_list;
const QStringList &MUtils::OS::arguments(void) const MUtils::OS::ArgumentMap &MUtils::OS::arguments(void)
{ {
QReadLocker readLock(&g_arguments_lock); QReadLocker readLock(&g_arguments_lock);
@ -93,15 +93,35 @@ const QStringList &MUtils::OS::arguments(void)
return (*(g_arguments_list.data())); return (*(g_arguments_list.data()));
} }
g_arguments_list.reset(new QStringList); g_arguments_list.reset(new ArgumentMap());
int nArgs = 0; int nArgs = 0;
LPWSTR *szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs); LPWSTR *szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
if(NULL != szArglist) if(NULL != szArglist)
{ {
const QChar separator = QLatin1Char('=');
const QString argPrefix = QLatin1String("--");
for(int i = 0; i < nArgs; i++) for(int i = 0; i < nArgs; i++)
{ {
*(g_arguments_list.data()) << MUTILS_QSTR(szArglist[i]); const QString argStr = MUTILS_QSTR(szArglist[i]).trimmed();
if(argStr.startsWith(argPrefix))
{
const QString argData = argStr.mid(2).trimmed();
if(argData.length() > 0)
{
const int separatorIndex = argData.indexOf(separator);
if(separatorIndex > 0)
{
const QString argKey = argData.left(separatorIndex).trimmed();
const QString argVal = argData.mid(separatorIndex + 1).trimmed();
g_arguments_list->insertMulti(argKey.toLower(), argVal);
}
else
{
g_arguments_list->insertMulti(argData.toLower(), QString());
}
}
}
} }
LocalFree(szArglist); LocalFree(szArglist);
} }

View File

@ -183,7 +183,7 @@ static const char *const g_imageformats[] = {"bmp", "png", "jpg", "gif", "ico",
QApplication *MUtils::Startup::create_qt(int &argc, char **argv, const QString &appName) QApplication *MUtils::Startup::create_qt(int &argc, char **argv, const QString &appName)
{ {
QMutexLocker lock(&g_init_lock); QMutexLocker lock(&g_init_lock);
const QStringList &arguments = MUtils::OS::arguments(); const OS::ArgumentMap &arguments = MUtils::OS::arguments();
//Don't initialized again, if done already //Don't initialized again, if done already
if(QApplication::instance() != NULL) if(QApplication::instance() != NULL)
@ -253,7 +253,7 @@ QApplication *MUtils::Startup::create_qt(int &argc, char **argv, const QString &
if(osVersion.overrideFlag && (osVersion <= MUtils::OS::Version::WINDOWS_WN100)) if(osVersion.overrideFlag && (osVersion <= MUtils::OS::Version::WINDOWS_WN100))
{ {
qWarning("Windows compatibility mode detected!"); qWarning("Windows compatibility mode detected!");
if(!arguments.contains("--ignore-compat-mode", Qt::CaseInsensitive)) if(!arguments.contains("ignore-compat-mode"))
{ {
qFatal("%s", QApplication::tr("Executable '%1' doesn't support Windows compatibility mode.").arg(executableName).toLatin1().constData()); qFatal("%s", QApplication::tr("Executable '%1' doesn't support Windows compatibility mode.").arg(executableName).toLatin1().constData());
return NULL; return NULL;
@ -299,10 +299,10 @@ QApplication *MUtils::Startup::create_qt(int &argc, char **argv, const QString &
//Enable larger/smaller font size //Enable larger/smaller font size
double fontScaleFactor = 1.0; double fontScaleFactor = 1.0;
if(arguments.contains("--huge-font", Qt::CaseInsensitive)) fontScaleFactor = 1.500; if(arguments.contains("huge-font" )) fontScaleFactor = 1.500;
if(arguments.contains("--big-font", Qt::CaseInsensitive)) fontScaleFactor = 1.250; if(arguments.contains("big-font" )) fontScaleFactor = 1.250;
if(arguments.contains("--small-font", Qt::CaseInsensitive)) fontScaleFactor = 0.875; if(arguments.contains("small-font")) fontScaleFactor = 0.875;
if(arguments.contains("--tiny-font", Qt::CaseInsensitive)) fontScaleFactor = 0.750; if(arguments.contains("tiny-font" )) fontScaleFactor = 0.750;
if(!qFuzzyCompare(fontScaleFactor, 1.0)) if(!qFuzzyCompare(fontScaleFactor, 1.0))
{ {
qWarning("Application font scale factor set to: %.3f\n", fontScaleFactor); qWarning("Application font scale factor set to: %.3f\n", fontScaleFactor);