diff options
| author | 2022-11-23 19:32:26 +0100 | |
|---|---|---|
| committer | 2022-11-23 19:32:26 +0100 | |
| commit | 7f458f4fd624a9182a5cf53c272f5ec6e47cb05a (patch) | |
| tree | 64629e6f508d8d6a3095b61a7b95587592117092 /data.c | |
| parent | New function list_del_md5(). (diff) | |
| download | snac2-7f458f4fd624a9182a5cf53c272f5ec6e47cb05a.tar.gz snac2-7f458f4fd624a9182a5cf53c272f5ec6e47cb05a.tar.xz snac2-7f458f4fd624a9182a5cf53c272f5ec6e47cb05a.zip | |
More listfile functions.
Diffstat (limited to 'data.c')
| -rw-r--r-- | data.c | 66 |
1 files changed, 62 insertions, 4 deletions
| @@ -295,7 +295,7 @@ int object_del(const char *id) | |||
| 295 | } | 295 | } |
| 296 | 296 | ||
| 297 | 297 | ||
| 298 | int list_add_md5(const char *fn, const char *md5) | 298 | int listfile_add_md5(const char *fn, const char *md5) |
| 299 | /* adds an md5 to a list */ | 299 | /* adds an md5 to a list */ |
| 300 | { | 300 | { |
| 301 | int status = 200; | 301 | int status = 200; |
| @@ -314,7 +314,7 @@ int list_add_md5(const char *fn, const char *md5) | |||
| 314 | } | 314 | } |
| 315 | 315 | ||
| 316 | 316 | ||
| 317 | int list_del_md5(const char *fn, const char *md5) | 317 | int listfile_del_md5(const char *fn, const char *md5) |
| 318 | /* deletes an md5 from a list */ | 318 | /* deletes an md5 from a list */ |
| 319 | { | 319 | { |
| 320 | int status = 404; | 320 | int status = 404; |
| @@ -324,10 +324,9 @@ int list_del_md5(const char *fn, const char *md5) | |||
| 324 | flock(fileno(i), LOCK_EX); | 324 | flock(fileno(i), LOCK_EX); |
| 325 | 325 | ||
| 326 | xs *nfn = xs_fmt("%s.new", fn); | 326 | xs *nfn = xs_fmt("%s.new", fn); |
| 327 | char line[32 + 1]; | ||
| 327 | 328 | ||
| 328 | if ((o = fopen(nfn, "w")) != NULL) { | 329 | if ((o = fopen(nfn, "w")) != NULL) { |
| 329 | char line[32 + 1]; | ||
| 330 | |||
| 331 | while (fgets(line, sizeof(line), i) != NULL) { | 330 | while (fgets(line, sizeof(line), i) != NULL) { |
| 332 | if (memcmp(line, md5, 32) != 0) | 331 | if (memcmp(line, md5, 32) != 0) |
| 333 | fwrite(line, sizeof(line), 1, o); | 332 | fwrite(line, sizeof(line), 1, o); |
| @@ -352,6 +351,65 @@ int list_del_md5(const char *fn, const char *md5) | |||
| 352 | } | 351 | } |
| 353 | 352 | ||
| 354 | 353 | ||
| 354 | d_char *listfile_get_n(const char *fn, int max) | ||
| 355 | /* returns a list */ | ||
| 356 | { | ||
| 357 | xs *list = NULL; | ||
| 358 | FILE *f; | ||
| 359 | int n = 0; | ||
| 360 | |||
| 361 | if ((f = fopen(fn, "r")) != NULL) { | ||
| 362 | flock(fileno(f), LOCK_SH); | ||
| 363 | |||
| 364 | char line[32 + 1]; | ||
| 365 | list = xs_list_new(); | ||
| 366 | |||
| 367 | while (n < max && fgets(line, sizeof(line), f) != NULL) { | ||
| 368 | line[32] = '\0'; | ||
| 369 | list = xs_list_append(list, line); | ||
| 370 | n++; | ||
| 371 | } | ||
| 372 | |||
| 373 | fclose(f); | ||
| 374 | } | ||
| 375 | |||
| 376 | return list; | ||
| 377 | } | ||
| 378 | |||
| 379 | |||
| 380 | d_char *listfile_get_inv_n(const char *fn, int max) | ||
| 381 | /* returns a list, inversely */ | ||
| 382 | { | ||
| 383 | xs *list = NULL; | ||
| 384 | FILE *f; | ||
| 385 | int n = 0; | ||
| 386 | |||
| 387 | if ((f = fopen(fn, "r")) != NULL) { | ||
| 388 | flock(fileno(f), LOCK_SH); | ||
| 389 | |||
| 390 | char line[32 + 1]; | ||
| 391 | list = xs_list_new(); | ||
| 392 | |||
| 393 | /* move to the end minus one entry */ | ||
| 394 | if (!fseek(f, 0, SEEK_END) && !fseek(f, -sizeof(line), SEEK_SET)) { | ||
| 395 | while (n < max && fgets(line, sizeof(line), f) != NULL) { | ||
| 396 | line[32] = '\0'; | ||
| 397 | list = xs_list_append(list, line); | ||
| 398 | n++; | ||
| 399 | |||
| 400 | /* move backwards 2 entries */ | ||
| 401 | if (fseek(f, -sizeof(line) * 2, SEEK_SET) == -1) | ||
| 402 | break; | ||
| 403 | } | ||
| 404 | } | ||
| 405 | |||
| 406 | fclose(f); | ||
| 407 | } | ||
| 408 | |||
| 409 | return list; | ||
| 410 | } | ||
| 411 | |||
| 412 | |||
| 355 | d_char *_follower_fn(snac *snac, char *actor) | 413 | d_char *_follower_fn(snac *snac, char *actor) |
| 356 | { | 414 | { |
| 357 | xs *md5 = xs_md5_hex(actor, strlen(actor)); | 415 | xs *md5 = xs_md5_hex(actor, strlen(actor)); |