Set pointer to Entry in Cursor

This feels a little redundant but the API makes sense, I think?
weechat-hashes
June McEnroe 2022-09-11 17:28:19 -04:00
parent b5bd702c3c
commit d043bad63d
3 changed files with 86 additions and 67 deletions

133
cache.c
View File

@ -107,15 +107,15 @@ static struct Node *insert(bool touch, uint id, const char *key) {
} }
} }
struct Entry *cacheInsert(bool touch, uint id, const char *key) {
return &insert(touch, id, key)->entry;
}
const struct Entry *cacheGet(uint id, const char *key) { const struct Entry *cacheGet(uint id, const char *key) {
struct Node *node = find(id, key); struct Node *node = find(id, key);
return (node ? &node->entry : &DefaultEntry); return (node ? &node->entry : &DefaultEntry);
} }
struct Entry *cacheInsert(bool touch, uint id, const char *key) {
return &insert(touch, id, key)->entry;
}
void cacheReplace(bool touch, const char *old, const char *new) { void cacheReplace(bool touch, const char *old, const char *new) {
struct Node *next = NULL; struct Node *next = NULL;
for (struct Node *node = head; node; node = next) { for (struct Node *node = head; node; node = next) {
@ -128,60 +128,6 @@ void cacheReplace(bool touch, const char *old, const char *new) {
} }
} }
const char *cacheComplete(struct Cursor *curs, uint id, const char *prefix) {
size_t len = strlen(prefix);
if (curs->gen != gen) curs->node = NULL;
for (
curs->gen = gen, curs->node = (curs->node ? curs->node->next : head);
curs->node;
curs->node = curs->node->next
) {
if (curs->node->id && curs->node->id != id) continue;
if (strncasecmp(curs->node->key, prefix, len)) continue;
return curs->node->key;
}
return NULL;
}
const char *cacheSearch(struct Cursor *curs, uint id, const char *substr) {
if (curs->gen != gen) curs->node = NULL;
for (
curs->gen = gen, curs->node = (curs->node ? curs->node->next : head);
curs->node;
curs->node = curs->node->next
) {
if (curs->node->id && curs->node->id != id) continue;
if (!strstr(curs->node->key, substr)) continue;
return curs->node->key;
}
return NULL;
}
uint cacheID(struct Cursor *curs, const char *key) {
if (curs->gen != gen) curs->node = NULL;
for (
curs->gen = gen, curs->node = (curs->node ? curs->node->next : head);
curs->node;
curs->node = curs->node->next
) {
if (!curs->node->id) continue;
if (strcmp(curs->node->key, key)) continue;
return curs->node->id;
}
return None;
}
void cacheAccept(struct Cursor *curs) {
if (curs->gen == gen && curs->node) {
prepend(detach(curs->node));
}
curs->node = NULL;
}
void cacheReject(struct Cursor *curs) {
curs->node = NULL;
}
void cacheRemove(uint id, const char *key) { void cacheRemove(uint id, const char *key) {
gen++; gen++;
struct Node *next = NULL; struct Node *next = NULL;
@ -207,3 +153,74 @@ void cacheClear(uint id) {
free(node); free(node);
} }
} }
const char *cacheComplete(struct Cursor *curs, uint id, const char *prefix) {
size_t len = strlen(prefix);
if (curs->gen != gen) curs->node = NULL;
for (
curs->gen = gen, curs->node = (curs->node ? curs->node->next : head);
curs->node;
curs->node = curs->node->next
) {
if (curs->node->id && curs->node->id != id) continue;
if (strncasecmp(curs->node->key, prefix, len)) continue;
curs->entry = &curs->node->entry;
return curs->node->key;
}
return NULL;
}
const char *cacheSearch(struct Cursor *curs, uint id, const char *substr) {
if (curs->gen != gen) curs->node = NULL;
for (
curs->gen = gen, curs->node = (curs->node ? curs->node->next : head);
curs->node;
curs->node = curs->node->next
) {
if (curs->node->id && curs->node->id != id) continue;
if (!strstr(curs->node->key, substr)) continue;
curs->entry = &curs->node->entry;
return curs->node->key;
}
return NULL;
}
const char *cacheNextKey(struct Cursor *curs, uint id) {
if (curs->gen != gen) curs->node = NULL;
for (
curs->gen = gen, curs->node = (curs->node ? curs->node->next : head);
curs->node;
curs->node = curs->node->next
) {
if (curs->node->id != id) continue;
curs->entry = &curs->node->entry;
return curs->node->key;
}
return NULL;
}
uint cacheNextID(struct Cursor *curs, const char *key) {
if (curs->gen != gen) curs->node = NULL;
for (
curs->gen = gen, curs->node = (curs->node ? curs->node->next : head);
curs->node;
curs->node = curs->node->next
) {
if (!curs->node->id) continue;
if (strcmp(curs->node->key, key)) continue;
curs->entry = &curs->node->entry;
return curs->node->id;
}
return None;
}
void cacheAccept(struct Cursor *curs) {
if (curs->gen == gen && curs->node) {
prepend(detach(curs->node));
}
curs->node = NULL;
}
void cacheReject(struct Cursor *curs) {
curs->node = NULL;
}

