commit 5921ec5

default  ·  2024-07-23 07:50:56 +0000 UTC
parent 78f383f
Reimplemented index_list_desc() by way of index_desc_first() and index_desc_next().
2 files changed,  +8, -16
M data.c
M snac.h
M data.c
+6, -16
 1@@ -651,26 +651,16 @@ xs_list *index_list_desc(const char *fn, int skip, int show)
 2 {
 3     xs_list *list = xs_list_new();
 4     FILE *f;
 5-    int n = 0;
 6 
 7     if ((f = fopen(fn, "r")) != NULL) {
 8-        flock(fileno(f), LOCK_SH);
 9+        char md5[33];
10 
11-        char line[256];
12+        if (index_desc_first(f, md5, skip)) {
13+            int n = 1;
14 
15-        /* move to the end minus one entry (or more, if skipping entries) */
16-        if (!fseek(f, 0, SEEK_END) && !fseek(f, (skip + 1) * -33, SEEK_CUR)) {
17-            while (n < show && fgets(line, sizeof(line), f) != NULL) {
18-                if (line[0] != '-') {
19-                    line[32] = '\0';
20-                    list = xs_list_append(list, line);
21-                    n++;
22-                }
23-
24-                /* move backwards 2 entries */
25-                if (fseek(f, -66, SEEK_CUR) == -1)
26-                    break;
27-            }
28+            do {
29+                list = xs_list_append(list, md5);
30+            } while (n++ < show && index_desc_next(f, md5));
31         }
32 
33         fclose(f);
M snac.h
+2, -0
1@@ -101,6 +101,8 @@ int index_gc(const char *fn);
2 int index_first(const char *fn, char *buf, int size);
3 int index_len(const char *fn);
4 xs_list *index_list(const char *fn, int max);
5+int index_desc_next(FILE *f, char md5[33]);
6+int index_desc_first(FILE *f, char md5[33], int skip);
7 xs_list *index_list_desc(const char *fn, int skip, int show);
8 
9 int object_add(const char *id, const xs_dict *obj);