OSDN Git Service

Documentation was updated.
[mutilities/MUtilities.git] / src / Global.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2018 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 //32-Bit wrapper for qrand()
67 #define QRAND() ((static_cast<quint32>(qrand()) & 0xFFFF) | (static_cast<quint32>(qrand()) << 16U))
68
69 //Robert Jenkins' 96 bit Mix Function
70 static quint32 mix_function(quint32 a, quint32 b, quint32 c)
71 {
72         a=a-b;  a=a-c;  a=a^(c >> 13);
73         b=b-c;  b=b-a;  b=b^(a <<  8); 
74         c=c-a;  c=c-b;  c=c^(b >> 13);
75         a=a-b;  a=a-c;  a=a^(c >> 12);
76         b=b-c;  b=b-a;  b=b^(a << 16);
77         c=c-a;  c=c-b;  c=c^(b >>  5);
78         a=a-b;  a=a-c;  a=a^(c >>  3);
79         b=b-c;  b=b-a;  b=b^(a << 10);
80         c=c-a;  c=c-b;  c=c^(b >> 15);
81
82         return a ^ b ^ c;
83 }
84
85 static void seed_rand(void)
86 {
87         QDateTime build(MUtils::Version::lib_build_date(), MUtils::Version::lib_build_time());
88         const quint32 seed_0 = mix_function(MUtils::OS::process_id(), MUtils::OS::thread_id(), build.toMSecsSinceEpoch());
89         qsrand(mix_function(clock(), time(NULL), seed_0));
90 }
91
92 static quint32 rand_fallback(void)
93 {
94         Q_ASSERT(RAND_MAX >= 0x7FFF);
95
96         if (!(g_srand_flag.hasLocalData() && g_srand_flag.localData()))
97         {
98                 seed_rand();
99                 g_srand_flag.setLocalData(true);
100         }
101
102         quint32 rnd_val = mix_function(0x32288EA3, clock(), time(NULL));
103
104         for (size_t i = 0; i < 42; i++)
105         {
106                 rnd_val = mix_function(rnd_val, QRAND(), QRAND());
107                 rnd_val = mix_function(QRAND(), rnd_val, QRAND());
108                 rnd_val = mix_function(QRAND(), QRAND(), rnd_val);
109         }
110
111         return rnd_val;
112 }
113
114 quint32 MUtils::next_rand_u32(void)
115 {
116         quint32 rnd;
117         if (rand_s(&rnd))
118         {
119                 return rand_fallback();
120         }
121         return rnd;
122 }
123
124 quint64 MUtils::next_rand_u64(void)
125 {
126         return (quint64(next_rand_u32()) << 32) | quint64(next_rand_u32());
127 }
128
129 QString MUtils::next_rand_str(const bool &bLong)
130 {
131         if (bLong)
132         {
133                 return next_rand_str(false) + next_rand_str(false);
134         }
135         else
136         {
137                 return QString::number(next_rand_u64(), 16).rightJustified(16, QLatin1Char('0'));
138         }
139 }
140
141 ///////////////////////////////////////////////////////////////////////////////
142 // STRING UTILITY FUNCTIONS
143 ///////////////////////////////////////////////////////////////////////////////
144
145 static QScopedPointer<QRegExp> g_str_trim_rx_r;
146 static QScopedPointer<QRegExp> g_str_trim_rx_l;
147 static QMutex                  g_str_trim_lock;
148
149 static QString& trim_helper(QString &str, QScopedPointer<QRegExp> &regex, const char *const pattern)
150 {
151         QMutexLocker lock(&g_str_trim_lock);
152         if (regex.isNull())
153         {
154                 regex.reset(new QRegExp(QLatin1String(pattern)));
155         }
156         str.remove(*regex.data());
157         return str;
158 }
159
160 QString& MUtils::trim_right(QString &str)
161 {
162         static const char *const TRIM_RIGHT = "\\s+$";
163         return trim_helper(str, g_str_trim_rx_r, TRIM_RIGHT);
164 }
165
166 QString& MUtils::trim_left(QString &str)
167 {
168         static const char *const TRIM_LEFT = "^\\s+";
169         return trim_helper(str, g_str_trim_rx_l, TRIM_LEFT);
170 }
171
172 QString MUtils::trim_right(const QString &str)
173 {
174         QString temp(str);
175         return trim_right(temp);
176 }
177
178 QString MUtils::trim_left(const QString &str)
179 {
180         QString temp(str);
181         return trim_left(temp);
182 }
183
184 ///////////////////////////////////////////////////////////////////////////////
185 // GENERATE FILE NAME
186 ///////////////////////////////////////////////////////////////////////////////
187
188 QString MUtils::make_temp_file(const QString &basePath, const QString &extension, const bool placeholder)
189 {
190         for(int i = 0; i < 4096; i++)
191         {
192                 const QString tempFileName = QString("%1/%2.%3").arg(basePath, next_rand_str(), extension);
193                 if(!QFileInfo(tempFileName).exists())
194                 {
195                         if(placeholder)
196                         {
197                                 QFile file(tempFileName);
198                                 if(file.open(QFile::ReadWrite))
199                                 {
200                                         file.close();
201                                         return tempFileName;
202                                 }
203                         }
204                         else
205                         {
206                                 return tempFileName;
207                         }
208                 }
209         }
210
211         qWarning("Failed to generate temp file name!");
212         return QString();
213 }
214
215 QString MUtils::make_unique_file(const QString &basePath, const QString &baseName, const QString &extension, const bool fancy)
216 {
217         quint32 n = fancy ? 2 : 0;
218         QString fileName = fancy ? QString("%1/%2.%3").arg(basePath, baseName, extension) : QString();
219         while (fileName.isEmpty() || QFileInfo(fileName).exists())
220         {
221                 if (n <= quint32(USHRT_MAX))
222                 {
223                         if (fancy)
224                         {
225                                 fileName = QString("%1/%2 (%3).%4").arg(basePath, baseName, QString::number(n++), extension);
226                         }
227                         else
228                         {
229                                 fileName = QString("%1/%2.%3.%4").arg(basePath, baseName, QString::number(n++, 16).rightJustified(4, QLatin1Char('0')), extension);
230                         }
231                 }
232                 else
233                 {
234                         qWarning("Failed to generate unique file name!");
235                         return QString();
236                 }
237         }
238         return fileName;
239 }
240
241 ///////////////////////////////////////////////////////////////////////////////
242 // COMPUTE PARITY
243 ///////////////////////////////////////////////////////////////////////////////
244
245 /*
246  * Compute parity in parallel
247  * http://www.graphics.stanford.edu/~seander/bithacks.html#ParityParallel
248  */
249 bool MUtils::parity(quint32 value)
250 {
251         value ^= value >> 16;
252         value ^= value >> 8;
253         value ^= value >> 4;
254         value &= 0xf;
255         return ((0x6996 >> value) & 1) != 0;
256 }
257
258 ///////////////////////////////////////////////////////////////////////////////
259 // TEMP FOLDER
260 ///////////////////////////////////////////////////////////////////////////////
261
262 static QScopedPointer<MUtils::Internal::DirLock> g_temp_folder_file;
263 static QReadWriteLock                            g_temp_folder_lock;
264
265 static QString try_create_subfolder(const QString &baseDir, const QString &postfix)
266 {
267         const QString baseDirPath = QDir(baseDir).absolutePath();
268         for(int i = 0; i < 32; i++)
269         {
270                 QDir directory(baseDirPath);
271                 if(directory.mkpath(postfix) && directory.cd(postfix))
272                 {
273                         return directory.canonicalPath();
274                 }
275         }
276         return QString();
277 }
278
279 static MUtils::Internal::DirLock *try_init_temp_folder(const QString &baseDir)
280 {
281         const QString tempPath = try_create_subfolder(baseDir, MUtils::next_rand_str());
282         if(!tempPath.isEmpty())
283         {
284                 for(int i = 0; i < 32; i++)
285                 {
286                         MUtils::Internal::DirLock *lockFile = NULL;
287                         try
288                         {
289                                 lockFile = new MUtils::Internal::DirLock(tempPath);
290                                 return lockFile;
291                         }
292                         catch(MUtils::Internal::DirLockException&)
293                         {
294                                 /*ignore error and try again*/
295                         }
296                 }
297         }
298         return NULL;
299 }
300
301 static bool temp_folder_cleanup_helper(const QString &tempPath)
302 {
303         size_t delay = 1;
304         static const size_t MAX_DELAY = 8192;
305         forever
306         {
307                 QDir::setCurrent(QDir::rootPath());
308                 if(MUtils::remove_directory(tempPath, true))
309                 {
310                         return true;
311                 }
312                 else
313                 {
314                         if(delay > MAX_DELAY)
315                         {
316                                 return false;
317                         }
318                         MUtils::OS::sleep_ms(delay);
319                         delay *= 2;
320                 }
321         }
322 }
323
324 static void temp_folder_cleaup(void)
325 {
326         QWriteLocker writeLock(&g_temp_folder_lock);
327
328         //Clean the directory
329         while(!g_temp_folder_file.isNull())
330         {
331                 const QString tempPath = g_temp_folder_file->getPath();
332                 g_temp_folder_file.reset(NULL);
333                 if(!temp_folder_cleanup_helper(tempPath))
334                 {
335                         MUtils::OS::system_message_wrn(L"Temp Cleaner", L"Warning: Not all temporary files could be removed!");
336                 }
337         }
338 }
339
340 const QString &MUtils::temp_folder(void)
341 {
342         QReadLocker readLock(&g_temp_folder_lock);
343
344         //Already initialized?
345         if(!g_temp_folder_file.isNull())
346         {
347                 return g_temp_folder_file->getPath();
348         }
349
350         //Obtain the write lock to initilaize
351         readLock.unlock();
352         QWriteLocker writeLock(&g_temp_folder_lock);
353         
354         //Still uninitilaized?
355         if(!g_temp_folder_file.isNull())
356         {
357                 return g_temp_folder_file->getPath();
358         }
359
360         //Try the %TMP% or %TEMP% directory first
361         if(MUtils::Internal::DirLock *lockFile = try_init_temp_folder(QDir::tempPath()))
362         {
363                 g_temp_folder_file.reset(lockFile);
364                 atexit(temp_folder_cleaup);
365                 return lockFile->getPath();
366         }
367
368         qWarning("%%TEMP%% directory not found -> trying fallback mode now!");
369         static const OS::known_folder_t FOLDER_ID[2] = { OS::FOLDER_LOCALAPPDATA, OS::FOLDER_SYSTROOT_DIR };
370         for(size_t id = 0; id < 2; id++)
371         {
372                 const QString &knownFolder = OS::known_folder(FOLDER_ID[id]);
373                 if(!knownFolder.isEmpty())
374                 {
375                         const QString tempRoot = try_create_subfolder(knownFolder, QLatin1String("TEMP"));
376                         if(!tempRoot.isEmpty())
377                         {
378                                 if(MUtils::Internal::DirLock *lockFile = try_init_temp_folder(tempRoot))
379                                 {
380                                         g_temp_folder_file.reset(lockFile);
381                                         atexit(temp_folder_cleaup);
382                                         return lockFile->getPath();
383                                 }
384                         }
385                 }
386         }
387
388         qFatal("Temporary directory could not be initialized !!!");
389         return (*((QString*)NULL));
390 }
391
392 ///////////////////////////////////////////////////////////////////////////////
393 // REMOVE DIRECTORY / FILE
394 ///////////////////////////////////////////////////////////////////////////////
395
396 static const QFile::Permissions FILE_PERMISSIONS_NONE = QFile::ReadOther | QFile::WriteOther;
397
398 bool MUtils::remove_file(const QString &fileName)
399 {
400         QFileInfo fileInfo(fileName);
401         if(!(fileInfo.exists() && fileInfo.isFile()))
402         {
403                 return true;
404         }
405
406         for(int i = 0; i < 32; i++)
407         {
408                 QFile file(fileName);
409                 file.setPermissions(FILE_PERMISSIONS_NONE);
410                 if((!(fileInfo.exists() && fileInfo.isFile())) || file.remove())
411                 {
412                         return true;
413                 }
414                 MUtils::OS::sleep_ms(1);
415                 fileInfo.refresh();
416         }
417
418         qWarning("Could not delete \"%s\"", MUTILS_UTF8(fileName));
419         return false;
420 }
421
422 static bool remove_directory_helper(const QDir &folder)
423 {
424         if(!folder.exists())
425         {
426                 return true;
427         }
428         const QString dirName = folder.dirName();
429         if(!dirName.isEmpty())
430         {
431                 QDir parent(folder);
432                 if(parent.cdUp())
433                 {
434                         QFile::setPermissions(folder.absolutePath(), FILE_PERMISSIONS_NONE);
435                         if(parent.rmdir(dirName))
436                         {
437                                 return true;
438                         }
439                 }
440         }
441         return false;
442 }
443
444 bool MUtils::remove_directory(const QString &folderPath, const bool &recursive)
445 {
446         QDir folder(folderPath);
447         if(!folder.exists())
448         {
449                 return true;
450         }
451
452         if(recursive)
453         {
454                 const QFileInfoList entryList = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
455                 for(QFileInfoList::ConstIterator iter = entryList.constBegin(); iter != entryList.constEnd(); iter++)
456                 {
457                         if(iter->isDir())
458                         {
459                                 remove_directory(iter->canonicalFilePath(), true);
460                         }
461                         else if(iter->isFile())
462                         {
463                                 remove_file(iter->canonicalFilePath());
464                         }
465                 }
466         }
467
468         for(int i = 0; i < 32; i++)
469         {
470                 if(remove_directory_helper(folder))
471                 {
472                         return true;
473                 }
474                 MUtils::OS::sleep_ms(1);
475                 folder.refresh();
476         }
477         
478         qWarning("Could not rmdir \"%s\"", MUTILS_UTF8(folderPath));
479         return false;
480 }
481
482 ///////////////////////////////////////////////////////////////////////////////
483 // PROCESS UTILS
484 ///////////////////////////////////////////////////////////////////////////////
485
486 static void prependToPath(QProcessEnvironment &env, const QString &value)
487 {
488         const QLatin1String PATH = QLatin1String("PATH");
489         const QString path = env.value(PATH, QString()).trimmed();
490         env.insert(PATH, path.isEmpty() ? value : QString("%1;%2").arg(value, path));
491 }
492
493 void MUtils::init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir, const QStringList *const extraPaths)
494 {
495         //Environment variable names
496         static const char *const s_envvar_names_temp[] =
497         {
498                 "TEMP", "TMP", "TMPDIR", "HOME", "USERPROFILE", "HOMEPATH", NULL
499         };
500         static const char *const s_envvar_names_remove[] =
501         {
502                 "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
503         };
504
505         //Initialize environment
506         QProcessEnvironment env = process.processEnvironment();
507         if(env.isEmpty()) env = QProcessEnvironment::systemEnvironment();
508
509         //Clean a number of enviroment variables that might affect our tools
510         for(size_t i = 0; s_envvar_names_remove[i]; i++)
511         {
512                 env.remove(QString::fromLatin1(s_envvar_names_remove[i]));
513                 env.remove(QString::fromLatin1(s_envvar_names_remove[i]).toLower());
514         }
515
516         const QString tempDir = QDir::toNativeSeparators(temp_folder());
517
518         //Replace TEMP directory in environment
519         if(bReplaceTempDir)
520         {
521                 for(size_t i = 0; s_envvar_names_temp[i]; i++)
522                 {
523                         env.insert(s_envvar_names_temp[i], tempDir);
524                 }
525         }
526
527         //Setup PATH variable
528         prependToPath(env, tempDir);
529         if (extraPaths && (!extraPaths->isEmpty()))
530         {
531                 QListIterator<QString> iter(*extraPaths);
532                 iter.toBack();
533                 while (iter.hasPrevious())
534                 {
535                         prependToPath(env, QDir::toNativeSeparators(iter.previous()));
536                 }
537         }
538         
539         //Setup QPorcess object
540         process.setWorkingDirectory(wokringDir);
541         process.setProcessChannelMode(QProcess::MergedChannels);
542         process.setReadChannel(QProcess::StandardOutput);
543         process.setProcessEnvironment(env);
544 }
545
546 ///////////////////////////////////////////////////////////////////////////////
547 // NATURAL ORDER STRING COMPARISON
548 ///////////////////////////////////////////////////////////////////////////////
549
550 static bool natural_string_sort_helper(const QString &str1, const QString &str2)
551 {
552         return (MUtils::Internal::NaturalSort::strnatcmp(MUTILS_WCHR(str1), MUTILS_WCHR(str2)) < 0);
553 }
554
555 static bool natural_string_sort_helper_fold_case(const QString &str1, const QString &str2)
556 {
557         return (MUtils::Internal::NaturalSort::strnatcasecmp(MUTILS_WCHR(str1), MUTILS_WCHR(str2)) < 0);
558 }
559
560 void MUtils::natural_string_sort(QStringList &list, const bool bIgnoreCase)
561 {
562         qSort(list.begin(), list.end(), bIgnoreCase ? natural_string_sort_helper_fold_case : natural_string_sort_helper);
563 }
564
565 ///////////////////////////////////////////////////////////////////////////////
566 // CLEAN FILE PATH
567 ///////////////////////////////////////////////////////////////////////////////
568
569 static QMutex                                              g_clean_file_name_mutex;
570 static QScopedPointer<const QList<QPair<QRegExp,QString>>> g_clean_file_name_regex;
571
572 static void clean_file_name_make_pretty(QString &str)
573 {
574         static const struct { const char *p; const char *r; } PATTERN[] =
575         {
576                 { "^\\s*\"([^\"]*)\"\\s*$",    "\\1"                         },  //Remove straight double quotes around the whole string
577                 { "\"([^\"]*)\"",              "\xE2\x80\x9C\\1\xE2\x80\x9D" },  //Replace remaining pairs of straight double quotes with opening/closing double quote
578                 { "^[\\\\/:]+([^\\\\/:]+.*)$", "\\1"                         },  //Remove leading slash, backslash and colon characters
579                 { "^(.*[^\\\\/:]+)[\\\\/:]+$", "\\1"                         },  //Remove trailing slash, backslash and colon characters
580                 { "(\\s*[\\\\/:]\\s*)+",       " - "                         },  //Replace any slash, backslash or colon character that appears in the middle
581                 { NULL, NULL }
582         };
583
584         QMutexLocker locker(&g_clean_file_name_mutex);
585
586         if (g_clean_file_name_regex.isNull())
587         {
588                 QScopedPointer<QList<QPair<QRegExp, QString>>> list(new QList<QPair<QRegExp, QString>>());
589                 for (size_t i = 0; PATTERN[i].p; ++i)
590                 {
591                         list->append(qMakePair(QRegExp(QString::fromUtf8(PATTERN[i].p), Qt::CaseInsensitive), PATTERN[i].r ? QString::fromUtf8(PATTERN[i].r) : QString()));
592                 }
593                 g_clean_file_name_regex.reset(list.take());
594         }
595
596         bool keepOnGoing = !str.isEmpty();
597         while(keepOnGoing)
598         {
599                 const QString prev = str;
600                 keepOnGoing = false;
601                 for (QList<QPair<QRegExp, QString>>::ConstIterator iter = g_clean_file_name_regex->constBegin(); iter != g_clean_file_name_regex->constEnd(); ++iter)
602                 {
603                         str.replace(iter->first, iter->second);
604                         if (str.compare(prev))
605                         {
606                                 str = str.simplified();
607                                 keepOnGoing = !str.isEmpty();
608                                 break;
609                         }
610                 }
611         }
612 }
613
614 QString MUtils::clean_file_name(const QString &name, const bool &pretty)
615 {
616         static const QLatin1Char REPLACEMENT_CHAR('_');
617         static const char FILENAME_ILLEGAL_CHARS[] = "<>:\"/\\|?*";
618         static const char *const FILENAME_RESERVED_NAMES[] =
619         {
620                 "CON", "PRN", "AUX", "NUL",
621                 "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
622                 "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", NULL
623         };
624
625         QString result(name);
626         if (pretty)
627         {
628                 clean_file_name_make_pretty(result);
629         }
630
631         for(QString::Iterator iter = result.begin(); iter != result.end(); iter++)
632         {
633                 if (iter->category() == QChar::Other_Control)
634                 {
635                         *iter = REPLACEMENT_CHAR;
636                 }
637         }
638                 
639         for(size_t i = 0; FILENAME_ILLEGAL_CHARS[i]; i++)
640         {
641                 result.replace(QLatin1Char(FILENAME_ILLEGAL_CHARS[i]), REPLACEMENT_CHAR);
642         }
643         
644         trim_right(result);
645         while (result.endsWith(QLatin1Char('.')))
646         {
647                 result.chop(1);
648                 trim_right(result);
649         }
650
651         for (size_t i = 0; FILENAME_RESERVED_NAMES[i]; i++)
652         {
653                 const QString reserved = QString::fromLatin1(FILENAME_RESERVED_NAMES[i]);
654                 if ((!result.compare(reserved, Qt::CaseInsensitive)) || result.startsWith(reserved + QLatin1Char('.'), Qt::CaseInsensitive))
655                 {
656                         result.replace(0, reserved.length(), QString().leftJustified(reserved.length(), REPLACEMENT_CHAR));
657                 }
658         }
659
660         return result;
661 }
662
663 static QPair<QString,QString> clean_file_path_get_prefix(const QString path)
664 {
665         static const char *const PREFIXES[] =
666         {
667                 "//?/", "//", "/", NULL
668         };
669         const QString posixPath = QDir::fromNativeSeparators(path.trimmed());
670         for (int i = 0; PREFIXES[i]; i++)
671         {
672                 const QString prefix = QString::fromLatin1(PREFIXES[i]);
673                 if (posixPath.startsWith(prefix))
674                 {
675                         return qMakePair(prefix, posixPath.mid(prefix.length()));
676                 }
677         }
678         return qMakePair(QString(), posixPath);
679 }
680
681 QString MUtils::clean_file_path(const QString &path, const bool &pretty)
682 {
683         const QPair<QString, QString> prefix = clean_file_path_get_prefix(path);
684
685         QStringList parts = prefix.second.split(QLatin1Char('/'), QString::SkipEmptyParts);
686         for(int i = 0; i < parts.count(); i++)
687         {
688                 if((i == 0) && (parts[i].length() == 2) && parts[i][0].isLetter() && (parts[i][1] == QLatin1Char(':')))
689                 {
690                         continue; //handle case "c:\"
691                 }
692                 parts[i] = MUtils::clean_file_name(parts[i], pretty);
693         }
694
695         const QString cleanPath = parts.join(QLatin1String("/"));
696         return prefix.first.isEmpty() ? cleanPath : prefix.first + cleanPath;
697 }
698
699 ///////////////////////////////////////////////////////////////////////////////
700 // REGULAR EXPESSION HELPER
701 ///////////////////////////////////////////////////////////////////////////////
702
703 bool MUtils::regexp_parse_uint32(const QRegExp &regexp, quint32 &value)
704 {
705         return regexp_parse_uint32(regexp, &value, 1U, 1U);
706 }
707
708 bool MUtils::regexp_parse_int32(const QRegExp &regexp, qint32 &value)
709 {
710         return regexp_parse_int32(regexp, &value, 1U, 1U);
711 }
712
713 bool MUtils::regexp_parse_uint32(const QRegExp &regexp, quint32 &value, const size_t &offset)
714 {
715         return regexp_parse_uint32(regexp, &value, offset, 1U);
716 }
717
718 bool MUtils::regexp_parse_int32(const QRegExp &regexp, qint32 &value, const size_t &offset)
719 {
720         return regexp_parse_int32(regexp, &value, offset, 1U);
721 }
722
723 bool MUtils::regexp_parse_uint32(const QRegExp &regexp, quint32 *values, const size_t &count)
724 {
725         return regexp_parse_uint32(regexp, values, 1U, count);
726 }
727
728 bool MUtils::regexp_parse_int32(const QRegExp &regexp, qint32 *values, const size_t &count)
729 {
730         return regexp_parse_int32(regexp, values, 1U, count);
731 }
732
733 bool MUtils::regexp_parse_uint32(const QRegExp &regexp, quint32 *values, const size_t &offset, const size_t &count)
734 {
735         const QStringList caps = regexp.capturedTexts();
736
737         if (caps.isEmpty() || (quint32(caps.count()) <= count))
738         {
739                 return false;
740         }
741
742         for (size_t i = 0; i < count; i++)
743         {
744                 bool ok = false;
745                 values[i] = caps[offset+i].toUInt(&ok);
746                 if (!ok)
747                 {
748                         return false;
749                 }
750         }
751
752         return true;
753 }
754
755 bool MUtils::regexp_parse_int32(const QRegExp &regexp, qint32 *values, const size_t &offset, const size_t &count)
756 {
757         const QStringList caps = regexp.capturedTexts();
758
759         if (caps.isEmpty() || (quint32(caps.count()) <= count))
760         {
761                 return false;
762         }
763
764         for (size_t i = 0; i < count; i++)
765         {
766                 bool ok = false;
767                 values[i] = caps[offset+i].toInt(&ok);
768                 if (!ok)
769                 {
770                         return false;
771                 }
772         }
773
774         return true;
775 }
776
777 ///////////////////////////////////////////////////////////////////////////////
778 // AVAILABLE CODEPAGES
779 ///////////////////////////////////////////////////////////////////////////////
780
781 QStringList MUtils::available_codepages(const bool &noAliases)
782 {
783         QStringList codecList;
784         QList<QByteArray> availableCodecs = QTextCodec::availableCodecs();
785
786         while(!availableCodecs.isEmpty())
787         {
788                 const QByteArray current = availableCodecs.takeFirst();
789                 if(!current.toLower().startsWith("system"))
790                 {
791                         codecList << QString::fromLatin1(current.constData(), current.size());
792                         if(noAliases)
793                         {
794                                 if(QTextCodec *const currentCodec = QTextCodec::codecForName(current.constData()))
795                                 {
796                                         const QList<QByteArray> aliases = currentCodec->aliases();
797                                         for(QList<QByteArray>::ConstIterator iter = aliases.constBegin(); iter != aliases.constEnd(); iter++)
798                                         {
799                                                 availableCodecs.removeAll(*iter);
800                                         }
801                                 }
802                         }
803                 }
804         }
805
806         return codecList;
807 }
808
809 ///////////////////////////////////////////////////////////////////////////////
810 // FP MATH SUPPORT
811 ///////////////////////////////////////////////////////////////////////////////
812
813 MUtils::fp_parts_t MUtils::break_fp(const double value)
814 {
815         fp_parts_t result = { };
816         if (_finite(value))
817         {
818                 result.parts[1] = modf(value, &result.parts[0]);
819         }
820         else
821         {
822                 result.parts[0] = std::numeric_limits<double>::quiet_NaN();
823                 result.parts[1] = std::numeric_limits<double>::quiet_NaN();
824         }
825         return result;
826 }
827
828 ///////////////////////////////////////////////////////////////////////////////
829 // SELF-TEST
830 ///////////////////////////////////////////////////////////////////////////////
831
832 int MUtils::Internal::selfTest(const char *const buildKey, const bool debug)
833 {
834         static const bool MY_DEBUG_FLAG = MUTILS_DEBUG;
835         static const char *const MY_BUILD_KEY = __DATE__ "@" __TIME__;
836
837         if(strncmp(buildKey, MY_BUILD_KEY, 13) || (MY_DEBUG_FLAG != debug))
838         {
839                 MUtils::OS::system_message_err(L"MUtils", L"FATAL ERROR: MUtils library version mismatch detected!");
840                 MUtils::OS::system_message_wrn(L"MUtils", L"Perform a clean(!) re-install of the application to fix the problem!");
841                 abort();
842         }
843         return 0;
844 }