'hidden' contacts
[dyskinesia.git] / src / k8sdb / k8sdb.h
blob00344bde75be3cd378e1dccde10af7bb1e93e787
1 /* coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://sam.zoy.org/wtfpl/COPYING for more details.
9 */
10 #ifndef K8SDB_H
11 #define K8SDB_H
13 #include <QByteArray>
14 #include <QFile>
15 #include <QHash>
16 #include <QList>
17 #include <QKeySequence>
18 #include <QSet>
19 #include <QString>
20 #include <QStringList>
23 class K8SDB {
24 public:
25 enum CreateMode {
26 OpenExisting=0,
27 OpenAlways=1,
28 WipeAlways=2
31 enum Type {
32 Invalid=0,
33 Boolean=1,
34 Integer=2, //32-bit signed integer
35 String=3,
36 ByteArray=4,
37 KeySequence=5,
38 MaxKeyType
41 K8SDB (const QString &aPathName, CreateMode cMode=OpenExisting);
42 ~K8SDB ();
44 inline bool isOpen () const { return mDB.isOpen(); }
45 inline QString pathname () const { return mDB.fileName(); }
48 * TypeOverwrite: overwrite existing keys with different type in set()?
49 * default: yes
51 inline bool typeOverwrite () const { return mTypeOverwrite; }
52 inline void setTypeOverwrite (bool tover) { mTypeOverwrite = tover; }
54 bool remove (const QString &name);
56 * remove all keys with names starting from 'name'
58 bool removeAll (const QString &name);
59 bool hasKey (const QString &name) const;
60 Type type (const QString &name);
62 bool set (const QString &name, bool v);
63 inline bool setBool (const QString &name, bool v) { return set(name, v); }
64 bool set (const QString &name, qint32 v);
65 inline bool setInt (const QString &name, qint32 v) { return set(name, v); }
66 bool set (const QString &name, const QString &v);
67 inline bool set (const QString &name, const char *v) { return set(name, QString(v)); }
68 bool set (const QString &name, const QKeySequence &v);
69 bool set (const QString &name, const QByteArray &v);
70 bool set (const QString &name, Type type, const QString &v);
72 bool toBool (const QString &name, bool *ok=0);
73 qint32 toInt (const QString &name, bool *ok=0);
74 QString toString (const QString &name, bool *ok=0);
75 QByteArray toByteArray (const QString &name, bool *ok=0);
76 QKeySequence toKeySequence (const QString &name, bool *ok=0);
78 QString asString (const QString &name, bool *ok=0);
80 QStringList keys () const;
81 QStringList keysPfx (const QString &pfx) const;
83 private:
84 struct tsPageHeader *keyPagePtr (const QString &name);
85 void *keyDataPtr (const QString &name);
87 uchar *updateSettingPtr (const QString &name, int dataSize);
90 * false: can't create key
91 * dup checks must be done prior to calling this method
92 * dataSize have any meaning only for String/KeySequence keys
94 struct tsPageHeader *addKeyNoData (const QString &name, Type type, int dataSize);
95 bool freePage (int idx);
97 void writeInitHeader ();
98 void readInitHeader ();
100 void flushKeys ();
101 void flushKey (const QString &name);
103 QFile mDB;
104 CreateMode mMode;
105 uchar *mMap;
106 struct tsHeader *mHeader;
107 bool mTypeOverwrite;
109 QHash<QString, qint32> mKeys; // key: page index in mPages
110 QList<qint32> mPages; // offset for all the pages
111 QSet<qint32> mFreePages; // indicies for all free pages
116 #endif