aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2015-05-16 14:14:16 -0400
committerFranklin Wei <git@fwei.tk>2015-05-16 14:14:16 -0400
commit2bb97375378c24bc62fea9445f5b445291e0a9ee (patch)
tree07b505348c524be452cf85121192731d2a1d2738
parent2ffadc256f7d7e5f0c3fc5f58aef4191eaa2607b (diff)
downloadmarket-sim-2bb97375378c24bc62fea9445f5b445291e0a9ee.zip
market-sim-2bb97375378c24bc62fea9445f5b445291e0a9ee.tar.gz
market-sim-2bb97375378c24bc62fea9445f5b445291e0a9ee.tar.bz2
market-sim-2bb97375378c24bc62fea9445f5b445291e0a9ee.tar.xz
stuff
-rw-r--r--Makefile4
-rw-r--r--src/buy.c4
-rw-r--r--src/globals.h6
-rw-r--r--src/load.c4
-rw-r--r--src/main.c46
-rw-r--r--src/util.c44
6 files changed, 66 insertions, 42 deletions
diff --git a/Makefile b/Makefile
index 3c7a01b..e49c926 100644
--- a/Makefile
+++ b/Makefile
@@ -3,13 +3,13 @@ CC = cc
SRC := $(wildcard src/*.c)
OBJ := $(SRC:.c=.o)
-CFLAGS = -Isrc/ -O3 -std=c99 -g -Wall -lcurl -fsanitize=address
+CFLAGS = -Isrc/ -O3 -g -Wall -fsanitize=address -std=c99
HEADERS := $(wildcard src/*.h)
market-sim: $(OBJ) Makefile $(HEADERS)
@echo "LD $@"
- @$(CC) $(OBJ) -o $@ $(CFLAGS)
+ @$(CC) $(OBJ) -o $@ $(CFLAGS) -lcurl
%.o: %.c Makefile $(HEADERS)
@echo "CC $<"
diff --git a/src/buy.c b/src/buy.c
index 455230c..91db5a1 100644
--- a/src/buy.c
+++ b/src/buy.c
@@ -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*);
diff --git a/src/load.c b/src/load.c
index f60aa52..5e5d2f3 100644
--- a/src/load.c
+++ b/src/load.c
@@ -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);
}
diff --git a/src/main.c b/src/main.c
index 4c07b8f..65249b4 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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");
}
}
diff --git a/src/util.c b/src/util.c
index 11da5c7..70c9b0a 100644
--- a/src/util.c
+++ b/src/util.c
@@ -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;
+}