1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
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.
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 //////////////////////////////////////////////////////////////////////////////////
23 //#define _CRT_RAND_S 1
27 #include <MUtils/Global.h>
28 #include <MUtils/OSSupport.h>
29 #include <MUtils/Version.h>
32 #include "DirLocker.h"
33 #include "3rd_party/strnatcmp/include/strnatcmp.h"
37 #include <QReadWriteLock>
41 #include <QListIterator>
43 #include <QThreadStorage>
55 ///////////////////////////////////////////////////////////////////////////////
57 ///////////////////////////////////////////////////////////////////////////////
60 #define rand_s(X) (true)
63 //Per-thread init flag
64 static QThreadStorage
<bool> g_srand_flag
;
66 //Robert Jenkins' 96 bit Mix Function
67 static quint32
mix_function(const quint32 x
, const quint32 y
, const quint32 z
)
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);
86 static void seed_rand(void)
88 fprintf(stderr
, "SEED RAND (TID: %u)\n", MUtils::OS::thread_id());
89 QDateTime
build(MUtils::Version::lib_build_date(), MUtils::Version::lib_build_time());
90 const quint32 seed
= mix_function(MUtils::OS::process_id(), MUtils::OS::thread_id(), build
.toMSecsSinceEpoch());
91 qsrand(mix_function(clock(), time(NULL
), seed
));
94 static quint32
rand_fallback(void)
96 Q_ASSERT(RAND_MAX
>= 0xFFF);
97 if (!(g_srand_flag
.hasLocalData() && g_srand_flag
.localData()))
100 g_srand_flag
.setLocalData(true);
102 quint32 rnd
= 0x32288EA3;
103 for (size_t i
= 0; i
< 3; i
++)
105 rnd
= (rnd
<< 12) ^ qrand();
110 quint32
MUtils::next_rand_u32(void)
115 return rand_fallback();
120 quint64
MUtils::next_rand_u64(void)
122 return (quint64(next_rand_u32()) << 32) | quint64(next_rand_u32());
125 QString
MUtils::next_rand_str(const bool &bLong
)
129 return next_rand_str(false) + next_rand_str(false);
133 return QString::number(next_rand_u64(), 16).rightJustified(16, QLatin1Char('0'));
137 ///////////////////////////////////////////////////////////////////////////////
138 // STRING UTILITY FUNCTIONS
139 ///////////////////////////////////////////////////////////////////////////////
141 static QScopedPointer
<QRegExp
> g_str_trim_rx_r
;
142 static QScopedPointer
<QRegExp
> g_str_trim_rx_l
;
143 static QMutex g_str_trim_lock
;
145 static QString
& trim_helper(QString
&str
, QScopedPointer
<QRegExp
> ®ex
, const char *const pattern
)
147 QMutexLocker
lock(&g_str_trim_lock
);
150 regex
.reset(new QRegExp(QLatin1String(pattern
)));
152 str
.remove(*regex
.data());
156 QString
& MUtils::trim_right(QString
&str
)
158 static const char *const TRIM_RIGHT
= "\\s+$";
159 return trim_helper(str
, g_str_trim_rx_r
, TRIM_RIGHT
);
162 QString
& MUtils::trim_left(QString
&str
)
164 static const char *const TRIM_LEFT
= "^\\s+";
165 return trim_helper(str
, g_str_trim_rx_l
, TRIM_LEFT
);
168 QString
MUtils::trim_right(const QString
&str
)
171 return trim_right(temp
);
174 QString
MUtils::trim_left(const QString
&str
)
177 return trim_left(temp
);
180 ///////////////////////////////////////////////////////////////////////////////
181 // GENERATE FILE NAME
182 ///////////////////////////////////////////////////////////////////////////////
184 QString
MUtils::make_temp_file(const QString
&basePath
, const QString
&extension
, const bool placeholder
)
186 for(int i
= 0; i
< 4096; i
++)
188 const QString tempFileName
= QString("%1/%2.%3").arg(basePath
, next_rand_str(), extension
);
189 if(!QFileInfo(tempFileName
).exists())
193 QFile
file(tempFileName
);
194 if(file
.open(QFile::ReadWrite
))
207 qWarning("Failed to generate temp file name!");
211 QString
MUtils::make_unique_file(const QString
&basePath
, const QString
&baseName
, const QString
&extension
, const bool fancy
)
213 quint32 n
= fancy
? 2 : 0;
214 QString fileName
= fancy
? QString("%1/%2.%3").arg(basePath
, baseName
, extension
) : QString();
215 while (fileName
.isEmpty() || QFileInfo(fileName
).exists())
217 if (n
<= quint32(USHRT_MAX
))
221 fileName
= QString("%1/%2 (%3).%4").arg(basePath
, baseName
, QString::number(n
++), extension
);
225 fileName
= QString("%1/%2.%3.%4").arg(basePath
, baseName
, QString::number(n
++, 16).rightJustified(4, QLatin1Char('0')), extension
);
230 qWarning("Failed to generate unique file name!");
237 ///////////////////////////////////////////////////////////////////////////////
239 ///////////////////////////////////////////////////////////////////////////////
242 * Compute parity in parallel
243 * http://www.graphics.stanford.edu/~seander/bithacks.html#ParityParallel
245 bool MUtils::parity(quint32 value
)
247 value
^= value
>> 16;
251 return ((0x6996 >> value
) & 1) != 0;
254 ///////////////////////////////////////////////////////////////////////////////
256 ///////////////////////////////////////////////////////////////////////////////
258 static QScopedPointer
<MUtils::Internal::DirLock
> g_temp_folder_file
;
259 static QReadWriteLock g_temp_folder_lock
;
261 static QString
try_create_subfolder(const QString
&baseDir
, const QString
&postfix
)
263 const QString baseDirPath
= QDir(baseDir
).absolutePath();
264 for(int i
= 0; i
< 32; i
++)
266 QDir
directory(baseDirPath
);
267 if(directory
.mkpath(postfix
) && directory
.cd(postfix
))
269 return directory
.canonicalPath();
275 static MUtils::Internal::DirLock
*try_init_temp_folder(const QString
&baseDir
)
277 const QString tempPath
= try_create_subfolder(baseDir
, MUtils::next_rand_str());
278 if(!tempPath
.isEmpty())
280 for(int i
= 0; i
< 32; i
++)
282 MUtils::Internal::DirLock
*lockFile
= NULL
;
285 lockFile
= new MUtils::Internal::DirLock(tempPath
);
288 catch(MUtils::Internal::DirLockException
&)
290 /*ignore error and try again*/
297 static bool temp_folder_cleanup_helper(const QString
&tempPath
)
300 static const size_t MAX_DELAY
= 8192;
303 QDir::setCurrent(QDir::rootPath());
304 if(MUtils::remove_directory(tempPath
, true))
310 if(delay
> MAX_DELAY
)
314 MUtils::OS::sleep_ms(delay
);
320 static void temp_folder_cleaup(void)
322 QWriteLocker
writeLock(&g_temp_folder_lock
);
324 //Clean the directory
325 while(!g_temp_folder_file
.isNull())
327 const QString tempPath
= g_temp_folder_file
->getPath();
328 g_temp_folder_file
.reset(NULL
);
329 if(!temp_folder_cleanup_helper(tempPath
))
331 MUtils::OS::system_message_wrn(L
"Temp Cleaner", L
"Warning: Not all temporary files could be removed!");
336 const QString
&MUtils::temp_folder(void)
338 QReadLocker
readLock(&g_temp_folder_lock
);
340 //Already initialized?
341 if(!g_temp_folder_file
.isNull())
343 return g_temp_folder_file
->getPath();
346 //Obtain the write lock to initilaize
348 QWriteLocker
writeLock(&g_temp_folder_lock
);
350 //Still uninitilaized?
351 if(!g_temp_folder_file
.isNull())
353 return g_temp_folder_file
->getPath();
356 //Try the %TMP% or %TEMP% directory first
357 if(MUtils::Internal::DirLock
*lockFile
= try_init_temp_folder(QDir::tempPath()))
359 g_temp_folder_file
.reset(lockFile
);
360 atexit(temp_folder_cleaup
);
361 return lockFile
->getPath();
364 qWarning("%%TEMP%% directory not found -> trying fallback mode now!");
365 static const OS::known_folder_t FOLDER_ID
[2] = { OS::FOLDER_LOCALAPPDATA
, OS::FOLDER_SYSTROOT_DIR
};
366 for(size_t id
= 0; id
< 2; id
++)
368 const QString
&knownFolder
= OS::known_folder(FOLDER_ID
[id
]);
369 if(!knownFolder
.isEmpty())
371 const QString tempRoot
= try_create_subfolder(knownFolder
, QLatin1String("TEMP"));
372 if(!tempRoot
.isEmpty())
374 if(MUtils::Internal::DirLock
*lockFile
= try_init_temp_folder(tempRoot
))
376 g_temp_folder_file
.reset(lockFile
);
377 atexit(temp_folder_cleaup
);
378 return lockFile
->getPath();
384 qFatal("Temporary directory could not be initialized !!!");
385 return (*((QString
*)NULL
));
388 ///////////////////////////////////////////////////////////////////////////////
389 // REMOVE DIRECTORY / FILE
390 ///////////////////////////////////////////////////////////////////////////////
392 static const QFile::Permissions FILE_PERMISSIONS_NONE
= QFile::ReadOther
| QFile::WriteOther
;
394 bool MUtils::remove_file(const QString
&fileName
)
396 QFileInfo
fileInfo(fileName
);
397 if(!(fileInfo
.exists() && fileInfo
.isFile()))
402 for(int i
= 0; i
< 32; i
++)
404 QFile
file(fileName
);
405 file
.setPermissions(FILE_PERMISSIONS_NONE
);
406 if((!(fileInfo
.exists() && fileInfo
.isFile())) || file
.remove())
413 qWarning("Could not delete \"%s\"", MUTILS_UTF8(fileName
));
417 static bool remove_directory_helper(const QDir
&folder
)
423 const QString dirName
= folder
.dirName();
424 if(!dirName
.isEmpty())
429 QFile::setPermissions(folder
.absolutePath(), FILE_PERMISSIONS_NONE
);
430 if(parent
.rmdir(dirName
))
439 bool MUtils::remove_directory(const QString
&folderPath
, const bool &recursive
)
441 QDir
folder(folderPath
);
449 const QFileInfoList entryList
= folder
.entryInfoList(QDir::AllEntries
| QDir::NoDotAndDotDot
| QDir::Hidden
);
450 for(QFileInfoList::ConstIterator iter
= entryList
.constBegin(); iter
!= entryList
.constEnd(); iter
++)
454 remove_directory(iter
->canonicalFilePath(), true);
456 else if(iter
->isFile())
458 remove_file(iter
->canonicalFilePath());
463 for(int i
= 0; i
< 32; i
++)
465 if(remove_directory_helper(folder
))
472 qWarning("Could not rmdir \"%s\"", MUTILS_UTF8(folderPath
));
476 ///////////////////////////////////////////////////////////////////////////////
478 ///////////////////////////////////////////////////////////////////////////////
480 static void prependToPath(QProcessEnvironment
&env
, const QString
&value
)
482 const QLatin1String PATH
= QLatin1String("PATH");
483 const QString path
= env
.value(PATH
, QString()).trimmed();
484 env
.insert(PATH
, path
.isEmpty() ? value
: QString("%1;%2").arg(value
, path
));
487 void MUtils::init_process(QProcess
&process
, const QString
&wokringDir
, const bool bReplaceTempDir
, const QStringList
*const extraPaths
)
489 //Environment variable names
490 static const char *const s_envvar_names_temp
[] =
492 "TEMP", "TMP", "TMPDIR", "HOME", "USERPROFILE", "HOMEPATH", NULL
494 static const char *const s_envvar_names_remove
[] =
496 "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
499 //Initialize environment
500 QProcessEnvironment env
= process
.processEnvironment();
501 if(env
.isEmpty()) env
= QProcessEnvironment::systemEnvironment();
503 //Clean a number of enviroment variables that might affect our tools
504 for(size_t i
= 0; s_envvar_names_remove
[i
]; i
++)
506 env
.remove(QString::fromLatin1(s_envvar_names_remove
[i
]));
507 env
.remove(QString::fromLatin1(s_envvar_names_remove
[i
]).toLower());
510 const QString tempDir
= QDir::toNativeSeparators(temp_folder());
512 //Replace TEMP directory in environment
515 for(size_t i
= 0; s_envvar_names_temp
[i
]; i
++)
517 env
.insert(s_envvar_names_temp
[i
], tempDir
);
521 //Setup PATH variable
522 prependToPath(env
, tempDir
);
523 if (extraPaths
&& (!extraPaths
->isEmpty()))
525 QListIterator
<QString
> iter(*extraPaths
);
527 while (iter
.hasPrevious())
529 prependToPath(env
, QDir::toNativeSeparators(iter
.previous()));
533 //Setup QPorcess object
534 process
.setWorkingDirectory(wokringDir
);
535 process
.setProcessChannelMode(QProcess::MergedChannels
);
536 process
.setReadChannel(QProcess::StandardOutput
);
537 process
.setProcessEnvironment(env
);
540 ///////////////////////////////////////////////////////////////////////////////
541 // NATURAL ORDER STRING COMPARISON
542 ///////////////////////////////////////////////////////////////////////////////
544 static bool natural_string_sort_helper(const QString
&str1
, const QString
&str2
)
546 return (MUtils::Internal::NaturalSort::strnatcmp(MUTILS_WCHR(str1
), MUTILS_WCHR(str2
)) < 0);
549 static bool natural_string_sort_helper_fold_case(const QString
&str1
, const QString
&str2
)
551 return (MUtils::Internal::NaturalSort::strnatcasecmp(MUTILS_WCHR(str1
), MUTILS_WCHR(str2
)) < 0);
554 void MUtils::natural_string_sort(QStringList
&list
, const bool bIgnoreCase
)
556 qSort(list
.begin(), list
.end(), bIgnoreCase
? natural_string_sort_helper_fold_case
: natural_string_sort_helper
);
559 ///////////////////////////////////////////////////////////////////////////////
561 ///////////////////////////////////////////////////////////////////////////////
563 QString
MUtils::clean_file_name(const QString
&name
)
565 static const QLatin1Char
REPLACEMENT_CHAR('_');
566 static const char FILENAME_ILLEGAL_CHARS
[] = "<>:\"/\\|?*";
567 static const char *const FILENAME_RESERVED_NAMES
[] =
569 "CON", "PRN", "AUX", "NUL",
570 "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
571 "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", NULL
574 QString
result(name
);
575 if (result
.contains(QLatin1Char('"')))
577 QRegExp
quoted("\"(.+)\"");
578 quoted
.setMinimal(true);
579 result
.replace(quoted
, "``\\1ยดยด");
582 for(QString::Iterator iter
= result
.begin(); iter
!= result
.end(); iter
++)
584 if (iter
->category() == QChar::Other_Control
)
586 *iter
= REPLACEMENT_CHAR
;
590 for(size_t i
= 0; FILENAME_ILLEGAL_CHARS
[i
]; i
++)
592 result
.replace(QLatin1Char(FILENAME_ILLEGAL_CHARS
[i
]), REPLACEMENT_CHAR
);
596 while (result
.endsWith(QLatin1Char('.')))
602 for (size_t i
= 0; FILENAME_RESERVED_NAMES
[i
]; i
++)
604 const QString reserved
= QString::fromLatin1(FILENAME_RESERVED_NAMES
[i
]);
605 if ((!result
.compare(reserved
, Qt::CaseInsensitive
)) || result
.startsWith(reserved
+ QLatin1Char('.'), Qt::CaseInsensitive
))
607 result
.replace(0, reserved
.length(), QString().leftJustified(reserved
.length(), REPLACEMENT_CHAR
));
614 static QPair
<QString
,QString
> clean_file_path_get_prefix(const QString path
)
616 static const char *const PREFIXES
[] =
618 "//?/", "//", "/", NULL
620 const QString posixPath
= QDir::fromNativeSeparators(path
.trimmed());
621 for (int i
= 0; PREFIXES
[i
]; i
++)
623 const QString prefix
= QString::fromLatin1(PREFIXES
[i
]);
624 if (posixPath
.startsWith(prefix
))
626 return qMakePair(prefix
, posixPath
.mid(prefix
.length()));
629 return qMakePair(QString(), posixPath
);
632 QString
MUtils::clean_file_path(const QString
&path
)
634 const QPair
<QString
, QString
> prefix
= clean_file_path_get_prefix(path
);
636 QStringList parts
= prefix
.second
.split(QLatin1Char('/'), QString::SkipEmptyParts
);
637 for(int i
= 0; i
< parts
.count(); i
++)
639 if((i
== 0) && (parts
[i
].length() == 2) && parts
[i
][0].isLetter() && (parts
[i
][1] == QLatin1Char(':')))
641 continue; //handle case "c:\"
643 parts
[i
] = MUtils::clean_file_name(parts
[i
]);
646 const QString cleanPath
= parts
.join(QLatin1String("/"));
647 return prefix
.first
.isEmpty() ? cleanPath
: prefix
.first
+ cleanPath
;
650 ///////////////////////////////////////////////////////////////////////////////
651 // REGULAR EXPESSION HELPER
652 ///////////////////////////////////////////////////////////////////////////////
654 bool MUtils::regexp_parse_uint32(const QRegExp
®exp
, quint32
&value
)
656 return regexp_parse_uint32(regexp
, &value
, 1);
659 bool MUtils::regexp_parse_uint32(const QRegExp
®exp
, quint32
*values
, const size_t &count
)
661 const QStringList caps
= regexp
.capturedTexts();
663 if(caps
.isEmpty() || (quint32(caps
.count()) <= count
))
668 for(size_t i
= 0; i
< count
; i
++)
671 values
[i
] = caps
[i
+1].toUInt(&ok
);
681 ///////////////////////////////////////////////////////////////////////////////
682 // AVAILABLE CODEPAGES
683 ///////////////////////////////////////////////////////////////////////////////
685 QStringList
MUtils::available_codepages(const bool &noAliases
)
687 QStringList codecList
;
688 QList
<QByteArray
> availableCodecs
= QTextCodec::availableCodecs();
690 while(!availableCodecs
.isEmpty())
692 const QByteArray current
= availableCodecs
.takeFirst();
693 if(!current
.toLower().startsWith("system"))
695 codecList
<< QString::fromLatin1(current
.constData(), current
.size());
698 if(QTextCodec
*const currentCodec
= QTextCodec::codecForName(current
.constData()))
700 const QList
<QByteArray
> aliases
= currentCodec
->aliases();
701 for(QList
<QByteArray
>::ConstIterator iter
= aliases
.constBegin(); iter
!= aliases
.constEnd(); iter
++)
703 availableCodecs
.removeAll(*iter
);
713 ///////////////////////////////////////////////////////////////////////////////
715 ///////////////////////////////////////////////////////////////////////////////
717 int MUtils::Internal::selfTest(const char *const buildKey
, const bool debug
)
719 static const bool MY_DEBUG_FLAG
= MUTILS_DEBUG
;
720 static const char *const MY_BUILD_KEY
= __DATE__
"@" __TIME__
;
722 if(strncmp(buildKey
, MY_BUILD_KEY
, 13) || (MY_DEBUG_FLAG
!= debug
))
724 MUtils::OS::system_message_err(L
"MUtils", L
"FATAL ERROR: MUtils library version mismatch detected!");
725 MUtils::OS::system_message_wrn(L
"MUtils", L
"Perform a clean(!) re-install of the application to fix the problem!");