///////////////////////////////////////////////////////////////////////////////
// MuldeR's Utilities for Qt
-// Copyright (C) 2004-2018 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2019 LoRd_MuldeR <MuldeR2@GMX.de>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
#include <MUtils/Global.h>
#include <MUtils/OSSupport.h>
#include <MUtils/Version.h>
+#include "Internal.h"
//Internal
#include "DirLocker.h"
#include <vld.h>
#endif
+//Global
+const QString MUtils::Internal::g_empty;
+
///////////////////////////////////////////////////////////////////////////////
// Random Support
///////////////////////////////////////////////////////////////////////////////
}
qWarning("%%TEMP%% directory not found -> trying fallback mode now!");
- static const OS::known_folder_t FOLDER_ID[2] = { OS::FOLDER_LOCALAPPDATA, OS::FOLDER_SYSTROOT_DIR };
+ static const OS::known_folder_t FOLDER_ID[2] = { OS::FOLDER_APPDATA_LOCA, OS::FOLDER_SYSROOT };
for(size_t id = 0; id < 2; id++)
{
const QString &knownFolder = OS::known_folder(FOLDER_ID[id]);
void MUtils::init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir, const QStringList *const extraPaths, const QHash<QString, QString> *const extraEnv)
{
//Environment variable names
- static const char *const s_envvar_names_temp[] =
+ static const char *const ENVVAR_NAMES_TMP[] =
{
"TEMP", "TMP", "TMPDIR", "HOME", "USERPROFILE", "HOMEPATH", NULL
};
- static const char *const s_envvar_names_remove[] =
+ static const char *const ENVVAR_NAMES_SYS[] =
+ {
+ "WINDIR", "SYSTEMROOT", NULL
+ };
+ static const char *const ENVVAR_NAMES_DEL[] =
{
- "WGETRC", "SYSTEM_WGETRC", "HTTP_PROXY", "FTP_PROXY", "NO_PROXY", "GNUPGHOME", "LC_ALL", "LC_COLLATE", "LC_CTYPE", "LC_MESSAGES", "LC_MONETARY", "LC_NUMERIC", "LC_TIME", "LANG", NULL
+ "HTTP_PROXY", "FTP_PROXY", "NO_PROXY", "HOME", "LC_ALL", "LC_COLLATE", "LC_CTYPE",
+ "LC_MESSAGES", "LC_MONETARY", "LC_NUMERIC", "LC_TIME", "LANG", NULL
};
//Initialize environment
QProcessEnvironment env = process.processEnvironment();
- if(env.isEmpty()) env = QProcessEnvironment::systemEnvironment();
+ if (env.isEmpty())
+ {
+ env = QProcessEnvironment::systemEnvironment();
+ }
- //Clean a number of enviroment variables that might affect our tools
- for(size_t i = 0; s_envvar_names_remove[i]; i++)
+ //Clean enviroment variables that might affect our tools
+ for(const char *const *ptr = ENVVAR_NAMES_DEL; *ptr; ++ptr)
{
- env.remove(QString::fromLatin1(s_envvar_names_remove[i]));
- env.remove(QString::fromLatin1(s_envvar_names_remove[i]).toLower());
+ env.remove(QString::fromLatin1(*ptr));
}
- const QString tempDir = QDir::toNativeSeparators(temp_folder());
+ //Set up system root directory
+ const QString sysRoot = QDir::toNativeSeparators(OS::known_folder(OS::FOLDER_SYSROOT));
+ if (!sysRoot.isEmpty())
+ {
+ for (const char *const *ptr = ENVVAR_NAMES_SYS; *ptr; ++ptr)
+ {
+ env.insert(QString::fromLatin1(*ptr), sysRoot);
+ }
+ }
//Replace TEMP directory in environment
+ const QString tempDir = QDir::toNativeSeparators(temp_folder());
if(bReplaceTempDir)
{
- for(size_t i = 0; s_envvar_names_temp[i]; i++)
+ for (const char *const *ptr = ENVVAR_NAMES_TMP; *ptr; ++ptr)
{
- env.insert(s_envvar_names_temp[i], tempDir);
+ env.insert(QString::fromLatin1(*ptr), tempDir);
}
}
}
///////////////////////////////////////////////////////////////////////////////
+// PARENT PATH
+///////////////////////////////////////////////////////////////////////////////
+
+static void remove_trailing_separators(QString &pathStr)
+{
+ while (pathStr.endsWith('/'))
+ {
+ pathStr.chop(1);
+ }
+}
+
+MUTILS_API QString MUtils::parent_path(const QString &path)
+{
+ QString parentPath(QDir::fromNativeSeparators(path));
+ remove_trailing_separators(parentPath);
+ const int pos = parentPath.lastIndexOf(QLatin1Char('/'));
+ if (pos >= 0)
+ {
+ parentPath.truncate(pos);
+ remove_trailing_separators(parentPath);
+ if (parentPath.isEmpty())
+ {
+ return QLatin1String("/");
+ }
+ if ((parentPath.length() == 2) && parentPath.at(0).isLetter() && (parentPath.at(1) == QLatin1Char(':')))
+ {
+ parentPath += QLatin1Char('/');
+ }
+ return parentPath;
+ }
+ return QString();
+}
+
+///////////////////////////////////////////////////////////////////////////////
// REGULAR EXPESSION HELPER
///////////////////////////////////////////////////////////////////////////////
}
///////////////////////////////////////////////////////////////////////////////
-// SELF-TEST
+// INITIALIZER
///////////////////////////////////////////////////////////////////////////////
-int MUtils::Internal::selfTest(const char *const buildKey, const bool debug)
+unsigned int MUtils::Internal::MUTILS_INITIALIZER(const unsigned int interfaceId)
{
- static const bool MY_DEBUG_FLAG = MUTILS_DEBUG;
- static const char *const MY_BUILD_KEY = __DATE__ "@" __TIME__;
-
- if(strncmp(buildKey, MY_BUILD_KEY, 13) || (MY_DEBUG_FLAG != debug))
+ if(interfaceId != ((unsigned int)MUTILS_INTERFACE))
{
- MUtils::OS::system_message_err(L"MUtils", L"FATAL ERROR: MUtils library version mismatch detected!");
- MUtils::OS::system_message_wrn(L"MUtils", L"Perform a clean(!) re-install of the application to fix the problem!");
- abort();
+ OS::system_message_err(L"MUtils", L"ERROR: MUtils library initialization has failed!");
+ for(;;) _exit((int)0xC0000142);
}
- return 0;
+
+ volatile unsigned int _result = MUTILS_INTERFACE;
+ return _result;
}