From: Christian Heller Date: Sun, 24 Nov 2013 23:48:44 +0000 (+0100) Subject: Transformed trouble_msg() into exit_err() wrapper exit_trouble(), eliminating some... X-Git-Tag: tce~917 X-Git-Url: https://plomlompom.com/repos/te"st.html?a=commitdiff_plain;h=4d6dba920fe02c38aa234c4991d72418e802314a;p=plomrogue Transformed trouble_msg() into exit_err() wrapper exit_trouble(), eliminating some code and memory management overhead. --- diff --git a/src/command_db.c b/src/command_db.c index c0aa523..7e60b1d 100644 --- a/src/command_db.c +++ b/src/command_db.c @@ -6,9 +6,8 @@ #include /* for uint8_t */ #include /* for strlen(), strtok() */ #include "main.h" /* for world global */ -#include "rexit.h" /* for exit_err() */ #include "readwrite.h" /* for textfile_sizes(), try_fopen(), try_fclose() */ -#include "misc.h" /* for try_malloc() */ +#include "misc.h" /* for try_malloc(), exit_trouble */ @@ -79,12 +78,12 @@ extern char * get_command_longdsc(char * dsc_short) extern void init_command_db() { char * f_name = "init_command_db()"; - char * err_s = "Trouble in init_command_db() with textfile_sizes()."; char * path = "config/commands"; FILE * file = try_fopen(path, "r", f_name); uint16_t lines, linemax; - exit_err(textfile_sizes(file, &linemax, &lines), err_s); + uint8_t test = textfile_sizes(file, &linemax, &lines); + exit_trouble(test, f_name, "textfile_sizes()"); char line[linemax + 1]; struct Command * cmds = try_malloc(lines * sizeof(struct Command), f_name); diff --git a/src/misc.c b/src/misc.c index e71770d..06c6a30 100644 --- a/src/misc.c +++ b/src/misc.c @@ -34,27 +34,25 @@ extern uint16_t rrand() -extern char * trouble_msg(char * parent, char * child) +extern void exit_trouble(uint8_t test, char * parent, char * child) { char * p1 = "Trouble in "; char * p2 = " with "; char * p3 = "."; uint16_t size = strlen(p1) + strlen(parent) + strlen(p2) + strlen(child) + strlen(p3) + 1; - char * msg = malloc(size); + char msg[size]; exit_err(NULL == msg, "malloc() in trouble_msg() failed."); sprintf(msg, "%s%s%s%s%s", p1, parent, p2, child, p3); - return msg; + exit_err(test, msg); } extern void * try_malloc(size_t size, char * f) { - char * msg = trouble_msg(f, "malloc()"); void * p = malloc(size); - exit_err(NULL == p, msg); - free(msg); + exit_trouble(NULL == p, f, "malloc()"); return p; } @@ -62,10 +60,8 @@ extern void * try_malloc(size_t size, char * f) extern void * try_calloc(size_t size1, size_t size2, char * f) { - char * msg = trouble_msg(f, "calloc()"); void * p = calloc(size1, size2); - exit_err(NULL == p, msg); - free(msg); + exit_trouble(NULL == p, f, "calloc()"); return p; } diff --git a/src/misc.h b/src/misc.h index bc3e274..ddc6be6 100644 --- a/src/misc.h +++ b/src/misc.h @@ -20,8 +20,8 @@ struct Map; */ extern uint16_t rrand(); -/* Returns message: "Trouble in ".parent." with ".child."." (try_*() helper) */ -extern char * trouble_msg(char * parent, char * child); +/* Do exit_err() with message: "Trouble in ".parent." with ".child."." */ +extern void exit_trouble(uint8_t test, char * parent, char * child); /* Wrappers to malloc(), calloc() from function called "f" calling exit_err() * with trouble_msg() error message if necessary. diff --git a/src/readwrite.c b/src/readwrite.c index 4bdf5ae..531409e 100644 --- a/src/readwrite.c +++ b/src/readwrite.c @@ -8,7 +8,7 @@ #include /* for strlen()*/ #include /* for unlink() */ #include "rexit.h" /* for exit_err() */ -#include "misc.h" /* for trouble_msg() */ +#include "misc.h" /* for exit_trouble() */ #include "main.h" /* for world global */ @@ -52,19 +52,15 @@ extern FILE * try_fopen(char * path, char * mode, char * f) extern void try_fclose(FILE * file, char * f) { - char * msg = trouble_msg(f, "fclose()"); - exit_err(fclose(file), msg); - free(msg); + exit_trouble(fclose(file), f, "fclose()"); } extern char * try_fgets(char * line, int linemax, FILE * file, char * f) { - char * msg = trouble_msg(f, "fgets()"); char * test = fgets(line, linemax, file); - exit_err(NULL == test && ferror(file), msg); - free(msg); + exit_trouble(NULL == test && ferror(file), f, "fgets()"); return test; } @@ -73,9 +69,7 @@ extern char * try_fgets(char * line, int linemax, FILE * file, char * f) extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream, char * f) { - char * msg = trouble_msg(f, "fwrite()"); - exit_err(0 == fwrite(ptr, size, nmemb, stream), msg); - free(msg); + exit_trouble(0 == fwrite(ptr, size, nmemb, stream), f, "fwrite()"); } @@ -108,10 +102,8 @@ extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2, extern uint16_t get_linemax(FILE * file, char * f) { - char * msg = trouble_msg(f, "textfile_sizes()"); uint16_t linemax; - exit_err(textfile_sizes(file, &linemax, NULL), msg); - free(msg); + exit_trouble(textfile_sizes(file, &linemax, NULL), f, "textfile_sizes()"); return linemax; } diff --git a/src/wincontrol.c b/src/wincontrol.c index 658a8ce..296c4a9 100644 --- a/src/wincontrol.c +++ b/src/wincontrol.c @@ -20,7 +20,7 @@ * draw_win_keybindings_winconf_keybindings(), * draw_winconf_geometry(), draw_winconf_keybindings() */ -#include "misc.h" /* for try_malloc(), trouble_msg() */ +#include "misc.h" /* for try_malloc(), exit_trouble() */ #include "dirent.h" /* for opendir(), closedir(), readdir() */ #include "errno.h" /* for errno */ #include "keybindings.h" /* for KeyBinding struct, free_keybindings() */ @@ -337,13 +337,9 @@ extern struct Win * get_win_by_id(char id) extern void init_winconfs() { char * f_name = "init_winconfs()"; - char * err_o = trouble_msg(f_name, "opendir()"); - char * err_r = trouble_msg(f_name, "readdir()"); - char * err_c = trouble_msg(f_name, "closedir()"); DIR * dp = opendir("config/windows"); - exit_err(NULL == dp, err_o); - free(err_o); + exit_trouble(NULL == dp, f_name, "opendir()"); struct dirent * fn; errno = 0; char * winconf_ids = try_malloc(256, f_name); @@ -359,10 +355,8 @@ extern void init_winconfs() } } winconf_ids[i] = '\0'; - exit_err(errno, err_r); - free(err_r); - exit_err(closedir(dp), err_c); - free(err_c); + exit_trouble(errno, f_name, "readdir()"); + exit_trouble(closedir(dp), f_name, "closedir()"); world.winconf_ids = try_malloc(strlen(winconf_ids) + 1, f_name); memcpy(world.winconf_ids, winconf_ids, strlen(winconf_ids) + 1); free(winconf_ids); @@ -422,9 +416,7 @@ extern void sorted_wintoggle_and_activate() try_fgets(win_order, linemax + 1, file, f_name); uint8_t a = 0; - char * err = trouble_msg(f_name, "read_uint8()"); - exit_err(read_uint8(file, &a), err); - free(err); + exit_trouble(read_uint8(file, &a), f_name, "read_uint8()"); try_fclose(file, f_name);