diff --git a/Makefile b/Makefile index a26b08b..29423f9 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,8 @@ LDFLAGS = -lncursesw EXECUTABLE = bin/ttysolitaire LIB_DIR = lib SRC = ${LIB_DIR}/ttysolitaire.c -LIB_OBJECTS = ${LIB_DIR}/frame.o \ +LIB_OBJECTS = ${LIB_DIR}/common.o \ + ${LIB_DIR}/frame.o \ ${LIB_DIR}/card.o \ ${LIB_DIR}/stack.o \ ${LIB_DIR}/deck.o \ diff --git a/lib/card.c b/lib/card.c index ee3be10..06a41ef 100644 --- a/lib/card.c +++ b/lib/card.c @@ -4,10 +4,11 @@ #include #include #include "card.h" +#include "common.h" void allocate_card(struct card **card) { if (!(*card = malloc(sizeof(**card)))) { - fprintf(stderr, "%s: %s (%s:%d)\n", program_name, strerror(errno), __FILE__, __LINE__ - 1); + fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__)); exit(errno); } diff --git a/lib/card.h b/lib/card.h index 996a5cf..6c03f62 100644 --- a/lib/card.h +++ b/lib/card.h @@ -41,8 +41,6 @@ struct card { enum face face; }; -extern const char *program_name; - void allocate_card(struct card **); void initialize_card(struct card *); struct card *duplicate_card(struct card *); diff --git a/lib/common.c b/lib/common.c new file mode 100644 index 0000000..07f4f36 --- /dev/null +++ b/lib/common.c @@ -0,0 +1,16 @@ +#include +#include +#include +#include "common.h" + +char *tty_solitaire_error_message(int errno, char *file, int line) { + char *message = malloc(sizeof(ERROR_MESSAGE_BUFFER_SIZE)); + snprintf(message, + ERROR_MESSAGE_BUFFER_SIZE, + "%s: %s (%s:%d)\n", + program_name, + strerror(errno), + file, + line - 1); + return(message); +} diff --git a/lib/common.h b/lib/common.h new file mode 100644 index 0000000..6fdd074 --- /dev/null +++ b/lib/common.h @@ -0,0 +1,8 @@ +#ifndef COMMON_H +#define COMMON_H + +extern const char *program_name; +#define ERROR_MESSAGE_BUFFER_SIZE 100 +char *tty_solitaire_error_message(int, char *, int); + +#endif diff --git a/lib/cursor.c b/lib/cursor.c index 1c74de4..10620cd 100644 --- a/lib/cursor.c +++ b/lib/cursor.c @@ -7,10 +7,11 @@ #include "display.h" #include "game.h" #include "cursor.h" +#include "common.h" void allocate_cursor(struct cursor **cursor) { if (!(*cursor = malloc(sizeof(**cursor)))) { - fprintf(stderr, "%s: %s (%s:%d)\n", program_name, strerror(errno), __FILE__, __LINE__ - 1); + fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__)); exit(errno); } } diff --git a/lib/cursor.h b/lib/cursor.h index 3d280e8..3d0c499 100644 --- a/lib/cursor.h +++ b/lib/cursor.h @@ -32,7 +32,6 @@ struct cursor { enum movement { LEFT, DOWN, UP, RIGHT }; extern struct deck *deck; -extern const char *program_name; void allocate_cursor(struct cursor **); void initialize_cursor(struct cursor *); diff --git a/lib/deck.c b/lib/deck.c index 1f7eca0..5639b17 100644 --- a/lib/deck.c +++ b/lib/deck.c @@ -4,10 +4,11 @@ #include #include #include "deck.h" +#include "common.h" void allocate_deck(struct deck **deck) { if (!(*deck = malloc(sizeof(**deck)))) { - fprintf(stderr, "%s: %s (%s:%d)\n", program_name, strerror(errno), __FILE__, __LINE__ - 1); + fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__)); exit(errno); } diff --git a/lib/deck.h b/lib/deck.h index 98959da..2107cfe 100644 --- a/lib/deck.h +++ b/lib/deck.h @@ -21,8 +21,6 @@ struct deck { struct stack *maneuvre_6; }; -extern const char *program_name; - void allocate_deck(struct deck **); void initialize_deck(struct deck *); void free_deck(struct deck *); diff --git a/lib/display.c b/lib/display.c index a6549f5..fa97ddb 100644 --- a/lib/display.c +++ b/lib/display.c @@ -6,12 +6,13 @@ #include #include "game.h" #include "display.h" +#include "common.h" static char *card_suit(enum suit suit) { char *card_suit; if (!(card_suit = malloc(5 * sizeof(*card_suit)))) { - fprintf(stderr, "%s: %s (%s:%d)\n", program_name, strerror(errno), __FILE__, __LINE__ - 1); + fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__)); exit(errno); } @@ -30,7 +31,7 @@ static char *card_value(enum value value) { char *card_value; if (!(card_value = malloc(2 * sizeof(*card_value)))) { - fprintf(stderr, "%s: %s (%s:%d)\n", program_name, strerror(errno), __FILE__, __LINE__ - 1); + fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__)); exit(errno); } diff --git a/lib/display.h b/lib/display.h index 8dc4168..12133e4 100644 --- a/lib/display.h +++ b/lib/display.h @@ -18,8 +18,6 @@ #define WHITE_ON_BLUE 3 #define WHITE_ON_GREEN 4 -extern const char *program_name; - void erase_stack(struct stack *); void draw_value(struct card *); void draw_suit(struct card *); diff --git a/lib/frame.c b/lib/frame.c index 3717ace..fd52903 100644 --- a/lib/frame.c +++ b/lib/frame.c @@ -4,10 +4,11 @@ #include #include #include "frame.h" +#include "common.h" void allocate_frame(struct frame **frame) { if (!(*frame = malloc(sizeof(**frame)))) { - fprintf(stderr, "%s: %s (%s:%d)\n", program_name, strerror(errno), __FILE__, __LINE__ - 1); + fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__)); exit(errno); } } diff --git a/lib/frame.h b/lib/frame.h index 1b4ad04..c519400 100644 --- a/lib/frame.h +++ b/lib/frame.h @@ -12,8 +12,6 @@ struct frame { int start_x; }; -extern const char *program_name; - void allocate_frame(struct frame **); void initialize_frame(struct frame *); struct frame *duplicate_frame(struct frame *); diff --git a/lib/game.c b/lib/game.c index 13c4972..5349259 100644 --- a/lib/game.c +++ b/lib/game.c @@ -6,6 +6,7 @@ #include #include "display.h" #include "util.h" +#include "common.h" #include "game.h" bool stock_stack(struct stack *stack) { @@ -198,7 +199,7 @@ static void shuffle_deck(struct deck *deck) { int random; if (!(stack = malloc(NUMBER_OF_CARDS * sizeof(*stack)))) { - fprintf(stderr, "%s: %s (%s:%d)\n", program_name, strerror(errno), __FILE__, __LINE__ - 1); + fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__)); exit(errno); } diff --git a/lib/game.h b/lib/game.h index cda2071..e1b307c 100644 --- a/lib/game.h +++ b/lib/game.h @@ -30,7 +30,6 @@ #define MANEUVRE_5_STARTING_X 41 #define MANEUVRE_6_STARTING_X 49 -extern const char *program_name; struct deck *deck; struct cursor *cursor; diff --git a/lib/stack.c b/lib/stack.c index 7ec5fdb..74b6aac 100644 --- a/lib/stack.c +++ b/lib/stack.c @@ -8,7 +8,7 @@ void allocate_stack(struct stack **stack) { if (!(*stack = malloc(sizeof(**stack)))) { - fprintf(stderr, "%s: %s (%s:%d)\n", program_name, strerror(errno), __FILE__, __LINE__ - 1); + fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__)); exit(errno); } diff --git a/lib/stack.h b/lib/stack.h index fe5b655..1a3efec 100644 --- a/lib/stack.h +++ b/lib/stack.h @@ -8,8 +8,6 @@ struct stack { struct stack *next; }; -extern const char *program_name; - void allocate_stack(struct stack **); void initialize_stack(struct stack *); struct stack *duplicate_stack(struct stack *);