/usr/include/KDb3/KDbUtils.h is in libkdb3-dev 3.1.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 | /* This file is part of the KDE project
Copyright (C) 2003-2016 Jarosław Staniek <staniek@kde.org>
Portions of kstandarddirs.cpp:
Copyright (C) 1999 Sirtaj Singh Kang <taj@kde.org>
Copyright (C) 1999,2007 Stephan Kulow <coolo@kde.org>
Copyright (C) 1999 Waldo Bastian <bastian@kde.org>
Copyright (C) 2009 David Faure <faure@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef KDB_TOOLS_UTILS_H
#define KDB_TOOLS_UTILS_H
#include "kdb_export.h"
#include "config-kdb.h"
#include <QObject>
#include <QDateTime>
#include <QMetaMethod>
#include <QDebug>
namespace KDbUtils
{
//! @return true if @a o has parent @a par (checks recursively)
KDB_EXPORT bool hasParent(QObject* par, QObject* o);
//! @return parent object of @a o that is of type @a type or @c nullptr if no such parent
template<class type>
inline type findParent(QObject* o, const char* className = nullptr)
{
if (!o)
return nullptr;
while ((o = o->parent())) {
if (::qobject_cast< type >(o) && (!className || o->inherits(className)))
return ::qobject_cast< type >(o);
}
return nullptr;
}
//! QDateTime - a hack needed because QVariant(QTime) has broken isNull()
KDB_EXPORT QDateTime stringToHackedQTime(const QString& s);
/*! Serializes @a map to the array pointed by @a array.
KDbUtils::deserializeMap() can be used to deserialize this array back to map.
Does nothing if @a array is @c nullptr. */
KDB_EXPORT void serializeMap(const QMap<QString, QString>& map, QByteArray *array);
/*! Serializes @a map to the string pointed by @a string.
KDbUtils::deserializeMap() can be used to deserialize this array back to map.
Does nothing if @a string is @c nullptr. */
KDB_EXPORT void serializeMap(const QMap<QString, QString>& map, QString *string);
/*! @return a map deserialized from a byte array @a array.
@a array need to contain data previously serialized using KexiUtils::serializeMap(). */
KDB_EXPORT QMap<QString, QString> deserializeMap(const QByteArray& array);
/*! @return a map deserialized from @a string.
@a string need to contain data previously serialized using KexiUtils::serializeMap(). */
KDB_EXPORT QMap<QString, QString> deserializeMap(const QString& string);
/*! @return a valid filename converted from @a string by:
- replacing \\, /, :, *, ?, ", <, >, |, \n \\t characters with a space
- simplifing whitespace by removing redundant space characters using QString::simplified()
Do not pass full paths here, but only filename strings. */
KDB_EXPORT QString stringToFileName(const QString& string);
/*! Performs a simple @a string encryption using rot47-like algorithm.
Each character's unicode value is increased by 47 + i (where i is index of the character).
The resulting string still contains readable characters but some of them can be non-ASCII.
Does nothing if @a string is @c nullptr.
@note Do not use this for data that can be accessed by attackers! */
KDB_EXPORT void simpleCrypt(QString *string);
/*! Performs a simple @a string decryption using rot47-like algorithm,
using opposite operations to KexiUtils::simpleCrypt().
@return true on success and false on failure. Failue means that one or more characters have unicode
numbers smaller than value of 47 + i. On failure @a string is not altered.
Does nothing and returns @c false if @a string is @c nullptr.
*/
KDB_EXPORT bool simpleDecrypt(QString *string);
//! @internal
KDB_EXPORT QString pointerToStringInternal(void* pointer, int size);
//! @internal
KDB_EXPORT void* stringToPointerInternal(const QString& string, int size);
//! @return a pointer @a pointer safely serialized to string
template<class type>
QString pointerToString(type *pointer)
{
return pointerToStringInternal(pointer, sizeof(type*));
}
//! @return a pointer of type @a type safely deserialized from @a string
template<class type>
type* stringToPointer(const QString& string)
{
return static_cast<type*>(stringToPointerInternal(string, sizeof(type*)));
}
//! @return value converted to text, squeezed to reasonable length, useful for debugging
//! If the value is not a byte array or string, or if it's not longer than 1024 characters,
//! @a value is returned.
//! @since 3.1
KDB_EXPORT QVariant squeezedValue(const QVariant &value);
//! @short Autodeleting hash
template <class Key, class T>
class AutodeletedHash : public QHash<Key, T>
{
public:
//! Creates autodeleting hash as a copy of @a other.
//! Auto-deletion is not enabled as it would cause double deletion for items.
//! If you enable auto-deletion on here, make sure you disable it in the @a other hash.
AutodeletedHash(const AutodeletedHash& other) : QHash<Key, T>(other), m_autoDelete(false) {}
//! Creates empty autodeleting hash.
//! Auto-deletion is enabled by default.
AutodeletedHash(bool autoDelete = true) : QHash<Key, T>(), m_autoDelete(autoDelete) {}
~AutodeletedHash() {
if (m_autoDelete) {
qDeleteAll(*this);
}
}
void setAutoDelete(bool set) {
m_autoDelete = set;
}
bool autoDelete() const {
return m_autoDelete;
}
void clear() {
if (m_autoDelete) {
qDeleteAll(*this);
}
QHash<Key, T>::clear();
}
typename QHash<Key, T>::iterator erase(typename QHash<Key, T>::iterator pos) {
typename QHash<Key, T>::iterator it = QHash<Key, T>::erase(pos);
if (m_autoDelete) {
delete it.value();
it.value() = 0;
return it;
}
}
typename QHash<Key, T>::iterator insert(const Key &key, const T &value) {
if (m_autoDelete) {
T &oldValue = QHash<Key, T>::operator[](key);
if (oldValue && oldValue != value) { // only delete if differs
delete oldValue;
}
}
return QHash<Key, T>::insert(key, value);
}
int remove(const Key &key) {
if (m_autoDelete) {
const QList<T> values(QHash<Key, T>::values(key));
const int result = QHash<Key, T>::remove(key);
for (T item : values) {
delete item;
}
return result;
} else {
return QHash<Key, T>::remove(key);
}
}
// note: no need to override insertMulti(), unite(), take(), they do not replace items
private:
bool m_autoDelete;
};
//! @short Autodeleting list
template <typename T>
class AutodeletedList : public QList<T>
{
public:
//! Creates autodeleting list as a copy of @a other.
//! Auto-deletion is not enabled as it would cause double deletion for items.
//! If you enable auto-deletion on here, make sure you disable it in the @a other list.
AutodeletedList(const AutodeletedList& other)
: QList<T>(other), m_autoDelete(false) {}
//! Creates empty autodeleting list.
//! Auto-deletion is enabled by default.
AutodeletedList(bool autoDelete = true) : QList<T>(), m_autoDelete(autoDelete) {}
~AutodeletedList() {
if (m_autoDelete) qDeleteAll(*this);
}
void setAutoDelete(bool set) {
m_autoDelete = set;
}
bool autoDelete() const {
return m_autoDelete;
}
void removeAt(int i) {
T item = QList<T>::takeAt(i); if (m_autoDelete) delete item;
}
void removeFirst() {
T item = QList<T>::takeFirst(); if (m_autoDelete) delete item;
}
void removeLast() {
T item = QList<T>::takeLast(); if (m_autoDelete) delete item;
}
void replace(int i, const T& value) {
T item = QList<T>::takeAt(i); insert(i, value); if (m_autoDelete) delete item;
}
void insert(int i, const T& value) {
QList<T>::insert(i, value);
}
typename QList<T>::iterator erase(typename QList<T>::iterator pos) {
T item = *pos;
typename QList<T>::iterator res = QList<T>::erase(pos);
if (m_autoDelete)
delete item;
return res;
}
typename QList<T>::iterator erase(
typename QList<T>::iterator afirst,
typename QList<T>::iterator alast) {
if (!m_autoDelete)
return QList<T>::erase(afirst, alast);
while (afirst != alast) {
T item = *afirst;
afirst = QList<T>::erase(afirst);
delete item;
}
return alast;
}
void pop_back() {
removeLast();
}
void pop_front() {
removeFirst();
}
int removeAll(const T& value) {
if (!m_autoDelete)
return QList<T>::removeAll(value);
typename QList<T>::iterator it(QList<T>::begin());
int removedCount = 0;
while (it != QList<T>::end()) {
if (*it == value) {
T item = *it;
it = QList<T>::erase(it);
delete item;
removedCount++;
} else
++it;
}
return removedCount;
}
void clear() {
if (!m_autoDelete)
return QList<T>::clear();
while (!QList<T>::isEmpty()) {
T item = QList<T>::takeFirst();
delete item;
}
}
private:
bool m_autoDelete;
};
//! @short Case insensitive hash container supporting QString or QByteArray keys.
//! Keys are turned to lowercase before inserting.
template <typename Key, typename T>
class CaseInsensitiveHash : public QHash<Key, T>
{
public:
CaseInsensitiveHash() : QHash<Key, T>() {}
typename QHash<Key, T>::iterator find(const Key& key) const {
return QHash<Key, T>::find(key.toLower());
}
typename QHash<Key, T>::const_iterator constFind(const Key& key) const {
return QHash<Key, T>::constFind(key.toLower());
}
bool contains(const Key& key) const {
return QHash<Key, T>::contains(key.toLower());
}
int count(const Key& key) const {
return QHash<Key, T>::count(key.toLower());
}
typename QHash<Key, T>::iterator insert(const Key& key, const T& value) {
return QHash<Key, T>::insert(key.toLower(), value);
}
typename QHash<Key, T>::iterator insertMulti(const Key& key, const T& value) {
return QHash<Key, T>::insertMulti(key.toLower(), value);
}
const Key key(const T& value, const Key& defaultKey) const {
return QHash<Key, T>::key(value, defaultKey.toLower());
}
int remove(const Key& key) {
return QHash<Key, T>::remove(key.toLower());
}
const T take(const Key& key) {
return QHash<Key, T>::take(key.toLower());
}
const T value(const Key& key) const {
return QHash<Key, T>::value(key.toLower());
}
const T value(const Key& key, const T& defaultValue) const {
return QHash<Key, T>::value(key.toLower(), defaultValue);
}
QList<T> values(const Key& key) const {
return QHash<Key, T>::values(key.toLower());
}
T& operator[](const Key& key) {
return QHash<Key, T>::operator[](key.toLower());
}
const T operator[](const Key& key) const {
return QHash<Key, T>::operator[](key.toLower());
}
};
//! A set created from static (0-terminated) array of raw null-terminated strings.
class KDB_EXPORT StaticSetOfStrings
{
public:
StaticSetOfStrings();
explicit StaticSetOfStrings(const char* const array[]);
~StaticSetOfStrings();
void setStrings(const char* const array[]);
bool isEmpty() const;
//! @return true if @a string can be found within set, comparison is case sensitive
bool contains(const QByteArray& string) const;
private:
class Private;
Private * const d;
Q_DISABLE_COPY(StaticSetOfStrings)
};
/*! @return debugging string for object @a object of type @a T */
template <typename T>
QString debugString(const T& object)
{
QString result;
QDebug dbg(&result);
dbg << object;
return result;
}
//! Used by findExe().
enum class FindExeOption {
//! No options
None = 0,
//! If set, the path returned may not have the executable bit set.
IgnoreExecBit = 1
};
Q_DECLARE_FLAGS(FindExeOptions, FindExeOption)
/**
* Finds the executable in the system path.
*
* A valid executable must be a file and have its executable bit set.
*
* @param appname The name of the executable file for which to search.
* if this contains a path separator, it will be resolved
* according to the current working directory
* (shell-like behavior).
* @param path The path which will be searched. If this is
* null (default), the @c $PATH environment variable will
* be searched.
* @param options Options, see FindExeOption.
*
* @return The path of the executable. If it was not found, returns QString().
*/
QString findExe(const QString& appname,
const QString& path = QString(),
FindExeOptions options = FindExeOption::None);
//! A single property
//! @note This property is general-purpose and not related to Qt Properties.
//! @see KDbUtils::PropertySet
class KDB_EXPORT Property {
public:
//! Constructs a null property
Property();
Property(const QVariant &aValue, const QString &aCaption);
Property(const Property &other);
~Property();
bool operator==(const Property &other) const;
bool operator!=(const Property &other) const { return !operator==(other); }
bool isNull() const;
QVariant value() const;
void setValue(const QVariant &value);
QString caption() const;
void setCaption(const QString &caption);
private:
class Private;
Private * const d;
};
//! A set of properties.
//! @note These properties are general-purpose and not related to Qt Properties.
//! @see KDbUtils::Property
class KDB_EXPORT PropertySet
{
public:
PropertySet();
PropertySet(const PropertySet &other);
~PropertySet();
//! Assigns @a other to this property set and returns a reference to this property set.
PropertySet& operator=(const PropertySet &other);
//! @return true if this property set has exactly the same properties as @a other
//! @since 3.1
bool operator==(const PropertySet &other) const;
//! @return true if this property differs in at least one property from @a other
//! @since 3.1
bool operator!=(const PropertySet &other) const { return !operator==(other); }
//! Inserts property with a given @a name, @a value and @a caption.
//! If @a caption is empty, caption from existing property is reused.
//! @a name must be a valid identifier (see KDb::isIdentifier()).
void insert(const QByteArray &name, const QVariant &value, const QString &caption = QString());
//! Sets caption for property @a name to @a caption.
//! If such property does not exist, does nothing.
//! @since 3.1
void setCaption(const QByteArray &name, const QString &caption);
//! Sets value for property @a name to @a value.
//! If such property does not exist, does nothing.
//! @since 3.1
void setValue(const QByteArray &name, const QVariant &value);
//! Removes property with a given @a name.
void remove(const QByteArray &name);
//! @return property with a given @a name.
//! If not found, a null Property is returned (Property::isNull).
Property property(const QByteArray &name) const;
//! @return a list of property names.
QList<QByteArray> names() const;
private:
class Private;
Private * const d;
};
} // KDbUtils
#endif //KDB_TOOLS_UTILS_H
|