From e5241a144e28fd3aaa803a592eb4656eddc53a27 Mon Sep 17 00:00:00 2001 From: dshaligram Date: Tue, 12 Dec 2006 10:39:05 +0000 Subject: Added -scorefile option to pass in a highscore file on the command-line for use with -[tv]scores. hiscores.cc silently ignores uids that don't exist on the current machine, instead of segfaulting. git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@618 c06c8d41-db1a-0410-9941-cceddc491573 --- crawl-ref/source/hiscores.cc | 53 ++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 21 deletions(-) (limited to 'crawl-ref/source/hiscores.cc') diff --git a/crawl-ref/source/hiscores.cc b/crawl-ref/source/hiscores.cc index 0ddc5f495f..8ebe79f1c6 100644 --- a/crawl-ref/source/hiscores.cc +++ b/crawl-ref/source/hiscores.cc @@ -64,8 +64,9 @@ static scorefile_entry hs_list[SCORE_FILE_ENTRIES]; // highscore printing (always -1 when run from command line). static int newest_entry = -1; -static FILE *hs_open(const char *mode, const char *filename); -static void hs_close(FILE *handle, const char *mode, const char *filename); +static FILE *hs_open(const char *mode, const std::string &filename); +static void hs_close(FILE *handle, const char *mode, + const std::string &filename); static bool hs_read(FILE *scores, scorefile_entry &dest); static void hs_parse_numeric(char *inbuf, scorefile_entry &dest); static void hs_parse_string(char *inbuf, scorefile_entry &dest); @@ -89,6 +90,19 @@ static bool lock_file_handle( FILE *handle, int type ); static bool unlock_file_handle( FILE *handle ); #endif // USE_FILE_LOCKING +std::string score_file_name() +{ + if (!SysEnv.scorefile.empty()) + return (SysEnv.scorefile); + + return (Options.save_dir + "scores"); +} + +std::string log_file_name() +{ + return (Options.save_dir + "logfile"); +} + void hiscores_new_entry( const scorefile_entry &ne ) { FILE *scores; @@ -96,7 +110,7 @@ void hiscores_new_entry( const scorefile_entry &ne ) bool inserted = false; // open highscore file (reading) -- note that NULL is not fatal! - scores = hs_open("r", "scores"); + scores = hs_open("r", score_file_name()); for (i = 0; i < SCORE_FILE_ENTRIES; ++i) hs_list[i].reset(); @@ -139,10 +153,10 @@ void hiscores_new_entry( const scorefile_entry &ne ) total_entries = i; // close so we can re-open for writing - hs_close(scores,"r", "scores"); + hs_close(scores,"r", score_file_name()); // open highscore file (writing) -- NULL *is* fatal here. - scores = hs_open("w", "scores"); + scores = hs_open("w", score_file_name()); if (scores == NULL) { perror("Entry not added - failure opening score file for writing."); @@ -156,7 +170,7 @@ void hiscores_new_entry( const scorefile_entry &ne ) } // close scorefile. - hs_close(scores, "w", "scores"); + hs_close(scores, "w", score_file_name()); } void logfile_new_entry( const scorefile_entry &ne ) @@ -165,7 +179,7 @@ void logfile_new_entry( const scorefile_entry &ne ) scorefile_entry le = ne; // open logfile (appending) -- NULL *is* fatal here. - logfile = hs_open("a", "logfile"); + logfile = hs_open("a", log_file_name()); if (logfile == NULL) { perror("Entry not added - failure opening logfile for appending."); @@ -175,7 +189,7 @@ void logfile_new_entry( const scorefile_entry &ne ) hs_write(logfile, le); // close logfile. - hs_close(logfile, "a", "logfile"); + hs_close(logfile, "a", log_file_name()); } void hiscores_print_list( int display_count, int format ) @@ -188,7 +202,7 @@ void hiscores_print_list( int display_count, int format ) display_count = SCORE_FILE_ENTRIES; // open highscore file (reading) - scores = hs_open("r", "scores"); + scores = hs_open("r", score_file_name()); if (scores == NULL) { // will only happen from command line @@ -205,7 +219,7 @@ void hiscores_print_list( int display_count, int format ) total_entries = i; // close off - hs_close( scores, "r", "scores" ); + hs_close( scores, "r", score_file_name() ); if (!use_printf) textcolor(LIGHTGREY); @@ -402,10 +416,8 @@ static bool unlock_file_handle( FILE *handle ) -FILE *hs_open( const char *mode, const char *filename ) +FILE *hs_open( const char *mode, const std::string &scores ) { - const std::string scores = Options.save_dir + filename; - FILE *handle = fopen(scores.c_str(), mode); #ifdef SHARED_FILES_CHMOD_PUBLIC chmod(scores.c_str(), SHARED_FILES_CHMOD_PUBLIC); @@ -426,7 +438,7 @@ FILE *hs_open( const char *mode, const char *filename ) return handle; } -void hs_close( FILE *handle, const char *mode, const char *filename ) +void hs_close( FILE *handle, const char *mode, const std::string &scores ) { UNUSED( mode ); @@ -442,11 +454,7 @@ void hs_close( FILE *handle, const char *mode, const char *filename ) #ifdef SHARED_FILES_CHMOD_PUBLIC if (stricmp(mode, "w") == 0) - { - std::string scores = Options.save_dir; - scores += filename; chmod(scores.c_str(), SHARED_FILES_CHMOD_PUBLIC); - } #endif } @@ -1367,9 +1375,12 @@ std::string scorefile_entry::game_time(death_desc_verbosity verbosity) const if (uid > 0) { struct passwd *pw_entry = getpwuid( uid ); - strncpy( username, pw_entry->pw_name, sizeof(username) ); - strncat( username, "'s", sizeof(username) ); - username[0] = toupper( username[0] ); + if (pw_entry) + { + strncpy( username, pw_entry->pw_name, sizeof(username) ); + strncat( username, "'s", sizeof(username) ); + username[0] = toupper( username[0] ); + } } #endif -- cgit v1.2.3-54-g00ecf