diff options
Diffstat (limited to 'crawl-ref')
-rw-r--r-- | crawl-ref/source/files.cc | 47 | ||||
-rw-r--r-- | crawl-ref/source/stuff.cc | 28 | ||||
-rw-r--r-- | crawl-ref/source/stuff.h | 3 |
3 files changed, 41 insertions, 37 deletions
diff --git a/crawl-ref/source/files.cc b/crawl-ref/source/files.cc index f975b53552..cb71f6161c 100644 --- a/crawl-ref/source/files.cc +++ b/crawl-ref/source/files.cc @@ -754,8 +754,7 @@ void load( unsigned char stair_taken, int load_mode, bool was_a_labyrinth, if (!determine_level_version( levelFile, majorVersion, minorVersion )) { - perror("\nLevel file appears to be invalid.\n"); - end(-1); + end(-1, false, "\nLevel file appears to be invalid.\n"); } restore_level_version( levelFile, majorVersion, minorVersion ); @@ -763,9 +762,8 @@ void load( unsigned char stair_taken, int load_mode, bool was_a_labyrinth, // sanity check - EOF if (!feof( levelFile )) { - snprintf( info, INFO_SIZE, "\nIncomplete read of \"%s\" - aborting.\n", cha_fil.c_str()); - perror(info); - end(-1); + end(-1, false, "\nIncomplete read of \"%s\" - aborting.\n", + cha_fil.c_str()); } fclose( levelFile ); @@ -1142,10 +1140,8 @@ void save_level(int level_saved, bool was_a_labyrinth, char where_were_you) if (saveFile == NULL) { - snprintf(info, INFO_SIZE, "Unable to open \"%s\" for writing!", + end(-1, true, "Unable to open \"%s\" for writing", cha_fil.c_str()); - perror(info); - end(-1); } // nail all items to the ground @@ -1209,12 +1205,8 @@ void save_game(bool leave_game) std::string charFile = get_savedir_filename(you.your_name, "", "sav"); FILE *charf = fopen(charFile.c_str(), "wb"); - if (!charf) { - snprintf(info, INFO_SIZE, "Unable to open \"%s\" for writing!\n", - charFile.c_str()); - perror(info); - end(-1); - } + if (!charf) + end(-1, true, "Unable to open \"%s\" for writing!\n", charFile.c_str()); write_tagged_file( charf, SAVE_MAJOR_VERSION, 0, TAGTYPE_PLAYER ); @@ -1360,12 +1352,7 @@ void restore_game(void) std::string charFile = get_savedir_filename(you.your_name, "", "sav"); FILE *charf = fopen(charFile.c_str(), "rb"); if (!charf ) - { - snprintf(info, INFO_SIZE, "Unable to open %s for reading!\n", - charFile.c_str() ); - perror(info); - end(-1); - } + end(-1, true, "Unable to open %s for reading!\n", charFile.c_str() ); char majorVersion = 0; char minorVersion = 0; @@ -1380,12 +1367,8 @@ void restore_game(void) // sanity check - EOF if (!feof(charf)) - { - snprintf( info, INFO_SIZE, "\nIncomplete read of \"%s\" - aborting.\n", - charFile.c_str()); - perror(info); - end(-1); - } + end(-1, false, "\nIncomplete read of \"%s\" - aborting.\n", + charFile.c_str()); fclose(charf); @@ -1448,10 +1431,8 @@ static void restore_version( FILE *restoreFile, // savefile versions. if (majorVersion != SAVE_MAJOR_VERSION) { - snprintf( info, INFO_SIZE, "\nSorry, this release cannot read a v%d.%d savefile.\n", + end(-1, false, "\nSorry, this release cannot read a v%d.%d savefile.\n", majorVersion, minorVersion); - perror(info); - end(-1); } switch(majorVersion) @@ -1516,10 +1497,9 @@ static void restore_level_version( FILE *levelFile, // savefile versions. if (majorVersion != SAVE_MAJOR_VERSION) { - snprintf( info, INFO_SIZE, "\nSorry, this release cannot read a v%d.%d level file.\n", + end(-1, false, + "\nSorry, this release cannot read a v%d.%d level file.\n", majorVersion, minorVersion); - perror(info); - end(-1); } switch(majorVersion) @@ -2159,8 +2139,7 @@ std::string readString(FILE *file) return (buf); } - fprintf(stderr, "String too long: %d bytes\n", length); - end(1); + end(1, false, "String too long: %d bytes\n", length); } return (""); diff --git a/crawl-ref/source/stuff.cc b/crawl-ref/source/stuff.cc index 521edb0699..cdbc70b862 100644 --- a/crawl-ref/source/stuff.cc +++ b/crawl-ref/source/stuff.cc @@ -479,10 +479,34 @@ void io_cleanup() #endif } -void end(int end_arg) +void end(int exit_code, bool print_error, const char *format, ...) { + std::string error = print_error? strerror(errno) : ""; + io_cleanup(); - exit(end_arg); + + if (format) + { + va_list arg; + va_start(arg, format); + char buffer[500]; + vsnprintf(buffer, sizeof buffer, format, arg); + va_end(arg); + + if (error.empty()) + error = std::string(buffer); + else + error = std::string(buffer) + ": " + error; + } + + if (error.length()) + { + if (error[error.length() - 1] != '\n') + error += "\n"; + fprintf(stderr, "%s", error.c_str()); + } + + exit(exit_code); } void redraw_screen(void) diff --git a/crawl-ref/source/stuff.h b/crawl-ref/source/stuff.h index 74fc563b95..9bda9bfabb 100644 --- a/crawl-ref/source/stuff.h +++ b/crawl-ref/source/stuff.h @@ -52,7 +52,8 @@ int skill_bump( int skill ); unsigned char get_ch(void); void io_cleanup(); -void end(int end_arg); +void end(int exit_code, bool print_err = false, + const char *message = NULL, ...); void modify_all_stats(int STmod, int IQmod, int DXmod); |