From c5eda1eaab1c6f024a5675e776aa7912e9bd1374 Mon Sep 17 00:00:00 2001 From: John Zaitseff Date: Mon, 11 Jul 2011 18:07:51 +1000 Subject: [PATCH] Add code to the load_game() function The load_game() function now checks for errors while opening the game file. --- src/game.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++- src/intf.h | 4 ++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/game.c b/src/game.c index 78d778f..0fea794 100644 --- a/src/game.c +++ b/src/game.c @@ -31,6 +31,13 @@ #include "trader.h" +/************************************************************************ +* Module constants * +************************************************************************/ + +#define GAME_BUFSIZE (1024) /* Buffer size for game save/load */ + + /************************************************************************ * Game function definitions * ************************************************************************/ @@ -119,7 +126,7 @@ void init_game (void) } else { // Ask which game to load - newtxwin(5, 49, LINE_OFFSET + 6, COL_CENTER(49)); + newtxwin(5, 50, LINE_OFFSET + 6, COL_CENTER(50)); wbkgd(curwin, ATTR_NORMAL_WINDOW); box(curwin, 0, 0); @@ -405,8 +412,59 @@ void end_game (void) bool load_game (int num) { + char *buf, *filename; + FILE *file; + int saved_errno; + + assert((num >= 1) && (num <= 9)); + + buf = malloc(GAME_BUFSIZE); + if (buf == NULL) { + err_exit("out of memory"); + } + + filename = game_filename(num); + assert(filename != NULL); + + file = fopen(filename, "r"); + if (file == NULL) { + // File could not be opened + + if (errno == ENOENT) { + // File not found + newtxwin(7, 40, LINE_OFFSET + 9, COL_CENTER(40)); + wbkgd(curwin, ATTR_ERROR_WINDOW); + box(curwin, 0, 0); + + center(curwin, 1, ATTR_ERROR_TITLE, " Game not found "); + center(curwin, 3, ATTR_ERROR_STR, + "Game %d has not been saved to disk", num); + + wait_for_key(curwin, 5, ATTR_WAITERROR_STR); + deltxwin(); + } else { + // Some other file error + saved_errno = errno; + + newtxwin(9, 70, LINE_OFFSET + 9, COL_CENTER(70)); + wbkgd(curwin, ATTR_ERROR_WINDOW); + box(curwin, 0, 0); + + center(curwin, 1, ATTR_ERROR_TITLE, " Game not loaded "); + center(curwin, 3, ATTR_ERROR_STR, + "Game %d could not be loaded from disk", num); + center(curwin, 5, ATTR_ERROR_WINDOW, "File %s: %s", filename, + strerror(saved_errno)); + + wait_for_key(curwin, 7, ATTR_WAITERROR_STR); + deltxwin(); + } + + return false; + } + // @@@ To be written return false; } diff --git a/src/intf.h b/src/intf.h index b47d073..85536af 100644 --- a/src/intf.h +++ b/src/intf.h @@ -120,11 +120,15 @@ enum color_pairs { #define ATTR_ROOT_WINDOW ATTR(COLOR_PAIR(WHITE_ON_BLACK), A_NORMAL) #define ATTR_NORMAL_WINDOW ATTR(COLOR_PAIR(WHITE_ON_BLUE), A_NORMAL) #define ATTR_STATUS_WINDOW ATTR(COLOR_PAIR(BLACK_ON_WHITE), A_REVERSE) +#define ATTR_ERROR_WINDOW ATTR(COLOR_PAIR(WHITE_ON_RED), A_REVERSE) #define ATTR_WINDOW_TITLE ATTR(COLOR_PAIR(YELLOW_ON_BLACK) | A_BOLD, A_REVERSE) +#define ATTR_ERROR_TITLE ATTR(COLOR_PAIR(YELLOW_ON_BLACK) | A_BOLD, A_BOLD) #define ATTR_INPUT_FIELD ATTR(COLOR_PAIR(WHITE_ON_BLACK), A_BOLD | '_') #define ATTR_KEYCODE_STR ATTR(COLOR_PAIR(YELLOW_ON_BLACK) | A_BOLD, A_REVERSE) #define ATTR_HIGHLIGHT_STR ATTR(COLOR_PAIR(YELLOW_ON_BLUE) | A_BOLD, A_BOLD) +#define ATTR_ERROR_STR ATTR(COLOR_PAIR(WHITE_ON_RED) | A_BOLD, A_REVERSE) #define ATTR_WAITNORMAL_STR ATTR(COLOR_PAIR(CYAN_ON_BLUE), A_NORMAL) +#define ATTR_WAITERROR_STR ATTR(COLOR_PAIR(WHITE_ON_RED), A_REVERSE) /************************************************************************