[KLF Application][KLF Tools][KLF Backend][KLF Home]
KLatexFormula Project
Classes | Macros | Enumerations | Functions
klfutil.h File Reference
#include <QString>
#include <QStringList>
#include <QUrl>
#include <QMap>
#include <QVariant>
#include <QProgressDialog>
#include <QLabel>
#include <QDomElement>
#include <klfdefs.h>
Include dependency graph for klfutil.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  klfEqualFunc< Value >
 Implements default equality tester with operator==. More...
 
class  klfStrCaseEqualFunc
 implements an equality tester between strings More...
 

Macros

#define KLFTOOLS_INIT   Q_INIT_RESOURCE(klftoolsres)
 

Enumerations

enum  KlfUrlCompareFlag {
  KlfUrlCompareEqual = 0x01, KlfUrlCompareLessSpecific = 0x02, KlfUrlCompareMoreSpecific = 0x04, KlfUrlCompareBaseEqual = 0x08,
  klfUrlCompareFlagIgnoreQueryItemValueCase = 0x1000000
}
 Some relevant values for klfUrlCompare() More...
 

Functions

KLF_EXPORT bool klfEnsureDir (const QString &dir)
 Ensure existence of a directory. More...
 
template<class Key , class Value , class ValCompareFunc >
bool klfMapIsIncludedIn (const QMap< Key, Value > &a, const QMap< Key, Value > &b, ValCompareFunc cfunc=klfEqualFunc< Value >())
 Compares two QMap's for inclusion. More...
 
template<class Key >
bool klfMapIsIncludedIn (const QMap< Key, QString > &a, const QMap< Key, QString > &b, Qt::CaseSensitivity cs)
 Compares two QMap's for inclusion (values QString's) More...
 
KLF_EXPORT uint klfUrlCompare (const QUrl &url1, const QUrl &url2, uint interestFlags=0xffffffff, const QStringList &interestQueryItems=QStringList())
 Compares two URLs and returns some flags as to how they differ. More...
 
KLF_EXPORT bool klfMatch (const QVariant &testForHitCandidateValue, const QVariant &queryValue, Qt::MatchFlags flags, const QString &queryStringCache=QString())
 Generalized value matching. More...
 
KLF_EXPORT QByteArray klfDataToEscaped (const QByteArray &data)
 
KLF_EXPORT QByteArray klfEscapedToData (const QByteArray &escaped)
 
KLF_EXPORT QByteArray klfSaveVariantToText (const QVariant &value, bool saveListAndMapsAsXML=false)
 
KLF_EXPORT QVariant klfLoadVariantFromText (const QByteArray &string, const char *dataTypeName, const char *listOrMapTypeName=NULL)
 
template<class T >
QVariantList klfListToVariantList (const QList< T > &list)
 
template<class T >
QList< T > klfVariantListToList (const QVariantList &vlist)
 
KLF_EXPORT QDomElement klfSaveVariantMapToXML (const QVariantMap &vmap, QDomElement xmlNode)
 Lossless save of full map to XML with type information. More...
 
KLF_EXPORT QVariantMap klfLoadVariantMapFromXML (const QDomElement &xmlNode)
 Load a map saved with klfSaveVariantMapToXML() More...
 
KLF_EXPORT QDomElement klfSaveVariantListToXML (const QVariantList &vlist, QDomElement xmlNode)
 Lossless save of full list to XML with type information. More...
 
KLF_EXPORT QVariantList klfLoadVariantListFromXML (const QDomElement &xmlNode)
 Load a list saved with klfSaveVariantListToXML() More...
 
KLF_EXPORT QString klfPrefixedPath (const QString &path, const QString &reference=QString())
 Returns absolute path to path as seen from reference. More...
 
KLF_EXPORT QString klfUrlLocalFilePath (const QUrl &url)
 

Macro Definition Documentation

#define KLFTOOLS_INIT   Q_INIT_RESOURCE(klftoolsres)

Call this from your main program, so that the klftools resource is initialized.

Definition at line 273 of file klfutil.h.

Enumeration Type Documentation

Some relevant values for klfUrlCompare()

Note
when multiple query items are given with the same key, ONLY the last one is taken into account and the other are ignored. For example, the two following URLs are equal:
    http://host.com/path/to/somewhere/file.php?x=ABC&otherkey=othervalue&x=XYZ    and
    http://host.com/path/to/somewhere/file.php?x=XYZ&otherkey=othervalue
