From 1af3c68d5c7632a71c96ae4ca1b9ebc9c7c002d5 Mon Sep 17 00:00:00 2001 From: Thomas Arp <357770+welcor@users.noreply.github.com> Date: Sun, 26 Jan 2020 22:02:17 +0100 Subject: [PATCH] code cleanup. Remove inline block, make variable names more understandable. Ref https://www.tbamud.com/forum/4-development/4525-confused-over-piece-of-code-in-parse-room --- src/db.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/db.c b/src/db.c index 746b3a2..5d91439 100644 --- a/src/db.c +++ b/src/db.c @@ -1239,6 +1239,24 @@ static bitvector_t asciiflag_conv_aff(char *flag) return (flags); } +/* Fix for crashes in the editor when formatting. E-descs are assumed to + * end with a \r\n. -Welcor */ +void ensure_newline_terminated(struct extra_descr_data* new_descr) { + char *with_term, *end; + + if (new_descr->description == NULL) { + return; + } + + end = strchr(new_descr->description, '\0'); + if (end > new_descr->description && *(end - 1) != '\n') { + CREATE(with_term, char, strlen(new_descr->description) + 3); + sprintf(with_term, "%s\r\n", new_descr->description); /* snprintf ok : size checked above*/ + free(new_descr->description); + new_descr->description = with_term; + } +} + /* load the rooms */ void parse_room(FILE *fl, int virtual_nr) { @@ -1346,17 +1364,8 @@ void parse_room(FILE *fl, int virtual_nr) CREATE(new_descr, struct extra_descr_data, 1); new_descr->keyword = fread_string(fl, buf2); new_descr->description = fread_string(fl, buf2); - /* Fix for crashes in the editor when formatting. E-descs are assumed to - * end with a \r\n. -Welcor */ - { - char *end = strchr(new_descr->description, '\0'); - if (end > new_descr->description && *(end-1) != '\n') { - CREATE(end, char, strlen(new_descr->description)+3); - sprintf(end, "%s\r\n", new_descr->description); /* snprintf ok : size checked above*/ - free(new_descr->description); - new_descr->description = end; - } - } + ensure_newline_terminated(new_descr); + new_descr->next = world[room_nr].ex_description; world[room_nr].ex_description = new_descr; break;