summary refs log tree commit diff
path: root/cache.c
diff options
context:
space:
mode:
Diffstat (limited to 'cache.c')
-rw-r--r--cache.c79
1 files changed, 48 insertions, 31 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);
-	}
-}