summaryrefslogtreecommitdiff
path: root/entry.c
diff options
context:
space:
mode:
authorlouie <lshprung@yahoo.com>2020-08-02 18:09:03 -0700
committerlouie <lshprung@yahoo.com>2020-08-02 18:09:03 -0700
commit7cab81c193a01775d804fb037f7d26c6a6806e74 (patch)
treeba6049ca4c3243496bcd46c1f5639d8f69591e7a /entry.c
parent2ee23565a15bd969828db7442f2f8b4902d5e261 (diff)
Added option to sort entries
Diffstat (limited to 'entry.c')
-rw-r--r--entry.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/entry.c b/entry.c
index d455f6b..4fd3d1e 100644
--- a/entry.c
+++ b/entry.c
@@ -20,6 +20,7 @@ ENTRY *create_entry(char *new_name, char *new_path, bool force);
void entry_rm(ENTRY *e, ENTRY *prev);
void clear_entries(ENTRY *head);
ENTRY *entry_add_last(ENTRY *tail, ENTRY *add);
+int entry_add_sorted(ENTRY *head, ENTRY *tail, ENTRY *add);
ENTRY **get_entries(ENTRY *head, int count);
char *get_ename(ENTRY *e);
char *get_epath(ENTRY *e);
@@ -57,6 +58,7 @@ void clear_entries(ENTRY *head){
return;
}
+//TODO consider joining entry_add_last and entry_add_sorted
ENTRY *entry_add_last(ENTRY *tail, ENTRY *add){
assert(add != NULL);
@@ -69,6 +71,41 @@ ENTRY *entry_add_last(ENTRY *tail, ENTRY *add){
return tail;
}
+//returns 0 if in the middle, 1 if new head, 2 if new tail, or 3 if both new head and tail
+//TODO this is kind of a stupid way of handling things
+int entry_add_sorted(ENTRY *head, ENTRY *tail, ENTRY *add){
+ assert(add != NULL);
+ ENTRY *ahead;
+
+ //Empty group (no need to sort)
+ if(head == NULL) return 3;
+
+ //add is the new tail
+ if(strcmp(tail->name, add->name) <= 0){
+ tail->next = add;
+ return 2;
+ }
+
+ //add is the new head
+ if(strcmp(add->name, head->name) <= 0){
+ add->next = head;
+ return 1;
+ }
+
+ ahead = head->next;
+
+ while(ahead != NULL){
+ if(strcmp(head->name, add->name) <= 0 && strcmp(add->name, ahead->name) <= 0) break;
+ head = head->next;
+ ahead = ahead->next;
+ }
+
+ head->next = add;
+ add->next = ahead;
+
+ return 0;
+}
+
ENTRY **get_entries(ENTRY *head, int count){
ENTRY **arr = malloc(sizeof(ENTRY *) * count);
ENTRY *trav = head;