diff options
| author | Franklin Wei <git@fwei.tk> | 2015-05-15 18:37:00 -0400 |
|---|---|---|
| committer | Franklin Wei <git@fwei.tk> | 2015-05-15 18:37:00 -0400 |
| commit | 367c1ab04df3732d538a4761be78f66c289a78bd (patch) | |
| tree | 91bc86e06f9643fa1c3e73350e0ded0dd6dc4d99 /src | |
| parent | 42706e46a606766bb0a56d1dd727e556f4c17669 (diff) | |
| download | market-sim-367c1ab04df3732d538a4761be78f66c289a78bd.zip market-sim-367c1ab04df3732d538a4761be78f66c289a78bd.tar.gz market-sim-367c1ab04df3732d538a4761be78f66c289a78bd.tar.bz2 market-sim-367c1ab04df3732d538a4761be78f66c289a78bd.tar.xz | |
fix lots of stuff, implement history
Diffstat (limited to 'src')
| -rw-r--r-- | src/globals.h | 20 | ||||
| -rw-r--r-- | src/load.c | 103 | ||||
| -rw-r--r-- | src/main.c | 2 | ||||
| -rw-r--r-- | src/save.c | 69 | ||||
| -rw-r--r-- | src/util.c | 25 |
5 files changed, 171 insertions, 48 deletions
diff --git a/src/globals.h b/src/globals.h index 5bb26d0..a699434 100644 --- a/src/globals.h +++ b/src/globals.h @@ -20,13 +20,24 @@ struct money_t { ullong cents; }; -enum history_action { BUY, SELL }; +enum history_action { BUY = 0, SELL }; + +struct history_time { + ushort year; /* since 2000 */ + uchar month; /* 0 = jan, 11 = dec */ + uchar day; + uchar hour; /* 0-23 */ + uchar minute; + uchar second; +}; struct history_item { enum history_action action; ullong count; struct money_t price; + struct history_time action_time; + struct history_item *next; }; @@ -35,7 +46,10 @@ struct stock_t { char *fullname; ullong count; struct money_t current_price; + struct history_item *history; + + uint history_len; }; struct player_t { @@ -53,6 +67,10 @@ void all_upper(char*); bool get_stock_info(char *sym, struct money_t*, char **name); uint64_t to_sys64(uint64_t); uint64_t to_be64(uint64_t); +uint32_t to_sys32(uint32_t); +uint32_t to_be32(uint32_t); +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); @@ -2,6 +2,40 @@ /* NOTE: integers are represented internally by unsigned long long ints, but in the save they are always 64 bits */ +#define FAIL() exit(*(char*)NULL); + +uint64_t read_be64(FILE *f) +{ + uint64_t n; + if(fread(&n, sizeof(n), 1, f) != 1) + FAIL(); + return to_sys64(n); +} + +uint32_t read_be32(FILE *f) +{ + uint32_t n; + if(fread(&n, sizeof(n), 1, f) != 1) + FAIL(); + return to_sys32(n); +} + +uint16_t read_be16(FILE *f) +{ + uint16_t n; + if(fread(&n, sizeof(n), 1, f) != 1) + FAIL(); + return to_sys16(n); +} + +uint8_t read_int8(FILE *f) +{ + uint8_t n; + if(fread(&n, sizeof(n), 1, f) != 1) + FAIL(); + return n; +} + void load_handler(struct player_t *player) { printf("Enter the file to load portfolio from: "); @@ -14,25 +48,16 @@ void load_handler(struct player_t *player) free(player->portfolio); player->portfolio_len = 0; - FILE *f = fopen(buf, "r"); + FILE *f = fopen(buf, "rb"); char magic[6]; - if(!f || fread(magic, 1, sizeof(magic), f) != 6 || memcmp(magic, "PORTv1", sizeof(magic)) != 0) + if(!f || fread(magic, 1, sizeof(magic), f) != 6 || memcmp(magic, "PORTv2", sizeof(magic)) != 0) { printf("FATAL: Failed to load save."); exit(EXIT_FAILURE); } - uint64_t cash; - if(fread(&cash, sizeof(cash), 1, f) != 1) - { - printf("FATAL: Failed to load save."); - exit(EXIT_FAILURE); - } - cash = to_sys64(cash); + player->cash.cents = read_be64(f); - player->cash.cents = cash; - - fflush(stdout); do { /* read portfolio data */ @@ -40,13 +65,11 @@ void load_handler(struct player_t *player) player->portfolio = realloc(player->portfolio, player->portfolio_len * sizeof(struct stock_t)); player->need_to_free_portfolio = true; - uint64_t symlen; - if(fread(&symlen, sizeof(symlen), 1, f) != 1) - { - printf("FATAL: Save is corrupted (symbol length too short).\n"); - exit(EXIT_FAILURE); - } - symlen = to_sys64(symlen); + struct stock_t *stock = player->portfolio + player->portfolio_len - 1; + + memset(stock, 0, sizeof(struct stock_t)); + + uint64_t symlen = read_be64(f); char *sym = malloc(symlen + 1); if(fread(sym, symlen + 1, 1, f) != 1) { @@ -54,18 +77,42 @@ void load_handler(struct player_t *player) exit(EXIT_FAILURE); } - player->portfolio[player->portfolio_len - 1].symbol = sym; + stock->symbol = sym; - uint64_t count; - if(fread(&count, sizeof(count), 1, f) != 1) - { - printf("FATAL: Save is corrupted (count too short).\n"); - exit(EXIT_FAILURE); - } - count = to_sys64(count); + stock->count = read_be64(f); - player->portfolio[player->portfolio_len - 1].count = count; + uint32_t histlen = read_be32(f); + /* load history */ + + if(histlen) + { + stock->history = malloc(histlen * sizeof(struct history_item)); + + struct history_item *hist = stock->history; + + for(uint i = 0; i < histlen; ++i) + { + hist->action = read_be32(f); + hist->count = read_be64(f); + hist->price.cents = read_be64(f); + + hist->action_time.year = read_be16(f); + hist->action_time.month = read_int8(f); + hist->action_time.day = read_int8(f); + hist->action_time.hour = read_int8(f); + hist->action_time.minute = read_int8(f); + hist->action_time.second = read_int8(f); + + if(i + 1 < histlen) + hist->next = hist + 1; + else + hist->next = NULL; + + ++stock->history_len; + hist = hist->next; + } + } int junk = fgetc(f); ungetc(junk, f); @@ -74,7 +74,7 @@ int main(int argc, char *argv[]) { "[B]uy", "buy", buy_handler }, { "[S]ell", "sell", sell_handler }, { "[U]pdate stock prices", "update", update_handler }, - { "Stock [h]istory", "history", history_handler }, + { "Stock [i]nfo", "info", history_handler }, { "[W]rite portfolio", "write", save_handler }, { "[L]oad portfolio", "load", load_handler }, { "[Q]uit", "quit", quit_handler }, @@ -1,6 +1,37 @@ #include "globals.h" -/* NOTE: integers are represented internally by unsigned long long ints, but in the save they are always 64 bits */ +/* NOTE: integers are represented internally by long long ints, but in the save they are always 64 bits */ + +static bool write_be64(FILE *f, uint64_t n) +{ + n = to_be64(n); + if(fwrite(&n, sizeof(n), 1, f) != 1) + return false; + return true; +} + +static bool write_be32(FILE *f, uint32_t n) +{ + n = to_be32(n); + if(fwrite(&n, sizeof(n), 1, f) != 1) + return false; + return true; +} + +static bool write_be16(FILE *f, uint16_t n) +{ + n = to_be16(n); + if(fwrite(&n, sizeof(n), 1, f) != 1) + return false; + return true; +} + +static bool write_int8(FILE *f, uint8_t n) +{ + if(fwrite(&n, sizeof(n), 1, f) != 1) + return false; + return true; +} void save_handler(struct player_t *player) { @@ -10,25 +41,43 @@ void save_handler(struct player_t *player) scanf("%127s", buf); printf("Writing data...\n"); - FILE *f = fopen(buf, "w"); + FILE *f = fopen(buf, "wb"); - const char *magic = "PORTv1"; + const char *magic = "PORTv2"; fwrite(magic, strlen(magic), 1, f); - uint64_t be_cash = to_be64(player->cash.cents); - - fwrite(&be_cash, sizeof(be_cash), 1, f); + write_be64(f, player->cash.cents); for(uint i = 0; i < player->portfolio_len; ++i) { struct stock_t *stock = player->portfolio + i; - uint64_t be_symlen = to_be64(strlen(stock->symbol)); - fwrite(&be_symlen, sizeof(be_symlen), 1, f); + write_be64(f, strlen(stock->symbol)); + fwrite(stock->symbol, strlen(stock->symbol) + 1, 1, f); - uint64_t be_count = to_be64(stock->count); - fwrite(&be_count, sizeof(be_count), 1, f); + write_be64(f, stock->count); + + write_be32(f, stock->history_len); + + /* write history */ + struct history_item *hist = stock->history; + while(hist) + { + printf("WRITING HISTORY ITEM %d\n", hist->action); + write_be32(f, hist->action); + write_be64(f, hist->count); + write_be64(f, hist->price.cents); + + write_be16(f, hist->action_time.year); + write_int8(f, hist->action_time.month); + write_int8(f, hist->action_time.day); + write_int8(f, hist->action_time.hour); + write_int8(f, hist->action_time.minute); + write_int8(f, hist->action_time.second); + + hist = hist->next; + } } fclose(f); @@ -1,6 +1,7 @@ #include "globals.h" -#include <curl/curl.h> +/* for htnos/htnol */ +#include <arpa/inet.h> void cleanup(void) { @@ -176,16 +177,24 @@ uint64_t to_sys64(uint64_t n) return to_be64(n); } -uint16_t to_sys16(uint16_t n) +uint32_t to_be32(uint32_t n) { - if(!endianness) - { - detect_endianness(); - } + return htonl(n); +} - if(endianness == BIG) - return n; +uint32_t to_sys32(uint32_t n) +{ + return ntohl(n); +} +uint16_t to_be16(uint16_t n) +{ + return htons(n); +} + +uint16_t to_sys16(uint16_t n) +{ + return ntohs(n); } struct stock_t *find_stock(struct player_t *player, char *sym) |