summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe2022-09-11 17:28:19 -0400
committerJune McEnroe2022-09-11 17:28:19 -0400
commitd043bad63ded27b71bf2762f6c07ca1186058cf2 (patch)
tree3d1383a083e13a18d5b305bacfd41cc75aa592c6
parentb5bd702c3cb9eab341bfca312b0a5cad3f5368be (diff)
Set pointer to Entry in Cursor
This feels a little redundant but the API makes sense, I think?
-rw-r--r--cache.c79
-rw-r--r--chat.h10
-rw-r--r--handle.c6
3 files changed, 57 insertions, 38 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,6 +128,32 @@ void cacheReplace(bool touch, const char *old, const char *new) {
 	}
 }
 
+void cacheRemove(uint id, const char *key) {
+	gen++;
+	struct Node *next = NULL;
+	for (struct Node *node = head; node; node = next) {
+		next = node->next;
+		if (id && node->id != id) continue;
+		if (strcmp(node->key, key)) continue;
+		detach(node);
+		free(node->key);
+		free(node);
+		if (id) break;
+	}
+}
+
+void cacheClear(uint id) {
+	gen++;
+	struct Node *next = NULL;
+	for (struct Node *node = head; node; node = next) {
+		next = node->next;
+		if (node->id != id) continue;
+		detach(node);
+		free(node->key);
+		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;
@@ -138,6 +164,7 @@ const char *cacheComplete(struct Cursor *curs, uint id, const char *prefix) {
 	) {
 		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;
@@ -152,12 +179,27 @@ const char *cacheSearch(struct Cursor *curs, uint id, const char *substr) {
 	) {
 		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;
 }
 
-uint cacheID(struct Cursor *curs, const char *key) {
+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);
@@ -166,6 +208,7 @@ uint cacheID(struct Cursor *curs, const char *key) {
 	) {
 		if (!curs->node->id) continue;
 		if (strcmp(curs->node->key, key)) continue;
+		curs->entry = &curs->node->entry;
 		return curs->node->id;
 	}
 	return None;
@@ -181,29 +224,3 @@ void cacheAccept(struct Cursor *curs) {
 void cacheReject(struct Cursor *curs) {
 	curs->node = NULL;
 }
-
-void cacheRemove(uint id, const char *key) {
-	gen++;
-	struct Node *next = NULL;
-	for (struct Node *node = head; node; node = next) {
-		next = node->next;
-		if (id && node->id != id) continue;
-		if (strcmp(node->key, key)) continue;
-		detach(node);
-		free(node->key);
-		free(node);
-		if (id) break;
-	}
-}
-
-void cacheClear(uint id) {
-	gen++;
-	struct Node *next = NULL;
-	for (struct Node *node = head; node; node = next) {
-		next = node->next;
-		if (node->id != id) continue;
-		detach(node);
-		free(node->key);
-		free(node);
-	}
-}
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);
+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 cacheID(struct Cursor *curs, const char *key);
+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);
-void cacheRemove(uint id, const char *key);
-void cacheClear(uint id);
 
 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(