You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
189 lines
4.3 KiB
189 lines
4.3 KiB
15 years ago
|
/* vi: ts=8 sts=4 sw=4
|
||
|
*
|
||
13 years ago
|
* This file is part of the KDE project, module tdesu.
|
||
15 years ago
|
* Copyright (C) 1999,2000 Geert Jansen <g.t.jansen@stud.tue.nl>
|
||
|
*/
|
||
|
|
||
|
#include <time.h>
|
||
|
#include <assert.h>
|
||
|
|
||
15 years ago
|
#include <tqcstring.h>
|
||
|
#include <tqmap.h>
|
||
|
#include <tqvaluestack.h>
|
||
15 years ago
|
#include <kdebug.h>
|
||
|
|
||
|
#include "repo.h"
|
||
|
|
||
|
|
||
|
Repository::Repository()
|
||
|
{
|
||
|
head_time = (unsigned) -1;
|
||
|
}
|
||
|
|
||
|
|
||
|
Repository::~Repository()
|
||
|
{}
|
||
|
|
||
|
|
||
15 years ago
|
void Repository::add(const TQCString &key, Data_entry &data)
|
||
15 years ago
|
{
|
||
13 years ago
|
RepoIterator it = repo.find(key);
|
||
15 years ago
|
if (it != repo.end())
|
||
|
remove(key);
|
||
|
if (data.timeout == 0)
|
||
|
data.timeout = (unsigned) -1;
|
||
|
else
|
||
|
data.timeout += time(0L);
|
||
|
head_time = QMIN(head_time, data.timeout);
|
||
|
repo.insert(key, data);
|
||
|
}
|
||
|
|
||
15 years ago
|
int Repository::remove(const TQCString &key)
|
||
15 years ago
|
{
|
||
|
if( key.isEmpty() )
|
||
|
return -1;
|
||
|
|
||
13 years ago
|
RepoIterator it = repo.find(key);
|
||
15 years ago
|
if (it == repo.end())
|
||
|
return -1;
|
||
|
it.data().value.fill('x');
|
||
|
it.data().group.fill('x');
|
||
|
repo.remove(it);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
15 years ago
|
int Repository::removeSpecialKey(const TQCString &key)
|
||
15 years ago
|
{
|
||
|
int found = -1;
|
||
|
if ( !key.isEmpty() )
|
||
|
{
|
||
15 years ago
|
TQValueStack<TQCString> rm_keys;
|
||
15 years ago
|
for (RepoCIterator it=repo.begin(); it!=repo.end(); ++it)
|
||
|
{
|
||
13 years ago
|
if ( key.find( static_cast<const char *>(it.data().group) ) == 0 &&
|
||
|
it.key().find( static_cast<const char *>(key) ) >= 0 )
|
||
15 years ago
|
{
|
||
|
rm_keys.push(it.key());
|
||
|
found = 0;
|
||
|
}
|
||
|
}
|
||
|
while (!rm_keys.isEmpty())
|
||
|
{
|
||
|
kdDebug(1205) << "Removed key: " << rm_keys.top() << endl;
|
||
|
remove(rm_keys.pop());
|
||
|
}
|
||
|
}
|
||
|
return found;
|
||
|
}
|
||
|
|
||
15 years ago
|
int Repository::removeGroup(const TQCString &group)
|
||
15 years ago
|
{
|
||
|
int found = -1;
|
||
|
if ( !group.isEmpty() )
|
||
|
{
|
||
15 years ago
|
TQValueStack<TQCString> rm_keys;
|
||
15 years ago
|
for (RepoCIterator it=repo.begin(); it!=repo.end(); ++it)
|
||
|
{
|
||
|
if (it.data().group == group)
|
||
|
{
|
||
|
rm_keys.push(it.key());
|
||
|
found = 0;
|
||
|
}
|
||
|
}
|
||
|
while (!rm_keys.isEmpty())
|
||
|
{
|
||
|
kdDebug(1205) << "Removed key: " << rm_keys.top() << endl;
|
||
|
remove(rm_keys.pop());
|
||
|
}
|
||
|
}
|
||
|
return found;
|
||
|
}
|
||
|
|
||
15 years ago
|
int Repository::hasGroup(const TQCString &group) const
|
||
15 years ago
|
{
|
||
|
if ( !group.isEmpty() )
|
||
|
{
|
||
|
RepoCIterator it;
|
||
|
for (it=repo.begin(); it!=repo.end(); ++it)
|
||
|
{
|
||
|
if (it.data().group == group)
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQCString Repository::findKeys(const TQCString &group, const char *sep ) const
|
||
15 years ago
|
{
|
||
15 years ago
|
TQCString list="";
|
||
15 years ago
|
if( !group.isEmpty() )
|
||
|
{
|
||
|
kdDebug(1205) << "Looking for matching key with group key: " << group << endl;
|
||
|
int pos;
|
||
15 years ago
|
TQCString key;
|
||
15 years ago
|
RepoCIterator it;
|
||
|
for (it=repo.begin(); it!=repo.end(); ++it)
|
||
|
{
|
||
|
if (it.data().group == group)
|
||
|
{
|
||
|
key = it.key().copy();
|
||
|
kdDebug(1205) << "Matching key found: " << key << endl;
|
||
13 years ago
|
pos = key.findRev(sep);
|
||
15 years ago
|
key.truncate( pos );
|
||
|
key.remove(0, 2);
|
||
|
if (!list.isEmpty())
|
||
|
{
|
||
|
// Add the same keys only once please :)
|
||
13 years ago
|
if( !list.contains(static_cast<const char *>(key)) )
|
||
15 years ago
|
{
|
||
|
kdDebug(1205) << "Key added to list: " << key << endl;
|
||
|
list += '\007'; // I do not know
|
||
|
list.append(key);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
list = key;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return list;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQCString Repository::find(const TQCString &key) const
|
||
15 years ago
|
{
|
||
|
if( key.isEmpty() )
|
||
|
return 0;
|
||
|
|
||
13 years ago
|
RepoCIterator it = repo.find(key);
|
||
15 years ago
|
if (it == repo.end())
|
||
|
return 0;
|
||
|
return it.data().value;
|
||
|
}
|
||
|
|
||
|
|
||
|
int Repository::expire()
|
||
|
{
|
||
|
unsigned current = time(0L);
|
||
|
if (current < head_time)
|
||
|
return 0;
|
||
|
|
||
|
unsigned t;
|
||
15 years ago
|
TQValueStack<TQCString> keys;
|
||
15 years ago
|
head_time = (unsigned) -1;
|
||
|
RepoIterator it;
|
||
|
for (it=repo.begin(); it!=repo.end(); ++it)
|
||
|
{
|
||
|
t = it.data().timeout;
|
||
|
if (t <= current)
|
||
|
keys.push(it.key());
|
||
|
else
|
||
|
head_time = QMIN(head_time, t);
|
||
|
}
|
||
|
|
||
|
int n = keys.count();
|
||
|
while (!keys.isEmpty())
|
||
|
remove(keys.pop());
|
||
|
return n;
|
||
|
}
|
||
|
|