diff options
| author | Franklin Wei <git@fwei.tk> | 2015-05-17 17:16:15 -0400 |
|---|---|---|
| committer | Franklin Wei <git@fwei.tk> | 2015-05-17 17:16:15 -0400 |
| commit | c13b48095f91b6c277854b9970c59c82943af74f (patch) | |
| tree | a9f9f0d9fdfa4c3158a50857c190393d5a2b03b5 /src | |
| parent | f1b434fd0e76d44056868ea4d2d8fd52138a663b (diff) | |
| download | market-sim-c13b48095f91b6c277854b9970c59c82943af74f.zip market-sim-c13b48095f91b6c277854b9970c59c82943af74f.tar.gz market-sim-c13b48095f91b6c277854b9970c59c82943af74f.tar.bz2 market-sim-c13b48095f91b6c277854b9970c59c82943af74f.tar.xz | |
functionize stuff, INTRODUCES MEMORY LEAKS
Diffstat (limited to 'src')
| -rw-r--r-- | src/buy.c | 9 | ||||
| -rw-r--r-- | src/globals.h | 4 | ||||
| -rw-r--r-- | src/info.c | 2 | ||||
| -rw-r--r-- | src/load.c | 5 | ||||
| -rw-r--r-- | src/main.c | 6 | ||||
| -rw-r--r-- | src/save.c | 9 | ||||
| -rw-r--r-- | src/sell.c | 13 | ||||
| -rw-r--r-- | src/util.c | 19 |
8 files changed, 37 insertions, 30 deletions
@@ -3,7 +3,7 @@ void buy_handler(struct player_t *player) { printf("Enter the ticker symbol of the stock you wish to purchase: "); - char *sym = get_ticker(); + char *sym = read_ticker(); struct money_t price; @@ -21,9 +21,7 @@ void buy_handler(struct player_t *player) printf("Price per share: $%llu.%02llu\n", price.cents / 100, price.cents % 100); printf("Enter the number of shares to be purchased (maximum %llu): ", player->cash.cents / price.cents); - ullong count = 0; - - scanf("%llu", &count); + ullong count = read_int(); if(count <= 0) { @@ -40,8 +38,7 @@ void buy_handler(struct player_t *player) } printf("This will cost $%llu.%02llu. Are you sure? ", cost / 100, cost % 100); - char response[16]; - scanf("%15s", response); + char *response = read_string(); all_lower(response); if(response[0] == 'y') diff --git a/src/globals.h b/src/globals.h index 9159c58..7da0238 100644 --- a/src/globals.h +++ b/src/globals.h @@ -75,7 +75,9 @@ uint16_t to_be16(uint16_t); struct stock_t *find_stock(struct player_t*, char*); void add_hist(struct stock_t*, enum history_action, ullong count); void print_history(struct stock_t*); -char *get_ticker(void); +char *read_ticker(void); +char *read_string(void); +ullong read_int(void); void buy_handler(struct player_t*); void sell_handler(struct player_t*); @@ -4,7 +4,7 @@ void info_handler(struct player_t *player) { char *sym; printf("Enter the ticker symbol of the stock to get information for: "); - sym = get_ticker(); + sym = read_ticker(); struct stock_t *stock = find_stock(player, sym); @@ -39,8 +39,7 @@ uint8_t read_int8(FILE *f) void load_handler(struct player_t *player) { printf("Enter the file to load portfolio from: "); - char buf[128]; - scanf("%127s", buf); + char *filename = read_string(); printf("Loading portfolio...\n"); @@ -48,7 +47,7 @@ void load_handler(struct player_t *player) free(player->portfolio); player->portfolio_len = 0; - FILE *f = fopen(buf, "rb"); + FILE *f = fopen(filename, "rb"); char magic[6]; if(!f || fread(magic, 1, sizeof(magic), f) != 6 || memcmp(magic, "PORTv2", sizeof(magic)) != 0) { @@ -58,14 +58,10 @@ int main(int argc, char *argv[]) } printf("What would you like to do? "); - char *cmdbuf = NULL; - size_t cmdlen = 0; - cmdlen = getline(&cmdbuf, &cmdlen, stdin); + char *cmdbuf = read_string(); all_lower(cmdbuf); - cmdbuf[cmdlen - 1] = '\0'; - /* find the best command */ int best_command = -1; @@ -37,11 +37,12 @@ void save_handler(struct player_t *player) { printf("Enter the file to save your portfolio in: "); - char buf[128]; - scanf("%127s", buf); + char *filename = read_string(); printf("Writing data...\n"); - FILE *f = fopen(buf, "wb"); + FILE *f = fopen(filename, "wb"); + + free(filename); const char *magic = "PORTv2"; fwrite(magic, strlen(magic), 1, f); @@ -81,5 +82,5 @@ void save_handler(struct player_t *player) fclose(f); - printf("Done saving."); + printf("Done saving.\n"); } @@ -2,10 +2,8 @@ void sell_handler(struct player_t *player) { - char *sym = malloc(16); printf("Enter the ticker symbol of the stock you wish to sell: "); - scanf("%15s", sym); - all_upper(sym); + char *sym = read_ticker(); printf("Getting stock information...\n"); @@ -26,9 +24,8 @@ void sell_handler(struct player_t *player) printf("You currently own %llu shares of '%s' (%s) valued at $%llu.%02llu each.\n", stock->count, stock->fullname, stock->symbol, stock->current_price.cents / 100, stock->current_price.cents % 100); - ullong sell_count = 0; printf("How many shares do you wish to sell? "); - scanf("%llu", &sell_count); + ullong sell_count = read_int(); if(!sell_count) { @@ -46,9 +43,7 @@ void sell_handler(struct player_t *player) printf("This will sell %llu shares for $%llu.%02llu total.\nProceed? ", sell_count, sell_total / 100, sell_total % 100); - char response[16]; - scanf("%15s", response); - all_lower(response); + char *response = read_string(); if(response[0] == 'y') { @@ -74,4 +69,6 @@ void sell_handler(struct player_t *player) { printf("Not confirmed.\n"); } + + free(response); } @@ -241,12 +241,27 @@ void print_handler(struct player_t *player) printf("Total capital: $%llu.%02llu\n", total / 100, total % 100); } -char* get_ticker(void) +char *read_string(void) { char *ret = NULL; size_t len = 0; len = getline(&ret, &len, stdin); - all_upper(ret); ret[len - 1] = '\0'; return ret; } + +char *read_ticker(void) +{ + char *str = read_string(); + all_upper(str); + return str; +} + +ullong read_int(void) +{ + char *str = read_string(); + ullong ret = -1; + sscanf(str, "%llu", &ret); + + return ret; +} |