diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/buy.c | 4 | ||||
| -rw-r--r-- | src/globals.h | 6 | ||||
| -rw-r--r-- | src/load.c | 4 | ||||
| -rw-r--r-- | src/main.c | 46 | ||||
| -rw-r--r-- | src/util.c | 44 |
5 files changed, 64 insertions, 40 deletions
@@ -2,10 +2,8 @@ void buy_handler(struct player_t *player) { - char *sym = malloc(16); printf("Enter the ticker symbol of the stock you wish to purchase: "); - scanf("%15s", sym); - all_upper(sym); + char *sym = get_ticker(); struct money_t price; diff --git a/src/globals.h b/src/globals.h index a699434..9159c58 100644 --- a/src/globals.h +++ b/src/globals.h @@ -1,3 +1,4 @@ +#include <assert.h> #include <ctype.h> #include <stdbool.h> #include <stdint.h> @@ -73,11 +74,14 @@ uint16_t to_sys16(uint16_t); 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); void buy_handler(struct player_t*); void sell_handler(struct player_t*); -void history_handler(struct player_t*); +void info_handler(struct player_t*); void update_handler(struct player_t*); void save_handler(struct player_t*); void load_handler(struct player_t*); void quit_handler(struct player_t*); +void print_handler(struct player_t*); @@ -2,7 +2,7 @@ /* NOTE: integers are represented internally by unsigned long long ints, but in the save they are always 64 bits */ -#define FAIL() exit(*(char*)NULL); +#define FAIL() exit(EXIT_FAILURE); uint64_t read_be64(FILE *f) { @@ -52,7 +52,7 @@ void load_handler(struct player_t *player) char magic[6]; if(!f || fread(magic, 1, sizeof(magic), f) != 6 || memcmp(magic, "PORTv2", sizeof(magic)) != 0) { - printf("FATAL: Failed to load save."); + printf("FATAL: Failed to load save.\n"); exit(EXIT_FAILURE); } @@ -31,39 +31,10 @@ int main(int argc, char *argv[]) update_handler(player); + print_handler(player); + while(1) { - printf("\nYour portfolio:\n"); - printf("================================================================================\n"); - - ullong portfolio_value = 0; - - if(player->portfolio_len == 0) - { - printf(" < EMPTY >\n"); - } - else - { - for(int i = 0; i < player->portfolio_len; ++i) - { - struct stock_t *stock = player->portfolio + i; - ullong total_value = stock->count * stock->current_price.cents; - printf("%6s %40s %5llu * $%5llu.%02llu = $%6llu.%02llu\n", - stock->symbol, stock->fullname, stock->count, stock->current_price.cents / 100, stock->current_price.cents % 100, - total_value / 100, total_value % 100); - - portfolio_value += stock->current_price.cents * stock->count; - } - } - printf("================================================================================\n"); - - printf("Portfolio value: $%llu.%02llu\n", portfolio_value / 100, portfolio_value % 100); - - printf("Current cash: $%llu.%02llu\n", player->cash.cents / 100, player->cash.cents % 100); - - ullong total = portfolio_value + player->cash.cents; - printf("Total capital: $%llu.%02llu\n", total / 100, total % 100); - struct command_t { const char *name; const char *command; @@ -73,8 +44,9 @@ int main(int argc, char *argv[]) const struct command_t commands[] = { { "[B]uy", "buy", buy_handler }, { "[S]ell", "sell", sell_handler }, + { "[P]rint portfolio", "print", print_handler }, { "[U]pdate stock prices", "update", update_handler }, - { "Stock [i]nfo", "info", history_handler }, + { "Stock [i]nfo", "info", info_handler }, { "[W]rite portfolio", "write", save_handler }, { "[L]oad portfolio", "load", load_handler }, { "[Q]uit", "quit", quit_handler }, @@ -86,10 +58,14 @@ int main(int argc, char *argv[]) } printf("What would you like to do? "); - char cmdbuf[32]; - scanf("%31s", cmdbuf); + char *cmdbuf = NULL; + size_t cmdlen = 0; + cmdlen = getline(&cmdbuf, &cmdlen, stdin); all_lower(cmdbuf); + + cmdbuf[cmdlen - 1] = '\0'; + /* find the best command */ int best_command = -1; @@ -141,5 +117,7 @@ int main(int argc, char *argv[]) if(best_command >= 0) commands[best_command].handler(player); + + printf("\n"); } } @@ -206,3 +206,47 @@ struct stock_t *find_stock(struct player_t *player, char *sym) return NULL; } + +void print_handler(struct player_t *player) +{ + printf("\nYour portfolio:\n"); + printf("================================================================================\n"); + + ullong portfolio_value = 0; + + if(player->portfolio_len == 0) + { + printf(" < EMPTY >\n"); + } + else + { + for(int i = 0; i < player->portfolio_len; ++i) + { + struct stock_t *stock = player->portfolio + i; + ullong total_value = stock->count * stock->current_price.cents; + printf("%6s %40s %5llu * $%5llu.%02llu = $%6llu.%02llu\n", + stock->symbol, stock->fullname, stock->count, stock->current_price.cents / 100, stock->current_price.cents % 100, + total_value / 100, total_value % 100); + + portfolio_value += stock->current_price.cents * stock->count; + } + } + printf("================================================================================\n"); + + printf("Portfolio value: $%llu.%02llu\n", portfolio_value / 100, portfolio_value % 100); + + printf("Current cash: $%llu.%02llu\n", player->cash.cents / 100, player->cash.cents % 100); + + ullong total = portfolio_value + player->cash.cents; + printf("Total capital: $%llu.%02llu\n", total / 100, total % 100); +} + +char* get_ticker(void) +{ + char *ret = NULL; + size_t len = 0; + len = getline(&ret, &len, stdin); + all_upper(ret); + ret[len - 1] = '\0'; + return ret; +} |