1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2018 LoRd_MuldeR <MuldeR2@GMX.de>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
27 #include <MUtils/Global.h>
28 #include <MUtils/OSSupport.h>
29 #include <MUtils/Version.h>
32 #include "DirLocker.h"
33 #include "3rd_party/strnatcmp/include/strnatcmp.h"
37 #include <QReadWriteLock>
41 #include <QListIterator>
43 #include <QThreadStorage>
55 ///////////////////////////////////////////////////////////////////////////////
57 ///////////////////////////////////////////////////////////////////////////////
60 #define rand_s(X) (true)
63 //Per-thread init flag
64 static QThreadStorage<bool> g_srand_flag;
66 //32-Bit wrapper for qrand()
67 #define QRAND() ((static_cast<quint32>(qrand()) & 0xFFFF) | (static_cast<quint32>(qrand()) << 16U))
69 //Robert Jenkins' 96 bit Mix Function
70 static quint32 mix_function(quint32 a, quint32 b, quint32 c)
72 a=a-b; a=a-c; a=a^(c >> 13);
73 b=b-c; b=b-a; b=b^(a << 8);
74 c=c-a; c=c-b; c=c^(b >> 13);
75 a=a-b; a=a-c; a=a^(c >> 12);
76 b=b-c; b=b-a; b=b^(a << 16);
77 c=c-a; c=c-b; c=c^(b >> 5);
78 a=a-b; a=a-c; a=a^(c >> 3);
79 b=b-c; b=b-a; b=b^(a << 10);
80 c=c-a; c=c-b; c=c^(b >> 15);
85 static void seed_rand(void)
87 QDateTime build(MUtils::Version::lib_build_date(), MUtils::Version::lib_build_time());
88 const quint32 seed_0 = mix_function(MUtils::OS::process_id(), MUtils::OS::thread_id(), build.toMSecsSinceEpoch());
89 qsrand(mix_function(clock(), time(NULL), seed_0));
92 static quint32 rand_fallback(void)
94 Q_ASSERT(RAND_MAX >= 0x7FFF);
96 if (!(g_srand_flag.hasLocalData() && g_srand_flag.localData()))
99 g_srand_flag.setLocalData(true);
102 quint32 rnd_val = mix_function(0x32288EA3, clock(), time(NULL));
104 for (size_t i = 0; i < 42; i++)
106 rnd_val = mix_function(rnd_val, QRAND(), QRAND());
107 rnd_val = mix_function(QRAND(), rnd_val, QRAND());
108 rnd_val = mix_function(QRAND(), QRAND(), rnd_val);
114 quint32 MUtils::next_rand_u32(void)
119 return rand_fallback();
124 quint64 MUtils::next_rand_u64(void)
126 return (quint64(next_rand_u32()) << 32) | quint64(next_rand_u32());
129 QString MUtils::next_rand_str(const bool &bLong)
133 return next_rand_str(false) + next_rand_str(false);
137 return QString::number(next_rand_u64(), 16).rightJustified(16, QLatin1Char('0'));
141 ///////////////////////////////////////////////////////////////////////////////
142 // STRING UTILITY FUNCTIONS
143 ///////////////////////////////////////////////////////////////////////////////
145 static QScopedPointer<QRegExp> g_str_trim_rx_r;
146 static QScopedPointer<QRegExp> g_str_trim_rx_l;
147 static QMutex g_str_trim_lock;
149 static QString& trim_helper(QString &str, QScopedPointer<QRegExp> ®ex, const char *const pattern)
151 QMutexLocker lock(&g_str_trim_lock);
154 regex.reset(new QRegExp(QLatin1String(pattern)));
156 str.remove(*regex.data());
160 QString& MUtils::trim_right(QString &str)
162 static const char *const TRIM_RIGHT = "\\s+$";
163 return trim_helper(str, g_str_trim_rx_r, TRIM_RIGHT);
166 QString& MUtils::trim_left(QString &str)
168 static const char *const TRIM_LEFT = "^\\s+";
169 return trim_helper(str, g_str_trim_rx_l, TRIM_LEFT);
172 QString MUtils::trim_right(const QString &str)
175 return trim_right(temp);
178 QString MUtils::trim_left(const QString &str)
181 return trim_left(temp);
184 ///////////////////////////////////////////////////////////////////////////////
185 // GENERATE FILE NAME
186 ///////////////////////////////////////////////////////////////////////////////
188 QString MUtils::make_temp_file(const QString &basePath, const QString &extension, const bool placeholder)
190 for(int i = 0; i < 4096; i++)
192 const QString tempFileName = QString("%1/%2.%3").arg(basePath, next_rand_str(), extension);
193 if(!QFileInfo(tempFileName).exists())
197 QFile file(tempFileName);
198 if(file.open(QFile::ReadWrite))
211 qWarning("Failed to generate temp file name!");
215 QString MUtils::make_unique_file(const QString &basePath, const QString &baseName, const QString &extension, const bool fancy)
217 quint32 n = fancy ? 2 : 0;
218 QString fileName = fancy ? QString("%1/%2.%3").arg(basePath, baseName, extension) : QString();
219 while (fileName.isEmpty() || QFileInfo(fileName).exists())
221 if (n <= quint32(USHRT_MAX))
225 fileName = QString("%1/%2 (%3).%4").arg(basePath, baseName, QString::number(n++), extension);
229 fileName = QString("%1/%2.%3.%4").arg(basePath, baseName, QString::number(n++, 16).rightJustified(4, QLatin1Char('0')), extension);
234 qWarning("Failed to generate unique file name!");
241 ///////////////////////////////////////////////////////////////////////////////
243 ///////////////////////////////////////////////////////////////////////////////
246 * Compute parity in parallel
247 * http://www.graphics.stanford.edu/~seander/bithacks.html#ParityParallel
249 bool MUtils::parity(quint32 value)
251 value ^= value >> 16;
255 return ((0x6996 >> value) & 1) != 0;
258 ///////////////////////////////////////////////////////////////////////////////
260 ///////////////////////////////////////////////////////////////////////////////
262 static QScopedPointer<MUtils::Internal::DirLock> g_temp_folder_file;
263 static QReadWriteLock g_temp_folder_lock;
265 static QString try_create_subfolder(const QString &baseDir, const QString &postfix)
267 const QString baseDirPath = QDir(baseDir).absolutePath();
268 for(int i = 0; i < 32; i++)
270 QDir directory(baseDirPath);
271 if(directory.mkpath(postfix) && directory.cd(postfix))
273 return directory.canonicalPath();
279 static MUtils::Internal::DirLock *try_init_temp_folder(const QString &baseDir)
281 const QString tempPath = try_create_subfolder(baseDir, MUtils::next_rand_str());
282 if(!tempPath.isEmpty())
284 for(int i = 0; i < 32; i++)
286 MUtils::Internal::DirLock *lockFile = NULL;
289 lockFile = new MUtils::Internal::DirLock(tempPath);
292 catch(MUtils::Internal::DirLockException&)
294 /*ignore error and try again*/
301 static bool temp_folder_cleanup_helper(const QString &tempPath)
304 static const size_t MAX_DELAY = 8192;
307 QDir::setCurrent(QDir::rootPath());
308 if(MUtils::remove_directory(tempPath, true))
314 if(delay > MAX_DELAY)
318 MUtils::OS::sleep_ms(delay);
324 static void temp_folder_cleaup(void)
326 QWriteLocker writeLock(&g_temp_folder_lock);
328 //Clean the directory
329 while(!g_temp_folder_file.isNull())
331 const QString tempPath = g_temp_folder_file->getPath();
332 g_temp_folder_file.reset(NULL);
333 if(!temp_folder_cleanup_helper(tempPath))
335 MUtils::OS::system_message_wrn(L"Temp Cleaner", L"Warning: Not all temporary files could be removed!");
340 const QString &MUtils::temp_folder(void)
342 QReadLocker readLock(&g_temp_folder_lock);
344 //Already initialized?
345 if(!g_temp_folder_file.isNull())
347 return g_temp_folder_file->getPath();
350 //Obtain the write lock to initilaize
352 QWriteLocker writeLock(&g_temp_folder_lock);
354 //Still uninitilaized?
355 if(!g_temp_folder_file.isNull())
357 return g_temp_folder_file->getPath();
360 //Try the %TMP% or %TEMP% directory first
361 if(MUtils::Internal::DirLock *lockFile = try_init_temp_folder(QDir::tempPath()))
363 g_temp_folder_file.reset(lockFile);
364 atexit(temp_folder_cleaup);
365 return lockFile->getPath();
368 qWarning("%%TEMP%% directory not found -> trying fallback mode now!");
369 static const OS::known_folder_t FOLDER_ID[2] = { OS::FOLDER_LOCALAPPDATA, OS::FOLDER_SYSTROOT_DIR };
370 for(size_t id = 0; id < 2; id++)
372 const QString &knownFolder = OS::known_folder(FOLDER_ID[id]);
373 if(!knownFolder.isEmpty())
375 const QString tempRoot = try_create_subfolder(knownFolder, QLatin1String("TEMP"));
376 if(!tempRoot.isEmpty())
378 if(MUtils::Internal::DirLock *lockFile = try_init_temp_folder(tempRoot))
380 g_temp_folder_file.reset(lockFile);
381 atexit(temp_folder_cleaup);
382 return lockFile->getPath();
388 qFatal("Temporary directory could not be initialized !!!");
389 return (*((QString*)NULL));
392 ///////////////////////////////////////////////////////////////////////////////
393 // REMOVE DIRECTORY / FILE
394 ///////////////////////////////////////////////////////////////////////////////
396 static const QFile::Permissions FILE_PERMISSIONS_NONE = QFile::ReadOther | QFile::WriteOther;
398 bool MUtils::remove_file(const QString &fileName)
400 QFileInfo fileInfo(fileName);
401 if(!(fileInfo.exists() && fileInfo.isFile()))
406 for(int i = 0; i < 32; i++)
408 QFile file(fileName);
409 file.setPermissions(FILE_PERMISSIONS_NONE);
410 if((!(fileInfo.exists() && fileInfo.isFile())) || file.remove())
414 MUtils::OS::sleep_ms(1);
418 qWarning("Could not delete \"%s\"", MUTILS_UTF8(fileName));
422 static bool remove_directory_helper(const QDir &folder)
428 const QString dirName = folder.dirName();
429 if(!dirName.isEmpty())
434 QFile::setPermissions(folder.absolutePath(), FILE_PERMISSIONS_NONE);
435 if(parent.rmdir(dirName))
444 bool MUtils::remove_directory(const QString &folderPath, const bool &recursive)
446 QDir folder(folderPath);
454 const QFileInfoList entryList = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
455 for(QFileInfoList::ConstIterator iter = entryList.constBegin(); iter != entryList.constEnd(); iter++)
459 remove_directory(iter->canonicalFilePath(), true);
461 else if(iter->isFile())
463 remove_file(iter->canonicalFilePath());
468 for(int i = 0; i < 32; i++)
470 if(remove_directory_helper(folder))
474 MUtils::OS::sleep_ms(1);
478 qWarning("Could not rmdir \"%s\"", MUTILS_UTF8(folderPath));
482 ///////////////////////////////////////////////////////////////////////////////
484 ///////////////////////////////////////////////////////////////////////////////
486 static void prependToPath(QProcessEnvironment &env, const QString &value)
488 const QLatin1String PATH = QLatin1String("PATH");
489 const QString path = env.value(PATH, QString()).trimmed();
490 env.insert(PATH, path.isEmpty() ? value : QString("%1;%2").arg(value, path));
493 void MUtils::init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir, const QStringList *const extraPaths)
495 //Environment variable names
496 static const char *const s_envvar_names_temp[] =
498 "TEMP", "TMP", "TMPDIR", "HOME", "USERPROFILE", "HOMEPATH", NULL
500 static const char *const s_envvar_names_remove[] =
502 "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
505 //Initialize environment
506 QProcessEnvironment env = process.processEnvironment();
507 if(env.isEmpty()) env = QProcessEnvironment::systemEnvironment();
509 //Clean a number of enviroment variables that might affect our tools
510 for(size_t i = 0; s_envvar_names_remove[i]; i++)
512 env.remove(QString::fromLatin1(s_envvar_names_remove[i]));
513 env.remove(QString::fromLatin1(s_envvar_names_remove[i]).toLower());
516 const QString tempDir = QDir::toNativeSeparators(temp_folder());
518 //Replace TEMP directory in environment
521 for(size_t i = 0; s_envvar_names_temp[i]; i++)
523 env.insert(s_envvar_names_temp[i], tempDir);
527 //Setup PATH variable
528 prependToPath(env, tempDir);
529 if (extraPaths && (!extraPaths->isEmpty()))
531 QListIterator<QString> iter(*extraPaths);
533 while (iter.hasPrevious())
535 prependToPath(env, QDir::toNativeSeparators(iter.previous()));
539 //Setup QPorcess object
540 process.setWorkingDirectory(wokringDir);
541 process.setProcessChannelMode(QProcess::MergedChannels);
542 process.setReadChannel(QProcess::StandardOutput);
543 process.setProcessEnvironment(env);
546 ///////////////////////////////////////////////////////////////////////////////
547 // NATURAL ORDER STRING COMPARISON
548 ///////////////////////////////////////////////////////////////////////////////
550 static bool natural_string_sort_helper(const QString &str1, const QString &str2)
552 return (MUtils::Internal::NaturalSort::strnatcmp(MUTILS_WCHR(str1), MUTILS_WCHR(str2)) < 0);
555 static bool natural_string_sort_helper_fold_case(const QString &str1, const QString &str2)
557 return (MUtils::Internal::NaturalSort::strnatcasecmp(MUTILS_WCHR(str1), MUTILS_WCHR(str2)) < 0);
560 void MUtils::natural_string_sort(QStringList &list, const bool bIgnoreCase)
562 qSort(list.begin(), list.end(), bIgnoreCase ? natural_string_sort_helper_fold_case : natural_string_sort_helper);
565 ///////////////////////////////////////////////////////////////////////////////
567 ///////////////////////////////////////////////////////////////////////////////
569 static QMutex g_clean_file_name_mutex;
570 static QScopedPointer<const QList<QPair<QRegExp,QString>>> g_clean_file_name_regex;
572 static void clean_file_name_make_pretty(QString &str)
574 static const struct { const char *p; const char *r; } PATTERN[] =
576 { "^\\s*\"([^\"]*)\"\\s*$", "\\1" }, //Remove straight double quotes around the whole string
577 { "\"([^\"]*)\"", "\xE2\x80\x9C\\1\xE2\x80\x9D" }, //Replace remaining pairs of straight double quotes with opening/closing double quote
578 { "^[\\\\/:]+([^\\\\/:]+.*)$", "\\1" }, //Remove leading slash, backslash and colon characters
579 { "^(.*[^\\\\/:]+)[\\\\/:]+$", "\\1" }, //Remove trailing slash, backslash and colon characters
580 { "(\\s*[\\\\/:]\\s*)+", " - " }, //Replace any slash, backslash or colon character that appears in the middle
584 QMutexLocker locker(&g_clean_file_name_mutex);
586 if (g_clean_file_name_regex.isNull())
588 QScopedPointer<QList<QPair<QRegExp, QString>>> list(new QList<QPair<QRegExp, QString>>());
589 for (size_t i = 0; PATTERN[i].p; ++i)
591 list->append(qMakePair(QRegExp(QString::fromUtf8(PATTERN[i].p), Qt::CaseInsensitive), PATTERN[i].r ? QString::fromUtf8(PATTERN[i].r) : QString()));
593 g_clean_file_name_regex.reset(list.take());
596 bool keepOnGoing = !str.isEmpty();
599 const QString prev = str;
601 for (QList<QPair<QRegExp, QString>>::ConstIterator iter = g_clean_file_name_regex->constBegin(); iter != g_clean_file_name_regex->constEnd(); ++iter)
603 str.replace(iter->first, iter->second);
604 if (str.compare(prev))
606 str = str.simplified();
607 keepOnGoing = !str.isEmpty();
614 QString MUtils::clean_file_name(const QString &name, const bool &pretty)
616 static const QLatin1Char REPLACEMENT_CHAR('_');
617 static const char FILENAME_ILLEGAL_CHARS[] = "<>:\"/\\|?*";
618 static const char *const FILENAME_RESERVED_NAMES[] =
620 "CON", "PRN", "AUX", "NUL",
621 "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
622 "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", NULL
625 QString result(name);
628 clean_file_name_make_pretty(result);
631 for(QString::Iterator iter = result.begin(); iter != result.end(); iter++)
633 if (iter->category() == QChar::Other_Control)
635 *iter = REPLACEMENT_CHAR;
639 for(size_t i = 0; FILENAME_ILLEGAL_CHARS[i]; i++)
641 result.replace(QLatin1Char(FILENAME_ILLEGAL_CHARS[i]), REPLACEMENT_CHAR);
645 while (result.endsWith(QLatin1Char('.')))
651 for (size_t i = 0; FILENAME_RESERVED_NAMES[i]; i++)
653 const QString reserved = QString::fromLatin1(FILENAME_RESERVED_NAMES[i]);
654 if ((!result.compare(reserved, Qt::CaseInsensitive)) || result.startsWith(reserved + QLatin1Char('.'), Qt::CaseInsensitive))
656 result.replace(0, reserved.length(), QString().leftJustified(reserved.length(), REPLACEMENT_CHAR));
663 static QPair<QString,QString> clean_file_path_get_prefix(const QString path)
665 static const char *const PREFIXES[] =
667 "//?/", "//", "/", NULL
669 const QString posixPath = QDir::fromNativeSeparators(path.trimmed());
670 for (int i = 0; PREFIXES[i]; i++)
672 const QString prefix = QString::fromLatin1(PREFIXES[i]);
673 if (posixPath.startsWith(prefix))
675 return qMakePair(prefix, posixPath.mid(prefix.length()));
678 return qMakePair(QString(), posixPath);
681 QString MUtils::clean_file_path(const QString &path, const bool &pretty)
683 const QPair<QString, QString> prefix = clean_file_path_get_prefix(path);
685 QStringList parts = prefix.second.split(QLatin1Char('/'), QString::SkipEmptyParts);
686 for(int i = 0; i < parts.count(); i++)
688 if((i == 0) && (parts[i].length() == 2) && parts[i][0].isLetter() && (parts[i][1] == QLatin1Char(':')))
690 continue; //handle case "c:\"
692 parts[i] = MUtils::clean_file_name(parts[i], pretty);
695 const QString cleanPath = parts.join(QLatin1String("/"));
696 return prefix.first.isEmpty() ? cleanPath : prefix.first + cleanPath;
699 ///////////////////////////////////////////////////////////////////////////////
700 // REGULAR EXPESSION HELPER
701 ///////////////////////////////////////////////////////////////////////////////
703 bool MUtils::regexp_parse_uint32(const QRegExp ®exp, quint32 &value)
705 return regexp_parse_uint32(regexp, &value, 1U, 1U);
708 bool MUtils::regexp_parse_int32(const QRegExp ®exp, qint32 &value)
710 return regexp_parse_int32(regexp, &value, 1U, 1U);
713 bool MUtils::regexp_parse_uint32(const QRegExp ®exp, quint32 &value, const size_t &offset)
715 return regexp_parse_uint32(regexp, &value, offset, 1U);
718 bool MUtils::regexp_parse_int32(const QRegExp ®exp, qint32 &value, const size_t &offset)
720 return regexp_parse_int32(regexp, &value, offset, 1U);
723 bool MUtils::regexp_parse_uint32(const QRegExp ®exp, quint32 *values, const size_t &count)
725 return regexp_parse_uint32(regexp, values, 1U, count);
728 bool MUtils::regexp_parse_int32(const QRegExp ®exp, qint32 *values, const size_t &count)
730 return regexp_parse_int32(regexp, values, 1U, count);
733 bool MUtils::regexp_parse_uint32(const QRegExp ®exp, quint32 *values, const size_t &offset, const size_t &count)
735 const QStringList caps = regexp.capturedTexts();
737 if (caps.isEmpty() || (quint32(caps.count()) <= count))
742 for (size_t i = 0; i < count; i++)
745 values[i] = caps[offset+i].toUInt(&ok);
755 bool MUtils::regexp_parse_int32(const QRegExp ®exp, qint32 *values, const size_t &offset, const size_t &count)
757 const QStringList caps = regexp.capturedTexts();
759 if (caps.isEmpty() || (quint32(caps.count()) <= count))
764 for (size_t i = 0; i < count; i++)
767 values[i] = caps[offset+i].toInt(&ok);
777 ///////////////////////////////////////////////////////////////////////////////
778 // AVAILABLE CODEPAGES
779 ///////////////////////////////////////////////////////////////////////////////
781 QStringList MUtils::available_codepages(const bool &noAliases)
783 QStringList codecList;
784 QList<QByteArray> availableCodecs = QTextCodec::availableCodecs();
786 while(!availableCodecs.isEmpty())
788 const QByteArray current = availableCodecs.takeFirst();
789 if(!current.toLower().startsWith("system"))
791 codecList << QString::fromLatin1(current.constData(), current.size());
794 if(QTextCodec *const currentCodec = QTextCodec::codecForName(current.constData()))
796 const QList<QByteArray> aliases = currentCodec->aliases();
797 for(QList<QByteArray>::ConstIterator iter = aliases.constBegin(); iter != aliases.constEnd(); iter++)
799 availableCodecs.removeAll(*iter);
809 ///////////////////////////////////////////////////////////////////////////////
811 ///////////////////////////////////////////////////////////////////////////////
813 MUtils::fp_parts_t MUtils::break_fp(const double value)
815 fp_parts_t result = { };
818 result.parts[1] = modf(value, &result.parts[0]);
822 result.parts[0] = std::numeric_limits<double>::quiet_NaN();
823 result.parts[1] = std::numeric_limits<double>::quiet_NaN();
828 ///////////////////////////////////////////////////////////////////////////////
830 ///////////////////////////////////////////////////////////////////////////////
832 int MUtils::Internal::selfTest(const char *const buildKey, const bool debug)
834 static const bool MY_DEBUG_FLAG = MUTILS_DEBUG;
835 static const char *const MY_BUILD_KEY = __DATE__ "@" __TIME__;
837 if(strncmp(buildKey, MY_BUILD_KEY, 13) || (MY_DEBUG_FLAG != debug))
839 MUtils::OS::system_message_err(L"MUtils", L"FATAL ERROR: MUtils library version mismatch detected!");
840 MUtils::OS::system_message_wrn(L"MUtils", L"Perform a clean(!) re-install of the application to fix the problem!");