Fixed is_library_file() for older Windows versions.
[MUtilities.git] / src / Global.cpp
blobb5cf83e9a4fd8c3aadf119b4c9befc8722ad9b9c
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2016 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 // GET TEMP FILE NAME
114 ///////////////////////////////////////////////////////////////////////////////
116 QString MUtils::make_temp_file(const QString &basePath, const QString &extension, const bool placeholder)
118 for(int i = 0; i < 4096; i++)
120 const QString tempFileName = QString("%1/%2.%3").arg(basePath, rand_str(), extension);
121 if(!QFileInfo(tempFileName).exists())
123 if(placeholder)
125 QFile file(tempFileName);
126 if(file.open(QFile::ReadWrite))
128 file.close();
129 return tempFileName;
132 else
134 return tempFileName;
139 qWarning("Failed to generate unique temp file name!");
140 return QString();
143 ///////////////////////////////////////////////////////////////////////////////
144 // COMPUTE PARITY
145 ///////////////////////////////////////////////////////////////////////////////
148 * Compute parity in parallel
149 * http://www.graphics.stanford.edu/~seander/bithacks.html#ParityParallel
151 bool MUtils::parity(quint32 value)
153 value ^= value >> 16;
154 value ^= value >> 8;
155 value ^= value >> 4;
156 value &= 0xf;
157 return ((0x6996 >> value) & 1) != 0;
160 ///////////////////////////////////////////////////////////////////////////////
161 // TEMP FOLDER
162 ///////////////////////////////////////////////////////////////////////////////
164 static QScopedPointer<MUtils::Internal::DirLock> g_temp_folder_file;
165 static QReadWriteLock g_temp_folder_lock;
167 static QString try_create_subfolder(const QString &baseDir, const QString &postfix)
169 const QString baseDirPath = QDir(baseDir).absolutePath();
170 for(int i = 0; i < 32; i++)
172 QDir directory(baseDirPath);
173 if(directory.mkpath(postfix) && directory.cd(postfix))
175 return directory.canonicalPath();
178 return QString();
181 static MUtils::Internal::DirLock *try_init_temp_folder(const QString &baseDir)
183 const QString tempPath = try_create_subfolder(baseDir, MUtils::rand_str());
184 if(!tempPath.isEmpty())
186 for(int i = 0; i < 32; i++)
188 MUtils::Internal::DirLock *lockFile = NULL;
191 lockFile = new MUtils::Internal::DirLock(tempPath);
192 return lockFile;
194 catch(MUtils::Internal::DirLockException&)
196 /*ignore error and try again*/
200 return NULL;
203 static bool temp_folder_cleanup_helper(const QString &tempPath)
205 size_t delay = 1;
206 static const size_t MAX_DELAY = 8192;
207 forever
209 QDir::setCurrent(QDir::rootPath());
210 if(MUtils::remove_directory(tempPath, true))
212 return true;
214 else
216 if(delay > MAX_DELAY)
218 return false;
220 MUtils::OS::sleep_ms(delay);
221 delay *= 2;
226 static void temp_folder_cleaup(void)
228 QWriteLocker writeLock(&g_temp_folder_lock);
230 //Clean the directory
231 while(!g_temp_folder_file.isNull())
233 const QString tempPath = g_temp_folder_file->getPath();
234 g_temp_folder_file.reset(NULL);
235 if(!temp_folder_cleanup_helper(tempPath))
237 MUtils::OS::system_message_wrn(L"Temp Cleaner", L"Warning: Not all temporary files could be removed!");
242 const QString &MUtils::temp_folder(void)
244 QReadLocker readLock(&g_temp_folder_lock);
246 //Already initialized?
247 if(!g_temp_folder_file.isNull())
249 return g_temp_folder_file->getPath();
252 //Obtain the write lock to initilaize
253 readLock.unlock();
254 QWriteLocker writeLock(&g_temp_folder_lock);
256 //Still uninitilaized?
257 if(!g_temp_folder_file.isNull())
259 return g_temp_folder_file->getPath();
262 //Try the %TMP% or %TEMP% directory first
263 if(MUtils::Internal::DirLock *lockFile = try_init_temp_folder(QDir::tempPath()))
265 g_temp_folder_file.reset(lockFile);
266 atexit(temp_folder_cleaup);
267 return lockFile->getPath();
270 qWarning("%%TEMP%% directory not found -> trying fallback mode now!");
271 static const OS::known_folder_t FOLDER_ID[2] = { OS::FOLDER_LOCALAPPDATA, OS::FOLDER_SYSTROOT_DIR };
272 for(size_t id = 0; id < 2; id++)
274 const QString &knownFolder = OS::known_folder(FOLDER_ID[id]);
275 if(!knownFolder.isEmpty())
277 const QString tempRoot = try_create_subfolder(knownFolder, QLatin1String("TEMP"));
278 if(!tempRoot.isEmpty())
280 if(MUtils::Internal::DirLock *lockFile = try_init_temp_folder(tempRoot))
282 g_temp_folder_file.reset(lockFile);
283 atexit(temp_folder_cleaup);
284 return lockFile->getPath();
290 qFatal("Temporary directory could not be initialized !!!");
291 return (*((QString*)NULL));
294 ///////////////////////////////////////////////////////////////////////////////
295 // REMOVE DIRECTORY / FILE
296 ///////////////////////////////////////////////////////////////////////////////
298 static const QFile::Permissions FILE_PERMISSIONS_NONE = QFile::ReadOther | QFile::WriteOther;
300 bool MUtils::remove_file(const QString &fileName)
302 QFileInfo fileInfo(fileName);
303 if(!(fileInfo.exists() && fileInfo.isFile()))
305 return true;
308 for(int i = 0; i < 32; i++)
310 QFile file(fileName);
311 file.setPermissions(FILE_PERMISSIONS_NONE);
312 if((!(fileInfo.exists() && fileInfo.isFile())) || file.remove())
314 return true;
316 fileInfo.refresh();
319 qWarning("Could not delete \"%s\"", MUTILS_UTF8(fileName));
320 return false;
323 static bool remove_directory_helper(const QDir &folder)
325 if(!folder.exists())
327 return true;
329 const QString dirName = folder.dirName();
330 if(!dirName.isEmpty())
332 QDir parent(folder);
333 if(parent.cdUp())
335 QFile::setPermissions(folder.absolutePath(), FILE_PERMISSIONS_NONE);
336 if(parent.rmdir(dirName))
338 return true;
342 return false;
345 bool MUtils::remove_directory(const QString &folderPath, const bool &recursive)
347 QDir folder(folderPath);
348 if(!folder.exists())
350 return true;
353 if(recursive)
355 const QFileInfoList entryList = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
356 for(QFileInfoList::ConstIterator iter = entryList.constBegin(); iter != entryList.constEnd(); iter++)
358 if(iter->isDir())
360 remove_directory(iter->canonicalFilePath(), true);
362 else if(iter->isFile())
364 remove_file(iter->canonicalFilePath());
369 for(int i = 0; i < 32; i++)
371 if(remove_directory_helper(folder))
373 return true;
375 folder.refresh();
378 qWarning("Could not rmdir \"%s\"", MUTILS_UTF8(folderPath));
379 return false;
382 ///////////////////////////////////////////////////////////////////////////////
383 // PROCESS UTILS
384 ///////////////////////////////////////////////////////////////////////////////
386 void MUtils::init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir)
388 //Environment variable names
389 static const char *const s_envvar_names_temp[] =
391 "TEMP", "TMP", "TMPDIR", "HOME", "USERPROFILE", "HOMEPATH", NULL
393 static const char *const s_envvar_names_remove[] =
395 "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
398 //Initialize environment
399 QProcessEnvironment env = process.processEnvironment();
400 if(env.isEmpty()) env = QProcessEnvironment::systemEnvironment();
402 //Clean a number of enviroment variables that might affect our tools
403 for(size_t i = 0; s_envvar_names_remove[i]; i++)
405 env.remove(QString::fromLatin1(s_envvar_names_remove[i]));
406 env.remove(QString::fromLatin1(s_envvar_names_remove[i]).toLower());
409 const QString tempDir = QDir::toNativeSeparators(temp_folder());
411 //Replace TEMP directory in environment
412 if(bReplaceTempDir)
414 for(size_t i = 0; s_envvar_names_temp[i]; i++)
416 env.insert(s_envvar_names_temp[i], tempDir);
420 //Setup PATH variable
421 const QString path = env.value("PATH", QString()).trimmed();
422 env.insert("PATH", path.isEmpty() ? tempDir : QString("%1;%2").arg(tempDir, path));
424 //Setup QPorcess object
425 process.setWorkingDirectory(wokringDir);
426 process.setProcessChannelMode(QProcess::MergedChannels);
427 process.setReadChannel(QProcess::StandardOutput);
428 process.setProcessEnvironment(env);
431 ///////////////////////////////////////////////////////////////////////////////
432 // NATURAL ORDER STRING COMPARISON
433 ///////////////////////////////////////////////////////////////////////////////
435 static bool natural_string_sort_helper(const QString &str1, const QString &str2)
437 return (MUtils::Internal::NaturalSort::strnatcmp(MUTILS_WCHR(str1), MUTILS_WCHR(str2)) < 0);
440 static bool natural_string_sort_helper_fold_case(const QString &str1, const QString &str2)
442 return (MUtils::Internal::NaturalSort::strnatcasecmp(MUTILS_WCHR(str1), MUTILS_WCHR(str2)) < 0);
445 void MUtils::natural_string_sort(QStringList &list, const bool bIgnoreCase)
447 qSort(list.begin(), list.end(), bIgnoreCase ? natural_string_sort_helper_fold_case : natural_string_sort_helper);
450 ///////////////////////////////////////////////////////////////////////////////
451 // CLEAN FILE PATH
452 ///////////////////////////////////////////////////////////////////////////////
454 static const struct
456 const char *const search;
457 const char *const replace;
459 CLEAN_FILE_NAME[] =
461 { "\\", "-" },
462 { " / ", ", " },
463 { "/", "," },
464 { ":", "-" },
465 { "*", "x" },
466 { "?", "!" },
467 { "<", "[" },
468 { ">", "]" },
469 { "|", "!" },
470 { "\"", "'" },
471 { NULL, NULL }
474 QString MUtils::clean_file_name(const QString &name)
476 QRegExp regExp("\"(.+)\"");
477 regExp.setMinimal(true);
479 QString str = QString(name).replace(regExp, "``\\1ยดยด").trimmed();
480 for(size_t i = 0; CLEAN_FILE_NAME[i].search; i++)
482 str.replace(CLEAN_FILE_NAME[i].search, CLEAN_FILE_NAME[i].replace);
485 while(str.endsWith(QLatin1Char('.')))
487 str.chop(1);
488 str = str.trimmed();
491 return str.trimmed();
494 QString MUtils::clean_file_path(const QString &path)
496 const bool root = path.startsWith(QLatin1Char('/')) || path.startsWith(QLatin1Char('\\'));
497 QStringList parts = QDir::fromNativeSeparators(path.trimmed()).split(QLatin1Char('/'), QString::SkipEmptyParts);
499 for(int i = 0; i < parts.count(); i++)
501 if((i == 0) && (!root) && (parts[i].length() == 2) && parts[i][0].isLetter() && (parts[i][1] == QLatin1Char(':')))
503 continue; //handle case "c:\"
505 parts[i] = MUtils::clean_file_name(parts[i]);
508 return root ? parts.join(QLatin1String("/")).prepend(QLatin1Char('/')) : parts.join(QLatin1String("/"));
511 ///////////////////////////////////////////////////////////////////////////////
512 // REGULAR EXPESSION HELPER
513 ///////////////////////////////////////////////////////////////////////////////
515 bool MUtils::regexp_parse_uint32(const QRegExp &regexp, quint32 &value)
517 return regexp_parse_uint32(regexp, &value, 1);
520 bool MUtils::regexp_parse_uint32(const QRegExp &regexp, quint32 *values, const size_t &count)
522 const QStringList caps = regexp.capturedTexts();
524 if(caps.isEmpty() || (quint32(caps.count()) <= count))
526 return false;
529 for(size_t i = 0; i < count; i++)
531 bool ok = false;
532 values[i] = caps[i+1].toUInt(&ok);
533 if(!ok)
535 return false;
539 return true;
542 ///////////////////////////////////////////////////////////////////////////////
543 // AVAILABLE CODEPAGES
544 ///////////////////////////////////////////////////////////////////////////////
546 QStringList MUtils::available_codepages(const bool &noAliases)
548 QStringList codecList;
549 QList<QByteArray> availableCodecs = QTextCodec::availableCodecs();
551 while(!availableCodecs.isEmpty())
553 const QByteArray current = availableCodecs.takeFirst();
554 if(!current.toLower().startsWith("system"))
556 codecList << QString::fromLatin1(current.constData(), current.size());
557 if(noAliases)
559 if(QTextCodec *const currentCodec = QTextCodec::codecForName(current.constData()))
561 const QList<QByteArray> aliases = currentCodec->aliases();
562 for(QList<QByteArray>::ConstIterator iter = aliases.constBegin(); iter != aliases.constEnd(); iter++)
564 availableCodecs.removeAll(*iter);
571 return codecList;
574 ///////////////////////////////////////////////////////////////////////////////
575 // SELF-TEST
576 ///////////////////////////////////////////////////////////////////////////////
578 int MUtils::Internal::selfTest(const char *const buildKey, const bool debug)
580 static const bool MY_DEBUG_FLAG = MUTILS_DEBUG;
581 static const char *const MY_BUILD_KEY = __DATE__ "@" __TIME__;
583 if(strncmp(buildKey, MY_BUILD_KEY, 13) || (MY_DEBUG_FLAG != debug))
585 MUtils::OS::system_message_err(L"MUtils", L"FATAL ERROR: MUtils library version mismatch detected!");
586 MUtils::OS::system_message_wrn(L"MUtils", L"Perform a clean(!) re-install of the application to fix the problem!");
587 abort();
589 return 0;