MuldeR's Utilities for Qt
MUtilities
Global.h
Go to the documentation of this file.
1 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2019 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
21 
27 #pragma once
28 
29 #include <QString>
30 
31 //Forward Declarations
32 class QProcess;
33 class QDir;
34 template<typename K, typename V> class QHash;
35 
37 
41  //Interface version
42 #define MUTILS_INTERFACE 2
43 
44 //Build key
45 #define MUTILS_BUILD_KEY_HELPER(X,Y) X##" "##Y
46 #define MUTILS_BUILD_KEY MUTILS_BUILD_KEY_HELPER(__DATE__, __TIME__)
47 
48 //MUtils API
49 #ifdef _MSC_VER
50 # ifdef MUTILS_DLL_EXPORT
51 # define MUTILS_API __declspec(dllexport)
52 # else
53 # ifndef MUTILS_STATIC_LIB
54 # define MUTILS_API __declspec(dllimport)
55 # else
56 # define MUTILS_API /*static lib*/
57 # endif
58 # endif
59 #else
60 # define MUTILS_API
61 #endif
62 
63 //Check Debug Flags
64 #if defined(_DEBUG) || defined(DEBUG) || (!defined(NDEBUG))
65 # define MUTILS_DEBUG (1)
66 # if defined(NDEBUG) || defined(QT_NO_DEBUG) || (!defined(QT_DEBUG))
67 # error Inconsistent DEBUG flags have been detected!
68 # endif
69 #else
70 # define MUTILS_DEBUG (0)
71 # if (!defined(NDEBUG)) || (!defined(QT_NO_DEBUG)) || defined(QT_DEBUG)
72 # error Inconsistent DEBUG flags have been detected!
73 # endif
74 #endif
75 
76 //Check CPU options
77 #if defined(_MSC_VER) && (!defined(__INTELLISENSE__)) && (!defined(_M_X64)) && defined(_M_IX86_FP)
78  #if (_M_IX86_FP != 0)
79  #error We should not enabled SSE or SSE2 in release builds!
80  #endif
81 #endif
82 
83 #define MUTILS_MAKE_STRING_HELPER(X) #X
84 #define MUTILS_MAKE_STRING(X) MUTILS_MAKE_STRING_HELPER(X)
85 #define MUTILS_COMPILER_WARNING(TXT) __pragma(message(__FILE__ "(" MUTILS_MAKE_STRING(__LINE__) ") : warning: " TXT))
86 
87 #define MUTILS_INIT_GLUE_HELPER(X,Y) X##Y
88 #define MUTILS_INIT_GLUE(X,Y) MUTILS_INIT_GLUE_HELPER(X,Y)
89 #define MUTILS_INITIALIZER MUTILS_INIT_GLUE(initialize_, MUTILS_INTERFACE)
90 
94 
99 namespace MUtils
100 {
104  typedef struct _fp_parts_t { double parts[2]; } fp_parts_t;
105 
113  MUTILS_API const QString& temp_folder(void);
114 
128  MUTILS_API void init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir = true, const QStringList *const extraPaths = NULL, const QHash<QString, QString> *const extraEnv = NULL);
129 
137  MUTILS_API quint32 next_rand_u32(void);
138  MUTILS_API quint32 next_rand_u32(const quint32 max);
139 
147  MUTILS_API quint64 next_rand_u64(void);
148 
158  MUTILS_API QString next_rand_str(const bool &bLong = false);
159 
173  MUTILS_API QString make_temp_file(const QString &basePath, const QString &extension, const bool placeholder = false);
174  MUTILS_API QString make_temp_file(const QDir &basePath, const QString &extension, const bool placeholder = false);
175 
191  MUTILS_API QString make_unique_file(const QString &basePath, const QString &baseName, const QString &extension, const bool fancy = false, const bool placeholder = false);
192  MUTILS_API QString make_unique_file(const QDir &basePath, const QString &baseName, const QString &extension, const bool fancy = false, const bool placeholder = false);
193 
201  MUTILS_API bool parity(quint32 value);
202 
212  MUTILS_API bool remove_file(const QString &fileName);
213 
225  MUTILS_API bool remove_directory(const QString &folderPath, const bool &recursive);
226 
236  MUTILS_API QString& trim_right(QString &str);
237 
247  MUTILS_API QString& trim_left(QString &str);
248 
258  MUTILS_API QString trim_right(const QString &str);
259 
269  MUTILS_API QString trim_left(const QString &str);
270 
280  MUTILS_API void natural_string_sort(QStringList &list, const bool bIgnoreCase);
281 
293  MUTILS_API QString clean_file_name(const QString &name, const bool &pretty);
294 
306  MUTILS_API QString clean_file_path(const QString &path, const bool &pretty);
307 
319  MUTILS_API bool regexp_parse_uint32(const QRegExp &regexp, quint32 &value);
320  MUTILS_API bool regexp_parse_int32(const QRegExp &regexp, qint32 &value);
321  MUTILS_API bool regexp_parse_uint32(const QRegExp &regexp, quint32 &value, const size_t &offset);
322  MUTILS_API bool regexp_parse_int32(const QRegExp &regexp, qint32 &value, const size_t &offset);
323 
337  MUTILS_API bool regexp_parse_uint32(const QRegExp &regexp, quint32 *values, const size_t &count);
338  MUTILS_API bool regexp_parse_int32(const QRegExp &regexp, qint32 *values, const size_t &count);
339  MUTILS_API bool regexp_parse_uint32(const QRegExp &regexp, quint32 *values, const size_t &offset, const size_t &count);
340  MUTILS_API bool regexp_parse_int32(const QRegExp &regexp, qint32 *values, const size_t &offset, const size_t &count);
341 
351  MUTILS_API QStringList available_codepages(const bool &noAliases = true);
352 
362  MUTILS_API fp_parts_t break_fp(const double value);
363 
364  //Internal
365  namespace Internal
366  {
367  MUTILS_API int MUTILS_INITIALIZER(const char *const buildKey, const bool debug);
368  static const int s_initializedFlag = MUTILS_INITIALIZER(MUTILS_BUILD_KEY, MUTILS_DEBUG);
369  }
370 }
371 
373 
376 #define MUTILS_DELETE(PTR) do { if((PTR)) { delete (PTR); (PTR) = NULL; } } while(0)
377 
380 #define MUTILS_DELETE_ARRAY(PTR) do { if((PTR)) { delete [] (PTR); (PTR) = NULL; } } while(0)
381 
384 #define MUTILS_ZERO_MEMORY(PTR) memset(&(PTR), 0, sizeof((PTR)))
385 
388 #define MUTILS_WCHR(STR) (reinterpret_cast<const wchar_t*>((STR).utf16()))
389 
392 #define MUTILS_UTF8(STR) ((STR).toUtf8().constData())
393 
396 #define MUTILS_L1STR(STR) ((STR).toLatin1().constData())
397 
400 #define MUTILS_QSTR(STR) (QString::fromUtf16(reinterpret_cast<const unsigned short*>((STR))))
401 
404 #define MUTILS_BOOL2STR(X) ((X) ? "1" : "0")
405 
408 #define MUTILS_BOOLIFY(X) (!(!(X)))
409 
412 #define MUTILS_ARR2LEN(X) (sizeof((X)) / sizeof((X)[0]))
413 
416 #define MUTILS_NO_COPY(CLASS) \
417  CLASS(const CLASS &) { throw "Copy constructor is disabled!"; } \
418  CLASS &operator=(const CLASS &) { throw "Assignment operator is disabled!"; }
MUTILS_API QStringList available_codepages(const bool &noAliases=true)
Retrieve a list of all available codepages.
struct MUtils::_fp_parts_t fp_parts_t
This struct containes the parts (integral and fractional part) of a floating-point number...
MUTILS_API QString next_rand_str(const bool &bLong=false)
Generates a random string.
MUTILS_API bool remove_file(const QString &fileName)
Deletes the specified file.
MUTILS_API QString clean_file_path(const QString &path, const bool &pretty)
Clean up a file path string.
This struct containes the parts (integral and fractional part) of a floating-point number...
Definition: Global.h:104
MUTILS_API void natural_string_sort(QStringList &list, const bool bIgnoreCase)
Sort a list of strings using "natural ordering" algorithm.
MUTILS_API bool regexp_parse_uint32(const QRegExp &regexp, quint32 &value)
Parse regular expression results.
MUTILS_API bool parity(quint32 value)
Computes the parity of the given unsigned 32-Bit value.
MUTILS_API fp_parts_t break_fp(const double value)
Break floating-point number into fractional and integral parts.
MUTILS_API quint64 next_rand_u64(void)
Generates a random unsigned 64-Bit value.
MUTILS_API QString & trim_right(QString &str)
Remove trailing white-space characters.
MUTILS_API const QString & temp_folder(void)
Rerieves the full path of the application&#39;s Temp folder.
Global MUtils namespace.
Definition: CPUFeatures.h:37
MUTILS_API QString clean_file_name(const QString &name, const bool &pretty)
Clean up a file name string.
MUTILS_API quint32 next_rand_u32(void)
Generates a random unsigned 32-Bit value.
MUTILS_API bool remove_directory(const QString &folderPath, const bool &recursive)
Recursively deletes the specified directory.
MUTILS_API QString make_temp_file(const QString &basePath, const QString &extension, const bool placeholder=false)
Generates a temporary file name.
MUTILS_API void init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir=true, const QStringList *const extraPaths=NULL, const QHash< QString, QString > *const extraEnv=NULL)
Initialize a given QProcess object.
MUTILS_API QString & trim_left(QString &str)
Remove leading white-space characters.
Definition: Global.h:34
MUTILS_API QString make_unique_file(const QString &basePath, const QString &baseName, const QString &extension, const bool fancy=false, const bool placeholder=false)
Generates a unique file name.