MuldeR's Utilities for Qt
MUtilities
Macros | Functions
Global.h File Reference

This file contains miscellaneous functions that are generally useful for Qt-based applications. More...

#include <QString>

Go to the source code of this file.

Macros

#define MUTILS_API
 
#define MUTILS_MAKE_STRING_HELPER(X)   #X
 
#define MUTILS_MAKE_STRING(X)   MUTILS_MAKE_STRING_HELPER(X)
 
#define MUTILS_COMPILER_WARNING(TXT)   __pragma(message(__FILE__ "(" MUTILS_MAKE_STRING(__LINE__) ") : warning: " TXT))
 
#define MUTILS_DEBUG   (1)
 
#define MUTILS_DELETE(PTR)   do { if((PTR)) { delete (PTR); (PTR) = NULL; } } while(0)
 
#define MUTILS_DELETE_ARRAY(PTR)   do { if((PTR)) { delete [] (PTR); (PTR) = NULL; } } while(0)
 
#define MUTILS_ZERO_MEMORY(PTR)   memset(&(PTR), 0, sizeof((PTR)))
 
#define MUTILS_WCHR(STR)   (reinterpret_cast<const wchar_t*>((STR).utf16()))
 
#define MUTILS_UTF8(STR)   ((STR).toUtf8().constData())
 
#define MUTILS_QSTR(STR)   (QString::fromUtf16(reinterpret_cast<const unsigned short*>((STR))))
 
#define MUTILS_BOOL2STR(X)   ((X) ? "1" : "0")
 

Functions

const QString & MUtils::temp_folder (void)
 Rerieves the full path of the application's Temp folder. More...
 
void MUtils::init_process (QProcess &process, const QString &wokringDir, const bool bReplaceTempDir=true, const QStringList *const extraPaths=NULL)
 
quint32 MUtils::next_rand_u32 (void)
 Generates a random unsigned 32-Bit value. More...
 
quint64 MUtils::next_rand_u64 (void)
 Generates a random unsigned 64-Bit value. More...
 
QString MUtils::next_rand_str (const bool &bLong=false)
 Generates a random string. More...
 
QString MUtils::make_temp_file (const QString &basePath, const QString &extension, const bool placeholder=false)
 Generates a temporary file name. More...
 
QString MUtils::make_unique_file (const QString &basePath, const QString &baseName, const QString &extension, const bool fancy=false)
 Generates a unique file name. More...
 
bool MUtils::parity (quint32 value)
 Computes the parity of the given unsigned 32-Bit value. More...
 
bool MUtils::remove_file (const QString &fileName)
 
bool MUtils::remove_directory (const QString &folderPath, const bool &recursive)
 
QString & MUtils::trim_right (QString &str)
 
QString & MUtils::trim_left (QString &str)
 
QString MUtils::trim_right (const QString &str)
 
QString MUtils::trim_left (const QString &str)
 
void MUtils::natural_string_sort (QStringList &list, const bool bIgnoreCase)
 
QString MUtils::clean_file_name (const QString &name)
 
QString MUtils::clean_file_path (const QString &path)
 
bool MUtils::regexp_parse_uint32 (const QRegExp &regexp, quint32 &value)
 
bool MUtils::regexp_parse_uint32 (const QRegExp &regexp, quint32 *values, const size_t &count)
 
QStringList MUtils::available_codepages (const bool &noAliases=true)
 
int MUtils::Internal::selfTest (const char *const buildKey, const bool debug)
 

Detailed Description

This file contains miscellaneous functions that are generally useful for Qt-based applications.

Function Documentation

§ make_temp_file()

QString MUtils::make_temp_file ( const QString &  basePath,
const QString &  extension,
const bool  placeholder = false 
)

Generates a temporary file name.

The function generates a file name that contains a random component and that is guaranteed to not exist yet. The generated file name follows a "<basedir>/<random>.<ext>" pattern. This is useful (not only) for creating temporary files.

