From 6fb6191f35abf3e7b4e9aeaea566d3d2f9cd276d Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Sun, 17 May 2015 19:08:10 -0400 Subject: random stuff --- src/debug.c | 11 +++++++ src/globals.h | 16 ++++++++++ src/help.c | 6 ++++ src/load.c | 1 + src/main.c | 100 ++++++++-------------------------------------------------- src/menu.c | 71 +++++++++++++++++++++++++++++++++++++++++ src/sell.c | 4 +-- src/util.c | 31 +++++++++++++++--- 8 files changed, 147 insertions(+), 93 deletions(-) create mode 100644 src/debug.c create mode 100644 src/help.c create mode 100644 src/menu.c (limited to 'src') diff --git a/src/debug.c b/src/debug.c new file mode 100644 index 0000000..fe0d7cb --- /dev/null +++ b/src/debug.c @@ -0,0 +1,11 @@ +#include "globals.h" + +void debug_init(void) +{ + +} + +void debug_handler(struct player_t *player) +{ + printf("go away\n"); +} diff --git a/src/globals.h b/src/globals.h index 7da0238..6411841 100644 --- a/src/globals.h +++ b/src/globals.h @@ -60,6 +60,12 @@ struct player_t { bool need_to_free_portfolio; }; +struct command_t { + const char *name; + const char *command; + void (*handler)(struct player_t*); +}; + /*** prototypes ***/ void cleanup(void); int compare_stocks(const void*, const void*); @@ -78,6 +84,7 @@ void print_history(struct stock_t*); char *read_ticker(void); char *read_string(void); ullong read_int(void); +void parse_args(int argc, char *argv[]); void buy_handler(struct player_t*); void sell_handler(struct player_t*); @@ -87,3 +94,12 @@ void save_handler(struct player_t*); void load_handler(struct player_t*); void quit_handler(struct player_t*); void print_handler(struct player_t*); + +#ifndef NDEBUG + +void debug_handler(struct player_t*); +void debug_init(void); + +#endif + +void do_menu(struct player_t*, const struct command_t*, uint len, const char *prompt); diff --git a/src/help.c b/src/help.c new file mode 100644 index 0000000..d4ce8e4 --- /dev/null +++ b/src/help.c @@ -0,0 +1,6 @@ +#include "globals.h" + +void help_handler(struct player_t *player) +{ + printf("not implemented\n"); +} diff --git a/src/load.c b/src/load.c index f8f7f32..c2e5b3d 100644 --- a/src/load.c +++ b/src/load.c @@ -45,6 +45,7 @@ void load_handler(struct player_t *player) if(player->need_to_free_portfolio) free(player->portfolio); + player->portfolio_len = 0; player->portfolio = NULL; diff --git a/src/main.c b/src/main.c index aea80c8..bb703b5 100644 --- a/src/main.c +++ b/src/main.c @@ -2,26 +2,22 @@ /*** utility functions ***/ -void update_handler(struct player_t *player) -{ - printf("Updating stock prices...\n"); - for(int i = 0; i < player->portfolio_len; ++i) - { - struct stock_t *stock = player->portfolio + i; - printf("%s...\n", stock->symbol); - get_stock_info(stock->symbol, &stock->current_price, &stock->fullname); - } -} - void quit_handler(struct player_t *player) { + (void) player; exit(EXIT_SUCCESS); } int main(int argc, char *argv[]) { + parse_args(argc, argv); + atexit(cleanup); +#ifndef NDEBUG + debug_init(); +#endif + curl_global_init(CURL_GLOBAL_DEFAULT); struct player_t *player = malloc(sizeof(struct player_t)); @@ -29,93 +25,25 @@ int main(int argc, char *argv[]) player->cash.cents = 1000000 * 100; - update_handler(player); - - print_handler(player); - while(1) { - struct command_t { - const char *name; - const char *command; - void (*handler)(struct player_t*); - }; - 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", info_handler }, + { "[H]elp", "help", help_handler }, { "[W]rite portfolio", "write", save_handler }, { "[L]oad portfolio", "load", load_handler }, +#ifndef NDEBUG + { "[D]ebug menu", "debug", debug_handler }, +#endif { "[Q]uit", "quit", quit_handler }, }; - - for(uint i = 0; i < ARRAYLEN(commands); ++i) - { - printf("%d. %s\n", i + 1, commands[i].name); - } - - printf("What would you like to do? "); - char *cmdbuf = read_string(); - - all_lower(cmdbuf); - - /* find the best command */ - - int best_command = -1; - - /* first, search for an exact match */ - for(int i = 0; i < ARRAYLEN(commands); ++i) - { - if(strcmp(cmdbuf, commands[i].command) == 0) - { - best_command = i; - goto exec_cmd; - } - } - - /* now look for a partial match */ - for(int i = 0; i < ARRAYLEN(commands); ++i) - { - int len = strlen(cmdbuf); - if(len > strlen(commands[i].command)) - continue; - for(int j = 1; j <= len; ++j) - { - char *buf1 = malloc(j + 1); - memset(buf1, 0, j + 1); - memcpy(buf1, cmdbuf, j); - buf1[j] = '\0'; - - char *buf2 = malloc(j + 1); - memset(buf2, 0, j + 1); - memcpy(buf2, commands[i].command, j); - buf2[j] = '\0'; - - if(strcmp(buf1, buf2) == 0) - { - best_command = i; - free(buf1); - free(buf2); - goto exec_cmd; - } - else - { - free(buf1); - free(buf2); - } - } - } - - exec_cmd: - - if(best_command >= 0) - commands[best_command].handler(player); - - free(cmdbuf); - + print_handler(player); printf("\n"); + + do_menu(player, commands, ARRAYLEN(commands), "What would you like to do? "); } } diff --git a/src/menu.c b/src/menu.c new file mode 100644 index 0000000..cf9226d --- /dev/null +++ b/src/menu.c @@ -0,0 +1,71 @@ +#include "globals.h" + +void do_menu(struct player_t *player, const struct command_t *commands, uint len, const char *prompt) +{ + for(uint i = 0; i < len; ++i) + { + printf("%d. %s\n", i + 1, commands[i].name); + } + + printf("%s", prompt); + char *cmdbuf = read_string(); + + all_lower(cmdbuf); + + /* find the best command */ + + int best_command = -1; + + /* first, search for an exact match */ + for(uint i = 0; i < len; ++i) + { + if(strcmp(cmdbuf, commands[i].command) == 0) + { + best_command = i; + goto exec_cmd; + } + } + + /* now look for a partial match */ + for(uint i = 0; i < len; ++i) + { + uint len = strlen(cmdbuf); + if(len > strlen(commands[i].command)) + continue; + for(uint j = 1; j <= len; ++j) + { + char *buf1 = malloc(j + 1); + memset(buf1, 0, j + 1); + memcpy(buf1, cmdbuf, j); + buf1[j] = '\0'; + + char *buf2 = malloc(j + 1); + memset(buf2, 0, j + 1); + memcpy(buf2, commands[i].command, j); + buf2[j] = '\0'; + + if(strcmp(buf1, buf2) == 0) + { + best_command = i; + free(buf1); + free(buf2); + goto exec_cmd; + } + else + { + free(buf1); + free(buf2); + } + } + } + +exec_cmd: + + if(best_command >= 0) + { + commands[best_command].handler(player); + printf("\n"); + } + + free(cmdbuf); +} diff --git a/src/sell.c b/src/sell.c index da5aaa0..89c6635 100644 --- a/src/sell.c +++ b/src/sell.c @@ -44,7 +44,7 @@ void sell_handler(struct player_t *player) ullong sell_total = stock->current_price.cents * sell_count; - printf("This will sell %llu shares for $%llu.%02llu total.\nProceed? ", sell_count, sell_total / 100, sell_total % 100); + printf("This will sell %llu share(s) for $%llu.%02llu total.\nProceed? ", sell_count, sell_total / 100, sell_total % 100); char *response = read_string(); @@ -67,7 +67,7 @@ void sell_handler(struct player_t *player) add_hist(stock, SELL, sell_count); - printf("%llu shares sold for $%llu.%02llu total.\n", sell_count, sell_total / 100, sell_total % 100); + printf("%llu share(s) sold for $%llu.%02llu total.\n", sell_count, sell_total / 100, sell_total % 100); } else { diff --git a/src/util.c b/src/util.c index 0740a09..5dd902d 100644 --- a/src/util.c +++ b/src/util.c @@ -196,7 +196,7 @@ uint16_t to_sys16(uint16_t n) struct stock_t *find_stock(struct player_t *player, char *sym) { - for(int i = 0; i < player->portfolio_len; ++i) + for(uint i = 0; i < player->portfolio_len; ++i) { if(strcmp(player->portfolio[i].symbol, sym) == 0) { @@ -209,7 +209,7 @@ struct stock_t *find_stock(struct player_t *player, char *sym) void print_handler(struct player_t *player) { - printf("\nYour portfolio:\n"); + printf("Your portfolio:\n"); printf("================================================================================\n"); ullong portfolio_value = 0; @@ -220,7 +220,7 @@ void print_handler(struct player_t *player) } else { - for(int i = 0; i < player->portfolio_len; ++i) + for(uint i = 0; i < player->portfolio_len; ++i) { struct stock_t *stock = player->portfolio + i; ullong total_value = stock->count * stock->current_price.cents; @@ -235,9 +235,10 @@ void print_handler(struct player_t *player) 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); + printf("Cash balance: $%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); } @@ -246,7 +247,8 @@ char *read_string(void) char *ret = NULL; size_t len = 0; len = getline(&ret, &len, stdin); - ret[len - 1] = '\0'; + if(len) + ret[len - 1] = '\0'; return ret; } @@ -267,3 +269,22 @@ ullong read_int(void) return ret; } + +void update_handler(struct player_t *player) +{ + printf("Updating stock prices...\n"); + for(uint i = 0; i < player->portfolio_len; ++i) + { + struct stock_t *stock = player->portfolio + i; + printf("%s...\n", stock->symbol); + get_stock_info(stock->symbol, &stock->current_price, &stock->fullname); + } +} + +void parse_args(int argc, char *argv[]) +{ + for(int i = 1; i < argc; ++i) + { + + } +} -- cgit v1.1