diff options
| author | Franklin Wei <git@fwei.tk> | 2015-05-31 18:40:52 -0400 |
|---|---|---|
| committer | Franklin Wei <git@fwei.tk> | 2015-05-31 18:40:52 -0400 |
| commit | 9ddbcfb5a63d2fb7105ab6fb2e7422858ba1615c (patch) | |
| tree | 81b0ee57f2d752bec02ebab45344f4eb9545e4b1 | |
| parent | d8745b2854d56c6687531a150153b2d28b0f6adb (diff) | |
| download | market-sim-9ddbcfb5a63d2fb7105ab6fb2e7422858ba1615c.zip market-sim-9ddbcfb5a63d2fb7105ab6fb2e7422858ba1615c.tar.gz market-sim-9ddbcfb5a63d2fb7105ab6fb2e7422858ba1615c.tar.bz2 market-sim-9ddbcfb5a63d2fb7105ab6fb2e7422858ba1615c.tar.xz | |
add quicksave option
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | src/globals.h | 3 | ||||
| -rw-r--r-- | src/load.c | 7 | ||||
| -rw-r--r-- | src/main.c | 30 | ||||
| -rw-r--r-- | src/save.c | 33 |
5 files changed, 58 insertions, 17 deletions
@@ -6,7 +6,7 @@ OBJ := $(SRC:.c=.o) GIT_VERSION := $(shell git describe --abbrev=8 --always --dirty) -CFLAGS = -Isrc/ -O2 -g -Wall -Wextra -std=gnu99 -DVERSION_INFO=\"$(GIT_VERSION)\" +CFLAGS = -Isrc/ -O2 -g -Wall -Wextra -std=gnu99 -DVERSION_INFO=\"$(GIT_VERSION)\" -fsanitize=address PREFIX = /usr BINDIR = $(PREFIX)/bin diff --git a/src/globals.h b/src/globals.h index c9ef461..7c5e816 100644 --- a/src/globals.h +++ b/src/globals.h @@ -93,7 +93,9 @@ struct player_t { struct money_t cash; uint portfolio_len; struct stock_t *portfolio; + bool need_to_free_portfolio; + char *filename; /* filename last loaded or written to, used for quicksave */ }; struct command_t { @@ -142,6 +144,7 @@ void curses_init(void); void do_menu(struct player_t*, const struct command_t*, uint, const char*); void fail(const char*, ...);; void load_portfolio(struct player_t*, const char*); +void save_portfolio(struct player_t*, const char*); void print_history(struct stock_t*); void print_usage(int argc, char *argv[]); void print_version(void); @@ -175,6 +175,11 @@ void load_portfolio(struct player_t *player, const char *filename) } while (!feof(f) && !ferror(f)); update_handler(player); + + if(player->filename && player->filename != filename) + free(player->filename); + + player->filename = (char*)filename; } void load_handler(struct player_t *player) @@ -188,6 +193,4 @@ void load_handler(struct player_t *player) char *filename = read_string(); load_portfolio(player, filename); - - free(filename); } @@ -10,6 +10,25 @@ void quit_handler(struct player_t *player) exit(EXIT_SUCCESS); } +void quicksave_handler(struct player_t *player) +{ + if(restricted) + { + output("Saving forbidden in restricted mode.\n"); + return; + } + + if(!player->filename) + { + save_handler(player); + return; + } + + output("Saving to '%s'.\n", player->filename); + + save_portfolio(player, player->filename); +} + int main(int argc, char *argv[]) { curl_global_init(CURL_GLOBAL_DEFAULT); @@ -48,7 +67,13 @@ int main(int argc, char *argv[]) html_out = true; if(args_status & ARG_LOADED) - load_portfolio(player, save_file); + { + /* save_file must be allocated with malloc(), make it so */ + char *filename = malloc(strlen(save_file) + 1); + strcpy(filename, save_file); + + load_portfolio(player, filename); + } else player->cash.cents = 1000 * 100; @@ -67,7 +92,8 @@ int main(int argc, char *argv[]) { "Stock [i]nfo", "info", info_handler }, { "[L]oad portfolio from disk", "load", load_handler }, { "[W]rite portfolio to disk", "write", save_handler }, - { "[Q]uit", "quit", quit_handler }, + { "[Q]uick save", "quicksave", quicksave_handler }, + { "[E]xit", "exit", quit_handler }, }; do_menu(player, commands, ARRAYLEN(commands), @@ -63,21 +63,11 @@ size_t ck_write(const char *buf, size_t sz, size_t nmemb, FILE *f) return nmemb; } -void save_handler(struct player_t *player) +void save_portfolio(struct player_t *player, const char *filename) { - if(restricted) - { - output("Saving forbidden in restricted mode.\n"); - return; - } - output("Enter the file to save your portfolio in: "); - - char *filename = read_string(); - output("Writing data...\n"); - FILE *f = fopen(filename, "wb"); - free(filename); + FILE *f = fopen(filename, "wb"); cksum = 0; @@ -120,5 +110,24 @@ void save_handler(struct player_t *player) fclose(f); + if(player->filename && player->filename != filename) + free(player->filename); + + player->filename = (char*)filename; + output("Done saving.\n"); } + +void save_handler(struct player_t *player) +{ + if(restricted) + { + output("Saving forbidden in restricted mode.\n"); + return; + } + output("Enter the file to save your portfolio in: "); + + char *filename = read_string(); + + save_portfolio(player, filename); +} |