14
chat.h
View File

@ -403,17 +403,19 @@ struct Entry {
struct Cursor { struct Cursor {
uint gen; uint gen;
struct Node *node; struct Node *node;
struct Entry *entry;
}; };
struct Entry *cacheInsert(bool touch, uint id, const char *key);
const struct Entry *cacheGet(uint id, const char *key); const struct Entry *cacheGet(uint id, const char *key);
struct Entry *cacheInsert(bool touch, uint id, const char *key);
void cacheReplace(bool touch, const char *old, const char *new); void cacheReplace(bool touch, const char *old, const char *new);
const char *cacheComplete(struct Cursor *curs, uint id, const char *prefix);
const char *cacheSearch(struct Cursor *curs, uint id, const char *substr);
uint cacheID(struct Cursor *curs, const char *key);
void cacheAccept(struct Cursor *curs);
void cacheReject(struct Cursor *curs);
void cacheRemove(uint id, const char *key); void cacheRemove(uint id, const char *key);
void cacheClear(uint id); void cacheClear(uint id);
const char *cacheComplete(struct Cursor *curs, uint id, const char *prefix);
const char *cacheSearch(struct Cursor *curs, uint id, const char *substr);
uint cacheNextID(struct Cursor *curs, const char *key);
const char *cacheNextKey(struct Cursor *curs, uint id);
void cacheAccept(struct Cursor *curs);
void cacheReject(struct Cursor *curs);
extern struct Util urlOpenUtil; extern struct Util urlOpenUtil;
extern struct Util urlCopyUtil; extern struct Util urlCopyUtil;

View File

@ -459,7 +459,7 @@ static void handleNick(struct Message *msg) {
inputUpdate(); inputUpdate();
} }
struct Cursor curs = {0}; struct Cursor curs = {0};
for (uint id; (id = cacheID(&curs, msg->nick));) { for (uint id; (id = cacheNextID(&curs, msg->nick));) {
if (!strcmp(idNames[id], msg->nick)) { if (!strcmp(idNames[id], msg->nick)) {
set(&idNames[id], msg->params[0]); set(&idNames[id], msg->params[0]);
} }
@ -480,7 +480,7 @@ static void handleNick(struct Message *msg) {
static void handleSetname(struct Message *msg) { static void handleSetname(struct Message *msg) {
require(msg, true, 1); require(msg, true, 1);
struct Cursor curs = {0}; struct Cursor curs = {0};
for (uint id; (id = cacheID(&curs, msg->nick));) { for (uint id; (id = cacheNextID(&curs, msg->nick));) {
uiFormat( uiFormat(
id, filterCheck(Cold, id, msg), tagTime(msg), id, filterCheck(Cold, id, msg), tagTime(msg),
"\3%02d%s\3\tis now known as \3%02d%s\3 (%s\17)", "\3%02d%s\3\tis now known as \3%02d%s\3 (%s\17)",
@ -493,7 +493,7 @@ static void handleSetname(struct Message *msg) {
static void handleQuit(struct Message *msg) { static void handleQuit(struct Message *msg) {
require(msg, true, 0); require(msg, true, 0);
struct Cursor curs = {0}; struct Cursor curs = {0};
for (uint id; (id = cacheID(&curs, msg->nick));) { for (uint id; (id = cacheNextID(&curs, msg->nick));) {
enum Heat heat = filterCheck(Cold, id, msg); enum Heat heat = filterCheck(Cold, id, msg);
if (heat > Ice) urlScan(id, msg->nick, msg->params[0]); if (heat > Ice) urlScan(id, msg->nick, msg->params[0]);
uiFormat( uiFormat(