Happy new year 2015 !!!
[MUtilities.git] / src / Global.cpp
blob8af6280741a0e20b4d524af62d7757db8feabf34
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 // TEMP FOLDER
114 ///////////////////////////////////////////////////////////////////////////////
116 static QScopedPointer<MUtils::Internal::DirLock> g_temp_folder_file;
117 static QReadWriteLock g_temp_folder_lock;
119 static QString try_create_subfolder(const QString &baseDir, const QString &postfix)
121 const QString baseDirPath = QDir(baseDir).absolutePath();
122 for(int i = 0; i < 32; i++)
124 QDir directory(baseDirPath);
125 if(directory.mkpath(postfix) && directory.cd(postfix))
127 return directory.canonicalPath();
130 return QString();
133 static MUtils::Internal::DirLock *try_init_temp_folder(const QString &baseDir)
135 const QString tempPath = try_create_subfolder(baseDir, MUtils::rand_str());
136 if(!tempPath.isEmpty())
138 for(int i = 0; i < 32; i++)
140 MUtils::Internal::DirLock *lockFile = NULL;
143 lockFile = new MUtils::Internal::DirLock(tempPath);
144 return lockFile;
146 catch(MUtils::Internal::DirLockException&)
148 /*ignore error and try again*/
152 return NULL;
155 static bool temp_folder_cleanup_helper(const QString &tempPath)
157 size_t delay = 1;
158 static const size_t MAX_DELAY = 8192;
159 forever
161 QDir::setCurrent(QDir::rootPath());
162 if(MUtils::remove_directory(tempPath, true))
164 return true;
166 else
168 if(delay > MAX_DELAY)
170 return false;
172 MUtils::OS::sleep_ms(delay);
173 delay *= 2;
178 static void temp_folder_cleaup(void)
180 QWriteLocker writeLock(&g_temp_folder_lock);
182 //Clean the directory
183 while(!g_temp_folder_file.isNull())
185 const QString tempPath = g_temp_folder_file->getPath();
186 g_temp_folder_file.reset(NULL);
187 if(!temp_folder_cleanup_helper(tempPath))
189 MUtils::OS::system_message_wrn(L"Temp Cleaner", L"Warning: Not all temporary files could be removed!");
194 const QString &MUtils::temp_folder(void)
196 QReadLocker readLock(&g_temp_folder_lock);
198 //Already initialized?
199 if(!g_temp_folder_file.isNull())
201 return g_temp_folder_file->getPath();
204 //Obtain the write lock to initilaize
205 readLock.unlock();
206 QWriteLocker writeLock(&g_temp_folder_lock);
208 //Still uninitilaized?
209 if(!g_temp_folder_file.isNull())
211 return g_temp_folder_file->getPath();
214 //Try the %TMP% or %TEMP% directory first
215 if(MUtils::Internal::DirLock *lockFile = try_init_temp_folder(QDir::tempPath()))
217 g_temp_folder_file.reset(lockFile);
218 atexit(temp_folder_cleaup);
219 return lockFile->getPath();
222 qWarning("%%TEMP%% directory not found -> trying fallback mode now!");
223 static const OS::known_folder_t FOLDER_ID[2] = { OS::FOLDER_LOCALAPPDATA, OS::FOLDER_SYSTROOT_DIR };
224 for(size_t id = 0; id < 2; id++)
226 const QString &knownFolder = OS::known_folder(FOLDER_ID[id]);
227 if(!knownFolder.isEmpty())
229 const QString tempRoot = try_create_subfolder(knownFolder, QLatin1String("TEMP"));
230 if(!tempRoot.isEmpty())
232 if(MUtils::Internal::DirLock *lockFile = try_init_temp_folder(tempRoot))
234 g_temp_folder_file.reset(lockFile);
235 atexit(temp_folder_cleaup);
236 return lockFile->getPath();
242 qFatal("Temporary directory could not be initialized !!!");
243 return (*((QString*)NULL));
246 ///////////////////////////////////////////////////////////////////////////////
247 // REMOVE DIRECTORY / FILE
248 ///////////////////////////////////////////////////////////////////////////////
250 static const QFile::Permissions FILE_PERMISSIONS_NONE = QFile::ReadOther | QFile::WriteOther;
252 bool MUtils::remove_file(const QString &fileName)
254 QFileInfo fileInfo(fileName);
255 if(!(fileInfo.exists() && fileInfo.isFile()))
257 return true;
260 for(int i = 0; i < 32; i++)
262 QFile file(fileName);
263 file.setPermissions(FILE_PERMISSIONS_NONE);
264 if((!(fileInfo.exists() && fileInfo.isFile())) || file.remove())
266 return true;
268 fileInfo.refresh();
271 qWarning("Could not delete \"%s\"", MUTILS_UTF8(fileName));
272 return false;
275 static bool remove_directory_helper(const QDir &folder)
277 if(!folder.exists())
279 return true;
281 const QString dirName = folder.dirName();
282 if(!dirName.isEmpty())
284 QDir parent(folder);
285 if(parent.cdUp())
287 QFile::setPermissions(folder.absolutePath(), FILE_PERMISSIONS_NONE);
288 if(parent.rmdir(dirName))
290 return true;
294 return false;
297 bool MUtils::remove_directory(const QString &folderPath, const bool &recursive)
299 QDir folder(folderPath);
300 if(!folder.exists())
302 return true;
305 if(recursive)
307 const QFileInfoList entryList = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
308 for(QFileInfoList::ConstIterator iter = entryList.constBegin(); iter != entryList.constEnd(); iter++)
310 if(iter->isDir())
312 remove_directory(iter->canonicalFilePath(), true);
314 else if(iter->isFile())
316 remove_file(iter->canonicalFilePath());
321 for(int i = 0; i < 32; i++)
323 if(remove_directory_helper(folder))
325 return true;
327 folder.refresh();
330 qWarning("Could not rmdir \"%s\"", MUTILS_UTF8(folderPath));
331 return false;
334 ///////////////////////////////////////////////////////////////////////////////
335 // PROCESS UTILS
336 ///////////////////////////////////////////////////////////////////////////////
338 void MUtils::init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir)
340 //Environment variable names
341 static const char *const s_envvar_names_temp[] =
343 "TEMP", "TMP", "TMPDIR", "HOME", "USERPROFILE", "HOMEPATH", NULL
345 static const char *const s_envvar_names_remove[] =
347 "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
350 //Initialize environment
351 QProcessEnvironment env = process.processEnvironment();
352 if(env.isEmpty()) env = QProcessEnvironment::systemEnvironment();
354 //Clean a number of enviroment variables that might affect our tools
355 for(size_t i = 0; s_envvar_names_remove[i]; i++)
357 env.remove(QString::fromLatin1(s_envvar_names_remove[i]));
358 env.remove(QString::fromLatin1(s_envvar_names_remove[i]).toLower());
361 const QString tempDir = QDir::toNativeSeparators(temp_folder());
363 //Replace TEMP directory in environment
364 if(bReplaceTempDir)
366 for(size_t i = 0; s_envvar_names_temp[i]; i++)
368 env.insert(s_envvar_names_temp[i], tempDir);
372 //Setup PATH variable
373 const QString path = env.value("PATH", QString()).trimmed();
374 env.insert("PATH", path.isEmpty() ? tempDir : QString("%1;%2").arg(tempDir, path));
376 //Setup QPorcess object
377 process.setWorkingDirectory(wokringDir);
378 process.setProcessChannelMode(QProcess::MergedChannels);
379 process.setReadChannel(QProcess::StandardOutput);
380 process.setProcessEnvironment(env);
383 ///////////////////////////////////////////////////////////////////////////////
384 // NATURAL ORDER STRING COMPARISON
385 ///////////////////////////////////////////////////////////////////////////////
387 static bool natural_string_sort_helper(const QString &str1, const QString &str2)
389 return (MUtils::Internal::NaturalSort::strnatcmp(MUTILS_WCHR(str1), MUTILS_WCHR(str2)) < 0);
392 static bool natural_string_sort_helper_fold_case(const QString &str1, const QString &str2)
394 return (MUtils::Internal::NaturalSort::strnatcasecmp(MUTILS_WCHR(str1), MUTILS_WCHR(str2)) < 0);
397 void MUtils::natural_string_sort(QStringList &list, const bool bIgnoreCase)
399 qSort(list.begin(), list.end(), bIgnoreCase ? natural_string_sort_helper_fold_case : natural_string_sort_helper);
402 ///////////////////////////////////////////////////////////////////////////////
403 // CLEAN FILE PATH
404 ///////////////////////////////////////////////////////////////////////////////
406 static const struct
408 const char *const search;
409 const char *const replace;
411 CLEAN_FILE_NAME[] =
413 { "\\", "-" },
414 { " / ", ", " },
415 { "/", "," },
416 { ":", "-" },
417 { "*", "x" },
418 { "?", "!" },
419 { "<", "[" },
420 { ">", "]" },
421 { "|", "!" },
422 { "\"", "'" },
423 { NULL, NULL }
426 QString MUtils::clean_file_name(const QString &name)
428 QString str = name.simplified();
430 for(size_t i = 0; CLEAN_FILE_NAME[i].search; i++)
432 str.replace(CLEAN_FILE_NAME[i].search, CLEAN_FILE_NAME[i].replace);
435 QRegExp regExp("\"(.+)\"");
436 regExp.setMinimal(true);
437 str.replace(regExp, "`\\1ยด");
439 return str.simplified();
442 QString MUtils::clean_file_path(const QString &path)
444 QStringList parts = path.simplified().replace("\\", "/").split("/", QString::SkipEmptyParts);
446 for(int i = 0; i < parts.count(); i++)
448 parts[i] = MUtils::clean_file_name(parts[i]);
451 return parts.join("/");
454 ///////////////////////////////////////////////////////////////////////////////
455 // REGULAR EXPESSION HELPER
456 ///////////////////////////////////////////////////////////////////////////////
458 bool MUtils::regexp_parse_uint32(const QRegExp &regexp, quint32 &value)
460 return regexp_parse_uint32(regexp, &value, 1);
463 bool MUtils::regexp_parse_uint32(const QRegExp &regexp, quint32 *values, const size_t &count)
465 const QStringList caps = regexp.capturedTexts();
467 if(caps.isEmpty() || (quint32(caps.count()) <= count))
469 return false;
472 for(size_t i = 0; i < count; i++)
474 bool ok = false;
475 values[i] = caps[i+1].toUInt(&ok);
476 if(!ok)
478 return false;
482 return true;
485 ///////////////////////////////////////////////////////////////////////////////
486 // AVAILABLE CODEPAGES
487 ///////////////////////////////////////////////////////////////////////////////
489 QStringList MUtils::available_codepages(const bool &noAliases)
491 QStringList codecList;
492 QList<QByteArray> availableCodecs = QTextCodec::availableCodecs();
494 while(!availableCodecs.isEmpty())
496 const QByteArray current = availableCodecs.takeFirst();
497 if(!current.toLower().startsWith("system"))
499 codecList << QString::fromLatin1(current.constData(), current.size());
500 if(noAliases)
502 if(QTextCodec *const currentCodec = QTextCodec::codecForName(current.constData()))
504 const QList<QByteArray> aliases = currentCodec->aliases();
505 for(QList<QByteArray>::ConstIterator iter = aliases.constBegin(); iter != aliases.constEnd(); iter++)
507 availableCodecs.removeAll(*iter);
514 return codecList;
517 ///////////////////////////////////////////////////////////////////////////////
518 // SELF-TEST
519 ///////////////////////////////////////////////////////////////////////////////
521 int MUtils::Internal::selfTest(const char *const buildKey, const bool debug)
523 static const bool MY_DEBUG_FLAG = MUTILS_DEBUG;
524 static const char *const MY_BUILD_KEY = __DATE__"@"__TIME__;
526 if(strncmp(buildKey, MY_BUILD_KEY, 14) || (MY_DEBUG_FLAG != debug))
528 MUtils::OS::system_message_err(L"MUtils", L"FATAL ERROR: MUtils library version mismatch detected!");
529 MUtils::OS::system_message_wrn(L"MUtils", L"Please re-build the complete solution in order to fix this issue!");
530 abort();
532 return 0;