OSDN Git Service

Some improvements to connectivity check: Start with small timeout and increase the...
[mutilities/MUtilities.git] / src / Global.cpp
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.
14 //
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
18 //
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
21
22 #if _MSC_VER
23 #define _CRT_RAND_S 1
24 #endif
25
26 //MUtils
27 #include <MUtils/Global.h>
28 #include <MUtils/OSSupport.h>
29 #include <MUtils/Version.h>
30
31 //Internal
32 #include "DirLocker.h"
33 #include "3rd_party/strnatcmp/include/strnatcmp.h"
34
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>
44
45 //CRT
46 #include <cstdlib>
47 #include <ctime>
48 #include <process.h>
49
50 //VLD
51 #ifdef _MSC_VER
52 #include <vld.h>
53 #endif
54
55 ///////////////////////////////////////////////////////////////////////////////
56 // Random Support
57 ///////////////////////////////////////////////////////////////////////////////
58
59 #ifndef _CRT_RAND_S
60 #define rand_s(X) (true)
61 #endif
62
63 //Per-thread init flag
64 static QThreadStorage<bool> g_srand_flag;
65
66 //Robert Jenkins' 96 bit Mix Function
67 static quint32 mix_function(const quint32 x, const quint32 y, const quint32 z)
68 {
69         quint32 a = x;
70         quint32 b = y;
71         quint32 c = z;
72         
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);
82
83         return a ^ b ^ c;
84 }
85
86 static void seed_rand(void)
87 {
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));
91 }
92
93 static quint32 rand_fallback(void)
94 {
95         Q_ASSERT(RAND_MAX >= 0xFFF);
96         if (!(g_srand_flag.hasLocalData() && g_srand_flag.localData()))
97         {
98                 seed_rand();
99                 g_srand_flag.setLocalData(true);
100         }
101         quint32 rnd = 0x32288EA3;
102         for (size_t i = 0; i < 3; i++)
103         {
104                 rnd = (rnd << 12) ^ qrand();
105         }
106         return rnd;
107 }
108
109 quint32 MUtils::next_rand_u32(void)
110 {
111         quint32 rnd;
112         if (rand_s(&rnd))
113         {
114                 return rand_fallback();
115         }
116         return rnd;
117 }
118
119 quint64 MUtils::next_rand_u64(void)
120 {
121         return (quint64(next_rand_u32()) << 32) | quint64(next_rand_u32());
122 }
123
124 QString MUtils::next_rand_str(const bool &bLong)
125 {
126         if (bLong)
127         {
128                 return next_rand_str(false) + next_rand_str(false);
129         }
130         else
131         {
132                 return QString::number(next_rand_u64(), 16).rightJustified(16, QLatin1Char('0'));
133         }
134 }
135
136 ///////////////////////////////////////////////////////////////////////////////
137 // STRING UTILITY FUNCTIONS
138 ///////////////////////////////////////////////////////////////////////////////
139
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;
143
144 static QString& trim_helper(QString &str, QScopedPointer<QRegExp> &regex, const char *const pattern)
145 {
146         QMutexLocker lock(&g_str_trim_lock);
147         if (regex.isNull())
148         {
149                 regex.reset(new QRegExp(QLatin1String(pattern)));
150         }
151         str.remove(*regex.data());
152         return str;
153 }
154
155 QString& MUtils::trim_right(QString &str)
156 {
157         static const char *const TRIM_RIGHT = "\\s+$";
158         return trim_helper(str, g_str_trim_rx_r, TRIM_RIGHT);
159 }
160
161 QString& MUtils::trim_left(QString &str)
162 {
163         static const char *const TRIM_LEFT = "^\\s+";
164         return trim_helper(str, g_str_trim_rx_l, TRIM_LEFT);
165 }
166
167 QString MUtils::trim_right(const QString &str)
168 {
169         QString temp(str);
170         return trim_right(temp);
171 }
172
173 QString MUtils::trim_left(const QString &str)
174 {
175         QString temp(str);
176         return trim_left(temp);
177 }
178
179 ///////////////////////////////////////////////////////////////////////////////
180 // GENERATE FILE NAME
181 ///////////////////////////////////////////////////////////////////////////////
182
183 QString MUtils::make_temp_file(const QString &basePath, const QString &extension, const bool placeholder)
184 {
185         for(int i = 0; i < 4096; i++)
186         {
187                 const QString tempFileName = QString("%1/%2.%3").arg(basePath, next_rand_str(), extension);
188                 if(!QFileInfo(tempFileName).exists())
189                 {
190                         if(placeholder)
191                         {
192                                 QFile file(tempFileName);
193                                 if(file.open(QFile::ReadWrite))
194                                 {
195                                         file.close();
196                                         return tempFileName;
197                                 }
198                         }
199                         else
200                         {
201                                 return tempFileName;
202                         }
203                 }
204         }
205
206         qWarning("Failed to generate temp file name!");
207         return QString();
208 }
209
210 QString MUtils::make_unique_file(const QString &basePath, const QString &baseName, const QString &extension, const bool fancy)
211 {
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())
215         {
216                 if (n <= quint32(USHRT_MAX))
217                 {
218                         if (fancy)
219                         {
220                                 fileName = QString("%1/%2 (%3).%4").arg(basePath, baseName, QString::number(n++), extension);
221                         }
222                         else
223                         {
224                                 fileName = QString("%1/%2.%3.%4").arg(basePath, baseName, QString::number(n++, 16).rightJustified(4, QLatin1Char('0')), extension);
225                         }
226                 }
227                 else
228                 {
229                         qWarning("Failed to generate unique file name!");
230                         return QString();
231                 }
232         }
233         return fileName;
234 }
235
236 ///////////////////////////////////////////////////////////////////////////////
237 // COMPUTE PARITY
238 ///////////////////////////////////////////////////////////////////////////////
239
240 /*
241  * Compute parity in parallel
242  * http://www.graphics.stanford.edu/~seander/bithacks.html#ParityParallel
243  */
244 bool MUtils::parity(quint32 value)
245 {
246         value ^= value >> 16;
247         value ^= value >> 8;
248         value ^= value >> 4;
249         value &= 0xf;
250         return ((0x6996 >> value) & 1) != 0;
251 }
252
253 ///////////////////////////////////////////////////////////////////////////////
254 // TEMP FOLDER
255 ///////////////////////////////////////////////////////////////////////////////
256
257 static QScopedPointer<MUtils::Internal::DirLock> g_temp_folder_file;
258 static QReadWriteLock                            g_temp_folder_lock;
259
260 static QString try_create_subfolder(const QString &baseDir, const QString &postfix)
261 {
262         const QString baseDirPath = QDir(baseDir).absolutePath();
263         for(int i = 0; i < 32; i++)
264         {
265                 QDir directory(baseDirPath);
266                 if(directory.mkpath(postfix) && directory.cd(postfix))
267                 {
268                         return directory.canonicalPath();
269                 }
270         }
271         return QString();
272 }
273
274 static MUtils::Internal::DirLock *try_init_temp_folder(const QString &baseDir)
275 {
276         const QString tempPath = try_create_subfolder(baseDir, MUtils::next_rand_str());
277         if(!tempPath.isEmpty())
278         {
279                 for(int i = 0; i < 32; i++)
280                 {
281                         MUtils::Internal::DirLock *lockFile = NULL;
282                         try
283                         {
284                                 lockFile = new MUtils::Internal::DirLock(tempPath);
285                                 return lockFile;
286                         }
287                         catch(MUtils::Internal::DirLockException&)
288                         {
289                                 /*ignore error and try again*/
290                         }
291                 }
292         }
293         return NULL;
294 }
295
296 static bool temp_folder_cleanup_helper(const QString &tempPath)
297 {
298         size_t delay = 1;
299         static const size_t MAX_DELAY = 8192;
300         forever
301         {
302                 QDir::setCurrent(QDir::rootPath());
303                 if(MUtils::remove_directory(tempPath, true))
304                 {
305                         return true;
306                 }
307                 else
308                 {
309                         if(delay > MAX_DELAY)
310                         {
311                                 return false;
312                         }
313                         MUtils::OS::sleep_ms(delay);
314                         delay *= 2;
315                 }
316         }
317 }
318
319 static void temp_folder_cleaup(void)
320 {
321         QWriteLocker writeLock(&g_temp_folder_lock);
322
323         //Clean the directory
324         while(!g_temp_folder_file.isNull())
325         {
326                 const QString tempPath = g_temp_folder_file->getPath();
327                 g_temp_folder_file.reset(NULL);
328                 if(!temp_folder_cleanup_helper(tempPath))
329                 {
330                         MUtils::OS::system_message_wrn(L"Temp Cleaner", L"Warning: Not all temporary files could be removed!");
331                 }
332         }
333 }
334
335 const QString &MUtils::temp_folder(void)
336 {
337         QReadLocker readLock(&g_temp_folder_lock);
338
339         //Already initialized?
340         if(!g_temp_folder_file.isNull())
341         {
342                 return g_temp_folder_file->getPath();
343         }
344
345         //Obtain the write lock to initilaize
346         readLock.unlock();
347         QWriteLocker writeLock(&g_temp_folder_lock);
348         
349         //Still uninitilaized?
350         if(!g_temp_folder_file.isNull())
351         {
352                 return g_temp_folder_file->getPath();
353         }
354
355         //Try the %TMP% or %TEMP% directory first
356         if(MUtils::Internal::DirLock *lockFile = try_init_temp_folder(QDir::tempPath()))
357         {
358                 g_temp_folder_file.reset(lockFile);
359                 atexit(temp_folder_cleaup);
360                 return lockFile->getPath();
361         }
362
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++)
366         {
367                 const QString &knownFolder = OS::known_folder(FOLDER_ID[id]);
368                 if(!knownFolder.isEmpty())
369                 {
370                         const QString tempRoot = try_create_subfolder(knownFolder, QLatin1String("TEMP"));
371                         if(!tempRoot.isEmpty())
372                         {
373                                 if(MUtils::Internal::DirLock *lockFile = try_init_temp_folder(tempRoot))
374                                 {
375                                         g_temp_folder_file.reset(lockFile);
376                                         atexit(temp_folder_cleaup);
377                                         return lockFile->getPath();
378                                 }
379                         }
380                 }
381         }
382
383         qFatal("Temporary directory could not be initialized !!!");
384         return (*((QString*)NULL));
385 }
386
387 ///////////////////////////////////////////////////////////////////////////////
388 // REMOVE DIRECTORY / FILE
389 ///////////////////////////////////////////////////////////////////////////////
390
391 static const QFile::Permissions FILE_PERMISSIONS_NONE = QFile::ReadOther | QFile::WriteOther;
392
393 bool MUtils::remove_file(const QString &fileName)
394 {
395         QFileInfo fileInfo(fileName);
396         if(!(fileInfo.exists() && fileInfo.isFile()))
397         {
398                 return true;
399         }
400
401         for(int i = 0; i < 32; i++)
402         {
403                 QFile file(fileName);
404                 file.setPermissions(FILE_PERMISSIONS_NONE);
405                 if((!(fileInfo.exists() && fileInfo.isFile())) || file.remove())
406                 {
407                         return true;
408                 }
409                 MUtils::OS::sleep_ms(1);
410                 fileInfo.refresh();
411         }
412
413         qWarning("Could not delete \"%s\"", MUTILS_UTF8(fileName));
414         return false;
415 }
416
417 static bool remove_directory_helper(const QDir &folder)
418 {
419         if(!folder.exists())
420         {
421                 return true;
422         }
423         const QString dirName = folder.dirName();
424         if(!dirName.isEmpty())
425         {
426                 QDir parent(folder);
427                 if(parent.cdUp())
428                 {
429                         QFile::setPermissions(folder.absolutePath(), FILE_PERMISSIONS_NONE);
430                         if(parent.rmdir(dirName))
431                         {
432                                 return true;
433                         }
434                 }
435         }
436         return false;
437 }
438
439 bool MUtils::remove_directory(const QString &folderPath, const bool &recursive)
440 {
441         QDir folder(folderPath);
442         if(!folder.exists())
443         {
444                 return true;
445         }
446
447         if(recursive)
448         {
449                 const QFileInfoList entryList = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
450                 for(QFileInfoList::ConstIterator iter = entryList.constBegin(); iter != entryList.constEnd(); iter++)
451                 {
452                         if(iter->isDir())
453                         {
454                                 remove_directory(iter->canonicalFilePath(), true);
455                         }
456                         else if(iter->isFile())
457                         {
458                                 remove_file(iter->canonicalFilePath());
459                         }
460                 }
461         }
462
463         for(int i = 0; i < 32; i++)
464         {
465                 if(remove_directory_helper(folder))
466                 {
467                         return true;
468                 }
469                 MUtils::OS::sleep_ms(1);
470                 folder.refresh();
471         }
472         
473         qWarning("Could not rmdir \"%s\"", MUTILS_UTF8(folderPath));
474         return false;
475 }
476
477 ///////////////////////////////////////////////////////////////////////////////
478 // PROCESS UTILS
479 ///////////////////////////////////////////////////////////////////////////////
480
481 static void prependToPath(QProcessEnvironment &env, const QString &value)
482 {
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));
486 }
487
488 void MUtils::init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir, const QStringList *const extraPaths)
489 {
490         //Environment variable names
491         static const char *const s_envvar_names_temp[] =
492         {
493                 "TEMP", "TMP", "TMPDIR", "HOME", "USERPROFILE", "HOMEPATH", NULL
494         };
495         static const char *const s_envvar_names_remove[] =
496         {
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
498         };
499
500         //Initialize environment
501         QProcessEnvironment env = process.processEnvironment();
502         if(env.isEmpty()) env = QProcessEnvironment::systemEnvironment();
503
504         //Clean a number of enviroment variables that might affect our tools
505         for(size_t i = 0; s_envvar_names_remove[i]; i++)
506         {
507                 env.remove(QString::fromLatin1(s_envvar_names_remove[i]));
508                 env.remove(QString::fromLatin1(s_envvar_names_remove[i]).toLower());
509         }
510
511         const QString tempDir = QDir::toNativeSeparators(temp_folder());
512
513         //Replace TEMP directory in environment
514         if(bReplaceTempDir)
515         {
516                 for(size_t i = 0; s_envvar_names_temp[i]; i++)
517                 {
518                         env.insert(s_envvar_names_temp[i], tempDir);
519                 }
520         }
521
522         //Setup PATH variable
523         prependToPath(env, tempDir);
524         if (extraPaths && (!extraPaths->isEmpty()))
525         {
526                 QListIterator<QString> iter(*extraPaths);
527                 iter.toBack();
528                 while (iter.hasPrevious())
529                 {
530                         prependToPath(env, QDir::toNativeSeparators(iter.previous()));
531                 }
532         }
533         
534         //Setup QPorcess object
535         process.setWorkingDirectory(wokringDir);
536         process.setProcessChannelMode(QProcess::MergedChannels);
537         process.setReadChannel(QProcess::StandardOutput);
538         process.setProcessEnvironment(env);
539 }
540
541 ///////////////////////////////////////////////////////////////////////////////
542 // NATURAL ORDER STRING COMPARISON
543 ///////////////////////////////////////////////////////////////////////////////
544
545 static bool natural_string_sort_helper(const QString &str1, const QString &str2)
546 {
547         return (MUtils::Internal::NaturalSort::strnatcmp(MUTILS_WCHR(str1), MUTILS_WCHR(str2)) < 0);
548 }
549
550 static bool natural_string_sort_helper_fold_case(const QString &str1, const QString &str2)
551 {
552         return (MUtils::Internal::NaturalSort::strnatcasecmp(MUTILS_WCHR(str1), MUTILS_WCHR(str2)) < 0);
553 }
554
555 void MUtils::natural_string_sort(QStringList &list, const bool bIgnoreCase)
556 {
557         qSort(list.begin(), list.end(), bIgnoreCase ? natural_string_sort_helper_fold_case : natural_string_sort_helper);
558 }
559
560 ///////////////////////////////////////////////////////////////////////////////
561 // CLEAN FILE PATH
562 ///////////////////////////////////////////////////////////////////////////////
563
564 QString MUtils::clean_file_name(const QString &name)
565 {
566         static const QLatin1Char REPLACEMENT_CHAR('_');
567         static const char FILENAME_ILLEGAL_CHARS[] = "<>:\"/\\|?*";
568         static const char *const FILENAME_RESERVED_NAMES[] =
569         {
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
573         };
574
575         QString result(name);
576         if (result.contains(QLatin1Char('"')))
577         {
578                 QRegExp quoted("\"(.+)\"");
579                 quoted.setMinimal(true);
580                 result.replace(quoted, "``\\1ยดยด");
581         }
582
583         for(QString::Iterator iter = result.begin(); iter != result.end(); iter++)
584         {
585                 if (iter->category() == QChar::Other_Control)
586                 {
587                         *iter = REPLACEMENT_CHAR;
588                 }
589         }
590                 
591         for(size_t i = 0; FILENAME_ILLEGAL_CHARS[i]; i++)
592         {
593                 result.replace(QLatin1Char(FILENAME_ILLEGAL_CHARS[i]), REPLACEMENT_CHAR);
594         }
595         
596         trim_right(result);
597         while (result.endsWith(QLatin1Char('.')))
598         {
599                 result.chop(1);
600                 trim_right(result);
601         }
602
603         for (size_t i = 0; FILENAME_RESERVED_NAMES[i]; i++)
604         {
605                 const QString reserved = QString::fromLatin1(FILENAME_RESERVED_NAMES[i]);
606                 if ((!result.compare(reserved, Qt::CaseInsensitive)) || result.startsWith(reserved + QLatin1Char('.'), Qt::CaseInsensitive))
607                 {
608                         result.replace(0, reserved.length(), QString().leftJustified(reserved.length(), REPLACEMENT_CHAR));
609                 }
610         }
611
612         return result;
613 }
614
615 static QPair<QString,QString> clean_file_path_get_prefix(const QString path)
616 {
617         static const char *const PREFIXES[] =
618         {
619                 "//?/", "//", "/", NULL
620         };
621         const QString posixPath = QDir::fromNativeSeparators(path.trimmed());
622         for (int i = 0; PREFIXES[i]; i++)
623         {
624                 const QString prefix = QString::fromLatin1(PREFIXES[i]);
625                 if (posixPath.startsWith(prefix))
626                 {
627                         return qMakePair(prefix, posixPath.mid(prefix.length()));
628                 }
629         }
630         return qMakePair(QString(), posixPath);
631 }
632
633 QString MUtils::clean_file_path(const QString &path)
634 {
635         const QPair<QString, QString> prefix = clean_file_path_get_prefix(path);
636
637         QStringList parts = prefix.second.split(QLatin1Char('/'), QString::SkipEmptyParts);
638         for(int i = 0; i < parts.count(); i++)
639         {
640                 if((i == 0) && (parts[i].length() == 2) && parts[i][0].isLetter() && (parts[i][1] == QLatin1Char(':')))
641                 {
642                         continue; //handle case "c:\"
643                 }
644                 parts[i] = MUtils::clean_file_name(parts[i]);
645         }
646
647         const QString cleanPath = parts.join(QLatin1String("/"));
648         return prefix.first.isEmpty() ? cleanPath : prefix.first + cleanPath;
649 }
650
651 ///////////////////////////////////////////////////////////////////////////////
652 // REGULAR EXPESSION HELPER
653 ///////////////////////////////////////////////////////////////////////////////
654
655 bool MUtils::regexp_parse_uint32(const QRegExp &regexp, quint32 &value)
656 {
657         return regexp_parse_uint32(regexp, &value, 1);
658 }
659
660 bool MUtils::regexp_parse_uint32(const QRegExp &regexp, quint32 *values, const size_t &count)
661 {
662         const QStringList caps = regexp.capturedTexts();
663         
664         if(caps.isEmpty() || (quint32(caps.count()) <= count))
665         {
666                 return false;
667         }
668
669         for(size_t i = 0; i < count; i++)
670         {
671                 bool ok = false;
672                 values[i] = caps[i+1].toUInt(&ok);
673                 if(!ok)
674                 {
675                         return false;
676                 }
677         }
678
679         return true;
680 }
681
682 ///////////////////////////////////////////////////////////////////////////////
683 // AVAILABLE CODEPAGES
684 ///////////////////////////////////////////////////////////////////////////////
685
686 QStringList MUtils::available_codepages(const bool &noAliases)
687 {
688         QStringList codecList;
689         QList<QByteArray> availableCodecs = QTextCodec::availableCodecs();
690
691         while(!availableCodecs.isEmpty())
692         {
693                 const QByteArray current = availableCodecs.takeFirst();
694                 if(!current.toLower().startsWith("system"))
695                 {
696                         codecList << QString::fromLatin1(current.constData(), current.size());
697                         if(noAliases)
698                         {
699                                 if(QTextCodec *const currentCodec = QTextCodec::codecForName(current.constData()))
700                                 {
701                                         const QList<QByteArray> aliases = currentCodec->aliases();
702                                         for(QList<QByteArray>::ConstIterator iter = aliases.constBegin(); iter != aliases.constEnd(); iter++)
703                                         {
704                                                 availableCodecs.removeAll(*iter);
705                                         }
706                                 }
707                         }
708                 }
709         }
710
711         return codecList;
712 }
713
714 ///////////////////////////////////////////////////////////////////////////////
715 // SELF-TEST
716 ///////////////////////////////////////////////////////////////////////////////
717
718 int MUtils::Internal::selfTest(const char *const buildKey, const bool debug)
719 {
720         static const bool MY_DEBUG_FLAG = MUTILS_DEBUG;
721         static const char *const MY_BUILD_KEY = __DATE__ "@" __TIME__;
722
723         if(strncmp(buildKey, MY_BUILD_KEY, 13) || (MY_DEBUG_FLAG != debug))
724         {
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();
728         }
729         return 0;
730 }