MuldeR's Utilities for Qt
MUtilities
|
This file contains miscellaneous functions that are generally useful for Qt-based applications. More...
#include <QString>
Go to the source code of this file.
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) |
Deletes the specified file. More... | |
bool | MUtils::remove_directory (const QString &folderPath, const bool &recursive) |
Recursively deletes the specified directory. More... | |
QString & | MUtils::trim_right (QString &str) |
Remove trailing white-space characters. More... | |
QString & | MUtils::trim_left (QString &str) |
Remove leading white-space characters. More... | |
QString | MUtils::trim_right (const QString &str) |
Remove trailing white-space characters. More... | |
QString | MUtils::trim_left (const QString &str) |
Remove trailing white-space characters. More... | |
void | MUtils::natural_string_sort (QStringList &list, const bool bIgnoreCase) |
Sort a list of strings using "natural ordering" algorithm. More... | |
QString | MUtils::clean_file_name (const QString &name) |
QString | MUtils::clean_file_path (const QString &path) |
bool | MUtils::regexp_parse_uint32 (const QRegExp ®exp, quint32 &value) |
Parse regular expression results. More... | |
bool | MUtils::regexp_parse_uint32 (const QRegExp ®exp, quint32 *values, const size_t &count) |
Parse regular expression results. More... | |
QStringList | MUtils::available_codepages (const bool &noAliases=true) |
Retrieve a list of all available codepages. More... | |
This file contains miscellaneous functions that are generally useful for Qt-based applications.
QStringList MUtils::available_codepages | ( | const bool & | noAliases = true | ) |
Retrieve a list of all available codepages.
The function generates a list of all codepages that are available on the system. Each codepage name returned by this function may be passed to the QTextCodec::codecForName()
function in order to obtain a corresponding QTextCodec object.
noAliases | If set to true , only distinct codepages are returned, i.e. any codepage aliases are discarded from the list; if set to false , the returned list may (and usually will) also contain codepage aliases. |
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.
basePath | Specifies the "base" directory where the temporary file is supposed to be created. This must be a valid existing directory. |
extension | Specifies the desired file extensions of the temporary file. Do not include a leading dot (. ) character. |
placeholder | If set to true , the function creates an empty "placeholder" file under the returned file name; if set to false , it does not. |
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.
basePath | Specifies the "base" directory where the unique file is supposed to be created. This must be a valid existing directory. |
baseName | Specifies the desired "base" file name of the unique file. Do not include a file extension. |
extension | Specifies the desired file extensions of the unique file. Do not include a leading dot (. ) character. |
fancy | If set to true , the unique file name is generated according to the "<basedir>/<basename> (N).<ext>" pattern; if set to false , it is generated according to the "<basedir>/<basename>.XXXX.<ext>" pattern. Also, if set to true , a counter is only included in the file name, if the file name without counter already exists; if set to false , a counter is always included. Finally, if set to true , the counter starts at 2 and is printed in decimal format; if set to false , the counter starts at 0 and is printed in zero-padded hexadecimal format. |
void MUtils::natural_string_sort | ( | QStringList & | list, |
const bool | bIgnoreCase | ||
) |
Sort a list of strings using "natural ordering" algorithm.
This function implements a sort algorithm that orders alphanumeric strings in the way a human being would. See Natural Order String Comparison for details!
list | A reference to the QStringList object to be sorted. The list will be sorted "in place". |
bIgnoreCase | If set to true , the list will be sorted disregarding the character case, i.e. upper-case and lower-case characters (of the same letter) are treated the same; if set to false , the character case is taken into account. |
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.
bLong | If set to true , a "long" random string (32 characters) will be generated; if set to false , a "short" random string (16 characters) is generated. |
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.
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.
bool MUtils::parity | ( | quint32 | value | ) |
Computes the parity of the given unsigned 32-Bit value.
value | The 32-Bit unsigned value from which the parity is to be computed. |
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. bool MUtils::regexp_parse_uint32 | ( | const QRegExp & | regexp, |
quint32 & | value | ||
) |
Parse regular expression results.
This function tries to parses the result (capture) of a regular expression as an unsigned 32-Bit value. The given regular expression must contain at least one capture. Only the first capture is considered, additional captures are ignored.
regexp | A read-only reference to the QRegExp object whose result (capture) will be parsed. This QRegExp must already have been successfully matched against the respective input string, e.g. via QRegExp::indexIn() , prior to calling this function. |
value | A reference to a variable of type quint32 , where the unsigned 32-Bit representation of the result will be stored. The contents of this variable are undefined, if the function failed. |
true
, if the regular expression's capture could be parsed successfully; it returns false
, if the capture contains an invalid string or if there are insufficient captures in given the QRegExp object. bool MUtils::regexp_parse_uint32 | ( | const QRegExp & | regexp, |
quint32 * | values, | ||
const size_t & | count | ||
) |
Parse regular expression results.
This function tries to parses the results (captures) of a regular expression as unsigned 32-Bit values. The given regular expression must contain at least count
captures. Only the first count
captures are considered, additional captures are ignored.
regexp | A read-only reference to the QRegExp object whose results (captures) will be parsed. This QRegExp must already have been successfully matched against the respective input string, e.g. via QRegExp::indexIn() , prior to calling this function. |
value | A pointer to an array of type quint32 , where the unsigned 32-Bit representations of the results will be stored (the n -th result is stored at value[n-1] ). The array must be at least count elements in length. The contents of this array are undefined, if the function failed. |
count | Specifies the number of results (captures) in the given QRegExp object. The function tries to parse the first count captures and ignores any additional captures that may exist. This parameter also determines the required (minimum) length of the value array. |
true
, if all of the regular expression's captures could be parsed successfully; it returns false
, if any of the captures contain an invalid string or if there are insufficient captures in given the QRegExp object. bool MUtils::remove_directory | ( | const QString & | folderPath, |
const bool & | recursive | ||
) |
Recursively deletes the specified directory.
The function deletes the specified directory. In recursive mode, the directory will be removed including all of its files and sub-directories. Files are deleted using the remove_file()
function.
folderPath | The path to the directory to be deleted. This should be a full path. |
recursive | If set to true the function removes all files and sub-directories in the specified directory; if set to false , the function will not try to delete any files or sub-directories, which means that it will fail on non-empty directories. |
true
, if the directory was deleted successfully or if the directory doesn't exist; it returns false
, if the directory could not be deleted. bool MUtils::remove_file | ( | const QString & | fileName | ) |
Deletes the specified file.
The function deletes the specified file, even if it has the "read only" flag set. If the file is currently locked (e.g. by another process), the function retries multiple times to delete the file before it fails.
fileName | The path to the file to be deleted. This should be a full path. |
true
, if the file was deleted successfully or if the file doesn't exist; it returns false
, if the file could not be deleted. 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 terminates normally, the application's Temp folder and all files or sub-directories thereof will be removed automatically!
QString& MUtils::trim_left | ( | QString & | str | ) |
Remove leading white-space characters.
The function removes all leading white-space characters from the specified string. Trailing white-space characters are not removed. White-space characters are defined by the \s
character class.
str | A reference to the QString object to be trimmed. This QString object will be modified directly. |
str
parameter. QString MUtils::trim_left | ( | const QString & | str | ) |
Remove trailing white-space characters.
The function removes all leading white-space characters from the specified string. Trailing white-space characters are not removed. White-space characters are defined by the \s
character class.
str | A read-only reference to the QString object to be trimmed. The original QString object is not modified. |
QString& MUtils::trim_right | ( | QString & | str | ) |
Remove trailing white-space characters.
The function removes all trailing white-space characters from the specified string. Leading white-space characters are not removed. White-space characters are defined by the \s
character class.
str | A reference to the QString object to be trimmed. This QString object will be modified directly. |
str
parameter. QString MUtils::trim_right | ( | const QString & | str | ) |
Remove trailing white-space characters.
The function removes all trailing white-space characters from the specified string. Leading white-space characters are not removed. White-space characters are defined by the \s
character class.
str | A read-only reference to the QString object to be trimmed. The original QString object is not modified. |