///////////////////////////////////////////////////////////////////////////////
// MuldeR's Utilities for Qt
-// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2016 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 <QReadWriteLock>
#include <QProcess>
#include <QTextCodec>
+#include <QPair>
//CRT
#include <cstdlib>
}
///////////////////////////////////////////////////////////////////////////////
+// GET TEMP FILE NAME
+///////////////////////////////////////////////////////////////////////////////
+
+QString MUtils::make_temp_file(const QString &basePath, const QString &extension, const bool placeholder)
+{
+ for(int i = 0; i < 4096; i++)
+ {
+ const QString tempFileName = QString("%1/%2.%3").arg(basePath, rand_str(), extension);
+ if(!QFileInfo(tempFileName).exists())
+ {
+ if(placeholder)
+ {
+ QFile file(tempFileName);
+ if(file.open(QFile::ReadWrite))
+ {
+ file.close();
+ return tempFileName;
+ }
+ }
+ else
+ {
+ return tempFileName;
+ }
+ }
+ }
+
+ qWarning("Failed to generate unique temp file name!");
+ return QString();
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// COMPUTE PARITY
+///////////////////////////////////////////////////////////////////////////////
+
+/*
+ * Compute parity in parallel
+ * http://www.graphics.stanford.edu/~seander/bithacks.html#ParityParallel
+ */
+bool MUtils::parity(quint32 value)
+{
+ value ^= value >> 16;
+ value ^= value >> 8;
+ value ^= value >> 4;
+ value &= 0xf;
+ return ((0x6996 >> value) & 1) != 0;
+}
+
+///////////////////////////////////////////////////////////////////////////////
// TEMP FOLDER
///////////////////////////////////////////////////////////////////////////////
return NULL;
}
-static void temp_folder_cleanup_helper(const QString &tempPath)
+static bool temp_folder_cleanup_helper(const QString &tempPath)
{
- bool okay = false;
- for(int i = 0; i < 32; i++)
+ size_t delay = 1;
+ static const size_t MAX_DELAY = 8192;
+ forever
{
+ QDir::setCurrent(QDir::rootPath());
if(MUtils::remove_directory(tempPath, true))
{
- okay = true;
- break;
+ return true;
+ }
+ else
+ {
+ if(delay > MAX_DELAY)
+ {
+ return false;
+ }
+ MUtils::OS::sleep_ms(delay);
+ delay *= 2;
}
- 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)
{
QWriteLocker writeLock(&g_temp_folder_lock);
-
+
//Clean the directory
while(!g_temp_folder_file.isNull())
{
const QString tempPath = g_temp_folder_file->getPath();
g_temp_folder_file.reset(NULL);
- temp_folder_cleanup_helper(tempPath);
+ if(!temp_folder_cleanup_helper(tempPath))
+ {
+ MUtils::OS::system_message_wrn(L"Temp Cleaner", L"Warning: Not all temporary files could be removed!");
+ }
}
}
// REMOVE DIRECTORY / FILE
///////////////////////////////////////////////////////////////////////////////
+static const QFile::Permissions FILE_PERMISSIONS_NONE = QFile::ReadOther | QFile::WriteOther;
+
bool MUtils::remove_file(const QString &fileName)
{
QFileInfo fileInfo(fileName);
for(int i = 0; i < 32; i++)
{
QFile file(fileName);
- file.setPermissions(QFile::ReadOther | QFile::WriteOther);
+ file.setPermissions(FILE_PERMISSIONS_NONE);
if((!(fileInfo.exists() && fileInfo.isFile())) || file.remove())
{
return true;
return false;
}
-static bool remove_directory_helper(QDir folder)
+static bool remove_directory_helper(const QDir &folder)
{
if(!folder.exists())
{
return true;
}
-
const QString dirName = folder.dirName();
- if(dirName.isEmpty() || (!folder.cdUp()))
+ if(!dirName.isEmpty())
{
- return false;
+ QDir parent(folder);
+ if(parent.cdUp())
+ {
+ QFile::setPermissions(folder.absolutePath(), FILE_PERMISSIONS_NONE);
+ if(parent.rmdir(dirName))
+ {
+ return true;
+ }
+ }
}
-
- return folder.rmdir(dirName);
+ return false;
}
bool MUtils::remove_directory(const QString &folderPath, const bool &recursive)
for(int i = 0; i < 32; i++)
{
- if(!folder.exists())
+ if(remove_directory_helper(folder))
{
return true;
}
- const QString dirName = folder.dirName();
- if(!dirName.isEmpty())
- {
- QDir parent(folder);
- if(parent.cdUp())
- {
- if(parent.rmdir(dirName))
- {
- return true;
- }
- }
- }
folder.refresh();
}
QString MUtils::clean_file_name(const QString &name)
{
- QString str = name.simplified();
+ QRegExp regExp("\"(.+)\"");
+ regExp.setMinimal(true);
+ QString str = QString(name).replace(regExp, "``\\1´´").trimmed();
for(size_t i = 0; CLEAN_FILE_NAME[i].search; i++)
{
str.replace(CLEAN_FILE_NAME[i].search, CLEAN_FILE_NAME[i].replace);
}
- QRegExp regExp("\"(.+)\"");
- regExp.setMinimal(true);
- str.replace(regExp, "`\\1´");
-
- return str.simplified();
+ while(str.endsWith(QLatin1Char('.')))
+ {
+ str.chop(1);
+ str = str.trimmed();
+ }
+
+ return str.trimmed();
+}
+
+static QPair<QString,QString> clean_file_path_get_prefix(const QString path)
+{
+ static const char *const PREFIXES[] =
+ {
+ "//?/", "//", "/", NULL
+ };
+ const QString posixPath = QDir::fromNativeSeparators(path.trimmed());
+ for (int i = 0; PREFIXES[i]; i++)
+ {
+ const QString prefix = QString::fromLatin1(PREFIXES[i]);
+ if (posixPath.startsWith(prefix))
+ {
+ return qMakePair(prefix, posixPath.mid(prefix.length()));
+ }
+ }
+ return qMakePair(QString(), posixPath);
}
QString MUtils::clean_file_path(const QString &path)
{
- QStringList parts = path.simplified().replace("\\", "/").split("/", QString::SkipEmptyParts);
+ const QPair<QString, QString> prefix = clean_file_path_get_prefix(path);
+ QStringList parts = prefix.second.split(QLatin1Char('/'), QString::SkipEmptyParts);
for(int i = 0; i < parts.count(); i++)
{
+ if((i == 0) && (parts[i].length() == 2) && parts[i][0].isLetter() && (parts[i][1] == QLatin1Char(':')))
+ {
+ continue; //handle case "c:\"
+ }
parts[i] = MUtils::clean_file_name(parts[i]);
}
- return parts.join("/");
+ const QString cleanPath = parts.join(QLatin1String("/"));
+ return prefix.first.isEmpty() ? cleanPath : prefix.first + cleanPath;
}
///////////////////////////////////////////////////////////////////////////////
int MUtils::Internal::selfTest(const char *const buildKey, const bool debug)
{
static const bool MY_DEBUG_FLAG = MUTILS_DEBUG;
- static const char *const MY_BUILD_KEY = __DATE__"@"__TIME__;
+ static const char *const MY_BUILD_KEY = __DATE__ "@" __TIME__;
- if(strncmp(buildKey, MY_BUILD_KEY, 14) || (MY_DEBUG_FLAG != debug))
+ if(strncmp(buildKey, MY_BUILD_KEY, 13) || (MY_DEBUG_FLAG != debug))
{
MUtils::OS::system_message_err(L"MUtils", L"FATAL ERROR: MUtils library version mismatch detected!");
- MUtils::OS::system_message_wrn(L"MUtils", L"Please re-build the complete solution in order to fix this issue!");
+ MUtils::OS::system_message_wrn(L"MUtils", L"Perform a clean(!) re-install of the application to fix the problem!");
abort();
}
return 0;