diff options
| author | 2022-11-24 13:06:28 +0100 | |
|---|---|---|
| committer | 2022-11-24 13:06:28 +0100 | |
| commit | 8be8a9282e0c552256ed95eded9364759db80c53 (patch) | |
| tree | 1c90b5f32c13b4f68d8e70d1f3964d808382f59a | |
| parent | New function object_admire(). (diff) | |
| download | snac2-8be8a9282e0c552256ed95eded9364759db80c53.tar.gz snac2-8be8a9282e0c552256ed95eded9364759db80c53.tar.xz snac2-8be8a9282e0c552256ed95eded9364759db80c53.zip | |
New function index_in().
| -rw-r--r-- | data.c | 34 |
1 files changed, 33 insertions, 1 deletions
| @@ -216,7 +216,6 @@ int index_add(const char *fn, const char *id) | |||
| 216 | /* adds an id to an index */ | 216 | /* adds an id to an index */ |
| 217 | { | 217 | { |
| 218 | xs *md5 = xs_md5_hex(id, strlen(id)); | 218 | xs *md5 = xs_md5_hex(id, strlen(id)); |
| 219 | |||
| 220 | return index_add_md5(fn, md5); | 219 | return index_add_md5(fn, md5); |
| 221 | } | 220 | } |
| 222 | 221 | ||
| @@ -259,6 +258,39 @@ int index_del(const char *fn, const char *md5) | |||
| 259 | } | 258 | } |
| 260 | 259 | ||
| 261 | 260 | ||
| 261 | int index_in_md5(const char *fn, const char *md5) | ||
| 262 | /* checks if the md5 is already in the index */ | ||
| 263 | { | ||
| 264 | FILE *f; | ||
| 265 | int ret = 0; | ||
| 266 | |||
| 267 | if ((f = fopen(fn, "r")) != NULL) { | ||
| 268 | flock(fileno(f), LOCK_SH); | ||
| 269 | |||
| 270 | char line[256]; | ||
| 271 | |||
| 272 | while (!ret && fgets(line, sizeof(line), f) != NULL) { | ||
| 273 | line[32] = '\0'; | ||
| 274 | |||
| 275 | if (strcmp(line, md5) == 0) | ||
| 276 | ret = 1; | ||
| 277 | } | ||
| 278 | |||
| 279 | fclose(f); | ||
| 280 | } | ||
| 281 | |||
| 282 | return ret; | ||
| 283 | } | ||
| 284 | |||
| 285 | |||
| 286 | int index_in(const char *fn, const char *id) | ||
| 287 | /* checks if the object id is already in the index */ | ||
| 288 | { | ||
| 289 | xs *md5 = xs_md5_hex(id, strlen(id)); | ||
| 290 | return index_in_md5(fn, md5); | ||
| 291 | } | ||
| 292 | |||
| 293 | |||
| 262 | d_char *index_list(const char *fn, int max) | 294 | d_char *index_list(const char *fn, int max) |
| 263 | /* returns an index as a list */ | 295 | /* returns an index as a list */ |
| 264 | { | 296 | { |