By base URL we mean what remains of the URL when all query items are stripped. This is: scheme, host, port, user info, path, and fragment (ie. anchor). See QUrl doc for more info.
Enumerator
KlfUrlCompareEqual 

Urls are equal. The order of query items may be different, but the same are given with the same values.

KlfUrlCompareLessSpecific 

Urls have same base URL. All query items in url1 are present in url2 with the same values, but although possibly they may be equal, url2 may specify more query items that are not present in url1.

KlfUrlCompareMoreSpecific 

Urls have same base URL. All query items in url2 are present in url1 with the same values, but although possibly they may be equal, url1 may specify more query items that are not present in url2.

KlfUrlCompareBaseEqual 

Urls have same base URL. Query items are ignored.

klfUrlCompareFlagIgnoreQueryItemValueCase 

This is NOT a specific test. It modifies the behavior of klfUrlCompare() by instructing it to compare query item values in a non-case-sensitive manner (query item name (=key) comparisions are always case sensitive). Namely, with this flag set "file:///path?name=value" is equal to "file:///path?name=VALUE" but is not equal to "file:///path?NAME=value".

Definition at line 119 of file klfutil.h.

Function Documentation

KLF_EXPORT QByteArray klfDataToEscaped ( const QByteArray data)

Escapes every character in data that is not in the range 32-126 (included) as \xHH whith HH the hex code of the character. Backslashes are replaced by double-backslashes.

Definition at line 278 of file klfutil.cpp.

References QString::arg(), QByteArray::constData(), and QByteArray::size().

Referenced by klfSaveVariantToText().

KLF_EXPORT bool klfEnsureDir ( const QString dir)

Ensure existence of a directory.

Definition at line 52 of file klfutil.cpp.

References QDir::mkpath(), and QFile::setPermissions().

KLF_EXPORT QByteArray klfEscapedToData ( const QByteArray escaped)

Performs the exact inverse of klfDataToEscaped().

Definition at line 297 of file klfutil.cpp.

References klf_is_hex_char(), QByteArray::mid(), and QByteArray::size().

Referenced by klfLoadVariantFromText().

template<class T >
QVariantList klfListToVariantList ( const QList< T > &  list)
inline

Definition at line 217 of file klfutil.h.

References QList::size().

KLF_EXPORT QVariant klfLoadVariantFromText ( const QByteArray string,
const char *  dataTypeName,
const char *  listOrMapTypeName = NULL 
)

Loads the value stored in string into a variant of data type dataTypeName. The string is parsed and the returned variant will by of the given type name, or invalid if the string could not be parsed.

Note
The data type must be known when loading the value. It may not necessarily exactly be guessed by looking at string.

Example use: to save/store settings values in QSettings in a human-read/writable format.

If dataTypeName is a variant list or map, then please specify the type of the values in the list or map (this function assumes all objects in list or map have the same type). As a special case, you can pass "XML" to load the list or map data with klfLoadVariantListFromXML() or klfLoadVariantMapFromXML(), which enables you to save arbitrary combination of types.

See also klfSaveVariantToText().

Definition at line 832 of file klfutil.cpp.

References QList::begin(), KLFStyle::bg_color, QRegExp::capturedTexts(), QString::compare(), QByteArray::constData(), decaps_list(), decaps_map(), KLFStyle::dpi, QList::end(), KLFStyle::fg_color, QPair::first(), fixed_value, format, QString::fromLatin1(), QString::fromLocal8Bit(), QDateTime::fromString(), QDate::fromString(), QTime::fromString(), QString::fromUtf8(), QRegExp::indexIn(), QByteArray::indexOf(), QByteArray::isEmpty(), QString::isEmpty(), QList::isEmpty(), QChar::isSpace(), QTime::isValid(), QDateTime::isValid(), QDate::isValid(), key, keyword, klf_brush_styles, KLF_DEBUG_TIME_BLOCK, KLF_FUNC_NAME, klf_is_hex_char(), klf_text_format_formats, klf_text_format_keywords, klf_text_format_props, klfDbg, klfEscapedToData(), klfLoadVariantFromText(), klfLoadVariantListFromXML(), klfLoadVariantMapFromXML(), KLFStyle::mathmode, QByteArray::mid(), KLFStyle::name, QVariant::nameToType(), parse_xml_wrapper(), QRegExp::pattern(), KLFStyle::preamble, propId, QByteArray::remove(), RX_COORD_SEP, RX_INT, RX_SIZE_SEP, QColor::setAlpha(), QRegExp::setMinimal(), QColor::setNamedColor(), QTextFormat::setProperty(), QFont::setStyle(), QByteArray::size(), QList::size(), QByteArray::startsWith(), QString::startsWith(), style, QList::takeFirst(), QByteArray::toDouble(), QByteArray::toInt(), QVariant::toInt(), QString::toInt(), QByteArray::toLong(), QByteArray::toLongLong(), QVariant::toMap(), QByteArray::toShort(), QVariant::toString(), QByteArray::toUInt(), QByteArray::toULong(), QByteArray::toULongLong(), QByteArray::toUShort(), QByteArray::trimmed(), type, and QVariant::value().

