diff options
author | dshaligram <dshaligram@c06c8d41-db1a-0410-9941-cceddc491573> | 2006-12-12 10:39:05 +0000 |
---|---|---|
committer | dshaligram <dshaligram@c06c8d41-db1a-0410-9941-cceddc491573> | 2006-12-12 10:39:05 +0000 |
commit | e5241a144e28fd3aaa803a592eb4656eddc53a27 (patch) | |
tree | 190f76c26c1ba9659c939dde54ac3077bd33a55a | |
parent | 336db3e336c38c44f8ff0b6b8bf81c14e6d9a8d8 (diff) | |
download | crawl-ref-e5241a144e28fd3aaa803a592eb4656eddc53a27.tar.gz crawl-ref-e5241a144e28fd3aaa803a592eb4656eddc53a27.zip |
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
-rw-r--r-- | crawl-ref/source/acr.cc | 13 | ||||
-rw-r--r-- | crawl-ref/source/externs.h | 2 | ||||
-rw-r--r-- | crawl-ref/source/hiscores.cc | 53 | ||||
-rw-r--r-- | crawl-ref/source/initfile.cc | 67 |
4 files changed, 82 insertions, 53 deletions
diff --git a/crawl-ref/source/acr.cc b/crawl-ref/source/acr.cc index d5225c6ebe..a382847a27 100644 --- a/crawl-ref/source/acr.cc +++ b/crawl-ref/source/acr.cc @@ -194,9 +194,10 @@ int main( int argc, char *argv[] ) puts("environment options (CRAWL_NAME, CRAWL_PIZZA, CRAWL_DIR, CRAWL_RC)."); puts(""); puts("Highscore list options: (Can now be redirected to more, etc)"); - puts(" -scores [N] highscore list"); - puts(" -tscores [N] terse highscore list"); - puts(" -vscores [N] verbose highscore list"); + puts(" -scores [N] highscore list"); + puts(" -tscores [N] terse highscore list"); + puts(" -vscores [N] verbose highscore list"); + puts(" -scorefile <filename> scorefile to report on"); exit(1); } @@ -212,6 +213,12 @@ int main( int argc, char *argv[] ) hiscores_print_list( Options.sc_entries, Options.sc_format ); exit(0); } + else + { + // Don't allow scorefile override for actual gameplay, only for + // score listings. + SysEnv.scorefile.clear(); + } bool game_start = initialise(); diff --git a/crawl-ref/source/externs.h b/crawl-ref/source/externs.h index 0992c8451d..7278c08c5b 100644 --- a/crawl-ref/source/externs.h +++ b/crawl-ref/source/externs.h @@ -699,7 +699,7 @@ struct system_environment char *home; // only used by MULTIUSER systems bool board_with_nail; // Easter Egg silliness - std::string crawl_executable_path; // path to crawl from argv[0] + std::string scorefile; }; extern system_environment SysEnv; 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 diff --git a/crawl-ref/source/initfile.cc b/crawl-ref/source/initfile.cc index 585c31d5a5..86225d4ffc 100644 --- a/crawl-ref/source/initfile.cc +++ b/crawl-ref/source/initfile.cc @@ -2193,26 +2193,32 @@ void get_system_environment(void) // parse args, filling in Options and game environment as we go. // returns true if no unknown or malformed arguments were found. +// Keep this in sync with the option names. +enum commandline_option_type { + CLO_SCORES, + CLO_NAME, + CLO_RACE, + CLO_CLASS, + CLO_PIZZA, + CLO_PLAIN, + CLO_DIR, + CLO_RC, + CLO_TSCORES, + CLO_VSCORES, + CLO_SCOREFILE, + + CLO_NOPS +}; + static const char *cmd_ops[] = { "scores", "name", "race", "class", "pizza", "plain", "dir", "rc", "tscores", - "vscores" }; + "vscores", "scorefile" }; -const int num_cmd_ops = 10; +const int num_cmd_ops = CLO_NOPS; bool arg_seen[num_cmd_ops]; -static void set_crawl_path(const std::string &path) -{ - const std::string::size_type slash = path.rfind(FILE_SEPARATOR); - SysEnv.crawl_executable_path = - slash != std::string::npos? path.substr(0, slash + 1) - : std::string(""); -} - bool parse_args( int argc, char **argv, bool rc_only ) { - if (argc >= 1) - set_crawl_path(argv[0]); - if (argc < 2) // no args! return (true); @@ -2277,9 +2283,9 @@ bool parse_args( int argc, char **argv, bool rc_only ) //.take action according to the cmd chosen switch(o) { - case 0: // scores - case 8: // tscores - case 9: // vscores + case CLO_SCORES: + case CLO_TSCORES: + case CLO_VSCORES: if (!next_is_param) ecount = SCORE_FILE_ENTRIES; // default else // optional number given @@ -2298,26 +2304,31 @@ bool parse_args( int argc, char **argv, bool rc_only ) { Options.sc_entries = ecount; - if (o == 8) + if (o == CLO_TSCORES) Options.sc_format = SCORE_TERSE; - else if (o == 9) + else if (o == CLO_VSCORES) Options.sc_format = SCORE_VERBOSE; - } break; - case 1: // name + case CLO_SCOREFILE: + if (!next_is_param) + return (false); + if (!rc_only) + SysEnv.scorefile = next_arg; + nextUsed = true; + break; + + case CLO_NAME: if (!next_is_param) return (false); - if (!rc_only) Options.player_name = next_arg; - nextUsed = true; break; - case 2: // race - case 3: // class + case CLO_RACE: + case CLO_CLASS: if (!next_is_param) return (false); @@ -2335,7 +2346,7 @@ bool parse_args( int argc, char **argv, bool rc_only ) nextUsed = true; break; - case 4: // pizza + case CLO_PIZZA: if (!next_is_param) return (false); @@ -2345,7 +2356,7 @@ bool parse_args( int argc, char **argv, bool rc_only ) nextUsed = true; break; - case 5: // plain + case CLO_PLAIN: if (next_is_param) return (false); @@ -2356,7 +2367,7 @@ bool parse_args( int argc, char **argv, bool rc_only ) } break; - case 6: // dir + case CLO_DIR: // ALWAYS PARSE if (!next_is_param) return (false); @@ -2365,7 +2376,7 @@ bool parse_args( int argc, char **argv, bool rc_only ) nextUsed = true; break; - case 7: + case CLO_RC: // ALWAYS PARSE if (!next_is_param) return (false); |