From 38133f9624d11b5435c3f54bf613196e38ca57dd Mon Sep 17 00:00:00 2001 From: Markus Uhlin Date: Tue, 13 Aug 2024 00:24:09 +0200 Subject: Changed ReadV1PlayerFmt() and added and made use of add_to_list() --- FICS/playerdb.c | 54 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 23 deletions(-) (limited to 'FICS/playerdb.c') diff --git a/FICS/playerdb.c b/FICS/playerdb.c index 8e76a8f..589bf46 100644 --- a/FICS/playerdb.c +++ b/FICS/playerdb.c @@ -356,6 +356,22 @@ player_remove(int p) return 0; } +PRIVATE int +add_to_list(FILE *fp, enum ListWhich lw, int *size, int p) +{ + char buf[MAX_STRING_LENGTH] = { '\0' }; + + _Static_assert(1023 < ARRAY_SIZE(buf), "Buffer too small"); + +#define SCAN_STR "%1023s" + + if (*size <= 0) + return -2; + while ((*size)-- > 0 && fscanf(fp, SCAN_STR, buf) == 1) + list_add(p, lw, buf); + return (*size <= 0 ? 0 : -1); +} + PRIVATE void ReadV1PlayerFmt(int p, player *pp, FILE *fp, char *file, int version) { @@ -585,29 +601,21 @@ ReadV1PlayerFmt(int p, player *pp, FILE *fp, char *file, int version) } } - _Static_assert(1023 < ARRAY_SIZE(tmp2), "Array too small"); - -#define SCAN_STR "%1023s" - - while (size_cens--) { - fscanf(fp, SCAN_STR, tmp2); - list_add(p, L_CENSOR, tmp2); - } - while (size_not--) { - fscanf(fp, SCAN_STR, tmp2); - list_add(p, L_NOTIFY, tmp2); - } - while (size_noplay--) { - fscanf(fp, SCAN_STR, tmp2); - list_add(p, L_NOPLAY, tmp2); - } - while (size_gnot--) { - fscanf(fp, SCAN_STR, tmp2); - list_add(p, L_GNOTIFY, tmp2); - } - while (size_chan--) { - fscanf(fp, SCAN_STR, tmp2); - list_add(p, L_CHANNEL, tmp2); + if (add_to_list(fp, L_CENSOR, &size_cens, p) == -1) { + warnx("%s: add to list error (L_CENSOR): player: %s", + __func__, parray[p].name); + } else if (add_to_list(fp, L_NOTIFY, &size_not, p) == -1) { + warnx("%s: add to list error (L_NOTIFY): player: %s", + __func__, parray[p].name); + } else if (add_to_list(fp, L_NOPLAY, &size_noplay, p) == -1) { + warnx("%s: add to list error (L_NOPLAY): player: %s", + __func__, parray[p].name); + } else if (add_to_list(fp, L_GNOTIFY, &size_gnot, p) == -1) { + warnx("%s: add to list error (L_GNOTIFY): player: %s", + __func__, parray[p].name); + } else if (add_to_list(fp, L_CHANNEL, &size_chan, p) == -1) { + warnx("%s: add to list error (L_CHANNEL): player: %s", + __func__, parray[p].name); } } -- cgit v1.2.3