diff options
author | default <nobody@localhost> | 2022-11-23 19:32:26 +0100 |
---|---|---|
committer | default <nobody@localhost> | 2022-11-23 19:32:26 +0100 |
commit | 7f458f4fd624a9182a5cf53c272f5ec6e47cb05a (patch) | |
tree | 64629e6f508d8d6a3095b61a7b95587592117092 | |
parent | 0ff3b32e712a7750feef9e19dca3507641eb01ea (diff) |
More listfile functions.
-rw-r--r-- | data.c | 66 |
1 files changed, 62 insertions, 4 deletions
@@ -295,7 +295,7 @@ int object_del(const char *id) } -int list_add_md5(const char *fn, const char *md5) +int listfile_add_md5(const char *fn, const char *md5) /* adds an md5 to a list */ { int status = 200; @@ -314,7 +314,7 @@ int list_add_md5(const char *fn, const char *md5) } -int list_del_md5(const char *fn, const char *md5) +int listfile_del_md5(const char *fn, const char *md5) /* deletes an md5 from a list */ { int status = 404; @@ -324,10 +324,9 @@ int list_del_md5(const char *fn, const char *md5) flock(fileno(i), LOCK_EX); xs *nfn = xs_fmt("%s.new", fn); + char line[32 + 1]; if ((o = fopen(nfn, "w")) != NULL) { - char line[32 + 1]; - while (fgets(line, sizeof(line), i) != NULL) { if (memcmp(line, md5, 32) != 0) fwrite(line, sizeof(line), 1, o); @@ -352,6 +351,65 @@ int list_del_md5(const char *fn, const char *md5) } +d_char *listfile_get_n(const char *fn, int max) +/* returns a list */ +{ + xs *list = NULL; + FILE *f; + int n = 0; + + if ((f = fopen(fn, "r")) != NULL) { + flock(fileno(f), LOCK_SH); + + char line[32 + 1]; + list = xs_list_new(); + + while (n < max && fgets(line, sizeof(line), f) != NULL) { + line[32] = '\0'; + list = xs_list_append(list, line); + n++; + } + + fclose(f); + } + + return list; +} + + +d_char *listfile_get_inv_n(const char *fn, int max) +/* returns a list, inversely */ +{ + xs *list = NULL; + FILE *f; + int n = 0; + + if ((f = fopen(fn, "r")) != NULL) { + flock(fileno(f), LOCK_SH); + + char line[32 + 1]; + list = xs_list_new(); + + /* move to the end minus one entry */ + if (!fseek(f, 0, SEEK_END) && !fseek(f, -sizeof(line), SEEK_SET)) { + while (n < max && fgets(line, sizeof(line), f) != NULL) { + line[32] = '\0'; + list = xs_list_append(list, line); + n++; + + /* move backwards 2 entries */ + if (fseek(f, -sizeof(line) * 2, SEEK_SET) == -1) + break; + } + } + + fclose(f); + } + + return list; +} + + d_char *_follower_fn(snac *snac, char *actor) { xs *md5 = xs_md5_hex(actor, strlen(actor)); |