diff options
| author | Franklin Wei <git@fwei.tk> | 2015-05-17 19:08:10 -0400 |
|---|---|---|
| committer | Franklin Wei <git@fwei.tk> | 2015-05-17 19:08:10 -0400 |
| commit | 6fb6191f35abf3e7b4e9aeaea566d3d2f9cd276d (patch) | |
| tree | 48613c085df3b2b88bcbe37e765a7ffb880dde4a /src/main.c | |
| parent | b7efc1fa7c783729fcdfa5cd6fc0df1166b04556 (diff) | |
| download | market-sim-6fb6191f35abf3e7b4e9aeaea566d3d2f9cd276d.zip market-sim-6fb6191f35abf3e7b4e9aeaea566d3d2f9cd276d.tar.gz market-sim-6fb6191f35abf3e7b4e9aeaea566d3d2f9cd276d.tar.bz2 market-sim-6fb6191f35abf3e7b4e9aeaea566d3d2f9cd276d.tar.xz | |
random stuff
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 100 |
1 files changed, 14 insertions, 86 deletions
@@ -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? "); } } |