Added function to compute parity.
[MUtilities.git] / src / Global.cpp
blob42d2189c6e005126f64f11e0c45f58a80818ce64
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2015 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>
30 //Internal
31 #include "DirLocker.h"
32 #include "3rd_party/strnatcmp/include/strnatcmp.h"
34 //Qt
35 #include <QDir>
36 #include <QReadWriteLock>
37 #include <QProcess>
38 #include <QTextCodec>
40 //CRT
41 #include <cstdlib>
42 #include <ctime>
43 #include <process.h>
45 //VLD
46 #ifdef _MSC_VER
47 #include <vld.h>
48 #endif
50 ///////////////////////////////////////////////////////////////////////////////
51 // Random Support
52 ///////////////////////////////////////////////////////////////////////////////
54 //Robert Jenkins' 96 bit Mix Function
55 static unsigned int mix_function(const unsigned int x, const unsigned int y, const unsigned int z)
57 unsigned int a = x;
58 unsigned int b = y;
59 unsigned int c = z;
61 a=a-b; a=a-c; a=a^(c >> 13);
62 b=b-c; b=b-a; b=b^(a << 8 );
63 c=c-a; c=c-b; c=c^(b >> 13);
64 a=a-b; a=a-c; a=a^(c >> 12);
65 b=b-c; b=b-a; b=b^(a << 16);
66 c=c-a; c=c-b; c=c^(b >> 5 );
67 a=a-b; a=a-c; a=a^(c >> 3 );
68 b=b-c; b=b-a; b=b^(a << 10);
69 c=c-a; c=c-b; c=c^(b >> 15);
71 return a ^ b ^ c;
74 void MUtils::seed_rand(void)
76 qsrand(mix_function(clock(), time(NULL), _getpid()));
79 quint32 MUtils::next_rand32(void)
81 quint32 rnd = 0xDEADBEEF;
83 #ifdef _CRT_RAND_S
84 if(rand_s(&rnd) == 0)
86 return rnd;
88 #endif //_CRT_RAND_S
90 for(size_t i = 0; i < sizeof(quint32); i++)
92 rnd = (rnd << 8) ^ qrand();
95 return rnd;
98 quint64 MUtils::next_rand64(void)
100 return (quint64(next_rand32()) << 32) | quint64(next_rand32());
103 QString MUtils::rand_str(const bool &bLong)
105 if(!bLong)
107 return QString::number(next_rand64(), 16).rightJustified(16, QLatin1Char('0'));
109 return QString("%1%2").arg(rand_str(false), rand_str(false));
112 ///////////////////////////////////////////////////////////////////////////////
113 // COMPUTE PARITY
114 ///////////////////////////////////////////////////////////////////////////////
117 * Compute parity in parallel
118 * http://www.graphics.stanford.edu/~seander/bithacks.html#ParityParallel
120 bool MUtils::parity(quint32 value)
122 value ^= value >> 16;
123 value ^= value >> 8;
124 value ^= value >> 4;
125 value &= 0xf;
126 return ((0x6996 >> value) & 1) != 0;
129 ///////////////////////////////////////////////////////////////////////////////
130 // TEMP FOLDER
131 ///////////////////////////////////////////////////////////////////////////////
133 static QScopedPointer<MUtils::Internal::DirLock> g_temp_folder_file;
134 static QReadWriteLock g_temp_folder_lock;
136 static QString try_create_subfolder(const QString &baseDir, const QString &postfix)
138 const QString baseDirPath = QDir(baseDir).absolutePath();
139 for(int i = 0; i < 32; i++)
141 QDir directory(baseDirPath);
142 if(directory.mkpath(postfix) && directory.cd(postfix))
144 return directory.canonicalPath();
147 return QString();
150 static MUtils::Internal::DirLock *try_init_temp_folder(const QString &baseDir)
152 const QString tempPath = try_create_subfolder(baseDir, MUtils::rand_str());
153 if(!tempPath.isEmpty())
155 for(int i = 0; i < 32; i++)
157 MUtils::Internal::DirLock *lockFile = NULL;
160 lockFile = new MUtils::Internal::DirLock(tempPath);
161 return lockFile;
163 catch(MUtils::Internal::DirLockException&)
165 /*ignore error and try again*/
169 return NULL;
172 static bool temp_folder_cleanup_helper(const QString &tempPath)
174 size_t delay = 1;
175 static const size_t MAX_DELAY = 8192;
176 forever
178 QDir::setCurrent(QDir::rootPath());
179 if(MUtils::remove_directory(tempPath, true))
181 return true;
183 else
185 if(delay > MAX_DELAY)
187 return false;
189 MUtils::OS::sleep_ms(delay);
190 delay *= 2;
195 static void temp_folder_cleaup(void)
197 QWriteLocker writeLock(&g_temp_folder_lock);
199 //Clean the directory
200 while(!g_temp_folder_file.isNull())
202 const QString tempPath = g_temp_folder_file->getPath();
203 g_temp_folder_file.reset(NULL);
204 if(!temp_folder_cleanup_helper(tempPath))
206 MUtils::OS::system_message_wrn(L"Temp Cleaner", L"Warning: Not all temporary files could be removed!");
211 const QString &MUtils::temp_folder(void)
213 QReadLocker readLock(&g_temp_folder_lock);
215 //Already initialized?
216 if(!g_temp_folder_file.isNull())
218 return g_temp_folder_file->getPath();
221 //Obtain the write lock to initilaize
222 readLock.unlock();
223 QWriteLocker writeLock(&g_temp_folder_lock);
225 //Still uninitilaized?
226 if(!g_temp_folder_file.isNull())
228 return g_temp_folder_file->getPath();
231 //Try the %TMP% or %TEMP% directory first
232 if(MUtils::Internal::DirLock *lockFile = try_init_temp_folder(QDir::tempPath()))
234 g_temp_folder_file.reset(lockFile);
235 atexit(temp_folder_cleaup);
236 return lockFile->getPath();
239 qWarning("%%TEMP%% directory not found -> trying fallback mode now!");
240 static const OS::known_folder_t FOLDER_ID[2] = { OS::FOLDER_LOCALAPPDATA, OS::FOLDER_SYSTROOT_DIR };
241 for(size_t id = 0; id < 2; id++)
243 const QString &knownFolder = OS::known_folder(FOLDER_ID[id]);
244 if(!knownFolder.isEmpty())
246 const QString tempRoot = try_create_subfolder(knownFolder, QLatin1String("TEMP"));
247 if(!tempRoot.isEmpty())
249 if(MUtils::Internal::DirLock *lockFile = try_init_temp_folder(tempRoot))
251 g_temp_folder_file.reset(lockFile);
252 atexit(temp_folder_cleaup);
253 return lockFile->getPath();
259 qFatal("Temporary directory could not be initialized !!!");
260 return (*((QString*)NULL));
263 ///////////////////////////////////////////////////////////////////////////////
264 // REMOVE DIRECTORY / FILE
265 ///////////////////////////////////////////////////////////////////////////////
267 static const QFile::Permissions FILE_PERMISSIONS_NONE = QFile::ReadOther | QFile::WriteOther;
269 bool MUtils::remove_file(const QString &fileName)
271 QFileInfo fileInfo(fileName);
272 if(!(fileInfo.exists() && fileInfo.isFile()))
274 return true;
277 for(int i = 0; i < 32; i++)
279 QFile file(fileName);
280 file.setPermissions(FILE_PERMISSIONS_NONE);
281 if((!(fileInfo.exists() && fileInfo.isFile())) || file.remove())
283 return true;
285 fileInfo.refresh();
288 qWarning("Could not delete \"%s\"", MUTILS_UTF8(fileName));
289 return false;
292 static bool remove_directory_helper(const QDir &folder)
294 if(!folder.exists())
296 return true;
298 const QString dirName = folder.dirName();
299 if(!dirName.isEmpty())
301 QDir parent(folder);
302 if(parent.cdUp())
304 QFile::setPermissions(folder.absolutePath(), FILE_PERMISSIONS_NONE);
305 if(parent.rmdir(dirName))
307 return true;
311 return false;
314 bool MUtils::remove_directory(const QString &folderPath, const bool &recursive)
316 QDir folder(folderPath);
317 if(!folder.exists())
319 return true;
322 if(recursive)
324 const QFileInfoList entryList = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
325 for(QFileInfoList::ConstIterator iter = entryList.constBegin(); iter != entryList.constEnd(); iter++)
327 if(iter->isDir())
329 remove_directory(iter->canonicalFilePath(), true);
331 else if(iter->isFile())
333 remove_file(iter->canonicalFilePath());
338 for(int i = 0; i < 32; i++)
340 if(remove_directory_helper(folder))
342 return true;
344 folder.refresh();
347 qWarning("Could not rmdir \"%s\"", MUTILS_UTF8(folderPath));
348 return false;
351 ///////////////////////////////////////////////////////////////////////////////
352 // PROCESS UTILS
353 ///////////////////////////////////////////////////////////////////////////////
355 void MUtils::init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir)
357 //Environment variable names
358 static const char *const s_envvar_names_temp[] =
360 "TEMP", "TMP", "TMPDIR", "HOME", "USERPROFILE", "HOMEPATH", NULL
362 static const char *const s_envvar_names_remove[] =
364 "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
367 //Initialize environment
368 QProcessEnvironment env = process.processEnvironment();
369 if(env.isEmpty()) env = QProcessEnvironment::systemEnvironment();
371 //Clean a number of enviroment variables that might affect our tools
372 for(size_t i = 0; s_envvar_names_remove[i]; i++)
374 env.remove(QString::fromLatin1(s_envvar_names_remove[i]));
375 env.remove(QString::fromLatin1(s_envvar_names_remove[i]).toLower());
378 const QString tempDir = QDir::toNativeSeparators(temp_folder());
380 //Replace TEMP directory in environment
381 if(bReplaceTempDir)
383 for(size_t i = 0; s_envvar_names_temp[i]; i++)
385 env.insert(s_envvar_names_temp[i], tempDir);
389 //Setup PATH variable
390 const QString path = env.value("PATH", QString()).trimmed();
391 env.insert("PATH", path.isEmpty() ? tempDir : QString("%1;%2").arg(tempDir, path));
393 //Setup QPorcess object
394 process.setWorkingDirectory(wokringDir);
395 process.setProcessChannelMode(QProcess::MergedChannels);
396 process.setReadChannel(QProcess::StandardOutput);
397 process.setProcessEnvironment(env);
400 ///////////////////////////////////////////////////////////////////////////////
401 // NATURAL ORDER STRING COMPARISON
402 ///////////////////////////////////////////////////////////////////////////////
404 static bool natural_string_sort_helper(const QString &str1, const QString &str2)
406 return (MUtils::Internal::NaturalSort::strnatcmp(MUTILS_WCHR(str1), MUTILS_WCHR(str2)) < 0);
409 static bool natural_string_sort_helper_fold_case(const QString &str1, const QString &str2)
411 return (MUtils::Internal::NaturalSort::strnatcasecmp(MUTILS_WCHR(str1), MUTILS_WCHR(str2)) < 0);
414 void MUtils::natural_string_sort(QStringList &list, const bool bIgnoreCase)
416 qSort(list.begin(), list.end(), bIgnoreCase ? natural_string_sort_helper_fold_case : natural_string_sort_helper);
419 ///////////////////////////////////////////////////////////////////////////////
420 // CLEAN FILE PATH
421 ///////////////////////////////////////////////////////////////////////////////
423 static const struct
425 const char *const search;
426 const char *const replace;
428 CLEAN_FILE_NAME[] =
430 { "\\", "-" },
431 { " / ", ", " },
432 { "/", "," },
433 { ":", "-" },
434 { "*", "x" },
435 { "?", "!" },
436 { "<", "[" },
437 { ">", "]" },
438 { "|", "!" },
439 { "\"", "'" },
440 { NULL, NULL }
443 QString MUtils::clean_file_name(const QString &name)
445 QString str = name.simplified();
447 for(size_t i = 0; CLEAN_FILE_NAME[i].search; i++)
449 str.replace(CLEAN_FILE_NAME[i].search, CLEAN_FILE_NAME[i].replace);
452 QRegExp regExp("\"(.+)\"");
453 regExp.setMinimal(true);
454 str.replace(regExp, "`\\1ยด");
456 return str.simplified();
459 QString MUtils::clean_file_path(const QString &path)
461 QStringList parts = path.simplified().replace("\\", "/").split("/", QString::SkipEmptyParts);
463 for(int i = 0; i < parts.count(); i++)
465 parts[i] = MUtils::clean_file_name(parts[i]);
468 return parts.join("/");
471 ///////////////////////////////////////////////////////////////////////////////
472 // REGULAR EXPESSION HELPER
473 ///////////////////////////////////////////////////////////////////////////////
475 bool MUtils::regexp_parse_uint32(const QRegExp &regexp, quint32 &value)
477 return regexp_parse_uint32(regexp, &value, 1);
480 bool MUtils::regexp_parse_uint32(const QRegExp &regexp, quint32 *values, const size_t &count)
482 const QStringList caps = regexp.capturedTexts();
484 if(caps.isEmpty() || (quint32(caps.count()) <= count))
486 return false;
489 for(size_t i = 0; i < count; i++)
491 bool ok = false;
492 values[i] = caps[i+1].toUInt(&ok);
493 if(!ok)
495 return false;
499 return true;
502 ///////////////////////////////////////////////////////////////////////////////
503 // AVAILABLE CODEPAGES
504 ///////////////////////////////////////////////////////////////////////////////
506 QStringList MUtils::available_codepages(const bool &noAliases)
508 QStringList codecList;
509 QList<QByteArray> availableCodecs = QTextCodec::availableCodecs();
511 while(!availableCodecs.isEmpty())
513 const QByteArray current = availableCodecs.takeFirst();
514 if(!current.toLower().startsWith("system"))
516 codecList << QString::fromLatin1(current.constData(), current.size());
517 if(noAliases)
519 if(QTextCodec *const currentCodec = QTextCodec::codecForName(current.constData()))
521 const QList<QByteArray> aliases = currentCodec->aliases();
522 for(QList<QByteArray>::ConstIterator iter = aliases.constBegin(); iter != aliases.constEnd(); iter++)
524 availableCodecs.removeAll(*iter);
531 return codecList;
534 ///////////////////////////////////////////////////////////////////////////////
535 // SELF-TEST
536 ///////////////////////////////////////////////////////////////////////////////
538 int MUtils::Internal::selfTest(const char *const buildKey, const bool debug)
540 static const bool MY_DEBUG_FLAG = MUTILS_DEBUG;
541 static const char *const MY_BUILD_KEY = __DATE__"@"__TIME__;
543 if(strncmp(buildKey, MY_BUILD_KEY, 13) || (MY_DEBUG_FLAG != debug))
545 MUtils::OS::system_message_err(L"MUtils", L"FATAL ERROR: MUtils library version mismatch detected!");
546 MUtils::OS::system_message_wrn(L"MUtils", L"Please re-build the complete solution in order to fix this issue!");
547 abort();
549 return 0;