Parameters
basePathSpecifies the "base" directory where the temporary file is supposed to be created. This must be a valid existing directory.
extensionSpecifies the desired file extensions of the temporary file. Do not include a leading dot (.) character.
placeholderIf set to true, the function creates an empty "placeholder" file under the returned file name; if set to false, it does not.
Returns
If the function succeeds, it returns a QString holding the full path of the temporary file; otherwise it returns a default-constructed QString.

§ make_unique_file()

QString MUtils::make_unique_file ( const QString &  basePath,
const QString &  baseName,
const QString &  extension,
const bool  fancy = false 
)

Generates a unique file name.

The function generates a unique file name in the specified directory. The function guarantees that the returned file name does not exist yet. If necessary, a counter will be included in the file name in order to ensure its uniqueness.

Parameters
basePathSpecifies the "base" directory where the unique file is supposed to be created. This must be a valid existing directory.
baseNameSpecifies the desired "base" file name of the unqiue file. Do not include a file extension.
extensionSpecifies the desired file extensions of the unqiue file. Do not include a leading dot (.) character.
fancyIf set to true, the file name is generated according to the "<basedir>/<basename> (N).<ext>" pattern; if set to false, the file name is generated according to the "<basedir>/<basename>.XXXX.<ext>" pattern.
Returns
If the function succeeds, it returns a QString holding the full path of the unique file; otherwise it returns a default-constructed QString.

§ next_rand_str()

QString MUtils::next_rand_str ( const bool &  bLong = false)

Generates a random string.

The random string is generated using the same PRNG as the next_rand_u64() function. The random bytes are converted to a hexadecimal string and, if necessary, zero-padded to a toal length of 16 or 32 characters. There is no 0x-prefix included in the result.

Parameters
bLongIf set to true, a "long" random string (32 characters) will be generated; if set to false, a "short" random string (16 characters) is generated.
Returns
The function retruns a QString holding a random hexadecimal string

§ next_rand_u32()

quint32 MUtils::next_rand_u32 ( void  )

Generates a random unsigned 32-Bit value.

The random value is created using a "strong" PRNG of the underlying system, if possible. Otherwise a fallback PRNG is used. It is not required or useful to call srand() or qsrand() prior to using this function. If necessary, the seeding of the PRNG happen automatically on the first call.

Returns
The function retruns a random unsigned 32-Bit value.

§ next_rand_u64()

quint64 MUtils::next_rand_u64 ( void  )

Generates a random unsigned 64-Bit value.

The random value is created using a "strong" PRNG of the underlying system, if possible. Otherwise a fallback PRNG is used. It is not required or useful to call srand() or qsrand() prior to using this function. If necessary, the seeding of the PRNG happen automatically on the first call.

Returns
The function retruns a random unsigned 64-Bit value.

§ parity()

bool MUtils::parity ( quint32  value)

Computes the parity of the given unsigned 32-Bit value.

Parameters
valueThe 32-Bit unsigned value from which the parity is to be computed.
Returns
The function returns true, if the number of 1 bits in the given value is odd; it returns false, if the number of 1 bits in the given value is even.

§ temp_folder()

const QString& MUtils::temp_folder ( void  )

Rerieves the full path of the application's Temp folder.

The application's Temp folder is a unique application-specific folder, intended to store any temporary files that the application may need. It will be created when this function is called for the first time (lazy initialization); subsequent calls are guaranteed to return the same path. Usually the application's Temp folder will be created as a sub-folder of the system's global Temp folder, as indicated by the TMP or TEMP environment variables. However, it may be created at a different place(e.g. in the users Profile directory), if those environment variables don't point to a usable directory. In any case, this function makes sure that the application's Temp folder exists for the whole lifetime of the application and that it is writable. When the application is about to terminate, the application's Temp folder and all files or sub-directories thereof will be removed automatically!

Returns
If the function succeeds, it returns a read-only reference to a QString holding the full path of the application's Temp folder; otherwise a read-only reference to a default-constructed QString is returned.