From d043bad63ded27b71bf2762f6c07ca1186058cf2 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Sun, 11 Sep 2022 17:28:19 -0400 Subject: [PATCH] Set pointer to Entry in Cursor This feels a little redundant but the API makes sense, I think? --- cache.c | 133 +++++++++++++++++++++++++++++++------------------------ chat.h | 14 +++--- handle.c | 6 +-- 3 files changed, 86 insertions(+), 67 deletions(-) diff --git a/cache.c b/cache.c index 30ebef4..970bd9c 100644 --- a/cache.c +++ b/cache.c @@ -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) { struct Node *node = find(id, key); 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) { struct Node *next = NULL; 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) { gen++; struct Node *next = NULL; @@ -207,3 +153,74 @@ void cacheClear(uint id) { 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; +} diff --git a/chat.h b/chat.h index 628d416..c7053c7 100644 --- a/chat.h +++ b/chat.h @@ -403,17 +403,19 @@ struct Entry { struct Cursor { uint gen; 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); +struct Entry *cacheInsert(bool touch, uint id, const char *key); 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 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 urlCopyUtil; diff --git a/handle.c b/handle.c index b8434c6..a075d02 100644 --- a/handle.c +++ b/handle.c @@ -459,7 +459,7 @@ static void handleNick(struct Message *msg) { inputUpdate(); } 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)) { set(&idNames[id], msg->params[0]); } @@ -480,7 +480,7 @@ static void handleNick(struct Message *msg) { static void handleSetname(struct Message *msg) { require(msg, true, 1); struct Cursor curs = {0}; - for (uint id; (id = cacheID(&curs, msg->nick));) { + for (uint id; (id = cacheNextID(&curs, msg->nick));) { uiFormat( id, filterCheck(Cold, id, msg), tagTime(msg), "\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) { require(msg, true, 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); if (heat > Ice) urlScan(id, msg->nick, msg->params[0]); uiFormat(