KDESu

repo.cpp
1/* vi: ts=8 sts=4 sw=4
2
3 This file is part of the KDE project, module kdesu.
4 SPDX-FileCopyrightText: 1999, 2000 Geert Jansen <g.t.jansen@stud.tue.nl>
5*/
6
7#include "repo.h"
8
9#include <ksud_debug.h>
10
11#include <assert.h>
12#include <time.h>
13
14#include <QStack>
15
16Repository::Repository()
17{
18 head_time = (unsigned)-1;
19}
20
21Repository::~Repository()
22{
23}
24
25void Repository::add(const QByteArray &key, Data_entry &data)
26{
27 RepoIterator it = repo.find(key);
28 if (it != repo.end()) {
29 remove(key);
30 }
31 if (data.timeout == 0) {
32 data.timeout = (unsigned)-1;
33 } else {
34 data.timeout += time(nullptr);
35 }
36 head_time = qMin(head_time, data.timeout);
37 repo.insert(key, data);
38}
39
41{
42 if (key.isEmpty()) {
43 return -1;
44 }
45
46 RepoIterator it = repo.find(key);
47 if (it == repo.end()) {
48 return -1;
49 }
50 it.value().value.fill('x');
51 it.value().group.fill('x');
52 repo.erase(it);
53 return 0;
54}
55
57{
58 int found = -1;
59 if (!key.isEmpty()) {
60 QStack<QByteArray> rm_keys;
61 for (RepoCIterator it = repo.constBegin(); it != repo.constEnd(); ++it) {
62 if (key.indexOf(it.value().group) == 0 && it.key().indexOf(key) >= 0) {
63 rm_keys.push(it.key());
64 found = 0;
65 }
66 }
67 while (!rm_keys.isEmpty()) {
68 qCDebug(KSUD_LOG) << "Removed key: " << rm_keys.top();
69 remove(rm_keys.pop());
70 }
71 }
72 return found;
73}
74
76{
77 int found = -1;
78 if (!group.isEmpty()) {
79 QStack<QByteArray> rm_keys;
80 for (RepoCIterator it = repo.constBegin(); it != repo.constEnd(); ++it) {
81 if (it.value().group == group) {
82 rm_keys.push(it.key());
83 found = 0;
84 }
85 }
86 while (!rm_keys.isEmpty()) {
87 qCDebug(KSUD_LOG) << "Removed key: " << rm_keys.top();
88 remove(rm_keys.pop());
89 }
90 }
91 return found;
92}
93
94int Repository::hasGroup(const QByteArray &group) const
95{
96 if (!group.isEmpty()) {
97 RepoCIterator it;
98 for (it = repo.begin(); it != repo.end(); ++it) {
99 if (it.value().group == group) {
100 return 0;
101 }
102 }
103 }
104 return -1;
105}
106
107QByteArray Repository::findKeys(const QByteArray &group, const char *sep) const
108{
109 QByteArray list = "";
110 if (!group.isEmpty()) {
111 qCDebug(KSUD_LOG) << "Looking for matching key with group key: " << group;
112 int pos;
113 QByteArray key;
114 RepoCIterator it;
115 for (it = repo.begin(); it != repo.end(); ++it) {
116 if (it.value().group == group) {
117 key = it.key();
118 qCDebug(KSUD_LOG) << "Matching key found: " << key;
119 pos = key.lastIndexOf(sep);
120 key.truncate(pos);
121 key.remove(0, 2);
122 if (!list.isEmpty()) {
123 // Add the same keys only once please :)
124 if (!list.contains(key)) {
125 qCDebug(KSUD_LOG) << "Key added to list: " << key;
126 list += '\007'; // I do not know
127 list.append(key);
128 }
129 } else {
130 list = key;
131 }
132 }
133 }
134 }
135 return list;
136}
137
139{
140 if (key.isEmpty()) {
141 return nullptr;
142 }
143
144 RepoCIterator it = repo.find(key);
145 if (it == repo.end()) {
146 return nullptr;
147 }
148 return it.value().value;
149}
150
152{
153 unsigned current = time(nullptr);
154 if (current < head_time) {
155 return 0;
156 }
157
158 unsigned t;
160 head_time = (unsigned)-1;
161 RepoIterator it;
162 for (it = repo.begin(); it != repo.end(); ++it) {
163 t = it.value().timeout;
164 if (t <= current) {
165 keys.push(it.key());
166 } else {
167 head_time = qMin(head_time, t);
168 }
169 }
170
171 int n = keys.count();
172 while (!keys.isEmpty()) {
173 remove(keys.pop());
174 }
175 return n;
176}
void add(const QByteArray &key, Data_entry &data)
Add a data element.
Definition repo.cpp:25
int remove(const QByteArray &key)
Delete a data element.
Definition repo.cpp:40
int expire()
Remove data elements which are expired.
Definition repo.cpp:151
QByteArray findKeys(const QByteArray &group, const char *sep="-") const
Returns the key values for the given group.
Definition repo.cpp:107
QByteArray find(const QByteArray &key) const
Return a data value.
Definition repo.cpp:138
int removeGroup(const QByteArray &group)
Delete all data entries having the given group.
Definition repo.cpp:75
int removeSpecialKey(const QByteArray &key)
Delete all data entries based on key.
Definition repo.cpp:56
int hasGroup(const QByteArray &group) const
Checks for the existence of the specified group.
Definition repo.cpp:94
qsizetype indexOf(QByteArrayView bv, qsizetype from) const const
bool isEmpty() const const
qsizetype lastIndexOf(QByteArrayView bv) const const
QByteArray & remove(qsizetype pos, qsizetype len)
void truncate(qsizetype pos)
void append(QList< T > &&value)
qsizetype count() const const
bool isEmpty() const const
iterator begin()
const_iterator constBegin() const const
const_iterator constEnd() const const
iterator end()
iterator erase(const_iterator first, const_iterator last)
iterator find(const Key &key)
iterator insert(const Key &key, const T &value)
void push(const T &t)
T & top()
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
Used internally.
Definition repo.h:16
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:38 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.