Referenced by klfLoadVariantFromText(), klfLoadVariantListFromXML(), and klfLoadVariantMapFromXML().

KLF_EXPORT QVariantList klfLoadVariantListFromXML ( const QDomElement xmlNode)
KLF_EXPORT QVariantMap klfLoadVariantMapFromXML ( const QDomElement xmlNode)
template<class Key , class Value , class ValCompareFunc >
bool klfMapIsIncludedIn ( const QMap< Key, Value > &  a,
const QMap< Key, Value > &  b,
ValCompareFunc  cfunc = klfEqualFunc<Value>() 
)
inline

Compares two QMap's for inclusion.

returns TRUE if all keys in a are present in b, with same values. It is still possible however that map b contains more keys than a.

This function uses a general value comparer helper, cfunc. You can give for example klfEqualFunc or klfStrCaseEqualFunc.

Definition at line 81 of file klfutil.h.

References QMap::begin(), QMap::contains(), QMap::end(), QMap::key(), and QMap::value().

Referenced by klfMapIsIncludedIn(), and klfUrlCompare().

template<class Key >
bool klfMapIsIncludedIn ( const QMap< Key, QString > &  a,
const QMap< Key, QString > &  b,
Qt::CaseSensitivity  cs 
)
inline

Compares two QMap's for inclusion (values QString's)

Same as klfMapIsIncludedIn(const QMap<Key,Value>&, const QMap<Key,Value>&, ValCompareFunc), except that values have to be QStrings, and that the value comparision is done by comparing strings for equality, with case sensitivity cs.

Definition at line 100 of file klfutil.h.

References klfMapIsIncludedIn().

KLF_EXPORT bool klfMatch ( const QVariant testForHitCandidateValue,
const QVariant queryValue,
Qt::MatchFlags  flags,
const QString queryStringCache = QString() 
)

Generalized value matching.

This function tests to see if the value testForHitCandidateValue matches the value queryValue according to the match flags flags.

If you call this function repeatedly with the same queryValue, the query value may be converted (unnecessarily) repeatedly to a string with queryValue.toString(). To optimize this, you may cache that string and pass each time the string representation for the queryValue as parameter to queryStringCache. If however a null string is passed, the conversion is performed automatically.

Definition at line 156 of file klfutil.cpp.

References QString::compare(), QString::contains(), QString::endsWith(), QString::isNull(), QString::startsWith(), and QVariant::toString().

KLF_EXPORT QString klfPrefixedPath ( const QString path,
const QString reference = QString() 
)

Returns absolute path to path as seen from reference.

If path is absolute, then path is returned as is. Otherwise, an absolute path constructed by concatenating path to reference is returned.

If reference is empty, then the reference is considered to be the application's location, see QCoreApplication::applicationDirPath().

Definition at line 1561 of file klfutil.cpp.

References QCoreApplication::applicationDirPath(), QString::endsWith(), QString::isEmpty(), KLF_DEBUG_TEE, and klfDbg.

KLF_EXPORT QDomElement klfSaveVariantListToXML ( const QVariantList &  vlist,
QDomElement  xmlNode 
)
KLF_EXPORT QDomElement klfSaveVariantMapToXML ( const QVariantMap &  vmap,
QDomElement  xmlNode 
)
KLF_EXPORT QByteArray klfSaveVariantToText ( const QVariant value,
bool  saveListAndMapsAsXML = false 
)

Saves the variant value into a string, stored in Local 8-bit encoding text in QByteArray. The saved string is both human and machine readable, ie. the exact string can be recast again to a variant with klfLoadVariantFromText().

