return err_needmoreparams(client, command), sendbuf(client), 0;
if (!strcmp(args[0], "0")) {
struct trie_find *f = NULL;
- if (trie_findfirst(&f, &client->channels) == 1)
- do {
- struct channel *chan = f->value;
- channel_part(client, chan, "I fell for a classic prank", NULL);
- } while(trie_findfirst(&f, &client->channels) == 1);
+ while (trie_findfirst(&f, &client->channels) == 1) {
+ struct channel *chan = f->value;
+ channel_part(client, chan, "I fell for a classic prank", NULL);
+ }
free(f);
return 0;
}
tls_free(tls_ctx);
#endif
free(databuf.buf);
- /* free any persisten channels */
+ /* free any persistent channels */
{
struct trie_find *f = NULL;
while (trie_findfirst(&f, &channel_map) == 1) {
root->ptr = 0;
root->len = root->cap = 0;
}
-static int fill(struct trie_find **pf, int index)
-{
- struct trie_find *f = *pf;
- int keylen = strlen(f->_root->ptr[index].key)+1;
- if (keylen > f->_cap) {
- while (keylen > f->_cap)
- f->_cap *= 2;
- struct trie_find *nf = realloc(f, sizeof(struct trie_find)+f->_cap);
- if (!nf) {
- *pf = 0;
- free(f);
- return -1;
- }
- *pf = f = nf;
- f->key = (char*)&f[1];
- }
- memcpy(f->key, f->_root->ptr[index].key, keylen);
- f->value = f->_root->ptr[index].value;
- return 1;
-}
int trie_findfirst(struct trie_find **pf, map_t *root)
{
if (!root->len)
return 0;
struct trie_find *f;
if (!*pf) {
- f = malloc(sizeof(struct trie_find)+32);
+ f = malloc(sizeof(struct trie_find));
if (!f)
return -1;
- f->key = (char*)&f[1];
- f->key[0] = 0;
- f->_cap = 32;
*pf = f;
} else {
f = *pf;
}
f->_root = root;
- return fill(pf, 0);
+ f->_index = -1;
+ return trie_findnext(pf);
}
int trie_findnext(struct trie_find **pf)
{
struct trie_find *f = *pf;
- int found;
- int i = ent_find(f->_root, f->key, &found);
- i += found;
- if (f->_root->len == (size_t)i) {
+ f->_index++;
+ if (f->_index == f->_root->len) {
*pf = 0;
free(f);
return 0;
}
- return fill(pf, i);
+ f->key = f->_root->ptr[f->_index].key;
+ f->value = f->_root->ptr[f->_index].value;
+ return 1;
}