commit 8be8a92

default  ·  2022-11-24 12:06:28 +0000 UTC
parent 233ba75
New function index_in().
1 files changed,  +33, -1
M data.c
M data.c
+33, -1
 1@@ -216,7 +216,6 @@ int index_add(const char *fn, const char *id)
 2 /* adds an id to an index */
 3 {
 4     xs *md5 = xs_md5_hex(id, strlen(id));
 5-
 6     return index_add_md5(fn, md5);
 7 }
 8 
 9@@ -259,6 +258,39 @@ int index_del(const char *fn, const char *md5)
10 }
11 
12 
13+int index_in_md5(const char *fn, const char *md5)
14+/* checks if the md5 is already in the index */
15+{
16+    FILE *f;
17+    int ret = 0;
18+
19+    if ((f = fopen(fn, "r")) != NULL) {
20+        flock(fileno(f), LOCK_SH);
21+
22+        char line[256];
23+
24+        while (!ret && fgets(line, sizeof(line), f) != NULL) {
25+            line[32] = '\0';
26+
27+            if (strcmp(line, md5) == 0)
28+                ret = 1;
29+        }
30+
31+        fclose(f);
32+    }
33+
34+    return ret;
35+}
36+
37+
38+int index_in(const char *fn, const char *id)
39+/* checks if the object id is already in the index */
40+{
41+    xs *md5 = xs_md5_hex(id, strlen(id));
42+    return index_in_md5(fn, md5);
43+}
44+
45+
46 d_char *index_list(const char *fn, int max)
47 /* returns an index as a list */
48 {