Happy new year 2017!
[MUtilities.git] / src / Global.cpp
blobdc48e8429ad72d2a4bb14d0e619cc9972d182b2a
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2017 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
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.
9 //
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 //////////////////////////////////////////////////////////////////////////////////
22 #if _MSC_VER
23 #define _CRT_RAND_S 1
24 #endif
26 //MUtils
27 #include <MUtils/Global.h>
28 #include <MUtils/OSSupport.h>
29 #include <MUtils/Version.h>
31 //Internal
32 #include "DirLocker.h"
33 #include "3rd_party/strnatcmp/include/strnatcmp.h"
35 //Qt
36 #include <QDir>
37 #include <QReadWriteLock>
38 #include <QProcess>
39 #include <QTextCodec>
40 #include <QPair>
41 #include <QListIterator>
42 #include <QMutex>
43 #include <QThreadStorage>
45 //CRT
46 #include <cstdlib>
47 #include <ctime>
48 #include <process.h>
50 //VLD
51 #ifdef _MSC_VER
52 #include <vld.h>
53 #endif
55 ///////////////////////////////////////////////////////////////////////////////
56 // Random Support
57 ///////////////////////////////////////////////////////////////////////////////
59 #ifndef _CRT_RAND_S
60 #define rand_s(X) (true)
61 #endif
63 //Per-thread init flag
64 static QThreadStorage<bool> g_srand_flag;
66 //Robert Jenkins' 96 bit Mix Function
67 static quint32 mix_function(const quint32 x, const quint32 y, const quint32 z)
69 quint32 a = x;
70 quint32 b = y;
71 quint32 c = z;
73 a=a-b; a=a-c; a=a^(c >> 13);
74 b=b-c; b=b-a; b=b^(a << 8);
75 c=c-a; c=c-b; c=c^(b >> 13);
76 a=a-b; a=a-c; a=a^(c >> 12);
77 b=b-c; b=b-a; b=b^(a << 16);
78 c=c-a; c=c-b; c=c^(b >> 5);
79 a=a-b; a=a-c; a=a^(c >> 3);
80 b=b-c; b=b-a; b=b^(a << 10);
81 c=c-a; c=c-b; c=c^(b >> 15);
83 return a ^ b ^ c;
86 static void seed_rand(void)
88 QDateTime build(MUtils::Version::lib_build_date(), MUtils::Version::lib_build_time());
89 const quint32 seed = mix_function(MUtils::OS::process_id(), MUtils::OS::thread_id(), build.toMSecsSinceEpoch());
90 qsrand(mix_function(clock(), time(NULL), seed));
93 static quint32 rand_fallback(void)
95 Q_ASSERT(RAND_MAX >= 0xFFF);
96 if (!(g_srand_flag.hasLocalData() && g_srand_flag.localData()))
98 seed_rand();
99 g_srand_flag.setLocalData(true);
101 quint32 rnd = 0x32288EA3;
102 for (size_t i = 0; i < 3; i++)
104 rnd = (rnd << 12) ^ qrand();
106 return rnd;
109 quint32 MUtils::next_rand_u32(void)
111 quint32 rnd;
112 if (rand_s(&rnd))
114 return rand_fallback();
116 return rnd;
119 quint64 MUtils::next_rand_u64(void)
121 return (quint64(next_rand_u32()) << 32) | quint64(next_rand_u32());
124 QString MUtils::next_rand_str(const bool &bLong)
126 if (bLong)
128 return next_rand_str(false) + next_rand_str(false);
130 else
132 return QString::number(next_rand_u64(), 16).rightJustified(16, QLatin1Char('0'));
136 ///////////////////////////////////////////////////////////////////////////////
137 // STRING UTILITY FUNCTIONS
138 ///////////////////////////////////////////////////////////////////////////////
140 static QScopedPointer<QRegExp> g_str_trim_rx_r;
141 static QScopedPointer<QRegExp> g_str_trim_rx_l;
142 static QMutex g_str_trim_lock;
144 static QString& trim_helper(QString &str, QScopedPointer<QRegExp> &regex, const char *const pattern)
146 QMutexLocker lock(&g_str_trim_lock);
147 if (regex.isNull())
149 regex.reset(new QRegExp(QLatin1String(pattern)));
151 str.remove(*regex.data());
152 return str;
155 QString& MUtils::trim_right(QString &str)
157 static const char *const TRIM_RIGHT = "\\s+$";
158 return trim_helper(str, g_str_trim_rx_r, TRIM_RIGHT);
161 QString& MUtils::trim_left(QString &str)
163 static const char *const TRIM_LEFT = "^\\s+";
164 return trim_helper(str, g_str_trim_rx_l, TRIM_LEFT);
167 QString MUtils::trim_right(const QString &str)
169 QString temp(str);
170 return trim_right(temp);
173 QString MUtils::trim_left(const QString &str)
175 QString temp(str);
176 return trim_left(temp);
179 ///////////////////////////////////////////////////////////////////////////////
180 // GENERATE FILE NAME
181 ///////////////////////////////////////////////////////////////////////////////
183 QString MUtils::make_temp_file(const QString &basePath, const QString &extension, const bool placeholder)
185 for(int i = 0; i < 4096; i++)
187 const QString tempFileName = QString("%1/%2.%3").arg(basePath, next_rand_str(), extension);
188 if(!QFileInfo(tempFileName).exists())
190 if(placeholder)
192 QFile file(tempFileName);
193 if(file.open(QFile::ReadWrite))
195 file.close();
196 return tempFileName;
199 else
201 return tempFileName;
206 qWarning("Failed to generate temp file name!");
207 return QString();
210 QString MUtils::make_unique_file(const QString &basePath, const QString &baseName, const QString &extension, const bool fancy)
212 quint32 n = fancy ? 2 : 0;
213 QString fileName = fancy ? QString("%1/%2.%3").arg(basePath, baseName, extension) : QString();
214 while (fileName.isEmpty() || QFileInfo(fileName).exists())
216 if (n <= quint32(USHRT_MAX))
218 if (fancy)
220 fileName = QString("%1/%2 (%3).%4").arg(basePath, baseName, QString::number(n++), extension);
222 else
224 fileName = QString("%1/%2.%3.%4").arg(basePath, baseName, QString::number(n++, 16).rightJustified(4, QLatin1Char('0')), extension);
227 else
229 qWarning("Failed to generate unique file name!");
230 return QString();
233 return fileName;
236 ///////////////////////////////////////////////////////////////////////////////
237 // COMPUTE PARITY
238 ///////////////////////////////////////////////////////////////////////////////
241 * Compute parity in parallel
242 * http://www.graphics.stanford.edu/~seander/bithacks.html#ParityParallel
244 bool MUtils::parity(quint32 value)
246 value ^= value >> 16;
247 value ^= value >> 8;
248 value ^= value >> 4;
249 value &= 0xf;
250 return ((0x6996 >> value) & 1) != 0;
253 ///////////////////////////////////////////////////////////////////////////////
254 // TEMP FOLDER
255 ///////////////////////////////////////////////////////////////////////////////
257 static QScopedPointer<MUtils::Internal::DirLock> g_temp_folder_file;
258 static QReadWriteLock g_temp_folder_lock;
260 static QString try_create_subfolder(const QString &baseDir, const QString &postfix)
262 const QString baseDirPath = QDir(baseDir).absolutePath();
263 for(int i = 0; i < 32; i++)
265 QDir directory(baseDirPath);
266 if(directory.mkpath(postfix) && directory.cd(postfix))
268 return directory.canonicalPath();
271 return QString();
274 static MUtils::Internal::DirLock *try_init_temp_folder(const QString &baseDir)
276 const QString tempPath = try_create_subfolder(baseDir, MUtils::next_rand_str());
277 if(!tempPath.isEmpty())
279 for(int i = 0; i < 32; i++)
281 MUtils::Internal::DirLock *lockFile = NULL;
284 lockFile = new MUtils::Internal::DirLock(tempPath);
285 return lockFile;
287 catch(MUtils::Internal::DirLockException&)
289 /*ignore error and try again*/
293 return NULL;
296 static bool temp_folder_cleanup_helper(const QString &tempPath)
298 size_t delay = 1;
299 static const size_t MAX_DELAY = 8192;
300 forever
302 QDir::setCurrent(QDir::rootPath());
303 if(MUtils::remove_directory(tempPath, true))
305 return true;
307 else
309 if(delay > MAX_DELAY)
311 return false;
313 MUtils::OS::sleep_ms(delay);
314 delay *= 2;
319 static void temp_folder_cleaup(void)
321 QWriteLocker writeLock(&g_temp_folder_lock);
323 //Clean the directory
324 while(!g_temp_folder_file.isNull())
326 const QString tempPath = g_temp_folder_file->getPath();
327 g_temp_folder_file.reset(NULL);
328 if(!temp_folder_cleanup_helper(tempPath))
330 MUtils::OS::system_message_wrn(L"Temp Cleaner", L"Warning: Not all temporary files could be removed!");
335 const QString &MUtils::temp_folder(void)
337 QReadLocker readLock(&g_temp_folder_lock);
339 //Already initialized?
340 if(!g_temp_folder_file.isNull())
342 return g_temp_folder_file->getPath();
345 //Obtain the write lock to initilaize
346 readLock.unlock();
347 QWriteLocker writeLock(&g_temp_folder_lock);
349 //Still uninitilaized?
350 if(!g_temp_folder_file.isNull())
352 return g_temp_folder_file->getPath();
355 //Try the %TMP% or %TEMP% directory first
356 if(MUtils::Internal::DirLock *lockFile = try_init_temp_folder(QDir::tempPath()))
358 g_temp_folder_file.reset(lockFile);
359 atexit(temp_folder_cleaup);
360 return lockFile->getPath();
363 qWarning("%%TEMP%% directory not found -> trying fallback mode now!");
364 static const OS::known_folder_t FOLDER_ID[2] = { OS::FOLDER_LOCALAPPDATA, OS::FOLDER_SYSTROOT_DIR };
365 for(size_t id = 0; id < 2; id++)
367 const QString &knownFolder = OS::known_folder(FOLDER_ID[id]);
368 if(!knownFolder.isEmpty())
370 const QString tempRoot = try_create_subfolder(knownFolder, QLatin1String("TEMP"));
371 if(!tempRoot.isEmpty())
373 if(MUtils::Internal::DirLock *lockFile = try_init_temp_folder(tempRoot))
375 g_temp_folder_file.reset(lockFile);
376 atexit(temp_folder_cleaup);
377 return lockFile->getPath();
383 qFatal("Temporary directory could not be initialized !!!");
384 return (*((QString*)NULL));
387 ///////////////////////////////////////////////////////////////////////////////
388 // REMOVE DIRECTORY / FILE
389 ///////////////////////////////////////////////////////////////////////////////
391 static const QFile::Permissions FILE_PERMISSIONS_NONE = QFile::ReadOther | QFile::WriteOther;
393 bool MUtils::remove_file(const QString &fileName)
395 QFileInfo fileInfo(fileName);
396 if(!(fileInfo.exists() && fileInfo.isFile()))
398 return true;
401 for(int i = 0; i < 32; i++)
403 QFile file(fileName);
404 file.setPermissions(FILE_PERMISSIONS_NONE);
405 if((!(fileInfo.exists() && fileInfo.isFile())) || file.remove())
407 return true;
409 MUtils::OS::sleep_ms(1);
410 fileInfo.refresh();
413 qWarning("Could not delete \"%s\"", MUTILS_UTF8(fileName));
414 return false;
417 static bool remove_directory_helper(const QDir &folder)
419 if(!folder.exists())
421 return true;
423 const QString dirName = folder.dirName();
424 if(!dirName.isEmpty())
426 QDir parent(folder);
427 if(parent.cdUp())
429 QFile::setPermissions(folder.absolutePath(), FILE_PERMISSIONS_NONE);
430 if(parent.rmdir(dirName))
432 return true;
436 return false;
439 bool MUtils::remove_directory(const QString &folderPath, const bool &recursive)
441 QDir folder(folderPath);
442 if(!folder.exists())
444 return true;
447 if(recursive)
449 const QFileInfoList entryList = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
450 for(QFileInfoList::ConstIterator iter = entryList.constBegin(); iter != entryList.constEnd(); iter++)
452 if(iter->isDir())
454 remove_directory(iter->canonicalFilePath(), true);
456 else if(iter->isFile())
458 remove_file(iter->canonicalFilePath());
463 for(int i = 0; i < 32; i++)
465 if(remove_directory_helper(folder))
467 return true;
469 MUtils::OS::sleep_ms(1);
470 folder.refresh();
473 qWarning("Could not rmdir \"%s\"", MUTILS_UTF8(folderPath));
474 return false;
477 ///////////////////////////////////////////////////////////////////////////////
478 // PROCESS UTILS
479 ///////////////////////////////////////////////////////////////////////////////
481 static void prependToPath(QProcessEnvironment &env, const QString &value)
483 const QLatin1String PATH = QLatin1String("PATH");
484 const QString path = env.value(PATH, QString()).trimmed();
485 env.insert(PATH, path.isEmpty() ? value : QString("%1;%2").arg(value, path));
488 void MUtils::init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir, const QStringList *const extraPaths)
490 //Environment variable names
491 static const char *const s_envvar_names_temp[] =
493 "TEMP", "TMP", "TMPDIR", "HOME", "USERPROFILE", "HOMEPATH", NULL
495 static const char *const s_envvar_names_remove[] =
497 "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
500 //Initialize environment
501 QProcessEnvironment env = process.processEnvironment();
502 if(env.isEmpty()) env = QProcessEnvironment::systemEnvironment();
504 //Clean a number of enviroment variables that might affect our tools
505 for(size_t i = 0; s_envvar_names_remove[i]; i++)
507 env.remove(QString::fromLatin1(s_envvar_names_remove[i]));
508 env.remove(QString::fromLatin1(s_envvar_names_remove[i]).toLower());
511 const QString tempDir = QDir::toNativeSeparators(temp_folder());
513 //Replace TEMP directory in environment
514 if(bReplaceTempDir)
516 for(size_t i = 0; s_envvar_names_temp[i]; i++)
518 env.insert(s_envvar_names_temp[i], tempDir);
522 //Setup PATH variable
523 prependToPath(env, tempDir);
524 if (extraPaths && (!extraPaths->isEmpty()))
526 QListIterator<QString> iter(*extraPaths);
527 iter.toBack();
528 while (iter.hasPrevious())
530 prependToPath(env, QDir::toNativeSeparators(iter.previous()));
534 //Setup QPorcess object
535 process.setWorkingDirectory(wokringDir);
536 process.setProcessChannelMode(QProcess::MergedChannels);
537 process.setReadChannel(QProcess::StandardOutput);
538 process.setProcessEnvironment(env);
541 ///////////////////////////////////////////////////////////////////////////////
542 // NATURAL ORDER STRING COMPARISON
543 ///////////////////////////////////////////////////////////////////////////////
545 static bool natural_string_sort_helper(const QString &str1, const QString &str2)
547 return (MUtils::Internal::NaturalSort::strnatcmp(MUTILS_WCHR(str1), MUTILS_WCHR(str2)) < 0);
550 static bool natural_string_sort_helper_fold_case(const QString &str1, const QString &str2)
552 return (MUtils::Internal::NaturalSort::strnatcasecmp(MUTILS_WCHR(str1), MUTILS_WCHR(str2)) < 0);
555 void MUtils::natural_string_sort(QStringList &list, const bool bIgnoreCase)
557 qSort(list.begin(), list.end(), bIgnoreCase ? natural_string_sort_helper_fold_case : natural_string_sort_helper);
560 ///////////////////////////////////////////////////////////////////////////////
561 // CLEAN FILE PATH
562 ///////////////////////////////////////////////////////////////////////////////
564 QString MUtils::clean_file_name(const QString &name)
566 static const QLatin1Char REPLACEMENT_CHAR('_');
567 static const char FILENAME_ILLEGAL_CHARS[] = "<>:\"/\\|?*";
568 static const char *const FILENAME_RESERVED_NAMES[] =
570 "CON", "PRN", "AUX", "NUL",
571 "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
572 "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", NULL
575 QString result(name);
576 if (result.contains(QLatin1Char('"')))
578 QRegExp quoted("\"(.+)\"");
579 quoted.setMinimal(true);
580 result.replace(quoted, "``\\1ยดยด");
583 for(QString::Iterator iter = result.begin(); iter != result.end(); iter++)
585 if (iter->category() == QChar::Other_Control)
587 *iter = REPLACEMENT_CHAR;
591 for(size_t i = 0; FILENAME_ILLEGAL_CHARS[i]; i++)
593 result.replace(QLatin1Char(FILENAME_ILLEGAL_CHARS[i]), REPLACEMENT_CHAR);
596 trim_right(result);
597 while (result.endsWith(QLatin1Char('.')))
599 result.chop(1);
600 trim_right(result);
603 for (size_t i = 0; FILENAME_RESERVED_NAMES[i]; i++)
605 const QString reserved = QString::fromLatin1(FILENAME_RESERVED_NAMES[i]);
606 if ((!result.compare(reserved, Qt::CaseInsensitive)) || result.startsWith(reserved + QLatin1Char('.'), Qt::CaseInsensitive))
608 result.replace(0, reserved.length(), QString().leftJustified(reserved.length(), REPLACEMENT_CHAR));
612 return result;
615 static QPair<QString,QString> clean_file_path_get_prefix(const QString path)
617 static const char *const PREFIXES[] =
619 "//?/", "//", "/", NULL
621 const QString posixPath = QDir::fromNativeSeparators(path.trimmed());
622 for (int i = 0; PREFIXES[i]; i++)
624 const QString prefix = QString::fromLatin1(PREFIXES[i]);
625 if (posixPath.startsWith(prefix))
627 return qMakePair(prefix, posixPath.mid(prefix.length()));
630 return qMakePair(QString(), posixPath);
633 QString MUtils::clean_file_path(const QString &path)
635 const QPair<QString, QString> prefix = clean_file_path_get_prefix(path);
637 QStringList parts = prefix.second.split(QLatin1Char('/'), QString::SkipEmptyParts);
638 for(int i = 0; i < parts.count(); i++)
640 if((i == 0) && (parts[i].length() == 2) && parts[i][0].isLetter() && (parts[i][1] == QLatin1Char(':')))
642 continue; //handle case "c:\"
644 parts[i] = MUtils::clean_file_name(parts[i]);
647 const QString cleanPath = parts.join(QLatin1String("/"));
648 return prefix.first.isEmpty() ? cleanPath : prefix.first + cleanPath;
651 ///////////////////////////////////////////////////////////////////////////////
652 // REGULAR EXPESSION HELPER
653 ///////////////////////////////////////////////////////////////////////////////
655 bool MUtils::regexp_parse_uint32(const QRegExp &regexp, quint32 &value)
657 return regexp_parse_uint32(regexp, &value, 1);
660 bool MUtils::regexp_parse_uint32(const QRegExp &regexp, quint32 *values, const size_t &count)
662 const QStringList caps = regexp.capturedTexts();
664 if(caps.isEmpty() || (quint32(caps.count()) <= count))
666 return false;
669 for(size_t i = 0; i < count; i++)
671 bool ok = false;
672 values[i] = caps[i+1].toUInt(&ok);
673 if(!ok)
675 return false;
679 return true;
682 ///////////////////////////////////////////////////////////////////////////////
683 // AVAILABLE CODEPAGES
684 ///////////////////////////////////////////////////////////////////////////////
686 QStringList MUtils::available_codepages(const bool &noAliases)
688 QStringList codecList;
689 QList<QByteArray> availableCodecs = QTextCodec::availableCodecs();
691 while(!availableCodecs.isEmpty())
693 const QByteArray current = availableCodecs.takeFirst();
694 if(!current.toLower().startsWith("system"))
696 codecList << QString::fromLatin1(current.constData(), current.size());
697 if(noAliases)
699 if(QTextCodec *const currentCodec = QTextCodec::codecForName(current.constData()))
701 const QList<QByteArray> aliases = currentCodec->aliases();
702 for(QList<QByteArray>::ConstIterator iter = aliases.constBegin(); iter != aliases.constEnd(); iter++)
704 availableCodecs.removeAll(*iter);
711 return codecList;
714 ///////////////////////////////////////////////////////////////////////////////
715 // SELF-TEST
716 ///////////////////////////////////////////////////////////////////////////////
718 int MUtils::Internal::selfTest(const char *const buildKey, const bool debug)
720 static const bool MY_DEBUG_FLAG = MUTILS_DEBUG;
721 static const char *const MY_BUILD_KEY = __DATE__ "@" __TIME__;
723 if(strncmp(buildKey, MY_BUILD_KEY, 13) || (MY_DEBUG_FLAG != debug))
725 MUtils::OS::system_message_err(L"MUtils", L"FATAL ERROR: MUtils library version mismatch detected!");
726 MUtils::OS::system_message_wrn(L"MUtils", L"Perform a clean(!) re-install of the application to fix the problem!");
727 abort();
729 return 0;