#include <stdint.h> /* for uint8_t */
#include <string.h> /* 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 */
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);
-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;
}
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;
}
*/
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.
#include <string.h> /* for strlen()*/
#include <unistd.h> /* 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 */
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;
}
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()");
}
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;
}
* 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() */
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);
}
}
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);
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);