From 02faa5bf7e77b6bbf111819164f9a15e3792215a Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Wed, 9 Jul 2014 23:14:37 +0200 Subject: [PATCH] Server: Merge duplicate string literals of "sprintf()". --- src/server/hardcoded_strings.c | 3 ++- src/server/hardcoded_strings.h | 5 +++-- src/server/init.c | 4 ++-- src/server/io.c | 6 +++--- src/server/run.c | 2 +- src/server/thing_actions.c | 7 ++++--- src/server/things.c | 3 ++- 7 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/server/hardcoded_strings.c b/src/server/hardcoded_strings.c index 8ce7475..652527a 100644 --- a/src/server/hardcoded_strings.c +++ b/src/server/hardcoded_strings.c @@ -4,7 +4,7 @@ -char * s[26]; +char * s[27]; @@ -36,4 +36,5 @@ extern void init_strings() s[S_CMD_PICKUP] = "pick_up"; s[S_CMD_DROP] = "drop"; s[S_CMD_USE] = "use"; + s[S_FCN_SPRINTF] = "sprintf()"; } diff --git a/src/server/hardcoded_strings.h b/src/server/hardcoded_strings.h index e017d7c..a14e85f 100644 --- a/src/server/hardcoded_strings.h +++ b/src/server/hardcoded_strings.h @@ -35,12 +35,13 @@ enum string_num S_CMD_MOVE, S_CMD_PICKUP, S_CMD_DROP, - S_CMD_USE + S_CMD_USE, + S_FCN_SPRINTF }; extern void init_strings(); -extern char * s[26]; +extern char * s[27]; diff --git a/src/server/init.c b/src/server/init.c index 766d92e..ea13bc1 100644 --- a/src/server/init.c +++ b/src/server/init.c @@ -101,7 +101,7 @@ extern void setup_server_io() world.file_out = try_fopen(s[S_PATH_OUT], "w", f_name); world.server_test = try_malloc(10 + 1 + 10 + 1 + 1, f_name); test = sprintf(world.server_test, "%d %d\n", getpid(), (int) time(0)); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, f_name, s[S_FCN_SPRINTF]); try_fwrite(world.server_test, strlen(world.server_test), 1, world.file_out, f_name); fflush(world.file_out); @@ -188,7 +188,7 @@ extern void run_game() char * command = s[S_CMD_MAKE_WORLD]; char * msg = try_malloc(strlen(command) + 1 + 11 + 1, f_name); int test = sprintf(msg, "%s %d", command, (int) time(NULL)); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, f_name, s[S_FCN_SPRINTF]); obey_msg(msg, 1); free(msg); } diff --git a/src/server/io.c b/src/server/io.c index e4a40d0..c703f0e 100644 --- a/src/server/io.c +++ b/src/server/io.c @@ -72,7 +72,7 @@ static void write_key_value(FILE * file, char * key, uint32_t value) try_fwrite(key, strlen(key), 1, file, f_name); try_fputc(' ', file, f_name); char * line = try_malloc(11, f_name); - exit_trouble(-1 == sprintf(line, "%u", value), f_name, "sprintf()"); + exit_trouble(-1 == sprintf(line, "%u", value), f_name, s[S_FCN_SPRINTF]); try_fwrite(line, strlen(line), 1, file, f_name); free(line); try_fputc('\n', file, f_name); @@ -183,7 +183,7 @@ static void update_worldstate_file() uint16_t size = strlen(s[S_PATH_WORLDSTATE])+strlen(s[S_PATH_SUFFIX_TMP])+1; char * path_tmp = try_malloc(size, f_name); int test=sprintf(path_tmp,"%s%s",s[S_PATH_WORLDSTATE],s[S_PATH_SUFFIX_TMP]); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, f_name, s[S_FCN_SPRINTF]); FILE * file = try_fopen(path_tmp, "w", f_name); struct Thing * player = get_player(); write_value_as_line(world.turn, file); @@ -211,7 +211,7 @@ static void write_value_as_line(uint32_t value, FILE * file) { char * f_name = "write_value_as_line()"; char write_buf[12]; /* Holds 10 digits of uint32_t maximum + \n + \0. */ - exit_trouble(sprintf(write_buf, "%u\n", value) < 0, f_name, "sprintf()"); + exit_trouble(sprintf(write_buf, "%u\n",value) < 0, f_name,s[S_FCN_SPRINTF]); try_fwrite(write_buf, strlen(write_buf), 1, file, f_name); } diff --git a/src/server/run.c b/src/server/run.c index d081ae3..452c10b 100644 --- a/src/server/run.c +++ b/src/server/run.c @@ -312,7 +312,7 @@ static void record_msg(char * msg) uint16_t size = strlen(s[S_PATH_RECORD]) + strlen(s[S_PATH_SUFFIX_TMP]) + 1; char * path_tmp = try_malloc(size, f_name); int test = sprintf(path_tmp, "%s%s", s[S_PATH_RECORD],s[S_PATH_SUFFIX_TMP]); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, f_name, s[S_FCN_SPRINTF]); FILE * file_tmp = try_fopen(path_tmp, "w", f_name); if (!access(s[S_PATH_RECORD], F_OK)) { diff --git a/src/server/thing_actions.c b/src/server/thing_actions.c index c1e337b..b153ff8 100644 --- a/src/server/thing_actions.c +++ b/src/server/thing_actions.c @@ -10,6 +10,7 @@ #include "../common/try_malloc.h" /* try_malloc() */ #include "../common/yx_uint8.h" /* struct yx_uint8 */ #include "field_of_view.h" /* build_fov_map() */ +#include "hardcoded_strings.h" /* s */ #include "things.h" /* structs Thing, ThingType, get_player(), own_thing(), * set_thing_position(), get_thing_type() */ @@ -105,7 +106,7 @@ static void update_log(char * text) char * new_text = try_malloc(len_whole, f_name); memcpy(new_text, world.log + offset, len_old); int test = sprintf(new_text + len_old, "%s", text); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, f_name, s[S_FCN_SPRINTF]); free(world.log); world.log = new_text; } @@ -133,7 +134,7 @@ static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted) uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2; char * msg = try_malloc(len, f_name); int test = sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, f_name, s[S_FCN_SPRINTF]); update_log(msg); free(msg); hitted->lifepoints--; @@ -189,7 +190,7 @@ static void playerbonus_move(char d, uint8_t passable) } char * msg = try_malloc(strlen(dsc_move) + strlen (dsc_dir) + 3, f_name); int test = sprintf(msg, "\n%s%s.", dsc_move, dsc_dir); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, f_name, s[S_FCN_SPRINTF]); update_log(msg); free(msg); } diff --git a/src/server/things.c b/src/server/things.c index eabded2..9425f9c 100644 --- a/src/server/things.c +++ b/src/server/things.c @@ -8,6 +8,7 @@ #include "../common/rexit.h" /* exit_err(), exit_trouble() */ #include "../common/try_malloc.h" /* try_malloc() */ #include "../common/yx_uint8.h" /* yx_uint8 */ +#include "hardcoded_strings.h" /* s */ #include "map.h" /* is_passable() */ #include "rrand.h" /* rrand() */ #include "world.h" /* global world */ @@ -192,7 +193,7 @@ extern struct ThingType * get_thing_type(uint8_t id) char * err_intro = "Requested thing type of unused ID "; uint16_t size = strlen(err_intro) + 3 + 1 + 1; char * err = try_malloc(size, f_name); - exit_trouble(sprintf(err, "%s%d.", err_intro, id) < 0, f_name, "sprintf()"); + exit_trouble(sprintf(err,"%s%d.",err_intro,id) < 0,f_name,s[S_FCN_SPRINTF]); exit_err(NULL == tt, err); free(err); return tt; -- 2.30.2