This function is aware of various QVariant formats, however maybe not all of them. The unknown formats are stored machine-readable only, by sending the variant in a datastream, and protecting the special characters from encoding artifacts (ascii chars only, proper escape sequences).

If saveListAndMapsAsXML is FALSE (the default), then variant-lists and -maps are saved in a format like "[element-1,element-2,...]" or "{key1=value1,key2=value2,....}", assuming that all elements are of the same QVariant type. If saveListAndMapsAsXML is TRUE, then variant lists and maps are saved with klfSaveVariantListToXML() and klfSaveVariantMapToXML(), which enables you to save arbitrary combination of types.

Note
Note however that the saved string does NOT save the type. The data type must be known when loading the value. See klfLoadVariantFromText().

Definition at line 519 of file klfutil.cpp.

References QColor::alpha(), QList::append(), QString::arg(), QMap::begin(), KLFStyle::bg_color, QColor::blue(), brushStyle, QTextCodec::canEncode(), QTextCodec::codecForLocale(), QBrush::color(), QByteArray::constData(), KLFStyle::dpi, encaps_list(), encaps_map(), QMap::end(), QFont::family(), KLFStyle::fg_color, fixed_value, format, formatId, QTextCodec::fromUnicode(), QVariant::fromValue(), QColor::green(), QRect::height(), QSize::height(), QByteArray::isNull(), QVariant::isNull(), QVariant::isValid(), key, klf_brush_styles, KLF_FUNC_NAME, klf_text_format_formats, klf_text_format_keywords, klf_text_format_props, klfDataToEscaped(), klfDbg, klfSaveVariantListToXML(), klfSaveVariantMapToXML(), klfSaveVariantToText(), QRect::left(), QString::length(), make_xml_wrapper(), KLFStyle::mathmode, QBrush::matrix(), QString::mid(), KLFStyle::name, QString::number(), KLFStyle::preamble, QTextFormat::properties(), propId, QColor::red(), QString::replace(), QList::size(), QByteArray::size(), QByteArray::startsWith(), QFont::style(), style, QVariant::toBool(), QVariant::toChar(), QVariant::toList(), QVariant::toMap(), QRect::top(), QVariant::toPoint(), QVariant::toRect(), QVariant::toSize(), QVariant::toString(), QVariant::toStringList(), QVariant::toUrl(), QTextFormat::type(), QVariant::type(), type, QVariant::typeName(), QChar::unicode(), QVariant::value(), QFont::weight(), QRect::width(), QSize::width(), QPoint::x(), and QPoint::y().

Referenced by klfSaveVariantListToXML(), klfSaveVariantMapToXML(), and klfSaveVariantToText().

KLF_EXPORT uint klfUrlCompare ( const QUrl url1,
const QUrl url2,
uint  interestFlags = 0xffffffff,
const QStringList interestQueryItems = QStringList() 
)

Compares two URLs and returns some flags as to how they differ.

The return value is an binary-OR'ed value of flags given in KlfUrlCompareFlag.

If the interestFlag parameter is set, only the tests that are given in interestFlags are performed. The returned flags are those flags set in interestFlags that are true.

If the interestQueryItems is set, all query items other than those specified in interestQueryItems are ignored. If interestQueryItems is an empty list, no query items are ignored, they are all taken into account.

Definition at line 89 of file klfutil.cpp.

References KLF_DEBUG_BLOCK, KLF_FUNC_NAME, klf_url_query_items_map(), klfDbg, klfMapIsIncludedIn(), KlfUrlCompareBaseEqual, KlfUrlCompareEqual, klfUrlCompareFlagIgnoreQueryItemValueCase, KlfUrlCompareLessSpecific, KlfUrlCompareMoreSpecific, and QUrl::setQueryItems().

KLF_EXPORT QString klfUrlLocalFilePath ( const QUrl url)

Returns the file path represented in url, interpreted as an (absolute) path to a local file.

On windows, this ensures that there is no slash preceeding the drive letter, eg. fixes "/C:/..." to "C:/...", but keeps forward-slashes.

Definition at line 1583 of file klfutil.cpp.

References QString::mid(), QUrl::path(), and QString::startsWith().

template<class T >
QList<T> klfVariantListToList ( const QVariantList &  vlist)
inline

Definition at line 229 of file klfutil.h.

References QList::value().


Generated by doxygen 1.8.6