Less mental retardation.

This commit is contained in:
Murilo Pereira 2011-05-08 20:20:22 -03:00
parent ea9ff3cfb5
commit 4802244efa
17 changed files with 40 additions and 21 deletions

View File

@ -5,7 +5,8 @@ LDFLAGS = -lncursesw
EXECUTABLE = bin/ttysolitaire EXECUTABLE = bin/ttysolitaire
LIB_DIR = lib LIB_DIR = lib
SRC = ${LIB_DIR}/ttysolitaire.c 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}/card.o \
${LIB_DIR}/stack.o \ ${LIB_DIR}/stack.o \
${LIB_DIR}/deck.o \ ${LIB_DIR}/deck.o \

View File

@ -4,10 +4,11 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include "card.h" #include "card.h"
#include "common.h"
void allocate_card(struct card **card) { void allocate_card(struct card **card) {
if (!(*card = malloc(sizeof(**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); exit(errno);
} }

View File

@ -41,8 +41,6 @@ struct card {
enum face face; enum face face;
}; };
extern const char *program_name;
void allocate_card(struct card **); void allocate_card(struct card **);
void initialize_card(struct card *); void initialize_card(struct card *);
struct card *duplicate_card(struct card *); struct card *duplicate_card(struct card *);

16
lib/common.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#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);
}

8
lib/common.h Normal file
View File

@ -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

View File

@ -7,10 +7,11 @@
#include "display.h" #include "display.h"
#include "game.h" #include "game.h"
#include "cursor.h" #include "cursor.h"
#include "common.h"
void allocate_cursor(struct cursor **cursor) { void allocate_cursor(struct cursor **cursor) {
if (!(*cursor = malloc(sizeof(**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); exit(errno);
} }
} }

View File

@ -32,7 +32,6 @@ struct cursor {
enum movement { LEFT, DOWN, UP, RIGHT }; enum movement { LEFT, DOWN, UP, RIGHT };
extern struct deck *deck; extern struct deck *deck;
extern const char *program_name;
void allocate_cursor(struct cursor **); void allocate_cursor(struct cursor **);
void initialize_cursor(struct cursor *); void initialize_cursor(struct cursor *);

View File

@ -4,10 +4,11 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include "deck.h" #include "deck.h"
#include "common.h"
void allocate_deck(struct deck **deck) { void allocate_deck(struct deck **deck) {
if (!(*deck = malloc(sizeof(**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); exit(errno);
} }

View File

@ -21,8 +21,6 @@ struct deck {
struct stack *maneuvre_6; struct stack *maneuvre_6;
}; };
extern const char *program_name;
void allocate_deck(struct deck **); void allocate_deck(struct deck **);
void initialize_deck(struct deck *); void initialize_deck(struct deck *);
void free_deck(struct deck *); void free_deck(struct deck *);

View File

@ -6,12 +6,13 @@
#include <ncurses.h> #include <ncurses.h>
#include "game.h" #include "game.h"
#include "display.h" #include "display.h"
#include "common.h"
static char *card_suit(enum suit suit) { static char *card_suit(enum suit suit) {
char *card_suit; char *card_suit;
if (!(card_suit = malloc(5 * sizeof(*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); exit(errno);
} }
@ -30,7 +31,7 @@ static char *card_value(enum value value) {
char *card_value; char *card_value;
if (!(card_value = malloc(2 * sizeof(*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); exit(errno);
} }

View File

@ -18,8 +18,6 @@
#define WHITE_ON_BLUE 3 #define WHITE_ON_BLUE 3
#define WHITE_ON_GREEN 4 #define WHITE_ON_GREEN 4
extern const char *program_name;
void erase_stack(struct stack *); void erase_stack(struct stack *);
void draw_value(struct card *); void draw_value(struct card *);
void draw_suit(struct card *); void draw_suit(struct card *);

View File

@ -4,10 +4,11 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include "frame.h" #include "frame.h"
#include "common.h"
void allocate_frame(struct frame **frame) { void allocate_frame(struct frame **frame) {
if (!(*frame = malloc(sizeof(**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); exit(errno);
} }
} }

View File

@ -12,8 +12,6 @@ struct frame {
int start_x; int start_x;
}; };
extern const char *program_name;
void allocate_frame(struct frame **); void allocate_frame(struct frame **);
void initialize_frame(struct frame *); void initialize_frame(struct frame *);
struct frame *duplicate_frame(struct frame *); struct frame *duplicate_frame(struct frame *);

View File

@ -6,6 +6,7 @@
#include <time.h> #include <time.h>
#include "display.h" #include "display.h"
#include "util.h" #include "util.h"
#include "common.h"
#include "game.h" #include "game.h"
bool stock_stack(struct stack *stack) { bool stock_stack(struct stack *stack) {
@ -198,7 +199,7 @@ static void shuffle_deck(struct deck *deck) {
int random; int random;
if (!(stack = malloc(NUMBER_OF_CARDS * sizeof(*stack)))) { 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); exit(errno);
} }

View File

@ -30,7 +30,6 @@
#define MANEUVRE_5_STARTING_X 41 #define MANEUVRE_5_STARTING_X 41
#define MANEUVRE_6_STARTING_X 49 #define MANEUVRE_6_STARTING_X 49
extern const char *program_name;
struct deck *deck; struct deck *deck;
struct cursor *cursor; struct cursor *cursor;

View File

@ -8,7 +8,7 @@
void allocate_stack(struct stack **stack) { void allocate_stack(struct stack **stack) {
if (!(*stack = malloc(sizeof(**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); exit(errno);
} }

View File

@ -8,8 +8,6 @@ struct stack {
struct stack *next; struct stack *next;
}; };
extern const char *program_name;
void allocate_stack(struct stack **); void allocate_stack(struct stack **);
void initialize_stack(struct stack *); void initialize_stack(struct stack *);
struct stack *duplicate_stack(struct stack *); struct stack *duplicate_stack(struct stack *);