1
0
mirror of https://git.zap.org.au/git/trader.git synced 2024-12-04 14:46:45 -05:00

Add code to the load_game() function

The load_game() function now checks for errors while opening the game
file.
This commit is contained in:
John Zaitseff 2011-07-11 18:07:51 +10:00
parent 89c3512a1b
commit c5eda1eaab
2 changed files with 63 additions and 1 deletions

View File

@ -31,6 +31,13 @@
#include "trader.h" #include "trader.h"
/************************************************************************
* Module constants *
************************************************************************/
#define GAME_BUFSIZE (1024) /* Buffer size for game save/load */
/************************************************************************ /************************************************************************
* Game function definitions * * Game function definitions *
************************************************************************/ ************************************************************************/
@ -119,7 +126,7 @@ void init_game (void)
} else { } else {
// Ask which game to load // 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); wbkgd(curwin, ATTR_NORMAL_WINDOW);
box(curwin, 0, 0); box(curwin, 0, 0);
@ -405,8 +412,59 @@ void end_game (void)
bool load_game (int num) bool load_game (int num)
{ {
char *buf, *filename;
FILE *file;
int saved_errno;
assert((num >= 1) && (num <= 9)); 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 // @@@ To be written
return false; return false;
} }

View File

@ -120,11 +120,15 @@ enum color_pairs {
#define ATTR_ROOT_WINDOW ATTR(COLOR_PAIR(WHITE_ON_BLACK), A_NORMAL) #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_NORMAL_WINDOW ATTR(COLOR_PAIR(WHITE_ON_BLUE), A_NORMAL)
#define ATTR_STATUS_WINDOW ATTR(COLOR_PAIR(BLACK_ON_WHITE), A_REVERSE) #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_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_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_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_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_WAITNORMAL_STR ATTR(COLOR_PAIR(CYAN_ON_BLUE), A_NORMAL)
#define ATTR_WAITERROR_STR ATTR(COLOR_PAIR(WHITE_ON_RED), A_REVERSE)
/************************************************************************ /************************************************************************