commit bc7e4fabd5482c72fda0764b6678665c1c65a5d6
parent 3881495ad1755faadda166e250e44967e2a25f95
Author: Jacob R. Edwards <n/a>
Date: Sun, 9 Oct 2022 12:04:22 -0700
Insert items after the current item instead of before
Before, items were inserted before the current item so as to keep
their order. This was due to a limitation with the queue_add function,
which has now had it's capabilities expanded to allow for items to
be inserted at arbitrary positions, accommodating the graceful
solution now seen in the add command.
(While in the add command's case, the order of items could still
be kept by incrementing in reverse, it could have made queue_add
very cumbersome to use in other situations.)
Diffstat:
5 files changed, 15 insertions(+), 27 deletions(-)
diff --git a/aps/command.c b/aps/command.c
@@ -40,9 +40,11 @@ char *
com_add(struct aps *aps, int fd, int argc, char **argv)
{
int i;
+ struct item *p;
+ p = NULL;
for (i = 0; i < argc; ++i) {
- if (queue_add(aps, argv[i]))
+ if (!(p = queue_add(aps, p, argv[i])))
return errstr;
}
return NULL;
diff --git a/aps/queue.c b/aps/queue.c
@@ -38,8 +38,8 @@ queue_set(struct aps *aps, struct item *item)
}
}
-int
-queue_add(struct aps *aps, char *s)
+struct item *
+queue_add(struct aps *aps, struct item *at, char *s)
{
struct item *item;
@@ -48,15 +48,14 @@ queue_add(struct aps *aps, char *s)
item = item_new(s);
if (item == NULL) {
aps_log(aps, ERROR, 0, "unable to create queue item");
- return 1;
+ return NULL;
}
if (aps->queue == NULL)
queue_set(aps, item);
else
- item_insert(aps->queue, item);
-
- return 0;
+ item_append(at ? at : aps->queue, item);
+ return item;
}
struct item *
diff --git a/aps/queue.h b/aps/queue.h
@@ -1,3 +1,3 @@
void queue_set(struct aps *, struct item *);
-int queue_add(struct aps *, char *);
+struct item *queue_add(struct aps *, struct item *, char *);
struct item *queue_remove(struct aps *, struct item *);
diff --git a/lib/ap/item.c b/lib/ap/item.c
@@ -54,25 +54,13 @@ item_free(struct item *item)
}
void
-item_insert(struct item *list, struct item *item)
+item_append(struct item *list, struct item *item)
{
item_unlink(item);
- list->prev->next = item;
- item->prev = list->prev;
- item->next = list;
- list->prev = item;
-}
-
-int
-item_insert_string(struct item *list, char *s)
-{
- struct item *item;
-
- item = item_new(s);
- if (item == NULL)
- return 1;
- item_insert(list, item);
- return 0;
+ item->next = list->next;
+ list->next->prev = item;
+ item->prev = list;
+ list->next = item;
}
void
diff --git a/lib/ap/item.h b/lib/ap/item.h
@@ -8,6 +8,5 @@ struct item {
struct item *item_new(char *);
void item_free(struct item *);
-void item_insert(struct item *, struct item *);
-int item_insert_string(struct item *, char *);
+void item_append(struct item *, struct item *);
void item_unlink(struct item *);